Skip to content

Commit

Permalink
chore: fix migration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed Jan 15, 2024
1 parent a2ebdd2 commit aa5a061
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
25 changes: 17 additions & 8 deletions x/evm/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/evmos/ethermint/x/evm/types"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/evmos/ethermint/encoding"
Expand All @@ -34,26 +35,34 @@ func TestMigrate(t *testing.T) {
storeKey := storetypes.NewKVStoreKey(types.ModuleName)
tKey := storetypes.NewTransientStoreKey(types.TransientKey)
ctx := testutil.DefaultContext(storeKey, tKey)
kvStore := ctx.KVStore(storeKey)
storeService := runtime.NewKVStoreService(storeKey)

legacySubspace := newMockSubspace(types.DefaultParams())
require.NoError(t, v4.MigrateStore(ctx, kvStore, legacySubspace, cdc))
require.NoError(t, v4.MigrateStore(ctx, storeService, legacySubspace, cdc))

kvStore := storeService.OpenKVStore(ctx)

// Get all the new parameters from the kvStore
var evmDenom string
bz := kvStore.Get(types.ParamStoreKeyEVMDenom)
bz, err := kvStore.Get(types.ParamStoreKeyEVMDenom)
require.NoError(t, err)
evmDenom = string(bz)

allowUnprotectedTx := kvStore.Has(types.ParamStoreKeyAllowUnprotectedTxs)
enableCreate := kvStore.Has(types.ParamStoreKeyEnableCreate)
enableCall := kvStore.Has(types.ParamStoreKeyEnableCall)
allowUnprotectedTx, err := kvStore.Has(types.ParamStoreKeyAllowUnprotectedTxs)
require.NoError(t, err)
enableCreate, err := kvStore.Has(types.ParamStoreKeyEnableCreate)
require.NoError(t, err)
enableCall, err := kvStore.Has(types.ParamStoreKeyEnableCall)
require.NoError(t, err)

var chainCfg v4types.V4ChainConfig
bz = kvStore.Get(types.ParamStoreKeyChainConfig)
bz, err = kvStore.Get(types.ParamStoreKeyChainConfig)
require.NoError(t, err)
cdc.MustUnmarshal(bz, &chainCfg)

var extraEIPs v4types.ExtraEIPs
bz = kvStore.Get(types.ParamStoreKeyExtraEIPs)
bz, err = kvStore.Get(types.ParamStoreKeyExtraEIPs)
require.NoError(t, err)
cdc.MustUnmarshal(bz, &extraEIPs)
require.Equal(t, []int64(nil), extraEIPs.EIPs)

Expand Down
28 changes: 19 additions & 9 deletions x/evm/migrations/v5/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/stretchr/testify/require"

Expand All @@ -20,13 +21,15 @@ func TestMigrate(t *testing.T) {
storeKey := storetypes.NewKVStoreKey(types.ModuleName)
tKey := storetypes.NewTransientStoreKey("transient_test")
ctx := testutil.DefaultContext(storeKey, tKey)
kvStore := ctx.KVStore(storeKey)
storeService := runtime.NewKVStoreService(storeKey)

extraEIPs := v5types.V5ExtraEIPs{EIPs: types.AvailableExtraEIPs}
extraEIPsBz := cdc.MustMarshal(&extraEIPs)
chainConfig := types.DefaultChainConfig()
chainConfigBz := cdc.MustMarshal(&chainConfig)

kvStore := storeService.OpenKVStore(ctx)

// Set the params in the store
kvStore.Set(types.ParamStoreKeyEVMDenom, []byte("aphoton"))
kvStore.Set(types.ParamStoreKeyEnableCreate, []byte{0x01})
Expand All @@ -35,10 +38,11 @@ func TestMigrate(t *testing.T) {
kvStore.Set(types.ParamStoreKeyExtraEIPs, extraEIPsBz)
kvStore.Set(types.ParamStoreKeyChainConfig, chainConfigBz)

err := v5.MigrateStore(ctx, kvStore, cdc)
err := v5.MigrateStore(ctx, storeService, cdc)
require.NoError(t, err)

paramsBz := kvStore.Get(types.KeyPrefixParams)
paramsBz, err := kvStore.Get(types.KeyPrefixParams)
require.NoError(t, err)
var params types.Params
cdc.MustUnmarshal(paramsBz, &params)

Expand All @@ -51,10 +55,16 @@ func TestMigrate(t *testing.T) {
require.Equal(t, extraEIPs.EIPs, params.ExtraEIPs)

// check that the keys are deleted
require.False(t, kvStore.Has(types.ParamStoreKeyEVMDenom))
require.False(t, kvStore.Has(types.ParamStoreKeyEnableCreate))
require.False(t, kvStore.Has(types.ParamStoreKeyEnableCall))
require.False(t, kvStore.Has(types.ParamStoreKeyAllowUnprotectedTxs))
require.False(t, kvStore.Has(types.ParamStoreKeyExtraEIPs))
require.False(t, kvStore.Has(types.ParamStoreKeyChainConfig))
value, _ := kvStore.Has(types.ParamStoreKeyEVMDenom)
require.False(t, value)
value, _ = kvStore.Has(types.ParamStoreKeyEnableCreate)
require.False(t, value)
value, _ = kvStore.Has(types.ParamStoreKeyEnableCall)
require.False(t, value)
value, _ = kvStore.Has(types.ParamStoreKeyAllowUnprotectedTxs)
require.False(t, value)
value, _ = kvStore.Has(types.ParamStoreKeyExtraEIPs)
require.False(t, value)
value, _ = kvStore.Has(types.ParamStoreKeyChainConfig)
require.False(t, value)
}
6 changes: 4 additions & 2 deletions x/feemarket/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ func MigrateStore(
legacySubspace types.Subspace,
cdc codec.BinaryCodec,
) error {
var params types.Params
var (
store = storeService.OpenKVStore(ctx)
params types.Params
)

legacySubspace.GetParamSetIfExists(ctx, &params)

Expand All @@ -29,7 +32,6 @@ func MigrateStore(
return err
}

store := storeService.OpenKVStore(ctx)
store.Set(types.ParamsKey, bz)

return nil
Expand Down
11 changes: 7 additions & 4 deletions x/feemarket/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/evmos/ethermint/encoding"
Expand Down Expand Up @@ -35,15 +36,17 @@ func TestMigrate(t *testing.T) {
storeKey := storetypes.NewKVStoreKey(types.ModuleName)
tKey := storetypes.NewTransientStoreKey("transient_test")
ctx := testutil.DefaultContext(storeKey, tKey)
kvStore := ctx.KVStore(storeKey)
storeService := runtime.NewKVStoreService(storeKey)

legacySubspaceEmpty := newMockSubspaceEmpty()
require.Error(t, v4.MigrateStore(ctx, kvStore, legacySubspaceEmpty, cdc))
require.Error(t, v4.MigrateStore(ctx, storeService, legacySubspaceEmpty, cdc))

legacySubspace := newMockSubspace(types.DefaultParams())
require.NoError(t, v4.MigrateStore(ctx, kvStore, legacySubspace, cdc))
require.NoError(t, v4.MigrateStore(ctx, storeService, legacySubspace, cdc))

paramsBz := kvStore.Get(types.ParamsKey)
kvStore := storeService.OpenKVStore(ctx)
paramsBz, err := kvStore.Get(types.ParamsKey)
require.NoError(t, err)
var params types.Params
cdc.MustUnmarshal(paramsBz, &params)

Expand Down

0 comments on commit aa5a061

Please sign in to comment.