github.com/cosmos/cosmos-sdk@v0.50.10/x/slashing/migrations/v4/migrate_test.go (about)

     1  package v4_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bits-and-blooms/bitset"
     7  	gogotypes "github.com/cosmos/gogoproto/types"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	storetypes "cosmossdk.io/store/types"
    11  
    12  	"github.com/cosmos/cosmos-sdk/testutil"
    13  	sdk "github.com/cosmos/cosmos-sdk/types"
    14  	moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
    15  	"github.com/cosmos/cosmos-sdk/x/slashing"
    16  	v4 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v4"
    17  	slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
    18  )
    19  
    20  var consAddr = sdk.ConsAddress(sdk.AccAddress([]byte("addr1_______________")))
    21  
    22  func TestMigrate(t *testing.T) {
    23  	cdc := moduletestutil.MakeTestEncodingConfig(slashing.AppModuleBasic{}).Codec
    24  	storeKey := storetypes.NewKVStoreKey(slashingtypes.ModuleName)
    25  	tKey := storetypes.NewTransientStoreKey("transient_test")
    26  	ctx := testutil.DefaultContext(storeKey, tKey)
    27  	store := ctx.KVStore(storeKey)
    28  	params := slashingtypes.Params{SignedBlocksWindow: 100}
    29  
    30  	// store old signing info and bitmap entries
    31  	bz := cdc.MustMarshal(&slashingtypes.ValidatorSigningInfo{Address: consAddr.String()})
    32  	store.Set(v4.ValidatorSigningInfoKey(consAddr), bz)
    33  
    34  	for i := int64(0); i < params.SignedBlocksWindow; i++ {
    35  		// all even blocks are missed
    36  		missed := &gogotypes.BoolValue{Value: i%2 == 0}
    37  		bz := cdc.MustMarshal(missed)
    38  		store.Set(v4.ValidatorMissedBlockBitArrayKey(consAddr, i), bz)
    39  	}
    40  
    41  	err := v4.Migrate(ctx, cdc, store, params)
    42  	require.NoError(t, err)
    43  
    44  	for i := int64(0); i < params.SignedBlocksWindow; i++ {
    45  		chunkIndex := i / v4.MissedBlockBitmapChunkSize
    46  		chunk := store.Get(v4.ValidatorMissedBlockBitmapKey(consAddr, chunkIndex))
    47  		require.NotNil(t, chunk)
    48  
    49  		bs := bitset.New(uint(v4.MissedBlockBitmapChunkSize))
    50  		require.NoError(t, bs.UnmarshalBinary(chunk))
    51  
    52  		// ensure all even blocks are missed
    53  		bitIndex := uint(i % v4.MissedBlockBitmapChunkSize)
    54  		require.Equal(t, i%2 == 0, bs.Test(bitIndex))
    55  		require.Equal(t, i%2 == 1, !bs.Test(bitIndex))
    56  	}
    57  
    58  	// ensure there's only one chunk for a window of size 100
    59  	chunk := store.Get(v4.ValidatorMissedBlockBitmapKey(consAddr, 1))
    60  	require.Nil(t, chunk)
    61  }