Skip to content

Commit 24acfa7

Browse files
codchenclaude
andcommitted
feat(evmonly): serve staking's current bonded set as the validator set
Autobahn keeps the committee it derives in its own epoch state, so what it needs from the app is the bonded set as it stands. GetValidators reads staking's lastValidators through BondedValidators again, and the epoch snapshot stays for rewards, built from the same read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6dc432b commit 24acfa7

8 files changed

Lines changed: 133 additions & 66 deletions

File tree

‎docs/giga-storage/staking-design-rationale.md‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -293,24 +293,22 @@ that cost is small.
293293
### The epoch snapshot
294294

295295
V2 hands CometBFT a new validator set every block. Autobahn changes its
296-
committee once per epoch, and derives epoch E+2's committee from the set it
297-
reads at the end of epoch E's last block. The node reads the set outside block
298-
execution, at whatever height the app has reached by then, so the set it gets
299-
could differ from node to node.
296+
committee once per epoch: it reads the bonded set after an epoch's last block,
297+
derives the committee two epochs ahead from it, and keeps that committee in
298+
consensus-owned state the precompiles do not read. Rewards need the committee
299+
that served a block.
300300

301301
So at the end of an epoch's last block, EndBlock copies `lastValidators`, with
302-
each member's consensus key, into `epochValidators[epoch]`, and the app serves
303-
consensus the latest snapshot. The snapshot is committed state, so every node
304-
serves the same one for an epoch, and a node that state-syncs gets it too.
305-
Consensus marks the block that closes an epoch, so the app never repeats the
306-
epoch length. The keys are copied because a member can leave the bonded set,
307-
finish unbonding with no shares and be removed while its snapshot is still
308-
served.
309-
310-
The snapshot is also what rewards read: the committee that served a block in
311-
epoch E is the snapshot of epoch E−2. Three are kept, E−2 to E, and EndBlock
312-
deletes the one before them when it takes a new one: about 150 slot writes once
313-
an epoch at 35 validators.
302+
each member's consensus key, into `epochValidators[epoch]`, and the committee
303+
that served a block in epoch E is the snapshot of epoch E−2. The snapshot is
304+
committed state, so every node reads the same one, and a node that state-syncs
305+
gets it too. Consensus marks the block that closes an epoch, so the app never
306+
repeats the epoch length. The keys are copied because a member can leave the
307+
bonded set, finish unbonding with no shares and be removed while its snapshot
308+
is still read.
309+
310+
Three are kept, E−2 to E, and EndBlock deletes the one before them when it
311+
takes a new one: about 150 slot writes once an epoch at 35 validators.
314312

315313
### Where the structures don't carry over
316314

‎giga/evmonly/precompiles/staking/bonded_set.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package staking
22

33
import (
4+
"fmt"
5+
46
"github.com/ethereum/go-ethereum/common"
57

68
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles"
@@ -13,6 +15,13 @@ type BondedValidator struct {
1315
Power uint64
1416
}
1517

18+
// BondedValidators returns the bonded set the last EndBlock stored, in the
19+
// order it ranked them, or none before staking has bonded a set. It fails when
20+
// the stored set does not add up.
21+
func BondedValidators(state precompiles.State) ([]BondedValidator, error) {
22+
return bondedValidators(newStore(state))
23+
}
24+
1625
// EpochValidators returns the bonded set snapshotted at the end of the latest
1726
// epoch, in the order it ranked them, or none before the first snapshot. It
1827
// fails when the stored set does not add up.
@@ -24,3 +33,21 @@ func EpochValidators(state precompiles.State) ([]BondedValidator, error) {
2433
}
2534
return s.epochValidators(epoch)
2635
}
36+
37+
// bondedValidators returns the bonded set the last EndBlock stored, with each
38+
// member's consensus key.
39+
func bondedValidators(s store) ([]BondedValidator, error) {
40+
last, err := s.lastValidators()
41+
if err != nil {
42+
return nil, err
43+
}
44+
bonded := make([]BondedValidator, len(last))
45+
for i, v := range last {
46+
pubkey := s.consensusPubkey(v.operator)
47+
if pubkey == (common.Hash{}) {
48+
return nil, fmt.Errorf("%w: %s", errUnknownValidator, v.operator)
49+
}
50+
bonded[i] = BondedValidator{Operator: v.operator, ConsensusPubkey: pubkey, Power: v.power}
51+
}
52+
return bonded, nil
53+
}

‎giga/evmonly/precompiles/staking/bonded_set_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,41 @@ import (
1010
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles/solstorage"
1111
)
1212

13+
func TestBondedValidatorsAreTheSetTheLastEndBlockStored(t *testing.T) {
14+
h := newHarness(t, testParams)
15+
want := []BondedValidator{
16+
{Operator: val1, ConsensusPubkey: common.BytesToHash(val1[:]), Power: 30},
17+
{Operator: val2, ConsensusPubkey: common.BytesToHash(val2[:]), Power: 20},
18+
}
19+
require.Equal(t, want, must(BondedValidators(h.state)))
20+
21+
require.NoError(t, h.delegate(alice, val3, sei(15)))
22+
require.Equal(t, want, must(BondedValidators(h.state)), "a delegation moves the set only at EndBlock")
23+
h.endBlock()
24+
require.Equal(t, []BondedValidator{
25+
{Operator: val1, ConsensusPubkey: common.BytesToHash(val1[:]), Power: 30},
26+
{Operator: val3, ConsensusPubkey: common.BytesToHash(val3[:]), Power: 25},
27+
}, must(BondedValidators(h.state)), "the set moves at any EndBlock, not only an epoch's last")
28+
}
29+
30+
func TestNoValidatorsAreBondedBeforeStakingBondsASet(t *testing.T) {
31+
state := newMemState()
32+
require.Empty(t, must(BondedValidators(state)))
33+
require.NoError(t, SetParams(state, testParams))
34+
require.Empty(t, must(BondedValidators(state)))
35+
}
36+
37+
func TestBondedValidatorsFailOnAStoredSetThatDoesNotAddUp(t *testing.T) {
38+
h := newHarness(t, testParams)
39+
h.store().setLastValidators([]validatorPower{{val1, 30}, {alice, 20}})
40+
_, err := BondedValidators(h.state)
41+
require.ErrorIs(t, err, errUnknownValidator, "a member with no validator")
42+
43+
h.store().SetUint64(solstorage.Slot(slotLastValidators), 0, 101)
44+
_, err = BondedValidators(h.state)
45+
require.ErrorIs(t, err, errInvalidStoredSet, "a set longer than any MaxValidators allows")
46+
}
47+
1348
func TestEpochValidatorsAreTheSetAtTheLatestEpochEnd(t *testing.T) {
1449
h := newHarness(t, testParams)
1550
require.Empty(t, must(EpochValidators(h.state)), "no epoch has ended")

‎giga/evmonly/precompiles/staking/endblock.go‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,20 @@ func (p Precompile) applyBondedSet(block precompiles.BlockContext, s store, para
162162
return nil
163163
}
164164

165-
// epochSnapshotsKept is how many epoch snapshots are kept, as an epoch's
166-
// committee is derived from the snapshot two epochs before it.
165+
// epochSnapshotsKept is how many epoch snapshots are kept, as the committee
166+
// serving an epoch is the bonded set at the end of the epoch two before it.
167167
const epochSnapshotsKept = 3
168168

169169
// snapshotEpoch copies the bonded set, with each member's consensus key, into
170170
// epoch's snapshot and deletes the one that no longer falls among the latest
171171
// epochSnapshotsKept.
172172
func snapshotEpoch(s store, epoch uint64) error {
173-
last, err := s.lastValidators()
173+
set, err := bondedValidators(s)
174174
if err != nil {
175175
return err
176176
}
177-
set := make([]BondedValidator, len(last))
178-
for i, v := range last {
179-
// The key is copied rather than looked up when the snapshot is read,
180-
// because a member can be removed before its snapshot is retired.
181-
pubkey := s.consensusPubkey(v.operator)
182-
if pubkey == (common.Hash{}) {
183-
return fmt.Errorf("%w: %s", errUnknownValidator, v.operator)
184-
}
185-
set[i] = BondedValidator{Operator: v.operator, ConsensusPubkey: pubkey, Power: v.power}
186-
}
177+
// The keys are copied rather than looked up when the snapshot is read,
178+
// because a member can be removed before its snapshot is retired.
187179
s.setEpochValidators(epoch, set)
188180
if epoch >= epochSnapshotsKept {
189181
s.setEpochValidators(epoch-epochSnapshotsKept, nil)

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ import (
2323
gigaconfig "github.com/sei-protocol/sei-chain/giga/config"
2424
"github.com/sei-protocol/sei-chain/giga/evmonly"
2525
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles"
26+
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles/staking"
2627
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
2728
"github.com/sei-protocol/sei-chain/sei-db/bootstrap"
2829
"github.com/sei-protocol/sei-chain/sei-db/common/keys"
2930
seidbmetrics "github.com/sei-protocol/sei-chain/sei-db/common/metrics"
3031
seidbconfig "github.com/sei-protocol/sei-chain/sei-db/config"
3132
"github.com/sei-protocol/sei-chain/sei-db/ledger_db/receipt"
3233
gigatypes "github.com/sei-protocol/sei-chain/sei-db/state_db/giga/types"
34+
"github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types"
3335
"github.com/sei-protocol/sei-chain/sei-tendermint/internal/proxy"
3436
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
3537
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require"
@@ -91,6 +93,33 @@ func evmOnlyTestEpochBlock(height int64, state proxy.EpochState) *proxy.RequestF
9193
return req
9294
}
9395

96+
// evmOnlyTestClosingBlock returns an empty block at height that closes epoch.
97+
func evmOnlyTestClosingBlock(height int64, epoch types.EpochIndex) *proxy.RequestFinalizeBlock {
98+
req := evmOnlyTestBlock(height)
99+
req.Header.ClosingEpoch = utils.Some(epoch)
100+
return req
101+
}
102+
103+
// closingEpochRecorder is staking, with each EndBlock only recording the epoch
104+
// its block closes.
105+
type closingEpochRecorder struct {
106+
staking.Precompile
107+
closed *[]utils.Option[uint64]
108+
}
109+
110+
func (r closingEpochRecorder) EndBlock(block precompiles.BlockContext, _ precompiles.State) ([]error, error) {
111+
*r.closed = append(*r.closed, block.ClosingEpoch)
112+
return nil, nil
113+
}
114+
115+
func TestEVMOnlyApplicationTellsEndBlockersWhichEpochABlockCloses(t *testing.T) {
116+
var closed []utils.Option[uint64]
117+
app := newStakingEVMOnlyTestApp(t, []proxy.ValidatorUpdate{{Power: 1}}, closingEpochRecorder{closed: &closed})
118+
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestBlock(1))
119+
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestClosingBlock(2, 7))
120+
require.Equal(t, []utils.Option[uint64]{utils.None[uint64](), utils.Some[uint64](7)}, closed)
121+
}
122+
94123
func finalizeAndCommitEVMOnlyTestBlock(t *testing.T, app proxy.EVMApplication, req *proxy.RequestFinalizeBlock) []byte {
95124
t.Helper()
96125
response, err := app.FinalizeBlock(t.Context(), req)

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
1313
)
1414

15-
// GetValidators returns the bonded set staking snapshotted at the end of the
16-
// latest epoch, and the configured validators until it has snapshotted one. It
17-
// returns none once a block is known to have failed to commit or the stored set
18-
// does not add up, and autobahn cannot stage an empty committee.
15+
// GetValidators returns staking's bonded set after the latest finalized block,
16+
// and the configured validators until staking has bonded one. It returns none
17+
// once a block is known to have failed to commit or the stored set does not add
18+
// up, and autobahn cannot stage an empty committee.
1919
func (a *evmOnlyApplication) GetValidators() []proxy.ValidatorUpdate {
20-
bonded, err := a.epochValidators()
20+
bonded, err := a.bondedValidators()
2121
if err != nil {
22-
logger.Error("EVM-only epoch validator set is unavailable", "err", err)
22+
logger.Error("EVM-only bonded validator set is unavailable", "err", err)
2323
return nil
2424
}
2525
if len(bonded) == 0 {
@@ -28,13 +28,12 @@ func (a *evmOnlyApplication) GetValidators() []proxy.ValidatorUpdate {
2828
return bonded
2929
}
3030

31-
// epochValidators returns the bonded set staking snapshotted at the end of the
32-
// latest epoch, after the latest finalized block, without waiting for that
33-
// block's commit.
34-
func (a *evmOnlyApplication) epochValidators() ([]proxy.ValidatorUpdate, error) {
31+
// bondedValidators returns staking's bonded set after the latest finalized
32+
// block, without waiting for that block's commit.
33+
func (a *evmOnlyApplication) bondedValidators() ([]proxy.ValidatorUpdate, error) {
3534
var bonded []staking.BondedValidator
3635
read := func(state precompiles.State) (err error) {
37-
bonded, err = staking.EpochValidators(state)
36+
bonded, err = staking.BondedValidators(state)
3837
return err
3938
}
4039
// Autobahn calls this before every block, so it must not hold execution

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

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
gigaconfig "github.com/sei-protocol/sei-chain/giga/config"
1010
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles"
11-
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles/solstorage"
1211
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles/staking"
1312
seidbconfig "github.com/sei-protocol/sei-chain/sei-db/config"
1413
"github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types"
@@ -39,27 +38,18 @@ func (g stakingGenesis) EndBlock(block precompiles.BlockContext, state precompil
3938
return g.Precompile.EndBlock(block, state)
4039
}
4140

42-
// corruptEpochSnapshot stores an epoch snapshot longer than staking allows at
43-
// every EndBlock.
44-
type corruptEpochSnapshot struct {
41+
// corruptBondedSet stores a bonded set longer than staking allows at every
42+
// EndBlock.
43+
type corruptBondedSet struct {
4544
staking.Precompile
4645
}
4746

48-
func (corruptEpochSnapshot) EndBlock(_ precompiles.BlockContext, state precompiles.State) ([]error, error) {
49-
// Slot 2 of StakingStorage maps each epoch to its snapshot, and slot 3 holds
50-
// the epoch after the latest.
51-
state.SetState(staking.Address, solstorage.MapUint64(solstorage.Slot(2), 0), common.BigToHash(big.NewInt(101)))
52-
state.SetState(staking.Address, solstorage.Slot(3), common.BigToHash(big.NewInt(1)))
47+
func (corruptBondedSet) EndBlock(_ precompiles.BlockContext, state precompiles.State) ([]error, error) {
48+
// Slot 0 of StakingStorage holds the bonded set's length.
49+
state.SetState(staking.Address, common.Hash{}, common.BigToHash(big.NewInt(101)))
5350
return nil, nil
5451
}
5552

56-
// evmOnlyTestClosingBlock returns an empty block at height that closes epoch.
57-
func evmOnlyTestClosingBlock(height int64, epoch types.EpochIndex) *proxy.RequestFinalizeBlock {
58-
req := evmOnlyTestBlock(height)
59-
req.Header.ClosingEpoch = utils.Some(epoch)
60-
return req
61-
}
62-
6353
// newStakingEVMOnlyTestApp returns an initialized application serving the
6454
// configured validators and running staking as contract.
6555
func newStakingEVMOnlyTestApp(t *testing.T, configured []proxy.ValidatorUpdate, contract precompiles.Contract) proxy.EVMApplication {
@@ -98,20 +88,17 @@ func testGenesisValidator(operator common.Address, key ed25519.PublicKey, self i
9888
}
9989
}
10090

101-
func TestEVMOnlyApplicationServesStakingsSetAtEachEpochEnd(t *testing.T) {
91+
func TestEVMOnlyApplicationServesStakingsBondedSet(t *testing.T) {
10292
configured := []proxy.ValidatorUpdate{{PubKey: autobahnKey(ed25519.TestSecretKey([]byte("configured")).Public()), Power: 1}}
10393
key1 := ed25519.TestSecretKey([]byte("validator-1")).Public()
10494
key2 := ed25519.TestSecretKey([]byte("validator-2")).Public()
10595
app := newStakingEVMOnlyTestApp(t, configured, stakingGenesis{validators: []staking.GenesisValidator{
10696
testGenesisValidator(common.HexToAddress("0xa2"), key2, 20),
10797
testGenesisValidator(common.HexToAddress("0xa1"), key1, 30),
10898
}})
109-
require.Equal(t, configured, app.GetValidators(), "the configured validators serve until staking snapshots a set")
99+
require.Equal(t, configured, app.GetValidators(), "the configured validators serve until staking bonds a set")
110100

111101
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestBlock(1))
112-
require.Equal(t, configured, app.GetValidators(), "staking bonded a set, but no epoch has ended")
113-
114-
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestClosingBlock(2, 0))
115102
require.Equal(t, []proxy.ValidatorUpdate{
116103
{PubKey: autobahnKey(key1), Power: 30},
117104
{PubKey: autobahnKey(key2), Power: 20},
@@ -139,8 +126,8 @@ func TestEVMOnlyApplicationServesNoValidatorsAfterAFailedCommit(t *testing.T) {
139126
require.Empty(t, app.GetValidators(), "block 1 failed to commit, so the committed state predates it")
140127
}
141128

142-
func TestEVMOnlyApplicationServesNoValidatorsFromACorruptEpochSnapshot(t *testing.T) {
143-
app := newStakingEVMOnlyTestApp(t, []proxy.ValidatorUpdate{{Power: 1}}, corruptEpochSnapshot{})
129+
func TestEVMOnlyApplicationServesNoValidatorsFromACorruptBondedSet(t *testing.T) {
130+
app := newStakingEVMOnlyTestApp(t, []proxy.ValidatorUpdate{{Power: 1}}, corruptBondedSet{})
144131
finalizeAndCommitEVMOnlyTestBlock(t, app, evmOnlyTestBlock(1))
145132
require.Empty(t, app.GetValidators())
146133
}

‎sei-tendermint/internal/p2p/giga_router_common.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ func (r *gigaRouterCommon) runPushAppHashes(ctx context.Context) error {
194194
}
195195
// PushAppHash needs the validator set from right after block n, and uses it
196196
// only when n closes an epoch. GetValidators() reads the app tip, which can
197-
// be past n. The EVM-only app serves the set it snapshotted when the latest
198-
// epoch closed, which is block n's until the tip closes the next one, and
199-
// the mock app's set never changes.
197+
// be past n, so the set is block n's only while no later block has changed
198+
// it. The mock app's set never changes; the EVM-only app's can change at
199+
// any block.
200200
// TODO: ask the app for the set as of block n, to drop that bound.
201201
weights, err := committeeWeights(r.app.GetValidators())
202202
if err != nil {

0 commit comments

Comments
 (0)