From 345fd8da0a2e114ff562c2f09e527d7248b7fa8a Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Mon, 3 Apr 2023 14:47:51 +0800 Subject: [PATCH 1/2] cmd, miner, singer: avoid panic if keystore is not available --- cmd/geth/accountcmd.go | 18 +++++++++++++++--- cmd/geth/main.go | 6 +++++- miner/stress/beacon/main.go | 6 +++++- signer/core/uiapi.go | 6 +++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/cmd/geth/accountcmd.go b/cmd/geth/accountcmd.go index 5158b7606cde..a36da7d55f2c 100644 --- a/cmd/geth/accountcmd.go +++ b/cmd/geth/accountcmd.go @@ -301,7 +301,11 @@ func accountUpdate(ctx *cli.Context) error { utils.Fatalf("No accounts specified to update") } stack, _ := makeConfigNode(ctx) - ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + backends := stack.AccountManager().Backends(keystore.KeyStoreType) + if len(backends) == 0 { + utils.Fatalf("Keystore is not available") + } + ks := backends[0].(*keystore.KeyStore) for _, addr := range ctx.Args().Slice() { account, oldPassword := unlockAccount(ks, addr, 0, nil) @@ -326,7 +330,11 @@ func importWallet(ctx *cli.Context) error { stack, _ := makeConfigNode(ctx) passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx)) - ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + 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) @@ -347,7 +355,11 @@ func accountImport(ctx *cli.Context) error { stack, _ := makeConfigNode(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)) - ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + 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) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index a970e7652342..69a4e9513142 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -464,7 +464,11 @@ func unlockAccounts(ctx *cli.Context, stack *node.Node) { if !stack.Config().InsecureUnlockAllowed && stack.Config().ExtRPCEnabled() { utils.Fatalf("Account unlock with HTTP access is forbidden!") } - ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + backends := stack.AccountManager().Backends(keystore.KeyStoreType) + if len(backends) == 0 { + utils.Fatalf("Keystore is not available") + } + ks := backends[0].(*keystore.KeyStore) passwords := utils.MakePasswordList(ctx) for i, account := range unlocks { unlockAccount(ks, account, i, passwords) diff --git a/miner/stress/beacon/main.go b/miner/stress/beacon/main.go index 516862c9cb52..65318f1a0e4e 100644 --- a/miner/stress/beacon/main.go +++ b/miner/stress/beacon/main.go @@ -127,7 +127,11 @@ func newNode(typ nodetype, genesis *core.Genesis, enodes []*enode.Node) *ethNode // Inject the signer key and start sealing with it stack.AccountManager().AddBackend(keystore.NewPlaintextKeyStore("beacon-stress")) - store := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + ks := stack.AccountManager().Backends(keystore.KeyStoreType) + if len(ks) == 0 { + panic("Keystore is not available") + } + store := ks[0].(*keystore.KeyStore) if _, err := store.NewAccount(""); err != nil { panic(err) } diff --git a/signer/core/uiapi.go b/signer/core/uiapi.go index 59466d8fa030..924203a139a7 100644 --- a/signer/core/uiapi.go +++ b/signer/core/uiapi.go @@ -111,7 +111,11 @@ func (s *UIServerAPI) DeriveAccount(url string, path string, pin *bool) (account // fetchKeystore retrieves the encrypted keystore from the account manager. func fetchKeystore(am *accounts.Manager) *keystore.KeyStore { - return am.Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + ks := am.Backends(keystore.KeyStoreType) + if len(ks) == 0 { + return nil + } + return ks[0].(*keystore.KeyStore) } // ImportRawKey stores the given hex encoded ECDSA key into the key directory, From d7d72867895fe37fcb2ae9fe461ccd58475e5279 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Mon, 3 Apr 2023 14:55:41 +0800 Subject: [PATCH 2/2] cmd/geth: print warning instead of panic --- cmd/geth/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 69a4e9513142..f55adb1449da 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -466,7 +466,8 @@ func unlockAccounts(ctx *cli.Context, stack *node.Node) { } backends := stack.AccountManager().Backends(keystore.KeyStoreType) if len(backends) == 0 { - utils.Fatalf("Keystore is not available") + log.Warn("Failed to unlock accounts, keystore is not available") + return } ks := backends[0].(*keystore.KeyStore) passwords := utils.MakePasswordList(ctx)