refactor: remove client/keys.KeysCdc (#13781)

This commit is contained in:
Jacob Kim 2022-11-09 16:23:55 +09:00 committed by GitHub
parent 22f3261285
commit f8e40d7c05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 8 additions and 129 deletions

View File

@ -85,6 +85,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#13236](https://github.com/cosmos/cosmos-sdk/pull/13236) Integrate Filter Logging
* [#13528](https://github.com/cosmos/cosmos-sdk/pull/13528) Update `ValidateMemoDecorator` to only check memo against `MaxMemoCharacters` param when a memo is present.
* [#13651](https://github.com/cosmos/cosmos-sdk/pull/13651) Update `server/config/config.GetConfig` function.
* [#13781](https://github.com/cosmos/cosmos-sdk/pull/13781) Remove `client/keys.KeysCdc`.
* [#13803](https://github.com/cosmos/cosmos-sdk/pull/13803) Add an error log if iavl set operation failed.
### State Machine Breaking

View File

@ -3,6 +3,7 @@ package keys
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"sort"
@ -313,7 +314,7 @@ func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemo
out.Mnemonic = mnemonic
}
jsonString, err := KeysCdc.MarshalJSON(out)
jsonString, err := json.Marshal(out)
if err != nil {
return err
}

View File

@ -1,27 +0,0 @@
package keys
import (
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
)
// TODO: remove this file https://github.com/cosmos/cosmos-sdk/issues/8047
// KeysCdc defines codec to be used with key operations
var KeysCdc *codec.LegacyAmino
func init() {
KeysCdc = codec.NewLegacyAmino()
cryptocodec.RegisterCrypto(KeysCdc)
KeysCdc.Seal()
}
// marshal keys
func MarshalJSON(o interface{}) ([]byte, error) {
return KeysCdc.MarshalJSON(o)
}
// unmarshal json
func UnmarshalJSON(bz []byte, ptr interface{}) error {
return KeysCdc.UnmarshalJSON(bz, ptr)
}

View File

@ -1,97 +0,0 @@
package keys_test
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)
type testCases struct {
Keys []keyring.KeyOutput
Answers []keyring.KeyOutput
JSON [][]byte
}
func getTestCases() testCases {
return testCases{
// nolint:govet
[]keyring.KeyOutput{
{"A", "B", "C", "D", "E"},
{"A", "B", "C", "D", ""},
{"", "B", "C", "D", ""},
{"", "", "", "", ""},
},
make([]keyring.KeyOutput, 4),
[][]byte{
[]byte(`{"name":"A","type":"B","address":"C","pubkey":"D","mnemonic":"E"}`),
[]byte(`{"name":"A","type":"B","address":"C","pubkey":"D"}`),
[]byte(`{"name":"","type":"B","address":"C","pubkey":"D"}`),
[]byte(`{"name":"","type":"","address":"","pubkey":""}`),
},
}
}
func TestMarshalJSON(t *testing.T) {
type args struct {
o keyring.KeyOutput
}
data := getTestCases()
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{"basic", args{data.Keys[0]}, data.JSON[0], false},
{"mnemonic is optional", args{data.Keys[1]}, data.JSON[1], false},
// REVIEW: Are the next results expected??
{"empty name", args{data.Keys[2]}, data.JSON[2], false},
{"empty object", args{data.Keys[3]}, data.JSON[3], false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got, err := keys.MarshalJSON(tt.args.o)
require.Equal(t, tt.wantErr, err != nil)
require.True(t, bytes.Equal(got, tt.want))
})
}
}
func TestUnmarshalJSON(t *testing.T) {
type args struct {
bz []byte
ptr interface{}
}
data := getTestCases()
tests := []struct {
name string
args args
wantErr bool
}{
{"basic", args{data.JSON[0], &data.Answers[0]}, false},
{"mnemonic is optional", args{data.JSON[1], &data.Answers[1]}, false},
// REVIEW: Are the next results expected??
{"empty name", args{data.JSON[2], &data.Answers[2]}, false},
{"empty object", args{data.JSON[3], &data.Answers[3]}, false},
}
for idx, tt := range tests {
idx, tt := idx, tt
t.Run(tt.name, func(t *testing.T) {
err := keys.UnmarshalJSON(tt.args.bz, tt.args.ptr)
require.Equal(t, tt.wantErr, err != nil)
// Confirm deserialized objects are the same
require.Equal(t, data.Keys[idx], data.Answers[idx])
})
}
}

View File

@ -2,6 +2,7 @@ package keys
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
@ -140,7 +141,7 @@ func displayParseKeyInfo(w io.Writer, stringer fmt.Stringer, output string) {
out, err = yaml.Marshal(&stringer)
case OutputFormatJSON:
out, err = KeysCdc.MarshalJSON(stringer)
out, err = json.Marshal(&stringer)
}
if err != nil {

View File

@ -1,6 +1,7 @@
package keys
import (
"encoding/json"
"fmt"
"io"
@ -30,7 +31,7 @@ func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKey
}
case OutputFormatJSON:
out, err := KeysCdc.MarshalJSON(ko)
out, err := json.Marshal(ko)
if err != nil {
return err
}
@ -57,8 +58,7 @@ func printKeyringRecords(w io.Writer, records []*cryptokeyring.Record, output st
case OutputFormatJSON:
// TODO https://github.com/cosmos/cosmos-sdk/issues/8046
// Replace AminoCdc with Proto JSON
out, err := KeysCdc.MarshalJSON(kos)
out, err := json.Marshal(kos)
if err != nil {
return err
}