2019-08-13 15:16:03 -07:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-07-30 07:53:02 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/kv"
|
2019-08-13 15:16:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
)
|
|
|
|
|
2020-04-21 14:33:56 -07:00
|
|
|
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
|
|
|
|
// Value to the corresponding staking type.
|
2020-07-30 07:53:02 -07:00
|
|
|
func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string {
|
|
|
|
return func(kvA, kvB kv.Pair) string {
|
2020-04-21 14:33:56 -07:00
|
|
|
switch {
|
|
|
|
case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey):
|
|
|
|
var powerA, powerB sdk.IntProto
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-21 14:33:56 -07:00
|
|
|
cdc.MustUnmarshalBinaryBare(kvA.Value, &powerA)
|
|
|
|
cdc.MustUnmarshalBinaryBare(kvB.Value, &powerB)
|
|
|
|
|
2020-05-02 12:26:59 -07:00
|
|
|
return fmt.Sprintf("%v\n%v", powerA, powerB)
|
2020-04-21 14:33:56 -07:00
|
|
|
case bytes.Equal(kvA.Key[:1], types.ValidatorsKey):
|
|
|
|
var validatorA, validatorB types.Validator
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-21 14:33:56 -07:00
|
|
|
cdc.MustUnmarshalBinaryBare(kvA.Value, &validatorA)
|
|
|
|
cdc.MustUnmarshalBinaryBare(kvB.Value, &validatorB)
|
|
|
|
|
2020-05-02 12:26:59 -07:00
|
|
|
return fmt.Sprintf("%v\n%v", validatorA, validatorB)
|
2020-04-21 14:33:56 -07:00
|
|
|
case bytes.Equal(kvA.Key[:1], types.LastValidatorPowerKey),
|
|
|
|
bytes.Equal(kvA.Key[:1], types.ValidatorsByConsAddrKey),
|
|
|
|
bytes.Equal(kvA.Key[:1], types.ValidatorsByPowerIndexKey):
|
|
|
|
return fmt.Sprintf("%v\n%v", sdk.ValAddress(kvA.Value), sdk.ValAddress(kvB.Value))
|
|
|
|
|
|
|
|
case bytes.Equal(kvA.Key[:1], types.DelegationKey):
|
|
|
|
var delegationA, delegationB types.Delegation
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-21 14:33:56 -07:00
|
|
|
cdc.MustUnmarshalBinaryBare(kvA.Value, &delegationA)
|
|
|
|
cdc.MustUnmarshalBinaryBare(kvB.Value, &delegationB)
|
|
|
|
|
2020-05-02 12:26:59 -07:00
|
|
|
return fmt.Sprintf("%v\n%v", delegationA, delegationB)
|
2020-04-21 14:33:56 -07:00
|
|
|
case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey),
|
|
|
|
bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey):
|
|
|
|
var ubdA, ubdB types.UnbondingDelegation
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-21 14:33:56 -07:00
|
|
|
cdc.MustUnmarshalBinaryBare(kvA.Value, &ubdA)
|
|
|
|
cdc.MustUnmarshalBinaryBare(kvB.Value, &ubdB)
|
|
|
|
|
2020-05-02 12:26:59 -07:00
|
|
|
return fmt.Sprintf("%v\n%v", ubdA, ubdB)
|
2020-04-21 14:33:56 -07:00
|
|
|
case bytes.Equal(kvA.Key[:1], types.RedelegationKey),
|
|
|
|
bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey):
|
|
|
|
var redA, redB types.Redelegation
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-04-21 14:33:56 -07:00
|
|
|
cdc.MustUnmarshalBinaryBare(kvA.Value, &redA)
|
|
|
|
cdc.MustUnmarshalBinaryBare(kvB.Value, &redB)
|
|
|
|
|
2020-05-02 12:26:59 -07:00
|
|
|
return fmt.Sprintf("%v\n%v", redA, redB)
|
2020-04-21 14:33:56 -07:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1]))
|
|
|
|
}
|
2019-08-13 15:16:03 -07:00
|
|
|
}
|
|
|
|
}
|