diff options
author | Felix Lange <fjl@twurst.com> | 2016-08-16 00:38:32 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2016-08-17 23:39:03 +0800 |
commit | 312263c7d9457fe7c24aac8e42a4cf2efc6ccd8e (patch) | |
tree | e10f9cc260af1ce13cb67b02bbce9c17c3b09de2 /cmd | |
parent | d6625ac34dad741f5659ca1dad3b0179603d37f0 (diff) | |
download | go-tangerine-312263c7d9457fe7c24aac8e42a4cf2efc6ccd8e.tar go-tangerine-312263c7d9457fe7c24aac8e42a4cf2efc6ccd8e.tar.gz go-tangerine-312263c7d9457fe7c24aac8e42a4cf2efc6ccd8e.tar.bz2 go-tangerine-312263c7d9457fe7c24aac8e42a4cf2efc6ccd8e.tar.lz go-tangerine-312263c7d9457fe7c24aac8e42a4cf2efc6ccd8e.tar.xz go-tangerine-312263c7d9457fe7c24aac8e42a4cf2efc6ccd8e.tar.zst go-tangerine-312263c7d9457fe7c24aac8e42a4cf2efc6ccd8e.zip |
cmd/utils, node: create account manager in package node
The account manager was previously created by packge cmd/utils as part
of flag processing and then passed down into eth.Ethereum through its
config struct. Since we are starting to create nodes which do not have
eth.Ethereum as a registered service, the code was rearranged to
register the account manager as its own service. Making it a service is
ugly though and it doesn't really fix the root cause: creating nodes
without eth.Ethereum requires duplicating lots of code.
This commit splits utils.MakeSystemNode into three functions, making
creation of other node/service configurations easier. It also moves the
account manager into Node so it can be used by those configurations
without requiring package eth.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/geth/accountcmd.go | 24 | ||||
-rw-r--r-- | cmd/geth/consolecmd.go | 4 | ||||
-rw-r--r-- | cmd/geth/main.go | 62 | ||||
-rw-r--r-- | cmd/gethrpctest/main.go | 32 | ||||
-rw-r--r-- | cmd/utils/flags.go | 135 |
5 files changed, 109 insertions, 148 deletions
diff --git a/cmd/geth/accountcmd.go b/cmd/geth/accountcmd.go index 7fea16a25..2069df6cd 100644 --- a/cmd/geth/accountcmd.go +++ b/cmd/geth/accountcmd.go @@ -168,8 +168,8 @@ nodes. ) func accountList(ctx *cli.Context) error { - accman := utils.MakeAccountManager(ctx) - for i, acct := range accman.Accounts() { + stack := utils.MakeNode(ctx, clientIdentifier, verString) + for i, acct := range stack.AccountManager().Accounts() { fmt.Printf("Account #%d: {%x} %s\n", i, acct.Address, acct.File) } return nil @@ -261,10 +261,10 @@ func ambiguousAddrRecovery(am *accounts.Manager, err *accounts.AmbiguousAddrErro // accountCreate creates a new account into the keystore defined by the CLI flags. func accountCreate(ctx *cli.Context) error { - accman := utils.MakeAccountManager(ctx) + stack := utils.MakeNode(ctx, clientIdentifier, verString) password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx)) - account, err := accman.NewAccount(password) + account, err := stack.AccountManager().NewAccount(password) if err != nil { utils.Fatalf("Failed to create account: %v", err) } @@ -278,11 +278,10 @@ func accountUpdate(ctx *cli.Context) error { if len(ctx.Args()) == 0 { utils.Fatalf("No accounts specified to update") } - accman := utils.MakeAccountManager(ctx) - - account, oldPassword := unlockAccount(ctx, accman, ctx.Args().First(), 0, nil) + stack := utils.MakeNode(ctx, clientIdentifier, verString) + account, oldPassword := unlockAccount(ctx, stack.AccountManager(), ctx.Args().First(), 0, nil) newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true, 0, nil) - if err := accman.Update(account, oldPassword, newPassword); err != nil { + if err := stack.AccountManager().Update(account, oldPassword, newPassword); err != nil { utils.Fatalf("Could not update the account: %v", err) } return nil @@ -298,10 +297,9 @@ func importWallet(ctx *cli.Context) error { utils.Fatalf("Could not read wallet file: %v", err) } - accman := utils.MakeAccountManager(ctx) + stack := utils.MakeNode(ctx, clientIdentifier, verString) passphrase := getPassPhrase("", false, 0, utils.MakePasswordList(ctx)) - - acct, err := accman.ImportPreSaleKey(keyJson, passphrase) + acct, err := stack.AccountManager().ImportPreSaleKey(keyJson, passphrase) if err != nil { utils.Fatalf("%v", err) } @@ -318,9 +316,9 @@ func accountImport(ctx *cli.Context) error { if err != nil { utils.Fatalf("Failed to load the private key: %v", err) } - accman := utils.MakeAccountManager(ctx) + stack := utils.MakeNode(ctx, clientIdentifier, verString) passphrase := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx)) - acct, err := accman.ImportECDSA(key, passphrase) + acct, err := stack.AccountManager().ImportECDSA(key, passphrase) if err != nil { utils.Fatalf("Could not create the account: %v", err) } diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index 8d53809ce..92d6f7f86 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -65,7 +65,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso // same time. func localConsole(ctx *cli.Context) error { // Create and start the node based on the CLI flags - node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx) + node := makeFullNode(ctx) startNode(ctx, node) defer node.Stop() @@ -149,7 +149,7 @@ func dialRPC(endpoint string) (*rpc.Client, error) { // everything down. func ephemeralConsole(ctx *cli.Context) error { // Create and start the node based on the CLI flags - node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx) + node := makeFullNode(ctx) startNode(ctx, node) defer node.Stop() diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 5f1157b90..de679ccca 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -29,7 +29,6 @@ import ( "time" "github.com/ethereum/ethash" - "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console" @@ -244,34 +243,13 @@ func main() { } } -func makeDefaultExtra() []byte { - var clientInfo = struct { - Version uint - Name string - GoVersion string - Os string - }{uint(versionMajor<<16 | versionMinor<<8 | versionPatch), clientIdentifier, runtime.Version(), runtime.GOOS} - extra, err := rlp.EncodeToBytes(clientInfo) - if err != nil { - glog.V(logger.Warn).Infoln("error setting canonical miner information:", err) - } - - if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() { - glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize) - glog.V(logger.Debug).Infof("extra: %x\n", extra) - return nil - } - return extra -} - // geth is the main entry point into the system if no special subcommand is ran. // It creates a default node based on the command line arguments and runs it in // blocking mode, waiting for it to be shut down. func geth(ctx *cli.Context) error { - node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx) + node := makeFullNode(ctx) startNode(ctx, node) node.Wait() - return nil } @@ -301,6 +279,38 @@ func initGenesis(ctx *cli.Context) error { return nil } +func makeFullNode(ctx *cli.Context) *node.Node { + node := utils.MakeNode(ctx, clientIdentifier, verString) + utils.RegisterEthService(ctx, node, relConfig, makeDefaultExtra()) + // Whisper must be explicitly enabled, but is auto-enabled in --dev mode. + shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name) + shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name) + if shhEnabled || shhAutoEnabled { + utils.RegisterShhService(node) + } + return node +} + +func makeDefaultExtra() []byte { + var clientInfo = struct { + Version uint + Name string + GoVersion string + Os string + }{uint(versionMajor<<16 | versionMinor<<8 | versionPatch), clientIdentifier, runtime.Version(), runtime.GOOS} + extra, err := rlp.EncodeToBytes(clientInfo) + if err != nil { + glog.V(logger.Warn).Infoln("error setting canonical miner information:", err) + } + + if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() { + glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize) + glog.V(logger.Debug).Infof("extra: %x\n", extra) + return nil + } + return extra +} + // startNode boots up the system node and all registered protocols, after which // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the // miner. @@ -311,12 +321,8 @@ func startNode(ctx *cli.Context, stack *node.Node) { utils.StartNode(stack) // Unlock any account specifically requested - var accman *accounts.Manager - if err := stack.Service(&accman); err != nil { - utils.Fatalf("ethereum service not running: %v", err) - } + accman := stack.AccountManager() passwords := utils.MakePasswordList(ctx) - accounts := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",") for i, account := range accounts { if trimmed := strings.TrimSpace(account); trimmed != "" { diff --git a/cmd/gethrpctest/main.go b/cmd/gethrpctest/main.go index 2e07e9426..d267dbf58 100644 --- a/cmd/gethrpctest/main.go +++ b/cmd/gethrpctest/main.go @@ -19,12 +19,10 @@ package main import ( "flag" - "io/ioutil" "log" "os" "os/signal" - "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/crypto" @@ -61,14 +59,8 @@ func main() { if !found { log.Fatalf("Requested test (%s) not found within suite", *testName) } - // Create the protocol stack to run the test with - keydir, err := ioutil.TempDir("", "") - if err != nil { - log.Fatalf("Failed to create temporary keystore directory: %v", err) - } - defer os.RemoveAll(keydir) - stack, err := MakeSystemNode(keydir, *testKey, test) + stack, err := MakeSystemNode(*testKey, test) if err != nil { log.Fatalf("Failed to assemble test stack: %v", err) } @@ -92,23 +84,24 @@ func main() { // MakeSystemNode configures a protocol stack for the RPC tests based on a given // keystore path and initial pre-state. -func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node.Node, error) { +func MakeSystemNode(privkey string, test *tests.BlockTest) (*node.Node, error) { // Create a networkless protocol stack stack, err := node.New(&node.Config{ - IPCPath: node.DefaultIPCEndpoint(), - HTTPHost: common.DefaultHTTPHost, - HTTPPort: common.DefaultHTTPPort, - HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"}, - WSHost: common.DefaultWSHost, - WSPort: common.DefaultWSPort, - WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"}, - NoDiscovery: true, + UseLightweightKDF: true, + IPCPath: node.DefaultIPCEndpoint(), + HTTPHost: common.DefaultHTTPHost, + HTTPPort: common.DefaultHTTPPort, + HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"}, + WSHost: common.DefaultWSHost, + WSPort: common.DefaultWSPort, + WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"}, + NoDiscovery: true, }) if err != nil { return nil, err } // Create the keystore and inject an unlocked account if requested - accman := accounts.NewPlaintextManager(keydir) + accman := stack.AccountManager() if len(privkey) > 0 { key, err := crypto.HexToECDSA(privkey) if err != nil { @@ -131,7 +124,6 @@ func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node TestGenesisState: db, TestGenesisBlock: test.Genesis, ChainConfig: &core.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock}, - AccountManager: accman, } if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil { return nil, err diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index de379f84f..067faf3ce 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -413,16 +413,6 @@ func MustMakeDataDir(ctx *cli.Context) string { return "" } -// MakeKeyStoreDir resolves the folder to use for storing the account keys from the -// set command line flags, returning the explicitly requested path, or one inside -// the data directory otherwise. -func MakeKeyStoreDir(datadir string, ctx *cli.Context) string { - if path := ctx.GlobalString(KeyStoreDirFlag.Name); path != "" { - return path - } - return filepath.Join(datadir, "keystore") -} - // MakeIPCPath creates an IPC path configuration from the set command line flags, // returning an empty string if IPC was explicitly disabled, or the set path. func MakeIPCPath(ctx *cli.Context) string { @@ -555,20 +545,6 @@ func MakeDatabaseHandles() int { return limit / 2 // Leave half for networking and other stuff } -// MakeAccountManager creates an account manager from set command line flags. -func MakeAccountManager(ctx *cli.Context) *accounts.Manager { - // Create the keystore crypto primitive, light if requested - scryptN := accounts.StandardScryptN - scryptP := accounts.StandardScryptP - if ctx.GlobalBool(LightKDFFlag.Name) { - scryptN = accounts.LightScryptN - scryptP = accounts.LightScryptP - } - datadir := MustMakeDataDir(ctx) - keydir := MakeKeyStoreDir(datadir, ctx) - return accounts.NewManager(keydir, scryptN, scryptP) -} - // MakeAddress converts an account specified directly as a hex encoded string or // a key index in the key store to an internal account representation. func MakeAddress(accman *accounts.Manager, account string) (accounts.Account, error) { @@ -631,9 +607,48 @@ func MakePasswordList(ctx *cli.Context) []string { return lines } -// MakeSystemNode sets up a local node, configures the services to launch and -// assembles the P2P protocol stack. -func MakeSystemNode(name, version string, relconf release.Config, extra []byte, ctx *cli.Context) *node.Node { +// MakeNode configures a node with no services from command line flags. +func MakeNode(ctx *cli.Context, name, version string) *node.Node { + config := &node.Config{ + DataDir: MustMakeDataDir(ctx), + KeyStoreDir: ctx.GlobalString(KeyStoreDirFlag.Name), + UseLightweightKDF: ctx.GlobalBool(LightKDFFlag.Name), + PrivateKey: MakeNodeKey(ctx), + Name: MakeNodeName(name, version, ctx), + NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name), + BootstrapNodes: MakeBootstrapNodes(ctx), + ListenAddr: MakeListenAddress(ctx), + NAT: MakeNAT(ctx), + MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name), + MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name), + IPCPath: MakeIPCPath(ctx), + HTTPHost: MakeHTTPRpcHost(ctx), + HTTPPort: ctx.GlobalInt(RPCPortFlag.Name), + HTTPCors: ctx.GlobalString(RPCCORSDomainFlag.Name), + HTTPModules: MakeRPCModules(ctx.GlobalString(RPCApiFlag.Name)), + WSHost: MakeWSRpcHost(ctx), + WSPort: ctx.GlobalInt(WSPortFlag.Name), + WSOrigins: ctx.GlobalString(WSAllowedOriginsFlag.Name), + WSModules: MakeRPCModules(ctx.GlobalString(WSApiFlag.Name)), + } + if ctx.GlobalBool(DevModeFlag.Name) { + if !ctx.GlobalIsSet(DataDirFlag.Name) { + config.DataDir = filepath.Join(os.TempDir(), "/ethereum_dev_mode") + } + // --dev mode does not need p2p networking. + config.MaxPeers = 0 + config.ListenAddr = ":0" + } + stack, err := node.New(config) + if err != nil { + Fatalf("Failed to create the protocol stack: %v", err) + } + return stack +} + +// RegisterEthService configures eth.Ethereum from command line flags and adds it to the +// given node. +func RegisterEthService(ctx *cli.Context, stack *node.Node, relconf release.Config, extra []byte) { // Avoid conflicting network flags networks, netFlags := 0, []cli.BoolFlag{DevModeFlag, TestNetFlag, OlympicFlag} for _, flag := range netFlags { @@ -644,29 +659,6 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, if networks > 1 { Fatalf("The %v flags are mutually exclusive", netFlags) } - // Configure the node's service container - stackConf := &node.Config{ - DataDir: MustMakeDataDir(ctx), - PrivateKey: MakeNodeKey(ctx), - Name: MakeNodeName(name, version, ctx), - NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name), - BootstrapNodes: MakeBootstrapNodes(ctx), - ListenAddr: MakeListenAddress(ctx), - NAT: MakeNAT(ctx), - MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name), - MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name), - IPCPath: MakeIPCPath(ctx), - HTTPHost: MakeHTTPRpcHost(ctx), - HTTPPort: ctx.GlobalInt(RPCPortFlag.Name), - HTTPCors: ctx.GlobalString(RPCCORSDomainFlag.Name), - HTTPModules: MakeRPCModules(ctx.GlobalString(RPCApiFlag.Name)), - WSHost: MakeWSRpcHost(ctx), - WSPort: ctx.GlobalInt(WSPortFlag.Name), - WSOrigins: ctx.GlobalString(WSAllowedOriginsFlag.Name), - WSModules: MakeRPCModules(ctx.GlobalString(WSApiFlag.Name)), - } - // Configure the Ethereum service - accman := MakeAccountManager(ctx) // initialise new random number generator rand := rand.New(rand.NewSource(time.Now().UnixNano())) @@ -679,14 +671,13 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, } ethConf := ð.Config{ + Etherbase: MakeEtherbase(stack.AccountManager(), ctx), ChainConfig: MustMakeChainConfig(ctx), FastSync: ctx.GlobalBool(FastSyncFlag.Name), BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name), DatabaseCache: ctx.GlobalInt(CacheFlag.Name), DatabaseHandles: MakeDatabaseHandles(), NetworkId: ctx.GlobalInt(NetworkIdFlag.Name), - AccountManager: accman, - Etherbase: MakeEtherbase(accman, ctx), MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name), ExtraData: MakeMinerExtra(extra, ctx), NatSpec: ctx.GlobalBool(NatspecEnabledFlag.Name), @@ -703,8 +694,6 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, SolcPath: ctx.GlobalString(SolcPathFlag.Name), AutoDAG: ctx.GlobalBool(AutoDAGFlag.Name) || ctx.GlobalBool(MiningEnabledFlag.Name), } - // Configure the Whisper service - shhEnable := ctx.GlobalBool(WhisperEnabledFlag.Name) // Override any default configs in dev mode or the test net switch { @@ -722,54 +711,30 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, state.StartingNonce = 1048576 // (2**20) case ctx.GlobalBool(DevModeFlag.Name): - // Override the base network stack configs - if !ctx.GlobalIsSet(DataDirFlag.Name) { - stackConf.DataDir = filepath.Join(os.TempDir(), "/ethereum_dev_mode") - } - if !ctx.GlobalIsSet(MaxPeersFlag.Name) { - stackConf.MaxPeers = 0 - } - if !ctx.GlobalIsSet(ListenPortFlag.Name) { - stackConf.ListenAddr = ":0" - } - // Override the Ethereum protocol configs ethConf.Genesis = core.OlympicGenesisBlock() if !ctx.GlobalIsSet(GasPriceFlag.Name) { ethConf.GasPrice = new(big.Int) } - if !ctx.GlobalIsSet(WhisperEnabledFlag.Name) { - shhEnable = true - } ethConf.PowTest = true } - // Assemble and return the protocol stack - stack, err := node.New(stackConf) - if err != nil { - Fatalf("Failed to create the protocol stack: %v", err) - } - - if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { - return accman, nil - }); err != nil { - Fatalf("Failed to register the account manager service: %v", err) - } if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil { Fatalf("Failed to register the Ethereum service: %v", err) } - if shhEnable { - if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil { - Fatalf("Failed to register the Whisper service: %v", err) - } - } if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return release.NewReleaseService(ctx, relconf) }); err != nil { Fatalf("Failed to register the Geth release oracle service: %v", err) } - return stack +} + +// RegisterShhService configures whisper and adds it to the given node. +func RegisterShhService(stack *node.Node) { + if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil { + Fatalf("Failed to register the Whisper service: %v", err) + } } // SetupNetwork configures the system for either the main net or some test network. |