github.com/cosmos/cosmos-sdk@v0.50.10/x/distribution/migrations/v2/store_test.go (about) 1 package v2_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 storetypes "cosmossdk.io/store/types" 10 11 "github.com/cosmos/cosmos-sdk/runtime" 12 "github.com/cosmos/cosmos-sdk/testutil" 13 "github.com/cosmos/cosmos-sdk/testutil/testdata" 14 sdk "github.com/cosmos/cosmos-sdk/types" 15 v1 "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v1" 16 v2 "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v2" 17 "github.com/cosmos/cosmos-sdk/x/distribution/types" 18 ) 19 20 func TestStoreMigration(t *testing.T) { 21 distributionKey := storetypes.NewKVStoreKey("distribution") 22 storeService := runtime.NewKVStoreService(distributionKey) 23 ctx := testutil.DefaultContext(distributionKey, storetypes.NewTransientStoreKey("transient_test")) 24 store := ctx.KVStore(distributionKey) 25 26 _, _, addr1 := testdata.KeyTestPubAddr() 27 valAddr := sdk.ValAddress(addr1) 28 _, _, addr2 := testdata.KeyTestPubAddr() 29 // Use dummy value for all keys. 30 value := []byte("foo") 31 32 testCases := []struct { 33 name string 34 oldKey []byte 35 newKey []byte 36 }{ 37 { 38 "FeePoolKey", 39 v1.FeePoolKey, 40 types.FeePoolKey, 41 }, 42 { 43 "ProposerKey", 44 v1.ProposerKey, 45 types.ProposerKey, 46 }, 47 { 48 "ValidatorOutstandingRewards", 49 v1.GetValidatorOutstandingRewardsKey(valAddr), 50 types.GetValidatorOutstandingRewardsKey(valAddr), 51 }, 52 { 53 "DelegatorWithdrawAddr", 54 v1.GetDelegatorWithdrawAddrKey(addr2), 55 types.GetDelegatorWithdrawAddrKey(addr2), 56 }, 57 { 58 "DelegatorStartingInfo", 59 v1.GetDelegatorStartingInfoKey(valAddr, addr2), 60 types.GetDelegatorStartingInfoKey(valAddr, addr2), 61 }, 62 { 63 "ValidatorHistoricalRewards", 64 v1.GetValidatorHistoricalRewardsKey(valAddr, 6), 65 types.GetValidatorHistoricalRewardsKey(valAddr, 6), 66 }, 67 { 68 "ValidatorCurrentRewards", 69 v1.GetValidatorCurrentRewardsKey(valAddr), 70 types.GetValidatorCurrentRewardsKey(valAddr), 71 }, 72 { 73 "ValidatorAccumulatedCommission", 74 v1.GetValidatorAccumulatedCommissionKey(valAddr), 75 types.GetValidatorAccumulatedCommissionKey(valAddr), 76 }, 77 { 78 "ValidatorSlashEvent", 79 v1.GetValidatorSlashEventKey(valAddr, 6, 8), 80 types.GetValidatorSlashEventKey(valAddr, 6, 8), 81 }, 82 } 83 84 // Set all the old keys to the store 85 for _, tc := range testCases { 86 store.Set(tc.oldKey, value) 87 } 88 89 // Run migrations. 90 err := v2.MigrateStore(ctx, storeService) 91 require.NoError(t, err) 92 93 // Make sure the new keys are set and old keys are deleted. 94 for _, tc := range testCases { 95 tc := tc 96 t.Run(tc.name, func(t *testing.T) { 97 if !bytes.Equal(tc.oldKey, tc.newKey) { 98 require.Nil(t, store.Get(tc.oldKey)) 99 } 100 require.Equal(t, value, store.Get(tc.newKey)) 101 }) 102 } 103 }