Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/geth: accountcmd no need to acquire the datadir lock #27084

Merged
merged 18 commits into from
Apr 27, 2023
Merged
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 46 additions & 36 deletions cmd/geth/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -188,14 +189,45 @@ nodes.
}
)

// getAccountConfig gets accountcmd related config options
func getAccountConfig(ctx *cli.Context) node.Config {
cfg := node.DefaultConfig

// Apply flags.
utils.SetDataDir(ctx, &cfg)
if ctx.IsSet(utils.KeyStoreDirFlag.Name) {
cfg.KeyStoreDir = ctx.String(utils.KeyStoreDirFlag.Name)
}
if ctx.IsSet(utils.LightKDFFlag.Name) {
cfg.UseLightweightKDF = ctx.Bool(utils.LightKDFFlag.Name)
}

return cfg
}

// makeKeyStore creates a keystore with given datadir and keystore dir
func makeKeyStore(ctx *cli.Context) *keystore.KeyStore {
cfg := getAccountConfig(ctx)
keydir, err := cfg.KeyDirConfig()
if err != nil {
utils.Fatalf("Failed to get the keystore directory: %v", err)
}

scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP
if cfg.UseLightweightKDF {
scryptN = keystore.LightScryptN
scryptP = keystore.LightScryptP
}
ks := keystore.NewKeyStore(keydir, scryptN, scryptP)

return ks
}

func accountList(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
var index int
for _, wallet := range stack.AccountManager().Wallets() {
for _, account := range wallet.Accounts() {
fmt.Printf("Account #%d: {%x} %s\n", index, account.Address, &account.URL)
index++
s1na marked this conversation as resolved.
Show resolved Hide resolved
}
ks := makeKeyStore(ctx)
for index, account := range ks.Accounts() {
fmt.Printf("Account #%d: {%x} %s\n", index, account.Address, &account.URL)
}
return nil
}
Expand Down Expand Up @@ -258,21 +290,14 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr

// accountCreate creates a new account into the keystore defined by the CLI flags.
func accountCreate(ctx *cli.Context) error {
cfg := gethConfig{Node: defaultNodeConfig()}
// Load config file.
if file := ctx.String(configFileFlag.Name); file != "" {
if err := loadConfig(file, &cfg); err != nil {
utils.Fatalf("%v", err)
}
}
utils.SetNodeConfig(ctx, &cfg.Node)
keydir, err := cfg.Node.KeyDirConfig()
cfg := getAccountConfig(ctx)
keydir, err := cfg.KeyDirConfig()
if err != nil {
utils.Fatalf("Failed to read configuration: %v", err)
utils.Fatalf("Failed to get the keystore directory: %v", err)
}
scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP
if cfg.Node.UseLightweightKDF {
if cfg.UseLightweightKDF {
scryptN = keystore.LightScryptN
scryptP = keystore.LightScryptP
}
Expand Down Expand Up @@ -300,12 +325,7 @@ func accountUpdate(ctx *cli.Context) error {
if ctx.Args().Len() == 0 {
utils.Fatalf("No accounts specified to update")
}
stack, _ := makeConfigNode(ctx)
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 {
utils.Fatalf("Keystore is not available")
}
ks := backends[0].(*keystore.KeyStore)
ks := makeKeyStore(ctx)
jsvisa marked this conversation as resolved.
Show resolved Hide resolved

for _, addr := range ctx.Args().Slice() {
account, oldPassword := unlockAccount(ks, addr, 0, nil)
Expand All @@ -327,14 +347,9 @@ func importWallet(ctx *cli.Context) error {
utils.Fatalf("Could not read wallet file: %v", err)
}

stack, _ := makeConfigNode(ctx)
ks := makeKeyStore(ctx)
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))

backends := stack.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 {
utils.Fatalf("Keystore is not available")
}
ks := backends[0].(*keystore.KeyStore)
acct, err := ks.ImportPreSaleKey(keyJSON, passphrase)
if err != nil {
utils.Fatalf("%v", err)
Expand All @@ -352,14 +367,9 @@ func accountImport(ctx *cli.Context) error {
if err != nil {
utils.Fatalf("Failed to load the private key: %v", err)
}
stack, _ := makeConfigNode(ctx)
ks := makeKeyStore(ctx)
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))

backends := stack.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 {
utils.Fatalf("Keystore is not available")
}
ks := backends[0].(*keystore.KeyStore)
acct, err := ks.ImportECDSA(key, passphrase)
if err != nil {
utils.Fatalf("Could not create the account: %v", err)
Expand Down