Skip to content

Commit 9e135ce

Browse files
feat(autobahn): store epoch state rows in the EVM-only app (#4752)
## Summary Adds the storage side for committing consensus epoch data with an Autobahn EVM-only block. Consensus will pass two opaque byte values at the first block of each epoch: - `epoch_registry`: the consensus registry snapshot. - `next_epoch`: the canonical `Epoch` bytes for the next epoch, copied out for dynasty proof construction. This PR adds: - `abci.RequestFinalizeBlock.EpochState`, an optional `abci.EpochState{EpochRegistry, NextEpoch []byte}`. `RequestFinalizeBlock` is a plain Go struct, so this is not an ABCI protocol change, and apps that do not commit epoch state ignore the field. - The EVM-only app writes copies of both values under the `autobahn` misc module through the existing block change-set encoder, the same path as the block cursor. The app does not decode them. An empty value is committed as an empty row, not a delete. A block finalized without epoch state writes no `autobahn` rows, and the state applies only to the block it is passed with. Nothing sets the field yet, so this PR writes no new rows and does not change the AppHash. Since #4725, blocks execute in `execution.Executor`, which does not read epoch state. Producing the two values and setting the field in the executor is a consensus change, planned together with populating `epoch_registry`. State sync restore (the seed step of PR 5) depends on that change. It needs `epoch_registry`: `next_epoch` holds only the next committee, and each epoch overwrites it, so it does not give the current committee that verifies the seed QC. ## Test Plan - `internal/evmonlyapp/app_test.go`: - both values are committed unchanged under `autobahn`; - an empty `EpochRegistry` is committed as an empty row; - a block without epoch state writes no `autobahn` rows, and epoch state does not carry over to the next block; - changing the request bytes after `FinalizeBlock` returns does not change the committed row. - `make fmtcheck` - `go build ./...` - `scripts/ramtest.sh -race ./sei-tendermint/abci/... ./sei-tendermint/internal/evmonlyapp/... ./sei-tendermint/internal/proxy/... ./sei-tendermint/internal/autobahn/execution/...` --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 0b107b5 commit 9e135ce

5 files changed

Lines changed: 197 additions & 14 deletions

File tree

‎sei-tendermint/abci/types/plain_types.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package types
33
import (
44
"time"
55

6+
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
67
tmproto "github.com/sei-protocol/sei-chain/sei-tendermint/proto/tendermint/types"
78
)
89

@@ -222,6 +223,15 @@ type RequestFinalizeBlock struct {
222223
Hash []byte
223224

224225
Header *tmproto.Header
226+
// EpochState is committed with this block. Only Autobahn EVM-only apps commit it.
227+
EpochState utils.Option[EpochState]
228+
}
229+
230+
// EpochState is consensus-owned epoch data that the app commits unchanged with a block.
231+
// Both values are written as given; an empty value is committed as an empty row.
232+
type EpochState struct {
233+
EpochRegistry []byte
234+
NextEpoch []byte
225235
}
226236

227237
type CommitInfo struct {

‎sei-tendermint/internal/evmonlyapp/app.go‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ const (
143143
type evmOnlyCursorState struct {
144144
committed evmOnlyCursor
145145
pending utils.Option[evmOnlyCursor]
146+
// epochState is committed with the block being finalized. It is set after
147+
// beginBlock accepts that block and read when the block is encoded.
148+
epochState utils.Option[abci.EpochState]
146149
// lastBlockTime is the Time of the most recently committed block, used by
147150
// EvmCall to reproduce that block's execution context for a read-only call.
148151
lastBlockTime uint64
@@ -237,6 +240,10 @@ func (a *evmOnlyApplication) encodeCursorChangeSet(
237240
if height != state.committed.height+1 {
238241
return nil, fmt.Errorf("EVM-only block height %d does not follow committed height %d", height, state.committed.height)
239242
}
243+
var epochChanges []gigatypes.MiscChange
244+
if epochState, ok := state.epochState.Get(); ok {
245+
epochChanges = epochStateMiscChanges(epochState)
246+
}
240247
appHash, err := hashEVMOnlyResult(state.committed.appHash, block.Number, block.BlockHash, result)
241248
if err != nil {
242249
return nil, err
@@ -249,7 +256,7 @@ func (a *evmOnlyApplication) encodeCursorChangeSet(
249256
}
250257
state.pending = utils.Some(next)
251258
state.pendingBlockTime = block.Time
252-
return []gigatypes.MiscChange{next.miscChange()}, nil
259+
return append([]gigatypes.MiscChange{next.miscChange()}, epochChanges...), nil
253260
}
254261
panic("unreachable")
255262
}
@@ -795,6 +802,7 @@ func (a *evmOnlyApplication) FinalizeBlock(ctx context.Context, req *abci.Reques
795802
if err != nil {
796803
return nil, err
797804
}
805+
a.stageEpochState(req.EpochState)
798806
// Closes the stage in flight, so the gap until the next block is charged to neither.
799807
defer a.finalizePhases.Reset()
800808
a.finalizePhases.SetPhase(finalizePhaseTakeSenders)
@@ -836,6 +844,14 @@ func (a *evmOnlyApplication) beginBlock(height int64) (evmOnlyCursor, error) {
836844
panic("unreachable")
837845
}
838846

847+
// stageEpochState sets the epoch state committed with the block being
848+
// finalized, replacing that of the previous block.
849+
func (a *evmOnlyApplication) stageEpochState(epochState utils.Option[abci.EpochState]) {
850+
for state := range a.cursor.Lock() {
851+
state.epochState = epochState
852+
}
853+
}
854+
839855
// abandonPending drops the cursor staged by a failed block unless the store
840856
// already holds that block's version, in which case the cursor is durable and
841857
// stays pending for Commit. The in-flight commit is landed first so the store

‎sei-tendermint/internal/evmonlyapp/app_test.go‎

Lines changed: 117 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ func evmOnlyTestBlock(height int64, txs ...[]byte) *abci.RequestFinalizeBlock {
8383
}
8484
}
8585

86+
// evmOnlyTestEpochBlock returns an empty block at height that carries state.
87+
func evmOnlyTestEpochBlock(height int64, state abci.EpochState) *abci.RequestFinalizeBlock {
88+
req := evmOnlyTestBlock(height)
89+
req.EpochState = utils.Some(state)
90+
return req
91+
}
92+
8693
func finalizeAndCommitEVMOnlyTestBlock(t *testing.T, app abci.Application, req *abci.RequestFinalizeBlock) []byte {
8794
t.Helper()
8895
response, err := app.FinalizeBlock(t.Context(), req)
@@ -137,6 +144,99 @@ func newInitializedEVMOnlyTestApp(t *testing.T) abci.Application {
137144
return app
138145
}
139146

147+
func TestEVMOnlyAppCommitsEpochState(t *testing.T) {
148+
app := newInitializedEVMOnlyTestApp(t).(*evmOnlyApplication)
149+
state := abci.EpochState{EpochRegistry: []byte("registry"), NextEpoch: []byte("next-epoch")}
150+
151+
require.NotEmpty(t, finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestEpochBlock(1, state)))
152+
require.NoError(t, app.AwaitCommits())
153+
154+
require.Equal(t, state.EpochRegistry, evmOnlyEpochStateRow(t, app, epochStateEpochRegistryKey))
155+
require.Equal(t, state.NextEpoch, evmOnlyEpochStateRow(t, app, epochStateNextEpochKey))
156+
}
157+
158+
func TestEVMOnlyAppCommitsEmptyEpochRegistryAsEmptyRow(t *testing.T) {
159+
app := newInitializedEVMOnlyTestApp(t).(*evmOnlyApplication)
160+
state := abci.EpochState{EpochRegistry: nil, NextEpoch: []byte("next-epoch")}
161+
162+
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestEpochBlock(1, state))
163+
require.NoError(t, app.AwaitCommits())
164+
165+
require.Empty(t, evmOnlyEpochStateRow(t, app, epochStateEpochRegistryKey))
166+
}
167+
168+
func TestEVMOnlyAppEpochStateRowsDoNotAliasTheRequest(t *testing.T) {
169+
app := newInitializedEVMOnlyTestApp(t).(*evmOnlyApplication)
170+
state := abci.EpochState{NextEpoch: []byte("next-epoch")}
171+
172+
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestEpochBlock(1, state))
173+
copy(state.NextEpoch, "XXXXXXXXXX")
174+
require.NoError(t, app.AwaitCommits())
175+
176+
require.Equal(t, []byte("next-epoch"), evmOnlyEpochStateRow(t, app, epochStateNextEpochKey))
177+
}
178+
179+
func TestEVMOnlyAppWithoutEpochStateWritesNoEpochStateRows(t *testing.T) {
180+
app := newInitializedEVMOnlyTestApp(t).(*evmOnlyApplication)
181+
182+
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestBlock(1))
183+
require.NoError(t, app.AwaitCommits())
184+
185+
_, found := app.storage.SC().Get(epochStateModule, []byte(epochStateEpochRegistryKey))
186+
require.False(t, found)
187+
_, found = app.storage.SC().Get(epochStateModule, []byte(epochStateNextEpochKey))
188+
require.False(t, found)
189+
}
190+
191+
func TestEVMOnlyAppEpochStateDoesNotCarryOver(t *testing.T) {
192+
app := newInitializedEVMOnlyTestApp(t).(*evmOnlyApplication)
193+
encoded := installRecordingEVMExecutor(app)
194+
state := abci.EpochState{EpochRegistry: nil, NextEpoch: []byte("next-epoch")}
195+
196+
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestEpochBlock(1, state))
197+
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestBlock(2))
198+
require.NoError(t, app.AwaitCommits())
199+
200+
for blocks := range encoded.Lock() {
201+
require.Equal(t, 2, epochStateRowCount(blocks[1]))
202+
require.Equal(t, 0, epochStateRowCount(blocks[2]))
203+
}
204+
}
205+
206+
// installRecordingEVMExecutor replaces app's executor with one that records the misc
207+
// changes app encodes for each block, by block number.
208+
func installRecordingEVMExecutor(app *evmOnlyApplication) *utils.Mutex[map[uint64][]gigatypes.MiscChange] {
209+
encoded := new(utils.NewMutex(map[uint64][]gigatypes.MiscChange{}))
210+
installEVMExecutorEncoder(app, func(block evmonly.BlockContext, result *evmonly.BlockResult) ([]gigatypes.MiscChange, error) {
211+
misc, err := app.encodeCursorChangeSet(block, result)
212+
if err != nil {
213+
return nil, err
214+
}
215+
for blocks := range encoded.Lock() {
216+
blocks[block.Number] = misc
217+
}
218+
return misc, nil
219+
})
220+
return encoded
221+
}
222+
223+
func epochStateRowCount(misc []gigatypes.MiscChange) int {
224+
count := 0
225+
for _, change := range misc {
226+
if change.Module == epochStateModule {
227+
count++
228+
}
229+
}
230+
return count
231+
}
232+
233+
func evmOnlyEpochStateRow(t *testing.T, app *evmOnlyApplication, key string) []byte {
234+
t.Helper()
235+
raw, found := app.storage.SC().Get(epochStateModule, []byte(key))
236+
require.True(t, found)
237+
return raw
238+
}
239+
140240
// newUndoLogEVMOnlyTestApp returns an initialized application whose state store
141241
// keeps EVM history as an undo log.
142242
func newUndoLogEVMOnlyTestApp(t *testing.T) *evmOnlyApplication {
@@ -1181,24 +1281,28 @@ func TestEVMOnlyApplicationReadsSettleBehindFinalizeBlock(t *testing.T) {
11811281
// into the EVM module, which the state DB refuses to commit. Each block executes and encodes cleanly, and its commit
11821282
// is the first thing that fails.
11831283
func installUnwritableEVMExecutor(app *evmOnlyApplication) {
1284+
installEVMExecutorEncoder(app, func(block evmonly.BlockContext, result *evmonly.BlockResult) ([]gigatypes.MiscChange, error) {
1285+
misc, err := app.encodeCursorChangeSet(block, result)
1286+
if err != nil {
1287+
return nil, fmt.Errorf("encode the cursor: %w", err)
1288+
}
1289+
return append(misc, gigatypes.MiscChange{
1290+
Module: keys.EVMStoreKey,
1291+
Key: []byte{1},
1292+
Value: []byte{1},
1293+
}), nil
1294+
})
1295+
}
1296+
1297+
// installEVMExecutorEncoder replaces app's executor with a new one that encodes each
1298+
// block's changesets with encode.
1299+
func installEVMExecutorEncoder(app *evmOnlyApplication, encode evmonly.BlockChangeSetEncoder) {
11841300
for slot := range app.executor.Lock() {
11851301
if previous, ok := slot.Get(); ok {
11861302
previous.Close()
11871303
}
11881304
executor := app.newExecutor()
1189-
evmonly.WithStoreIndependentBlockChangeSetEncoder(
1190-
func(block evmonly.BlockContext, result *evmonly.BlockResult) ([]gigatypes.MiscChange, error) {
1191-
misc, err := app.encodeCursorChangeSet(block, result)
1192-
if err != nil {
1193-
return nil, fmt.Errorf("encode the cursor: %w", err)
1194-
}
1195-
return append(misc, gigatypes.MiscChange{
1196-
Module: keys.EVMStoreKey,
1197-
Key: []byte{1},
1198-
Value: []byte{1},
1199-
}), nil
1200-
},
1201-
)(executor)
1305+
evmonly.WithStoreIndependentBlockChangeSetEncoder(encode)(executor)
12021306
*slot = utils.Some(executor)
12031307
app.settler.Store(utils.Some(executor))
12041308
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package evmonlyapp
2+
3+
import (
4+
"slices"
5+
6+
gigatypes "github.com/sei-protocol/sei-chain/sei-db/state_db/giga/types"
7+
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
8+
)
9+
10+
const (
11+
epochStateModule = "autobahn"
12+
epochStateEpochRegistryKey = "epoch_registry"
13+
epochStateNextEpochKey = "next_epoch"
14+
)
15+
16+
func epochStateMiscChanges(state abci.EpochState) []gigatypes.MiscChange {
17+
return []gigatypes.MiscChange{
18+
epochStateMiscChange(epochStateEpochRegistryKey, state.EpochRegistry),
19+
epochStateMiscChange(epochStateNextEpochKey, state.NextEpoch),
20+
}
21+
}
22+
23+
func epochStateMiscChange(key string, value []byte) gigatypes.MiscChange {
24+
return gigatypes.MiscChange{
25+
Module: epochStateModule,
26+
Key: []byte(key),
27+
Value: slices.Clone(value),
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package evmonlyapp
2+
3+
import (
4+
"testing"
5+
6+
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
7+
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require"
8+
)
9+
10+
func TestEpochStateMiscChanges(t *testing.T) {
11+
state := abci.EpochState{
12+
EpochRegistry: nil,
13+
NextEpoch: []byte("next-epoch"),
14+
}
15+
16+
changes := epochStateMiscChanges(state)
17+
require.Len(t, changes, 2)
18+
require.Equal(t, epochStateModule, changes[0].Module)
19+
require.Equal(t, []byte(epochStateEpochRegistryKey), changes[0].Key)
20+
require.Equal(t, state.EpochRegistry, changes[0].Value)
21+
require.Equal(t, epochStateModule, changes[1].Module)
22+
require.Equal(t, []byte(epochStateNextEpochKey), changes[1].Key)
23+
require.Equal(t, state.NextEpoch, changes[1].Value)
24+
}

0 commit comments

Comments
 (0)