Merge PR #4847: Sim refactor 1 move store decoders to modules
* move store decoders to modules * fix * module pattern * compile * update Decoders params * fix * address @colin-axner comments * Update cmd_test.go * simulation manager * mino fixes * cleanup * Apply suggestions from code review Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * address @rigelrozanski comments * changelog * Apply suggestions from code review Co-Authored-By: colin axner <colinaxner@berkeley.edu> * restructure modules simulation pkgs * address @alexanderbez comments * fix
This commit is contained in:
committed by
frog power 4000
co-authored by
frog power 4000
colin axner
parent
81f86fefe5
commit
c441ce2fab
@@ -0,0 +1,57 @@
|
||||
package simulation
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
cmn "github.com/tendermint/tendermint/libs/common"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
// DecodeStore unmarshals the KVPair's Value to the corresponding staking type
|
||||
func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
|
||||
switch {
|
||||
case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey):
|
||||
var powerA, powerB sdk.Int
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &powerA)
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &powerB)
|
||||
return fmt.Sprintf("%v\n%v", powerA, powerB)
|
||||
|
||||
case bytes.Equal(kvA.Key[:1], types.ValidatorsKey):
|
||||
var validatorA, validatorB types.Validator
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &validatorA)
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &validatorB)
|
||||
return fmt.Sprintf("%v\n%v", validatorA, validatorB)
|
||||
|
||||
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
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &delegationA)
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &delegationB)
|
||||
return fmt.Sprintf("%v\n%v", delegationA, delegationB)
|
||||
|
||||
case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey),
|
||||
bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey):
|
||||
var ubdA, ubdB types.UnbondingDelegation
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &ubdA)
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &ubdB)
|
||||
return fmt.Sprintf("%v\n%v", ubdA, ubdB)
|
||||
|
||||
case bytes.Equal(kvA.Key[:1], types.RedelegationKey),
|
||||
bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey):
|
||||
var redA, redB types.Redelegation
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &redA)
|
||||
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &redB)
|
||||
return fmt.Sprintf("%v\n%v", redA, redB)
|
||||
|
||||
default:
|
||||
panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1]))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package simulation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
cmn "github.com/tendermint/tendermint/libs/common"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
var (
|
||||
delPk1 = ed25519.GenPrivKey().PubKey()
|
||||
delAddr1 = sdk.AccAddress(delPk1.Address())
|
||||
valAddr1 = sdk.ValAddress(delPk1.Address())
|
||||
consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes())
|
||||
)
|
||||
|
||||
func makeTestCodec() (cdc *codec.Codec) {
|
||||
cdc = codec.New()
|
||||
sdk.RegisterCodec(cdc)
|
||||
codec.RegisterCrypto(cdc)
|
||||
types.RegisterCodec(cdc)
|
||||
return
|
||||
}
|
||||
|
||||
func TestDecodeStore(t *testing.T) {
|
||||
cdc := makeTestCodec()
|
||||
|
||||
bondTime := time.Now().UTC()
|
||||
|
||||
val := types.NewValidator(valAddr1, delPk1, types.NewDescription("test", "test", "test", "test", "test"))
|
||||
del := types.NewDelegation(delAddr1, valAddr1, sdk.OneDec())
|
||||
ubd := types.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, sdk.OneInt())
|
||||
red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec())
|
||||
|
||||
kvPairs := cmn.KVPairs{
|
||||
cmn.KVPair{Key: types.LastTotalPowerKey, Value: cdc.MustMarshalBinaryLengthPrefixed(sdk.OneInt())},
|
||||
cmn.KVPair{Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(val)},
|
||||
cmn.KVPair{Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()},
|
||||
cmn.KVPair{Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(del)},
|
||||
cmn.KVPair{Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(ubd)},
|
||||
cmn.KVPair{Key: types.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(red)},
|
||||
cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expectedLog string
|
||||
}{
|
||||
{"LastTotalPower", fmt.Sprintf("%v\n%v", sdk.OneInt(), sdk.OneInt())},
|
||||
{"Validator", fmt.Sprintf("%v\n%v", val, val)},
|
||||
{"LastValidatorPower/ValidatorsByConsAddr/ValidatorsByPowerIndex", fmt.Sprintf("%v\n%v", valAddr1, valAddr1)},
|
||||
{"Delegation", fmt.Sprintf("%v\n%v", del, del)},
|
||||
{"UnbondingDelegation", fmt.Sprintf("%v\n%v", ubd, ubd)},
|
||||
{"Redelegation", fmt.Sprintf("%v\n%v", red, red)},
|
||||
{"other", ""},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
switch i {
|
||||
case len(tests) - 1:
|
||||
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
|
||||
default:
|
||||
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package simulation
|
||||
package operations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
Reference in New Issue
Block a user