Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions modules/migrationmngr/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ func init() {
type ModuleInputs struct {
depinject.In

Config *modulev1.Module
Cdc codec.Codec
StoreService store.KVStoreService
AddressCodec address.Codec
// optional, used to detect if IBC module is enabled.
// When IBC module is present, use `depinject.Provide(IBCStoreKey(ibcStoreKey))`
IBCStoreKey keeper.IbcKVStoreKeyAlias `optional:"true"`

Config *modulev1.Module
Cdc codec.Codec
StoreService store.KVStoreService
AddressCodec address.Codec
StakingKeeper types.StakingKeeper
}

Expand All @@ -59,7 +55,6 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.StoreService,
in.AddressCodec,
in.StakingKeeper,
in.IBCStoreKey,
authority.String(),
)
m := NewAppModule(in.Cdc, k)
Expand Down
14 changes: 3 additions & 11 deletions modules/migrationmngr/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,9 @@ func (k Keeper) EndBlock(ctx context.Context) ([]abci.ValidatorUpdate, error) {
}

var updates []abci.ValidatorUpdate
if !k.isIBCEnabled(ctx) {
// if IBC is not enabled, we can migrate immediately
// but only return updates on the first block of migration (start height)
if uint64(sdkCtx.BlockHeight()) == start {
updates, err = k.migrateNow(ctx, migration, validatorSet)
if err != nil {
return nil, err
}
}
} else {
updates, err = k.migrateOver(sdkCtx, migration, validatorSet)
// Always perform immediate migration updates at the start height.
if uint64(sdkCtx.BlockHeight()) == start {
updates, err = k.migrateNow(ctx, migration, validatorSet)
if err != nil {
return nil, err
}
Expand Down
36 changes: 0 additions & 36 deletions modules/migrationmngr/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func initFixture(tb testing.TB) *fixture {
storeService,
addressCodec,
stakingKeeper,
nil,
sdk.AccAddress(address.Module(types.ModuleName)).String(),
)

Expand Down Expand Up @@ -165,41 +164,6 @@ func TestIsMigrating(t *testing.T) {
require.Equal(t, uint64(2), resp.EndBlockHeight)
}

func TestIsMigrating_IBCEnabled(t *testing.T) {
stakingKeeper := &mockStakingKeeper{}
key := storetypes.NewKVStoreKey(types.ModuleName)
storeService := runtime.NewKVStoreService(key)
encCfg := moduletestutil.MakeTestEncodingConfig(migrationmngr.AppModuleBasic{})
addressCodec := addresscodec.NewBech32Codec("cosmos")
ibcKey := storetypes.NewKVStoreKey("ibc")
ctx := testutil.DefaultContextWithKeys(map[string]*storetypes.KVStoreKey{
types.ModuleName: key,
"ibc": ibcKey,
}, nil, nil)

k := keeper.NewKeeper(
encCfg.Codec,
storeService,
addressCodec,
stakingKeeper,
func() *storetypes.KVStoreKey { return key },
sdk.AccAddress(address.Module(types.ModuleName)).String(),
)

// set up migration
require.NoError(t, k.Migration.Set(ctx, types.EvolveMigration{
BlockHeight: 1,
Sequencer: types.Sequencer{Name: "foo"},
}))

ctx = ctx.WithBlockHeight(1)
resp, err := keeper.NewQueryServer(k).IsMigrating(ctx, &types.QueryIsMigratingRequest{})
require.NoError(t, err)
require.True(t, resp.IsMigrating)
require.Equal(t, uint64(1), resp.StartBlockHeight)
require.Equal(t, 1+keeper.IBCSmoothingFactor, resp.EndBlockHeight)
}

func TestSequencer_Migrating(t *testing.T) {
s := initFixture(t)

Expand Down
42 changes: 2 additions & 40 deletions modules/migrationmngr/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,18 @@ import (
"cosmossdk.io/core/address"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/evstack/ev-abci/modules/migrationmngr/types"
)

// IbcStoreKey is the store key used for IBC-related data.
// It is an alias for storetypes.StoreKey to allow depinject to resolve it as a dependency (as runtime assumes 1 module = 1 store key maximum).
type IbcKVStoreKeyAlias = func() *storetypes.KVStoreKey

type Keeper struct {
storeService corestore.KVStoreService
cdc codec.BinaryCodec
addressCodec address.Codec
authority string

ibcStoreKey IbcKVStoreKeyAlias
stakingKeeper types.StakingKeeper

Schema collections.Schema
Expand All @@ -40,7 +34,6 @@ func NewKeeper(
storeService corestore.KVStoreService,
addressCodec address.Codec,
stakingKeeper types.StakingKeeper,
ibcStoreKey IbcKVStoreKeyAlias,
authority string,
) Keeper {
// ensure that authority is a valid account address
Expand All @@ -55,7 +48,6 @@ func NewKeeper(
authority: authority,
addressCodec: addressCodec,
stakingKeeper: stakingKeeper,
ibcStoreKey: ibcStoreKey,
Sequencer: collections.NewItem(
sb,
types.SequencerKey,
Expand Down Expand Up @@ -103,42 +95,12 @@ func (k Keeper) IsMigrating(ctx context.Context) (start, end uint64, ok bool) {
return 0, 0, false
}

// smoothen the migration over IBCSmoothingFactor blocks, in order to migrate the validator set to the sequencer or attesters network when IBC is enabled.
migrationEndHeight := migration.BlockHeight + IBCSmoothingFactor

// If IBC is not enabled, the migration can be done in one block.
if !k.isIBCEnabled(ctx) {
migrationEndHeight = migration.BlockHeight + 1
}
// Migration is performed in a single step regardless of IBC.
migrationEndHeight := migration.BlockHeight + 1

sdkCtx := sdk.UnwrapSDKContext(ctx)
currentHeight := uint64(sdkCtx.BlockHeight())
migrationInProgress := currentHeight >= migration.BlockHeight && currentHeight <= migrationEndHeight

return migration.BlockHeight, migrationEndHeight, migrationInProgress
}

// isIBCEnabled checks if IBC is enabled on the chain.
// In order to not import the IBC module, we only check if the IBC store exists,
// but not the ibc params. This should be sufficient for our use case.
func (k Keeper) isIBCEnabled(ctx context.Context) bool {
enabled := true

if k.ibcStoreKey == nil {
return false
}

sdkCtx := sdk.UnwrapSDKContext(ctx)

ms := sdkCtx.MultiStore().CacheMultiStore()
defer func() {
if r := recover(); r != nil {
// If we panic, it means the store does not exist, so IBC is not enabled.
enabled = false
}
}()
ms.GetKVStore(k.ibcStoreKey())

// has not panicked, so store exists
return enabled
}
Loading