From 18c91c647c17c0b9f80403735e232c116de5ef60 Mon Sep 17 00:00:00 2001 From: arcane4096 Date: Sat, 28 Sep 2024 20:06:44 +0800 Subject: [PATCH] chore: redefine namespace constants for better code structure --- cmd/clef/main.go | 2 +- cmd/geth/config.go | 14 +++++++------- cmd/utils/flags.go | 2 +- consensus/clique/clique.go | 2 +- eth/backend.go | 10 +++++----- eth/catalyst/api.go | 2 +- eth/catalyst/simulated_beacon.go | 2 +- eth/downloader/modes.go | 2 +- eth/tracers/api.go | 2 +- ethclient/gethclient/gethclient_test.go | 2 +- ethclient/simulated/backend.go | 2 +- internal/ethapi/backend.go | 14 +++++++------- node/api.go | 8 ++++---- node/node.go | 2 +- node/node_auth_test.go | 4 ++-- node/utils_test.go | 6 +++--- rpc/types.go | 17 +++++++++++++++++ 17 files changed, 55 insertions(+), 38 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index dde4ae853f..639573399d 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -727,7 +727,7 @@ func signer(c *cli.Context) error { ) rpcAPI := []rpc.API{ { - Namespace: "account", + Namespace: rpc.NamespaceAccount, Service: api, }, } diff --git a/cmd/geth/config.go b/cmd/geth/config.go index fd57ff40de..d1aede04ad 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -328,13 +328,6 @@ func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) { } func setAccountManagerBackends(conf *node.Config, am *accounts.Manager, keydir string) error { - scryptN := keystore.StandardScryptN - scryptP := keystore.StandardScryptP - if conf.UseLightweightKDF { - scryptN = keystore.LightScryptN - scryptP = keystore.LightScryptP - } - // Assemble the supported backends if len(conf.ExternalSigner) > 0 { log.Info("Using external signer", "url", conf.ExternalSigner) @@ -346,6 +339,13 @@ func setAccountManagerBackends(conf *node.Config, am *accounts.Manager, keydir s } } + scryptN := keystore.StandardScryptN + scryptP := keystore.StandardScryptP + if conf.UseLightweightKDF { + scryptN = keystore.LightScryptN + scryptP = keystore.LightScryptP + } + // For now, we're using EITHER external signer OR local signers. // If/when we implement some form of lockfile for USB and keystore wallets, // we can have both, but it's very confusing for the user to see the same diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index da2814855a..de18d89c4f 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1955,7 +1955,7 @@ func RegisterFilterAPI(stack *node.Node, backend ethapi.Backend, ethcfg *ethconf LogCacheSize: ethcfg.FilterLogCacheSize, }) stack.RegisterAPIs([]rpc.API{{ - Namespace: "eth", + Namespace: rpc.NamespaceEth, Service: filters.NewFilterAPI(filterSystem), }}) return filterSystem diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index c9e9484002..674b076c6b 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -716,7 +716,7 @@ func (c *Clique) Close() error { // controlling the signer voting. func (c *Clique) APIs(chain consensus.ChainHeaderReader) []rpc.API { return []rpc.API{{ - Namespace: "clique", + Namespace: rpc.NamespaceClique, Service: &API{chain: chain, clique: c}, }} } diff --git a/eth/backend.go b/eth/backend.go index f10d99c3a7..0cbecfe30e 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -302,19 +302,19 @@ func (s *Ethereum) APIs() []rpc.API { // Append all the local APIs and return return append(apis, []rpc.API{ { - Namespace: "miner", + Namespace: rpc.NamespaceMiner, Service: NewMinerAPI(s), }, { - Namespace: "eth", + Namespace: rpc.NamespaceEth, Service: downloader.NewDownloaderAPI(s.handler.downloader, s.blockchain, s.eventMux), }, { - Namespace: "admin", + Namespace: rpc.NamespaceAdmin, Service: NewAdminAPI(s), }, { - Namespace: "debug", + Namespace: rpc.NamespaceDebug, Service: NewDebugAPI(s), }, { - Namespace: "net", + Namespace: rpc.NamespaceNet, Service: s.netRPCService, }, }...) diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 991cdf93f3..b3948ed5dc 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -48,7 +48,7 @@ func Register(stack *node.Node, backend *eth.Ethereum) error { log.Warn("Engine API enabled", "protocol", "eth") stack.RegisterAPIs([]rpc.API{ { - Namespace: "engine", + Namespace: rpc.NamespaceEngine, Service: NewConsensusAPI(backend), Authenticated: true, }, diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index dbf561ca41..8ce22d859d 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -328,7 +328,7 @@ func RegisterSimulatedBeaconAPIs(stack *node.Node, sim *SimulatedBeacon) { api := newSimulatedBeaconAPI(sim) stack.RegisterAPIs([]rpc.API{ { - Namespace: "dev", + Namespace: rpc.NamespaceDev, Service: api, Version: "1.0", }, diff --git a/eth/downloader/modes.go b/eth/downloader/modes.go index 9d8e1f313c..d9877b89c1 100644 --- a/eth/downloader/modes.go +++ b/eth/downloader/modes.go @@ -19,7 +19,7 @@ package downloader import "fmt" // SyncMode represents the synchronisation mode of the downloader. -// It is a uint32 as it is used with atomic operations. +// It is an uint32 as it is used with atomic operations. type SyncMode uint32 const ( diff --git a/eth/tracers/api.go b/eth/tracers/api.go index a828951206..b7049ab25d 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -1049,7 +1049,7 @@ func APIs(backend Backend) []rpc.API { // Append all the local APIs and return return []rpc.API{ { - Namespace: "debug", + Namespace: rpc.NamespaceDebug, Service: NewAPI(backend), }, } diff --git a/ethclient/gethclient/gethclient_test.go b/ethclient/gethclient/gethclient_test.go index 36ea290a85..e843944e3c 100644 --- a/ethclient/gethclient/gethclient_test.go +++ b/ethclient/gethclient/gethclient_test.go @@ -64,7 +64,7 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { } filterSystem := filters.NewFilterSystem(ethservice.APIBackend, filters.Config{}) n.RegisterAPIs([]rpc.API{{ - Namespace: "eth", + Namespace: rpc.NamespaceEth, Service: filters.NewFilterAPI(filterSystem), }}) diff --git a/ethclient/simulated/backend.go b/ethclient/simulated/backend.go index 6e07aa68d0..ef4c73e809 100644 --- a/ethclient/simulated/backend.go +++ b/ethclient/simulated/backend.go @@ -113,7 +113,7 @@ func newWithNode(stack *node.Node, conf *eth.Config, blockPeriod uint64) (*Backe // Register the filter system filterSystem := filters.NewFilterSystem(backend.APIBackend, filters.Config{}) stack.RegisterAPIs([]rpc.API{{ - Namespace: "eth", + Namespace: rpc.NamespaceEth, Service: filters.NewFilterAPI(filterSystem), }}) // Start the node diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index 2a45ba0921..ed52b6b854 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -103,25 +103,25 @@ func GetAPIs(apiBackend Backend) []rpc.API { nonceLock := new(AddrLocker) return []rpc.API{ { - Namespace: "eth", + Namespace: rpc.NamespaceEth, Service: NewEthereumAPI(apiBackend), }, { - Namespace: "eth", + Namespace: rpc.NamespaceEth, Service: NewBlockChainAPI(apiBackend), }, { - Namespace: "eth", + Namespace: rpc.NamespaceEth, Service: NewTransactionAPI(apiBackend, nonceLock), }, { - Namespace: "txpool", + Namespace: rpc.NamespaceTxpool, Service: NewTxPoolAPI(apiBackend), }, { - Namespace: "debug", + Namespace: rpc.NamespaceDebug, Service: NewDebugAPI(apiBackend), }, { - Namespace: "eth", + Namespace: rpc.NamespaceEth, Service: NewEthereumAccountAPI(apiBackend.AccountManager()), }, { - Namespace: "personal", + Namespace: rpc.NamespacePersonal, Service: NewPersonalAccountAPI(apiBackend, nonceLock), }, } diff --git a/node/api.go b/node/api.go index 33dfb3a1cc..0ad8447824 100644 --- a/node/api.go +++ b/node/api.go @@ -35,16 +35,16 @@ import ( func (n *Node) apis() []rpc.API { return []rpc.API{ { - Namespace: "admin", + Namespace: rpc.NamespaceAdmin, Service: &adminAPI{n}, }, { - Namespace: "debug", + Namespace: rpc.NamespaceDebug, Service: debug.Handler, }, { - Namespace: "debug", + Namespace: rpc.NamespaceDebug, Service: &p2pDebugAPI{n}, }, { - Namespace: "web3", + Namespace: rpc.NamespaceWeb3, Service: &web3API{n}, }, } diff --git a/node/node.go b/node/node.go index 633f88f058..b3ab505226 100644 --- a/node/node.go +++ b/node/node.go @@ -378,7 +378,7 @@ func (n *Node) startRPC() error { // Filter out personal api var apis []rpc.API for _, api := range n.rpcAPIs { - if api.Namespace == "personal" { + if api.Namespace == rpc.NamespacePersonal { if n.config.EnablePersonal { log.Warn("Deprecated personal namespace activated") } else { diff --git a/node/node_auth_test.go b/node/node_auth_test.go index 900f53440c..d9935ee5c8 100644 --- a/node/node_auth_test.go +++ b/node/node_auth_test.go @@ -122,14 +122,14 @@ func TestAuthEndpoints(t *testing.T) { // register dummy apis so we can test the modules are available and reachable with authentication node.RegisterAPIs([]rpc.API{ { - Namespace: "engine", + Namespace: rpc.NamespaceEngine, Version: "1.0", Service: helloRPC("hello engine"), Public: true, Authenticated: true, }, { - Namespace: "eth", + Namespace: rpc.NamespaceEth, Version: "1.0", Service: helloRPC("hello eth"), Public: true, diff --git a/node/utils_test.go b/node/utils_test.go index 681f3a8b28..5616c4fb6e 100644 --- a/node/utils_test.go +++ b/node/utils_test.go @@ -94,13 +94,13 @@ func (f *FullService) Protocols() []p2p.Protocol { func (f *FullService) APIs() []rpc.API { return []rpc.API{ { - Namespace: "admin", + Namespace: rpc.NamespaceAdmin, }, { - Namespace: "debug", + Namespace: rpc.NamespaceDebug, }, { - Namespace: "net", + Namespace: rpc.NamespaceNet, }, } } diff --git a/rpc/types.go b/rpc/types.go index 2e53174b87..0d99958cd1 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -28,6 +28,23 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" ) +// These constants define the namespaces for the JSON-RPC APIs provided by Geth. +// They categorize the RPC methods into logical groups for easier access and management. +const ( + NamespaceAdmin = "admin" + NamespaceEth = "eth" + NamespaceWeb3 = "web3" + NamespaceAccount = "account" + NamespaceClique = "clique" + NamespaceTxpool = "txpool" + NamespaceMiner = "miner" + NamespaceDebug = "debug" + NamespaceDev = "dev" + NamespaceNet = "net" + NamespacePersonal = "personal" + NamespaceEngine = "engine" +) + // API describes the set of methods offered over the RPC interface type API struct { Namespace string // namespace under which the rpc methods of Service are exposed