From a3f494d9777193c71f49e6388c2d4d47b27de285 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Thu, 4 Apr 2019 17:13:35 +0800 Subject: [PATCH] cmd/geth: check before printing warning log --- cmd/geth/accountcmd.go | 4 ++-- cmd/geth/main.go | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/geth/accountcmd.go b/cmd/geth/accountcmd.go index 071be85397..940290899c 100644 --- a/cmd/geth/accountcmd.go +++ b/cmd/geth/accountcmd.go @@ -205,7 +205,7 @@ func accountList(ctx *cli.Context) error { } // tries unlocking the specified account a few times. -func unlockAccount(ctx *cli.Context, ks *keystore.KeyStore, address string, i int, passwords []string) (accounts.Account, string) { +func unlockAccount(ks *keystore.KeyStore, address string, i int, passwords []string) (accounts.Account, string) { account, err := utils.MakeAddress(ks, address) if err != nil { utils.Fatalf("Could not list accounts: %v", err) @@ -326,7 +326,7 @@ func accountUpdate(ctx *cli.Context) error { ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) for _, addr := range ctx.Args() { - account, oldPassword := unlockAccount(ctx, ks, addr, 0, nil) + account, oldPassword := unlockAccount(ks, addr, 0, nil) newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true, 0, nil) if err := ks.Update(account, oldPassword, newPassword); err != nil { utils.Fatalf("Could not update the account: %v", err) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index d3c60838da..c49caef06e 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -397,6 +397,17 @@ func startNode(ctx *cli.Context, stack *node.Node) { // unlockAccounts unlocks any account specifically requested. func unlockAccounts(ctx *cli.Context, stack *node.Node) { + var unlocks []string + inputs := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",") + for _, input := range inputs { + if trimmed := strings.TrimSpace(input); trimmed != "" { + unlocks = append(unlocks, trimmed) + } + } + // Short circuit if there is no account to unlock. + if len(unlocks) == 0 { + return + } // If insecure account unlocking is not allowed if node's APIs are exposed to external. // Print warning log to user and skip unlocking. if !stack.Config().InsecureUnlockAllowed && stack.Config().ExtRPCEnabled() { @@ -405,10 +416,7 @@ func unlockAccounts(ctx *cli.Context, stack *node.Node) { } ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) passwords := utils.MakePasswordList(ctx) - unlocks := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",") for i, account := range unlocks { - if trimmed := strings.TrimSpace(account); trimmed != "" { - unlockAccount(ctx, ks, trimmed, i, passwords) - } + unlockAccount(ks, account, i, passwords) } }