merged in master
This commit is contained in:
@@ -7,13 +7,14 @@ import (
|
||||
gogotypes "github.com/gogo/protobuf/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/kv"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||
)
|
||||
|
||||
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
|
||||
// Value to the corresponding slashing type.
|
||||
func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string {
|
||||
func NewDecodeStore(cdc codec.BinaryMarshaler) func(kvA, kvB kv.Pair) string {
|
||||
return func(kvA, kvB kv.Pair) string {
|
||||
switch {
|
||||
case bytes.Equal(kvA.Key[:1], types.ValidatorSigningInfoKeyPrefix):
|
||||
@@ -29,10 +30,14 @@ func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string {
|
||||
return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA.Value, missedB.Value)
|
||||
|
||||
case bytes.Equal(kvA.Key[:1], types.AddrPubkeyRelationKeyPrefix):
|
||||
var pubKeyA, pubKeyB gogotypes.StringValue
|
||||
cdc.MustUnmarshalBinaryBare(kvA.Value, &pubKeyA)
|
||||
cdc.MustUnmarshalBinaryBare(kvB.Value, &pubKeyB)
|
||||
return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", pubKeyA.Value, pubKeyB.Value)
|
||||
var pubKeyA, pubKeyB cryptotypes.PubKey
|
||||
if err := cdc.UnmarshalInterface(kvA.Value, &pubKeyA); err != nil {
|
||||
panic(fmt.Sprint("Can't unmarshal kvA; ", err))
|
||||
}
|
||||
if err := cdc.UnmarshalInterface(kvB.Value, &pubKeyB); err != nil {
|
||||
panic(fmt.Sprint("Can't unmarshal kvB; ", err))
|
||||
}
|
||||
return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", pubKeyA, pubKeyB)
|
||||
|
||||
default:
|
||||
panic(fmt.Sprintf("invalid slashing key prefix %X", kvA.Key[:1]))
|
||||
|
||||
@@ -29,34 +29,35 @@ func TestDecodeStore(t *testing.T) {
|
||||
dec := simulation.NewDecodeStore(cdc)
|
||||
|
||||
info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0)
|
||||
bechPK := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, delPk1)
|
||||
missed := gogotypes.BoolValue{Value: true}
|
||||
bz, err := cdc.MarshalInterface(delPk1)
|
||||
require.NoError(t, err)
|
||||
|
||||
kvPairs := kv.Pairs{
|
||||
Pairs: []kv.Pair{
|
||||
{Key: types.ValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryBare(&info)},
|
||||
{Key: types.ValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryBare(&missed)},
|
||||
{Key: types.AddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: bechPK})},
|
||||
{Key: []byte{0x99}, Value: []byte{0x99}},
|
||||
{Key: types.AddrPubkeyRelationKey(delAddr1), Value: bz},
|
||||
{Key: []byte{0x99}, Value: []byte{0x99}}, // This test should panic
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expectedLog string
|
||||
panics bool
|
||||
}{
|
||||
{"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info)},
|
||||
{"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v", missed.Value, missed.Value)},
|
||||
{"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPK, bechPK)},
|
||||
{"other", ""},
|
||||
{"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info), false},
|
||||
{"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v", missed.Value, missed.Value), false},
|
||||
{"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", delPk1, delPk1), false},
|
||||
{"other", "", true},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
i, tt := i, tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
switch i {
|
||||
case len(tests) - 1:
|
||||
if tt.panics {
|
||||
require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name)
|
||||
default:
|
||||
} else {
|
||||
require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user