Skip to content

Commit 312263c

Browse files
committed
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.
1 parent d6625ac commit 312263c

11 files changed

Lines changed: 207 additions & 195 deletions

File tree

‎accounts/account_manager.go‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ import (
3434

3535
"github.com/ethereum/go-ethereum/common"
3636
"github.com/ethereum/go-ethereum/crypto"
37-
"github.com/ethereum/go-ethereum/p2p"
38-
"github.com/ethereum/go-ethereum/rpc"
3937
)
4038

4139
var (
@@ -342,23 +340,3 @@ func zeroKey(k *ecdsa.PrivateKey) {
342340
b[i] = 0
343341
}
344342
}
345-
346-
// APIs implements node.Service
347-
func (am *Manager) APIs() []rpc.API {
348-
return nil
349-
}
350-
351-
// Protocols implements node.Service
352-
func (am *Manager) Protocols() []p2p.Protocol {
353-
return nil
354-
}
355-
356-
// Start implements node.Service
357-
func (am *Manager) Start(srvr *p2p.Server) error {
358-
return nil
359-
}
360-
361-
// Stop implements node.Service
362-
func (am *Manager) Stop() error {
363-
return nil
364-
}

‎cmd/geth/accountcmd.go‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ nodes.
168168
)
169169

170170
func accountList(ctx *cli.Context) error {
171-
accman := utils.MakeAccountManager(ctx)
172-
for i, acct := range accman.Accounts() {
171+
stack := utils.MakeNode(ctx, clientIdentifier, verString)
172+
for i, acct := range stack.AccountManager().Accounts() {
173173
fmt.Printf("Account #%d: {%x} %s\n", i, acct.Address, acct.File)
174174
}
175175
return nil
@@ -261,10 +261,10 @@ func ambiguousAddrRecovery(am *accounts.Manager, err *accounts.AmbiguousAddrErro
261261

262262
// accountCreate creates a new account into the keystore defined by the CLI flags.
263263
func accountCreate(ctx *cli.Context) error {
264-
accman := utils.MakeAccountManager(ctx)
264+
stack := utils.MakeNode(ctx, clientIdentifier, verString)
265265
password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
266266

267-
account, err := accman.NewAccount(password)
267+
account, err := stack.AccountManager().NewAccount(password)
268268
if err != nil {
269269
utils.Fatalf("Failed to create account: %v", err)
270270
}
@@ -278,11 +278,10 @@ func accountUpdate(ctx *cli.Context) error {
278278
if len(ctx.Args()) == 0 {
279279
utils.Fatalf("No accounts specified to update")
280280
}
281-
accman := utils.MakeAccountManager(ctx)
282-
283-
account, oldPassword := unlockAccount(ctx, accman, ctx.Args().First(), 0, nil)
281+
stack := utils.MakeNode(ctx, clientIdentifier, verString)
282+
account, oldPassword := unlockAccount(ctx, stack.AccountManager(), ctx.Args().First(), 0, nil)
284283
newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true, 0, nil)
285-
if err := accman.Update(account, oldPassword, newPassword); err != nil {
284+
if err := stack.AccountManager().Update(account, oldPassword, newPassword); err != nil {
286285
utils.Fatalf("Could not update the account: %v", err)
287286
}
288287
return nil
@@ -298,10 +297,9 @@ func importWallet(ctx *cli.Context) error {
298297
utils.Fatalf("Could not read wallet file: %v", err)
299298
}
300299

301-
accman := utils.MakeAccountManager(ctx)
300+
stack := utils.MakeNode(ctx, clientIdentifier, verString)
302301
passphrase := getPassPhrase("", false, 0, utils.MakePasswordList(ctx))
303-
304-
acct, err := accman.ImportPreSaleKey(keyJson, passphrase)
302+
acct, err := stack.AccountManager().ImportPreSaleKey(keyJson, passphrase)
305303
if err != nil {
306304
utils.Fatalf("%v", err)
307305
}
@@ -318,9 +316,9 @@ func accountImport(ctx *cli.Context) error {
318316
if err != nil {
319317
utils.Fatalf("Failed to load the private key: %v", err)
320318
}
321-
accman := utils.MakeAccountManager(ctx)
319+
stack := utils.MakeNode(ctx, clientIdentifier, verString)
322320
passphrase := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
323-
acct, err := accman.ImportECDSA(key, passphrase)
321+
acct, err := stack.AccountManager().ImportECDSA(key, passphrase)
324322
if err != nil {
325323
utils.Fatalf("Could not create the account: %v", err)
326324
}

‎cmd/geth/consolecmd.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
6565
// same time.
6666
func localConsole(ctx *cli.Context) error {
6767
// Create and start the node based on the CLI flags
68-
node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx)
68+
node := makeFullNode(ctx)
6969
startNode(ctx, node)
7070
defer node.Stop()
7171

@@ -149,7 +149,7 @@ func dialRPC(endpoint string) (*rpc.Client, error) {
149149
// everything down.
150150
func ephemeralConsole(ctx *cli.Context) error {
151151
// Create and start the node based on the CLI flags
152-
node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx)
152+
node := makeFullNode(ctx)
153153
startNode(ctx, node)
154154
defer node.Stop()
155155

‎cmd/geth/main.go‎

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"time"
3030

3131
"github.com/ethereum/ethash"
32-
"github.com/ethereum/go-ethereum/accounts"
3332
"github.com/ethereum/go-ethereum/cmd/utils"
3433
"github.com/ethereum/go-ethereum/common"
3534
"github.com/ethereum/go-ethereum/console"
@@ -244,34 +243,13 @@ func main() {
244243
}
245244
}
246245

247-
func makeDefaultExtra() []byte {
248-
var clientInfo = struct {
249-
Version uint
250-
Name string
251-
GoVersion string
252-
Os string
253-
}{uint(versionMajor<<16 | versionMinor<<8 | versionPatch), clientIdentifier, runtime.Version(), runtime.GOOS}
254-
extra, err := rlp.EncodeToBytes(clientInfo)
255-
if err != nil {
256-
glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
257-
}
258-
259-
if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
260-
glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
261-
glog.V(logger.Debug).Infof("extra: %x\n", extra)
262-
return nil
263-
}
264-
return extra
265-
}
266-
267246
// geth is the main entry point into the system if no special subcommand is ran.
268247
// It creates a default node based on the command line arguments and runs it in
269248
// blocking mode, waiting for it to be shut down.
270249
func geth(ctx *cli.Context) error {
271-
node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx)
250+
node := makeFullNode(ctx)
272251
startNode(ctx, node)
273252
node.Wait()
274-
275253
return nil
276254
}
277255

@@ -301,6 +279,38 @@ func initGenesis(ctx *cli.Context) error {
301279
return nil
302280
}
303281

282+
func makeFullNode(ctx *cli.Context) *node.Node {
283+
node := utils.MakeNode(ctx, clientIdentifier, verString)
284+
utils.RegisterEthService(ctx, node, relConfig, makeDefaultExtra())
285+
// Whisper must be explicitly enabled, but is auto-enabled in --dev mode.
286+
shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name)
287+
shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
288+
if shhEnabled || shhAutoEnabled {
289+
utils.RegisterShhService(node)
290+
}
291+
return node
292+
}
293+
294+
func makeDefaultExtra() []byte {
295+
var clientInfo = struct {
296+
Version uint
297+
Name string
298+
GoVersion string
299+
Os string
300+
}{uint(versionMajor<<16 | versionMinor<<8 | versionPatch), clientIdentifier, runtime.Version(), runtime.GOOS}
301+
extra, err := rlp.EncodeToBytes(clientInfo)
302+
if err != nil {
303+
glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
304+
}
305+
306+
if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
307+
glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
308+
glog.V(logger.Debug).Infof("extra: %x\n", extra)
309+
return nil
310+
}
311+
return extra
312+
}
313+
304314
// startNode boots up the system node and all registered protocols, after which
305315
// it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
306316
// miner.
@@ -311,12 +321,8 @@ func startNode(ctx *cli.Context, stack *node.Node) {
311321
utils.StartNode(stack)
312322

313323
// Unlock any account specifically requested
314-
var accman *accounts.Manager
315-
if err := stack.Service(&accman); err != nil {
316-
utils.Fatalf("ethereum service not running: %v", err)
317-
}
324+
accman := stack.AccountManager()
318325
passwords := utils.MakePasswordList(ctx)
319-
320326
accounts := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
321327
for i, account := range accounts {
322328
if trimmed := strings.TrimSpace(account); trimmed != "" {

‎cmd/gethrpctest/main.go‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ package main
1919

2020
import (
2121
"flag"
22-
"io/ioutil"
2322
"log"
2423
"os"
2524
"os/signal"
2625

27-
"github.com/ethereum/go-ethereum/accounts"
2826
"github.com/ethereum/go-ethereum/common"
2927
"github.com/ethereum/go-ethereum/core"
3028
"github.com/ethereum/go-ethereum/crypto"
@@ -61,14 +59,8 @@ func main() {
6159
if !found {
6260
log.Fatalf("Requested test (%s) not found within suite", *testName)
6361
}
64-
// Create the protocol stack to run the test with
65-
keydir, err := ioutil.TempDir("", "")
66-
if err != nil {
67-
log.Fatalf("Failed to create temporary keystore directory: %v", err)
68-
}
69-
defer os.RemoveAll(keydir)
7062

71-
stack, err := MakeSystemNode(keydir, *testKey, test)
63+
stack, err := MakeSystemNode(*testKey, test)
7264
if err != nil {
7365
log.Fatalf("Failed to assemble test stack: %v", err)
7466
}
@@ -92,23 +84,24 @@ func main() {
9284

9385
// MakeSystemNode configures a protocol stack for the RPC tests based on a given
9486
// keystore path and initial pre-state.
95-
func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node.Node, error) {
87+
func MakeSystemNode(privkey string, test *tests.BlockTest) (*node.Node, error) {
9688
// Create a networkless protocol stack
9789
stack, err := node.New(&node.Config{
98-
IPCPath: node.DefaultIPCEndpoint(),
99-
HTTPHost: common.DefaultHTTPHost,
100-
HTTPPort: common.DefaultHTTPPort,
101-
HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
102-
WSHost: common.DefaultWSHost,
103-
WSPort: common.DefaultWSPort,
104-
WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
105-
NoDiscovery: true,
90+
UseLightweightKDF: true,
91+
IPCPath: node.DefaultIPCEndpoint(),
92+
HTTPHost: common.DefaultHTTPHost,
93+
HTTPPort: common.DefaultHTTPPort,
94+
HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
95+
WSHost: common.DefaultWSHost,
96+
WSPort: common.DefaultWSPort,
97+
WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
98+
NoDiscovery: true,
10699
})
107100
if err != nil {
108101
return nil, err
109102
}
110103
// Create the keystore and inject an unlocked account if requested
111-
accman := accounts.NewPlaintextManager(keydir)
104+
accman := stack.AccountManager()
112105
if len(privkey) > 0 {
113106
key, err := crypto.HexToECDSA(privkey)
114107
if err != nil {
@@ -131,7 +124,6 @@ func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node
131124
TestGenesisState: db,
132125
TestGenesisBlock: test.Genesis,
133126
ChainConfig: &core.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock},
134-
AccountManager: accman,
135127
}
136128
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
137129
return nil, err

0 commit comments

Comments
 (0)