Skip to content

Commit 8f37322

Browse files
jsvisaholimans1na
authored
cmd/geth: make account commands not require datadir lock (ethereum#27084)
Makes the `geth account ... ` commands usable even if a geth-process is already executing, since the account commands do not read the chaindata, it was not required for those to use the same locking mechanism. --- Signed-off-by: jsvisa <[email protected]> Co-authored-by: Martin Holst Swende <[email protected]> Co-authored-by: Sina Mahmoodi <[email protected]>
1 parent 66c0c4e commit 8f37322

File tree

4 files changed

+53
-33
lines changed

4 files changed

+53
-33
lines changed

cmd/geth/accountcmd.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,34 @@ nodes.
188188
}
189189
)
190190

191+
// makeAccountManager creates an account manager with backends
192+
func makeAccountManager(ctx *cli.Context) *accounts.Manager {
193+
cfg := loadBaseConfig(ctx)
194+
am := accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: cfg.Node.InsecureUnlockAllowed})
195+
keydir, isEphemeral, err := cfg.Node.GetKeyStoreDir()
196+
if err != nil {
197+
utils.Fatalf("Failed to get the keystore directory: %v", err)
198+
}
199+
if isEphemeral {
200+
utils.Fatalf("Can't use ephemeral directory as keystore path")
201+
}
202+
203+
if err := setAccountManagerBackends(&cfg.Node, am, keydir); err != nil {
204+
utils.Fatalf("Failed to set account manager backends: %v", err)
205+
}
206+
return am
207+
}
208+
191209
func accountList(ctx *cli.Context) error {
192-
stack, _ := makeConfigNode(ctx)
210+
am := makeAccountManager(ctx)
193211
var index int
194-
for _, wallet := range stack.AccountManager().Wallets() {
212+
for _, wallet := range am.Wallets() {
195213
for _, account := range wallet.Accounts() {
196214
fmt.Printf("Account #%d: {%x} %s\n", index, account.Address, &account.URL)
197215
index++
198216
}
199217
}
218+
200219
return nil
201220
}
202221

@@ -258,17 +277,13 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
258277

259278
// accountCreate creates a new account into the keystore defined by the CLI flags.
260279
func accountCreate(ctx *cli.Context) error {
261-
cfg := gethConfig{Node: defaultNodeConfig()}
262-
// Load config file.
263-
if file := ctx.String(configFileFlag.Name); file != "" {
264-
if err := loadConfig(file, &cfg); err != nil {
265-
utils.Fatalf("%v", err)
266-
}
267-
}
268-
utils.SetNodeConfig(ctx, &cfg.Node)
269-
keydir, err := cfg.Node.KeyDirConfig()
280+
cfg := loadBaseConfig(ctx)
281+
keydir, isEphemeral, err := cfg.Node.GetKeyStoreDir()
270282
if err != nil {
271-
utils.Fatalf("Failed to read configuration: %v", err)
283+
utils.Fatalf("Failed to get the keystore directory: %v", err)
284+
}
285+
if isEphemeral {
286+
utils.Fatalf("Can't use ephemeral directory as keystore path")
272287
}
273288
scryptN := keystore.StandardScryptN
274289
scryptP := keystore.StandardScryptP
@@ -300,8 +315,8 @@ func accountUpdate(ctx *cli.Context) error {
300315
if ctx.Args().Len() == 0 {
301316
utils.Fatalf("No accounts specified to update")
302317
}
303-
stack, _ := makeConfigNode(ctx)
304-
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
318+
am := makeAccountManager(ctx)
319+
backends := am.Backends(keystore.KeyStoreType)
305320
if len(backends) == 0 {
306321
utils.Fatalf("Keystore is not available")
307322
}
@@ -327,14 +342,14 @@ func importWallet(ctx *cli.Context) error {
327342
utils.Fatalf("Could not read wallet file: %v", err)
328343
}
329344

330-
stack, _ := makeConfigNode(ctx)
331-
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))
332-
333-
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
345+
am := makeAccountManager(ctx)
346+
backends := am.Backends(keystore.KeyStoreType)
334347
if len(backends) == 0 {
335348
utils.Fatalf("Keystore is not available")
336349
}
337350
ks := backends[0].(*keystore.KeyStore)
351+
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))
352+
338353
acct, err := ks.ImportPreSaleKey(keyJSON, passphrase)
339354
if err != nil {
340355
utils.Fatalf("%v", err)
@@ -352,14 +367,14 @@ func accountImport(ctx *cli.Context) error {
352367
if err != nil {
353368
utils.Fatalf("Failed to load the private key: %v", err)
354369
}
355-
stack, _ := makeConfigNode(ctx)
356-
passphrase := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
357-
358-
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
370+
am := makeAccountManager(ctx)
371+
backends := am.Backends(keystore.KeyStoreType)
359372
if len(backends) == 0 {
360373
utils.Fatalf("Keystore is not available")
361374
}
362375
ks := backends[0].(*keystore.KeyStore)
376+
passphrase := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
377+
363378
acct, err := ks.ImportECDSA(key, passphrase)
364379
if err != nil {
365380
utils.Fatalf("Could not create the account: %v", err)

cmd/geth/config.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/urfave/cli/v2"
2828

29+
"github.com/ethereum/go-ethereum/accounts"
2930
"github.com/ethereum/go-ethereum/accounts/external"
3031
"github.com/ethereum/go-ethereum/accounts/keystore"
3132
"github.com/ethereum/go-ethereum/accounts/scwallet"
@@ -119,8 +120,9 @@ func defaultNodeConfig() node.Config {
119120
return cfg
120121
}
121122

122-
// makeConfigNode loads geth configuration and creates a blank node instance.
123-
func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
123+
// loadBaseConfig loads the gethConfig based on the given command line
124+
// parameters and config file.
125+
func loadBaseConfig(ctx *cli.Context) gethConfig {
124126
// Load defaults.
125127
cfg := gethConfig{
126128
Eth: ethconfig.Defaults,
@@ -137,12 +139,18 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
137139

138140
// Apply flags.
139141
utils.SetNodeConfig(ctx, &cfg.Node)
142+
return cfg
143+
}
144+
145+
// makeConfigNode loads geth configuration and creates a blank node instance.
146+
func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
147+
cfg := loadBaseConfig(ctx)
140148
stack, err := node.New(&cfg.Node)
141149
if err != nil {
142150
utils.Fatalf("Failed to create the protocol stack: %v", err)
143151
}
144152
// Node doesn't by default populate account manager backends
145-
if err := setAccountManagerBackends(stack); err != nil {
153+
if err := setAccountManagerBackends(stack.Config(), stack.AccountManager(), stack.KeyStoreDir()); err != nil {
146154
utils.Fatalf("Failed to set account manager backends: %v", err)
147155
}
148156

@@ -269,10 +277,7 @@ func deprecated(field string) bool {
269277
}
270278
}
271279

272-
func setAccountManagerBackends(stack *node.Node) error {
273-
conf := stack.Config()
274-
am := stack.AccountManager()
275-
keydir := stack.KeyStoreDir()
280+
func setAccountManagerBackends(conf *node.Config, am *accounts.Manager, keydir string) error {
276281
scryptN := keystore.StandardScryptN
277282
scryptP := keystore.StandardScryptP
278283
if conf.UseLightweightKDF {

node/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,10 @@ func (c *Config) KeyDirConfig() (string, error) {
448448
return keydir, err
449449
}
450450

451-
// getKeyStoreDir retrieves the key directory and will create
451+
// GetKeyStoreDir retrieves the key directory and will create
452452
// and ephemeral one if necessary.
453-
func getKeyStoreDir(conf *Config) (string, bool, error) {
454-
keydir, err := conf.KeyDirConfig()
453+
func (c *Config) GetKeyStoreDir() (string, bool, error) {
454+
keydir, err := c.KeyDirConfig()
455455
if err != nil {
456456
return "", false, err
457457
}

node/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func New(conf *Config) (*Node, error) {
119119
if err := node.openDataDir(); err != nil {
120120
return nil, err
121121
}
122-
keyDir, isEphem, err := getKeyStoreDir(conf)
122+
keyDir, isEphem, err := conf.GetKeyStoreDir()
123123
if err != nil {
124124
return nil, err
125125
}

0 commit comments

Comments
 (0)