Skip to content

Commit e8ddc72

Browse files
feat: add migration pause handler (#4405)
A halted chain cannot pass a parameter-change proposal to set `NumKeysToMigratePerBlock` to zero. A recovery binary needs a deterministic state transition that pauses migration without changing the active write mode or discarding its persisted boundary. This adds `migrationpause.HardForkHandler`. A recovery patch can register it for one chain and the first uncommitted height. The handler writes zero to the migration parameter subspace before `applyMigrationBatchSize` reads it in the same `BeginBlock`, so the pause is stored in consensus state and survives later restarts. This handler affects consensus and must run at the same height on every validator. `TestMigrationPauseHardForkPreservesInFlightMode` starts an EVM migration, executes the handler through `BeginBlock`, and verifies that both the stored and effective rates become zero while the mode remains `migrate_evm`. The focused app test and `make fmtcheck` pass. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 299613c commit e8ddc72

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎app/abci_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
sdkerrors "github.com/sei-protocol/sei-chain/sei-cosmos/types/errors"
1111

1212
"github.com/sei-protocol/sei-chain/app/migration"
13+
"github.com/sei-protocol/sei-chain/app/upgrades/migrationpause"
14+
sctypes "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/types"
1315
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
1416
tmproto "github.com/sei-protocol/sei-chain/sei-tendermint/proto/tendermint/types"
1517
"github.com/stretchr/testify/require"
@@ -73,6 +75,40 @@ func TestApplyMigrationBatchSize(t *testing.T) {
7375
require.Equal(t, int(migration.MaxNumKeysToMigratePerBlock), got)
7476
}
7577

78+
func TestMigrationPauseHardForkPreservesInFlightMode(t *testing.T) {
79+
a := Setup(t, false, false, false)
80+
ctx := a.NewContext(false, tmproto.Header{Height: 1, ChainID: "sei-test", Time: time.Now()})
81+
82+
subspace, ok := a.ParamsKeeper.GetSubspace(migration.SubspaceName)
83+
require.True(t, ok)
84+
subspace.Set(ctx, migration.KeyNumKeysToMigratePerBlock, uint64(500))
85+
a.applyMigrationBatchSize(ctx)
86+
87+
modeBeforePause, ok := a.rootStore.GetWriteMode()
88+
require.True(t, ok)
89+
require.Equal(t, sctypes.MigrateEVM, modeBeforePause)
90+
91+
const recoveryHeight = int64(2)
92+
a.HardForkManager.RegisterHandler(
93+
migrationpause.NewHardForkHandler(recoveryHeight, ctx.ChainID(), a.ParamsKeeper),
94+
)
95+
ctx = ctx.WithBlockHeight(recoveryHeight)
96+
require.NotPanics(t, func() {
97+
a.BeginBlock(ctx, recoveryHeight, nil, nil, false)
98+
})
99+
100+
var storedRate uint64
101+
subspace.Get(ctx, migration.KeyNumKeysToMigratePerBlock, &storedRate)
102+
require.Zero(t, storedRate)
103+
effectiveRate, ok := a.rootStore.GetMigrationBatchSize()
104+
require.True(t, ok)
105+
require.Zero(t, effectiveRate)
106+
107+
modeAfterPause, ok := a.rootStore.GetWriteMode()
108+
require.True(t, ok)
109+
require.Equal(t, modeBeforePause, modeAfterPause)
110+
}
111+
76112
// TestBeginBlockAppliesMigrationBatchSize exercises the full BeginBlock path
77113
// (not the helper in isolation): it mimics a governance ParameterChangeProposal
78114
// having set NumKeysToMigratePerBlock, then runs app.BeginBlock and asserts the
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package migrationpause
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/sei-protocol/sei-chain/app/migration"
7+
"github.com/sei-protocol/sei-chain/app/upgrades"
8+
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
9+
paramskeeper "github.com/sei-protocol/sei-chain/sei-cosmos/x/params/keeper"
10+
)
11+
12+
const upgradeName = "pause-state-migration"
13+
14+
// HardForkHandler sets the state migration rate to zero at a target height.
15+
type HardForkHandler struct {
16+
targetHeight int64
17+
targetChainID string
18+
paramsKeeper paramskeeper.Keeper
19+
}
20+
21+
// NewHardForkHandler returns a handler for pausing state migration on one chain.
22+
func NewHardForkHandler(
23+
height int64,
24+
chainID string,
25+
paramsKeeper paramskeeper.Keeper,
26+
) upgrades.HardForkHandler {
27+
return HardForkHandler{
28+
targetHeight: height,
29+
targetChainID: chainID,
30+
paramsKeeper: paramsKeeper,
31+
}
32+
}
33+
34+
func (h HardForkHandler) GetName() string {
35+
return fmt.Sprintf("%s-%d", upgradeName, h.targetHeight)
36+
}
37+
38+
func (h HardForkHandler) GetTargetChainID() string {
39+
return h.targetChainID
40+
}
41+
42+
func (h HardForkHandler) GetTargetHeight() int64 {
43+
return h.targetHeight
44+
}
45+
46+
func (h HardForkHandler) ExecuteHandler(ctx sdk.Context) error {
47+
subspace, ok := h.paramsKeeper.GetSubspace(migration.SubspaceName)
48+
if !ok {
49+
return fmt.Errorf("migration parameter subspace is not registered")
50+
}
51+
subspace.Set(ctx, migration.KeyNumKeysToMigratePerBlock, uint64(0))
52+
return nil
53+
}
54+
55+
var _ upgrades.HardForkHandler = HardForkHandler{}

0 commit comments

Comments
 (0)