github.com/Finschia/finschia-sdk@v0.48.1/x/staking/legacy/v043/store_test.go (about)

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