github.com/cosmos/cosmos-sdk@v0.50.10/x/slashing/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/slashing/migrations/v1"
    16  	v2 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v2"
    17  	"github.com/cosmos/cosmos-sdk/x/slashing/types"
    18  )
    19  
    20  func TestStoreMigration(t *testing.T) {
    21  	slashingKey := storetypes.NewKVStoreKey("slashing")
    22  	ctx := testutil.DefaultContext(slashingKey, storetypes.NewTransientStoreKey("transient_test"))
    23  	storeService := runtime.NewKVStoreService(slashingKey)
    24  	store := storeService.OpenKVStore(ctx)
    25  
    26  	_, _, addr1 := testdata.KeyTestPubAddr()
    27  	consAddr := sdk.ConsAddress(addr1)
    28  	// Use dummy value for all keys.
    29  	value := []byte("foo")
    30  
    31  	testCases := []struct {
    32  		name   string
    33  		oldKey []byte
    34  		newKey []byte
    35  	}{
    36  		{
    37  			"ValidatorSigningInfoKey",
    38  			v1.ValidatorSigningInfoKey(consAddr),
    39  			types.ValidatorSigningInfoKey(consAddr),
    40  		},
    41  		{
    42  			"ValidatorMissedBlockBitArrayKey",
    43  			v1.ValidatorMissedBlockBitArrayKey(consAddr, 2),
    44  			v2.ValidatorMissedBlockBitArrayKey(consAddr, 2),
    45  		},
    46  		{
    47  			"AddrPubkeyRelationKey",
    48  			v1.AddrPubkeyRelationKey(consAddr),
    49  			types.AddrPubkeyRelationKey(consAddr),
    50  		},
    51  	}
    52  
    53  	// Set all the old keys to the store
    54  	for _, tc := range testCases {
    55  		err := store.Set(tc.oldKey, value)
    56  		require.NoError(t, err)
    57  	}
    58  
    59  	// Run migrations.
    60  	err := v2.MigrateStore(ctx, storeService)
    61  	require.NoError(t, err)
    62  
    63  	// Make sure the new keys are set and old keys are deleted.
    64  	for _, tc := range testCases {
    65  		tc := tc
    66  		t.Run(tc.name, func(t *testing.T) {
    67  			if !bytes.Equal(tc.oldKey, tc.newKey) {
    68  				v, err := store.Get(tc.oldKey)
    69  				require.NoError(t, err)
    70  				require.Nil(t, v)
    71  			}
    72  			v, err := store.Get(tc.newKey)
    73  			require.NoError(t, err)
    74  			require.Equal(t, value, v)
    75  		})
    76  	}
    77  }