Skip to content

Commit 4882f93

Browse files
authored
perf: reduce user's password prompts when calling keyring List function (cosmos#13207)
* Reduce user password prompts by taking advantage of the already existing MigrateAll function * Print message when no records were found on the keyring * Update changelog * Fix migration test * Add keys sort
1 parent 5912f75 commit 4882f93

File tree

5 files changed

+20
-46
lines changed

5 files changed

+20
-46
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
3939

4040
### Features
4141

42+
* (cli) [#13207](https://github.com/cosmos/cosmos-sdk/pull/13207) Reduce user's password prompts when calling keyring `List()` function
4243
* (x/authz) [#12648](https://github.com/cosmos/cosmos-sdk/pull/12648) Add an allow list, an optional list of addresses allowed to receive bank assets via authz MsgSend grant.
4344
* (sdk.Coins) [#12627](https://github.com/cosmos/cosmos-sdk/pull/12627) Make a Denoms method on sdk.Coins.
4445
* (testutil) [#12973](https://github.com/cosmos/cosmos-sdk/pull/12973) Add generic `testutil.RandSliceElem` function which selects a random element from the list.

client/keys/list.go

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func runListCmd(cmd *cobra.Command, _ []string) error {
3333
return err
3434
}
3535

36+
if len(records) == 0 {
37+
cmd.Println("No records were found in keyring")
38+
return nil
39+
}
40+
3641
if ok, _ := cmd.Flags().GetBool(flagListNames); !ok {
3742
return printKeyringRecords(cmd.OutOrStdout(), records, clientCtx.OutputFormat)
3843
}

client/keys/migrate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func runMigrateCmd(cmd *cobra.Command, _ []string) error {
3434
return err
3535
}
3636

37-
if err = clientCtx.Keyring.MigrateAll(); err != nil {
37+
if _, err = clientCtx.Keyring.MigrateAll(); err != nil {
3838
return err
3939
}
4040

crypto/keyring/keyring.go

+11-43
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ type Importer interface {
120120

121121
// Migrator is implemented by key stores and enables migration of keys from amino to proto
122122
type Migrator interface {
123-
MigrateAll() error
123+
MigrateAll() ([]*Record, error)
124124
}
125125

126126
// Exporter is implemented by key stores that support export of public and private keys.
@@ -517,43 +517,7 @@ func wrapKeyNotFound(err error, msg string) error {
517517
}
518518

519519
func (ks keystore) List() ([]*Record, error) {
520-
if err := ks.MigrateAll(); err != nil {
521-
return nil, err
522-
}
523-
524-
keys, err := ks.db.Keys()
525-
if err != nil {
526-
return nil, err
527-
}
528-
529-
var res []*Record //nolint:prealloc
530-
sort.Strings(keys)
531-
for _, key := range keys {
532-
// Recall that each key is twice in the keyring:
533-
// - once with the `.info` suffix, which holds the key info
534-
// - another time with the `.address` suffix, which only holds a reference to its associated `.info` key
535-
if !strings.HasSuffix(key, infoSuffix) {
536-
continue
537-
}
538-
539-
item, err := ks.db.Get(key)
540-
if err != nil {
541-
return nil, err
542-
}
543-
544-
if len(item.Data) == 0 {
545-
return nil, sdkerrors.ErrKeyNotFound.Wrap(key)
546-
}
547-
548-
k, err := ks.protoUnmarshalRecord(item.Data)
549-
if err != nil {
550-
return nil, err
551-
}
552-
553-
res = append(res, k)
554-
}
555-
556-
return res, nil
520+
return ks.MigrateAll()
557521
}
558522

559523
func (ks keystore) NewMnemonic(uid string, language Language, hdPath, bip39Passphrase string, algo SignatureAlgo) (*Record, string, error) {
@@ -895,30 +859,34 @@ func (ks keystore) writeMultisigKey(name string, pk types.PubKey) (*Record, erro
895859
return k, ks.writeRecord(k)
896860
}
897861

898-
func (ks keystore) MigrateAll() error {
862+
func (ks keystore) MigrateAll() ([]*Record, error) {
899863
keys, err := ks.db.Keys()
900864
if err != nil {
901-
return err
865+
return nil, err
902866
}
903867

904868
if len(keys) == 0 {
905-
return nil
869+
return nil, nil
906870
}
907871

872+
sort.Strings(keys)
873+
var recs []*Record
908874
for _, key := range keys {
909875
// The keyring items only with `.info` consists the key info.
910876
if !strings.HasSuffix(key, infoSuffix) {
911877
continue
912878
}
913879

914-
_, err := ks.migrate(key)
880+
rec, err := ks.migrate(key)
915881
if err != nil {
916882
fmt.Printf("migrate err for key %s: %q\n", key, err)
917883
continue
918884
}
885+
886+
recs = append(recs, rec)
919887
}
920888

921-
return nil
889+
return recs, nil
922890
}
923891

924892
// migrate converts keyring.Item from amino to proto serialization format.

crypto/keyring/migration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ func (s *MigrationTestSuite) TestMigrateAllLegacyMultiOffline() {
191191

192192
s.Require().NoError(s.ks.SetItem(item))
193193

194-
err = s.kb.MigrateAll()
194+
_, err = s.kb.MigrateAll()
195195
s.Require().NoError(err)
196196
}
197197

198198
func (s *MigrationTestSuite) TestMigrateAllNoItem() {
199-
err := s.kb.MigrateAll()
199+
_, err := s.kb.MigrateAll()
200200
s.Require().NoError(err)
201201
}
202202

0 commit comments

Comments
 (0)