github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/migrations/v2/store_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	storetypes "cosmossdk.io/store/types"
    11  
    12  	"github.com/cosmos/cosmos-sdk/codec/address"
    13  	sdktestuil "github.com/cosmos/cosmos-sdk/testutil"
    14  	"github.com/cosmos/cosmos-sdk/testutil/testdata"
    15  	sdk "github.com/cosmos/cosmos-sdk/types"
    16  	v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1"
    17  	v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2"
    18  	"github.com/cosmos/cosmos-sdk/x/staking/testutil"
    19  	"github.com/cosmos/cosmos-sdk/x/staking/types"
    20  )
    21  
    22  func TestStoreMigration(t *testing.T) {
    23  	stakingKey := storetypes.NewKVStoreKey("staking")
    24  	tStakingKey := storetypes.NewTransientStoreKey("transient_test")
    25  	ctx := sdktestuil.DefaultContext(stakingKey, tStakingKey)
    26  	store := ctx.KVStore(stakingKey)
    27  
    28  	_, pk1, addr1 := testdata.KeyTestPubAddr()
    29  	valAddr1 := sdk.ValAddress(addr1)
    30  	val := testutil.NewValidator(t, valAddr1, pk1)
    31  	_, _, addr2 := testdata.KeyTestPubAddr()
    32  	valAddr2 := sdk.ValAddress(addr2)
    33  	_, _, addr3 := testdata.KeyTestPubAddr()
    34  	consAddr := sdk.ConsAddress(addr3.String())
    35  	_, _, addr4 := testdata.KeyTestPubAddr()
    36  	now := time.Now()
    37  	// Use dummy value for all keys.
    38  	value := []byte("foo")
    39  
    40  	testCases := []struct {
    41  		name   string
    42  		oldKey []byte
    43  		newKey []byte
    44  	}{
    45  		{
    46  			"LastValidatorPowerKey",
    47  			v1.GetLastValidatorPowerKey(valAddr1),
    48  			types.GetLastValidatorPowerKey(valAddr1),
    49  		},
    50  		{
    51  			"LastTotalPowerKey",
    52  			v1.LastTotalPowerKey,
    53  			types.LastTotalPowerKey,
    54  		},
    55  		{
    56  			"ValidatorsKey",
    57  			v1.GetValidatorKey(valAddr1),
    58  			types.GetValidatorKey(valAddr1),
    59  		},
    60  		{
    61  			"ValidatorsByConsAddrKey",
    62  			v1.GetValidatorByConsAddrKey(consAddr),
    63  			types.GetValidatorByConsAddrKey(consAddr),
    64  		},
    65  		{
    66  			"ValidatorsByPowerIndexKey",
    67  			v1.GetValidatorsByPowerIndexKey(val),
    68  			types.GetValidatorsByPowerIndexKey(val, sdk.DefaultPowerReduction, address.NewBech32Codec("cosmosvaloper")),
    69  		},
    70  		{
    71  			"DelegationKey",
    72  			v1.GetDelegationKey(addr4, valAddr1),
    73  			types.GetDelegationKey(addr4, valAddr1),
    74  		},
    75  		{
    76  			"UnbondingDelegationKey",
    77  			v1.GetUBDKey(addr4, valAddr1),
    78  			types.GetUBDKey(addr4, valAddr1),
    79  		},
    80  		{
    81  			"UnbondingDelegationByValIndexKey",
    82  			v1.GetUBDByValIndexKey(addr4, valAddr1),
    83  			types.GetUBDByValIndexKey(addr4, valAddr1),
    84  		},
    85  		{
    86  			"RedelegationKey",
    87  			v1.GetREDKey(addr4, valAddr1, valAddr2),
    88  			types.GetREDKey(addr4, valAddr1, valAddr2),
    89  		},
    90  		{
    91  			"RedelegationByValSrcIndexKey",
    92  			v1.GetREDByValSrcIndexKey(addr4, valAddr1, valAddr2),
    93  			types.GetREDByValSrcIndexKey(addr4, valAddr1, valAddr2),
    94  		},
    95  		{
    96  			"RedelegationByValDstIndexKey",
    97  			v1.GetREDByValDstIndexKey(addr4, valAddr1, valAddr2),
    98  			types.GetREDByValDstIndexKey(addr4, valAddr1, valAddr2),
    99  		},
   100  		{
   101  			"UnbondingQueueKey",
   102  			v1.GetUnbondingDelegationTimeKey(now),
   103  			types.GetUnbondingDelegationTimeKey(now),
   104  		},
   105  		{
   106  			"RedelegationQueueKey",
   107  			v1.GetRedelegationTimeKey(now),
   108  			types.GetRedelegationTimeKey(now),
   109  		},
   110  		{
   111  			"ValidatorQueueKey",
   112  			v1.GetValidatorQueueKey(now, 4),
   113  			types.GetValidatorQueueKey(now, 4),
   114  		},
   115  		{
   116  			"HistoricalInfoKey",
   117  			v1.GetHistoricalInfoKey(4),
   118  			v2.GetHistoricalInfoKey(4),
   119  		},
   120  	}
   121  
   122  	// Set all the old keys to the store
   123  	for _, tc := range testCases {
   124  		store.Set(tc.oldKey, value)
   125  	}
   126  
   127  	// Run migrations.
   128  	err := v2.MigrateStore(ctx, store)
   129  	require.NoError(t, err)
   130  
   131  	// Make sure the new keys are set and old keys are deleted.
   132  	for _, tc := range testCases {
   133  		tc := tc
   134  		t.Run(tc.name, func(t *testing.T) {
   135  			if !bytes.Equal(tc.oldKey, tc.newKey) {
   136  				require.Nil(t, store.Get(tc.oldKey))
   137  			}
   138  			require.Equal(t, value, store.Get(tc.newKey))
   139  		})
   140  	}
   141  }