github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/simulation/decoder_test.go (about)

     1  package simulation_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"cosmossdk.io/math"
    11  
    12  	"github.com/cosmos/cosmos-sdk/codec/address"
    13  	"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
    14  	sdk "github.com/cosmos/cosmos-sdk/types"
    15  	"github.com/cosmos/cosmos-sdk/types/kv"
    16  	"github.com/cosmos/cosmos-sdk/types/module/testutil"
    17  	"github.com/cosmos/cosmos-sdk/x/staking/simulation"
    18  	"github.com/cosmos/cosmos-sdk/x/staking/types"
    19  )
    20  
    21  var (
    22  	delPk1   = ed25519.GenPrivKey().PubKey()
    23  	delAddr1 = sdk.AccAddress(delPk1.Address())
    24  	valAddr1 = sdk.ValAddress(delPk1.Address())
    25  )
    26  
    27  func TestDecodeStore(t *testing.T) {
    28  	cdc := testutil.MakeTestEncodingConfig().Codec
    29  	dec := simulation.NewDecodeStore(cdc)
    30  	bondTime := time.Now().UTC()
    31  
    32  	val, err := types.NewValidator(valAddr1.String(), delPk1, types.NewDescription("test", "test", "test", "test", "test"))
    33  	require.NoError(t, err)
    34  	del := types.NewDelegation(delAddr1.String(), valAddr1.String(), math.LegacyOneDec())
    35  	ubd := types.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, math.OneInt(), 1, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
    36  	red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, math.OneInt(), math.LegacyOneDec(), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
    37  
    38  	kvPairs := kv.Pairs{
    39  		Pairs: []kv.Pair{
    40  			{Key: types.LastTotalPowerKey, Value: cdc.MustMarshal(&sdk.IntProto{Int: math.OneInt()})},
    41  			{Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshal(&val)},
    42  			{Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()},
    43  			{Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshal(&del)},
    44  			{Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshal(&ubd)},
    45  			{Key: types.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshal(&red)},
    46  			{Key: []byte{0x99}, Value: []byte{0x99}},
    47  		},
    48  	}
    49  
    50  	tests := []struct {
    51  		name        string
    52  		expectedLog string
    53  	}{
    54  		{"LastTotalPower", fmt.Sprintf("%v\n%v", math.OneInt(), math.OneInt())},
    55  		{"Validator", fmt.Sprintf("%v\n%v", val, val)},
    56  		{"LastValidatorPower/ValidatorsByConsAddr/ValidatorsByPowerIndex", fmt.Sprintf("%v\n%v", valAddr1, valAddr1)},
    57  		{"Delegation", fmt.Sprintf("%v\n%v", del, del)},
    58  		{"UnbondingDelegation", fmt.Sprintf("%v\n%v", ubd, ubd)},
    59  		{"Redelegation", fmt.Sprintf("%v\n%v", red, red)},
    60  		{"other", ""},
    61  	}
    62  	for i, tt := range tests {
    63  		i, tt := i, tt
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			switch i {
    66  			case len(tests) - 1:
    67  				require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name)
    68  			default:
    69  				require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name)
    70  			}
    71  		})
    72  	}
    73  }