diff --git a/CHANGELOG.md b/CHANGELOG.md index b9815b66928..3d70f1886f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -167,6 +167,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* [\#10844](https://github.com/cosmos/cosmos-sdk/pull/10844) Automatic recovering non-consistent keyring storage during public key import * (store) [\#11117](https://github.com/cosmos/cosmos-sdk/pull/11117) Fix data race in store trace component * (cli) [\#11065](https://github.com/cosmos/cosmos-sdk/pull/11065) Ensure the `tendermint-validator-set` query command respects the `-o` output flag. * (grpc) [\#10985](https://github.com/cosmos/cosmos-sdk/pull/10992) The `/cosmos/tx/v1beta1/txs/{hash}` endpoint returns a 404 when a tx does not exist. diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 4011b2a2847..db4eb8fbb9c 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -810,18 +810,29 @@ func (ks keystore) writeRecord(k *Record) error { // existsInDb returns (true, nil) if either addr or name exist is in keystore DB. // On the other hand, it returns (false, error) if Get method returns error different from keyring.ErrKeyNotFound +// In case of inconsistent keyring, it recovers it automatically. func (ks keystore) existsInDb(addr sdk.Address, name string) (bool, error) { - - if _, err := ks.db.Get(addrHexKeyAsString(addr)); err == nil { - return true, nil // address lookup succeeds - info exists - } else if err != keyring.ErrKeyNotFound { - return false, err // received unexpected error - returns error + _, errAddr := ks.db.Get(addrHexKeyAsString(addr)) + if errAddr != nil && !errors.Is(errAddr, keyring.ErrKeyNotFound) { + return false, errAddr } - if _, err := ks.db.Get(name); err == nil { + _, errInfo := ks.db.Get(infoKey(name)) + if errInfo == nil { return true, nil // uid lookup succeeds - info exists - } else if err != keyring.ErrKeyNotFound { - return false, err // received unexpected error - returns + } else if !errors.Is(errInfo, keyring.ErrKeyNotFound) { + return false, errInfo // received unexpected error - returns + } + + // looking for an issue, record with meta (getByAddress) exists, but record with public key itself does not + if errAddr == nil && errors.Is(errInfo, keyring.ErrKeyNotFound) { + fmt.Fprintf(os.Stderr, "address \"%s\" exists but pubkey itself does not\n", hex.EncodeToString(addr.Bytes())) + fmt.Fprintln(os.Stderr, "recreating pubkey record") + err := ks.db.Remove(addrHexKeyAsString(addr)) + if err != nil { + return true, err + } + return false, nil } // both lookups failed, info does not exist diff --git a/crypto/keyring/keyring_test.go b/crypto/keyring/keyring_test.go index 3afd35676fb..ad3e109b74d 100644 --- a/crypto/keyring/keyring_test.go +++ b/crypto/keyring/keyring_test.go @@ -1064,6 +1064,43 @@ func TestAltKeyring_SaveOfflineKey(t *testing.T) { require.Len(t, list, 1) } +func TestNonConsistentKeyring_SavePubKey(t *testing.T) { + cdc := getCodec() + kr, err := New(t.Name(), BackendTest, t.TempDir(), nil, cdc) + require.NoError(t, err) + + list, err := kr.List() + require.NoError(t, err) + require.Empty(t, list) + + key := someKey + priv := ed25519.GenPrivKey() + pub := priv.PubKey() + + k, err := kr.SaveOfflineKey(key, pub) + require.Nil(t, err) + + // broken keyring state test + unsafeKr, ok := kr.(keystore) + require.True(t, ok) + // we lost public key for some reason, but still have an address record + unsafeKr.db.Remove(infoKey(key)) + list, err = kr.List() + require.NoError(t, err) + require.Equal(t, 0, len(list)) + + k, err = kr.SaveOfflineKey(key, pub) + require.Nil(t, err) + pubKey, err := k.GetPubKey() + require.NoError(t, err) + require.Equal(t, pub, pubKey) + require.Equal(t, key, k.Name) + + list, err = kr.List() + require.NoError(t, err) + require.Equal(t, 1, len(list)) +} + func TestAltKeyring_SaveMultisig(t *testing.T) { cdc := getCodec() kr, err := New(t.Name(), BackendTest, t.TempDir(), nil, cdc)