Skip to content

Commit d10331a

Browse files
raynaudoemergify[bot]
authored andcommitted
perf: reduce user's password prompts when calling keyring List function (#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 (cherry picked from commit 4882f93) # Conflicts: # CHANGELOG.md
1 parent 9454b97 commit d10331a

File tree

5 files changed

+27
-46
lines changed

5 files changed

+27
-46
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ Ref: https://keepachangelog.com/en/1.0.0/
4343

4444
### Features
4545

46+
<<<<<<< HEAD
4647
* (cli) [#13304](https://github.com/cosmos/cosmos-sdk/pull/13304) Add `tx gov draft-proposal` command for generating proposal JSONs.
48+
=======
49+
* (cli) [#13207](https://github.com/cosmos/cosmos-sdk/pull/13207) Reduce user's password prompts when calling keyring `List()` function
50+
* (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.
51+
* (sdk.Coins) [#12627](https://github.com/cosmos/cosmos-sdk/pull/12627) Make a Denoms method on sdk.Coins.
52+
* (testutil) [#12973](https://github.com/cosmos/cosmos-sdk/pull/12973) Add generic `testutil.RandSliceElem` function which selects a random element from the list.
53+
* (client) [#12936](https://github.com/cosmos/cosmos-sdk/pull/12936) Add capability to preprocess transactions before broadcasting from a higher level chain.
54+
>>>>>>> 4882f933b (perf: reduce user's password prompts when calling keyring List function (#13207))
4755
* (x/authz) [#13047](https://github.com/cosmos/cosmos-sdk/pull/13047) Add a GetAuthorization function to the keeper.
4856
* (cli) [#12742](https://github.com/cosmos/cosmos-sdk/pull/12742) Add the `prune` CLI cmd to manually prune app store history versions based on the pruning options.
4957

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.
@@ -492,43 +492,7 @@ func wrapKeyNotFound(err error, msg string) error {
492492
}
493493

494494
func (ks keystore) List() ([]*Record, error) {
495-
if err := ks.MigrateAll(); err != nil {
496-
return nil, err
497-
}
498-
499-
keys, err := ks.db.Keys()
500-
if err != nil {
501-
return nil, err
502-
}
503-
504-
var res []*Record //nolint:prealloc
505-
sort.Strings(keys)
506-
for _, key := range keys {
507-
// Recall that each key is twice in the keyring:
508-
// - once with the `.info` suffix, which holds the key info
509-
// - another time with the `.address` suffix, which only holds a reference to its associated `.info` key
510-
if !strings.HasSuffix(key, infoSuffix) {
511-
continue
512-
}
513-
514-
item, err := ks.db.Get(key)
515-
if err != nil {
516-
return nil, err
517-
}
518-
519-
if len(item.Data) == 0 {
520-
return nil, sdkerrors.ErrKeyNotFound.Wrap(key)
521-
}
522-
523-
k, err := ks.protoUnmarshalRecord(item.Data)
524-
if err != nil {
525-
return nil, err
526-
}
527-
528-
res = append(res, k)
529-
}
530-
531-
return res, nil
495+
return ks.MigrateAll()
532496
}
533497

534498
func (ks keystore) NewMnemonic(uid string, language Language, hdPath, bip39Passphrase string, algo SignatureAlgo) (*Record, string, error) {
@@ -870,30 +834,34 @@ func (ks keystore) writeMultisigKey(name string, pk types.PubKey) (*Record, erro
870834
return k, ks.writeRecord(k)
871835
}
872836

873-
func (ks keystore) MigrateAll() error {
837+
func (ks keystore) MigrateAll() ([]*Record, error) {
874838
keys, err := ks.db.Keys()
875839
if err != nil {
876-
return err
840+
return nil, err
877841
}
878842

879843
if len(keys) == 0 {
880-
return nil
844+
return nil, nil
881845
}
882846

847+
sort.Strings(keys)
848+
var recs []*Record
883849
for _, key := range keys {
884850
// The keyring items only with `.info` consists the key info.
885851
if !strings.HasSuffix(key, infoSuffix) {
886852
continue
887853
}
888854

889-
_, err := ks.migrate(key)
855+
rec, err := ks.migrate(key)
890856
if err != nil {
891857
fmt.Printf("migrate err for key %s: %q\n", key, err)
892858
continue
893859
}
860+
861+
recs = append(recs, rec)
894862
}
895863

896-
return nil
864+
return recs, nil
897865
}
898866

899867
// 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)