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

     1  package simulation_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"cosmossdk.io/math"
    10  
    11  	"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
    12  	sdk "github.com/cosmos/cosmos-sdk/types"
    13  	"github.com/cosmos/cosmos-sdk/types/kv"
    14  	moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
    15  	"github.com/cosmos/cosmos-sdk/x/distribution"
    16  	"github.com/cosmos/cosmos-sdk/x/distribution/simulation"
    17  	"github.com/cosmos/cosmos-sdk/x/distribution/types"
    18  )
    19  
    20  var (
    21  	delPk1    = ed25519.GenPrivKey().PubKey()
    22  	delAddr1  = sdk.AccAddress(delPk1.Address())
    23  	valAddr1  = sdk.ValAddress(delPk1.Address())
    24  	consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes())
    25  )
    26  
    27  func TestDecodeDistributionStore(t *testing.T) {
    28  	encodingConfig := moduletestutil.MakeTestEncodingConfig(distribution.AppModuleBasic{})
    29  	cdc := encodingConfig.Codec
    30  
    31  	dec := simulation.NewDecodeStore(cdc)
    32  
    33  	decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyOneDec())}
    34  	feePool := types.InitialFeePool()
    35  	feePool.CommunityPool = decCoins
    36  	info := types.NewDelegatorStartingInfo(2, math.LegacyOneDec(), 200)
    37  	outstanding := types.ValidatorOutstandingRewards{Rewards: decCoins}
    38  	commission := types.ValidatorAccumulatedCommission{Commission: decCoins}
    39  	historicalRewards := types.NewValidatorHistoricalRewards(decCoins, 100)
    40  	currentRewards := types.NewValidatorCurrentRewards(decCoins, 5)
    41  	slashEvent := types.NewValidatorSlashEvent(10, math.LegacyOneDec())
    42  
    43  	kvPairs := kv.Pairs{
    44  		Pairs: []kv.Pair{
    45  			{Key: types.FeePoolKey, Value: cdc.MustMarshal(&feePool)},
    46  			{Key: types.ProposerKey, Value: consAddr1.Bytes()},
    47  			{Key: types.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshal(&outstanding)},
    48  			{Key: types.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()},
    49  			{Key: types.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshal(&info)},
    50  			{Key: types.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshal(&historicalRewards)},
    51  			{Key: types.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshal(&currentRewards)},
    52  			{Key: types.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshal(&commission)},
    53  			{Key: types.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshal(&slashEvent)},
    54  			{Key: []byte{0x99}, Value: []byte{0x99}},
    55  		},
    56  	}
    57  
    58  	tests := []struct {
    59  		name        string
    60  		expectedLog string
    61  	}{
    62  		{"FeePool", fmt.Sprintf("%v\n%v", feePool, feePool)},
    63  		{"Proposer", fmt.Sprintf("%v\n%v", consAddr1, consAddr1)},
    64  		{"ValidatorOutstandingRewards", fmt.Sprintf("%v\n%v", outstanding, outstanding)},
    65  		{"DelegatorWithdrawAddr", fmt.Sprintf("%v\n%v", delAddr1, delAddr1)},
    66  		{"DelegatorStartingInfo", fmt.Sprintf("%v\n%v", info, info)},
    67  		{"ValidatorHistoricalRewards", fmt.Sprintf("%v\n%v", historicalRewards, historicalRewards)},
    68  		{"ValidatorCurrentRewards", fmt.Sprintf("%v\n%v", currentRewards, currentRewards)},
    69  		{"ValidatorAccumulatedCommission", fmt.Sprintf("%v\n%v", commission, commission)},
    70  		{"ValidatorSlashEvent", fmt.Sprintf("%v\n%v", slashEvent, slashEvent)},
    71  		{"other", ""},
    72  	}
    73  	for i, tt := range tests {
    74  		i, tt := i, tt
    75  		t.Run(tt.name, func(t *testing.T) {
    76  			switch i {
    77  			case len(tests) - 1:
    78  				require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name)
    79  			default:
    80  				require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name)
    81  			}
    82  		})
    83  	}
    84  }