Skip to content

Commit 0dc0b4a

Browse files
authored
Made first RoadIndex configurable. (#4593)
First step towards superblock impl, where evm block height will be equal to RoadIndex instead of GlobalBlockNumber. We start with setting first both RoadIndex and GlobalBlockNumber to initialHeight, so that indexing switch becomes implementation detail.
1 parent a2a0dad commit 0dc0b4a

25 files changed

Lines changed: 427 additions & 334 deletions

‎sei-tendermint/AGENTS.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
Within sei-tendermint subdirectory
22
* sei-tendermint is a root of the go module, but it is not the root of the repo. When refactoring check references across of the whole repo.
33
Comply with repo's root AGENTS.md, except where they conflict with the rules below.
4+
* ensure type safety wherever possible, in particular
5+
* use strong types rather than underlying primitives (urls should use URL type instead of string, time duration should use time.Duration instead of int, etc.)
6+
* avoid dynamic casts - type information should be passed along the callstack, not rediscovered at the bottom
7+
* use strongly typed utils/require asserts instead of testify/require
48
* use sei-tendermint/libs/utils.Option for optional values, passing nil as function value is not allowed,
59
unless explicitly documented (except for maps and slices, for which nil is a valid empty value).
6-
* struct fields are assumed to be non-nil by default. Do not add defensive nil-checks in internal logic.
10+
* struct fields are assumed to be non-nil by default. Storing nil as field value is not allowed (except for slices and constant maps).
11+
Do not add defensive nil-checks in internal logic.
712
External data, like proto generated code for example, might require nil checks.
813
* use utils.Recv/utils.Send instead of select { case <-ctx.Done(); ... }
9-
* use strongly typed utils/require asserts instead of testify/require
1014
* use sei-tendermint/libs/utils/scope.Run for structured concurrency (instead of waitgroup).
1115
* Don't spawn goroutines with plain "go func(){...}"
1216
* using testing.T assertions (inclusing those from "require" library) is not allowed from non-main test goroutine.
@@ -22,7 +26,7 @@ Within sei-tendermint subdirectory
2226
of sync.Mutex.
2327
* use public api of types in tests, never malform internal state of types. For read-only access,
2428
you can use private fields directly in case it is not accessible via public api.
25-
* when writing tests, focus on asserting the publically visible properties, especially the API contract. Avoid asserting implementation details.
29+
* when writing tests, focus on asserting the publicly visible properties, especially the API contract. Avoid asserting implementation details.
2630
* Avoid sleeping and active polling in tests. Prefer waiting for updates (for example by using sei-tendermint/libs/utils.Watch or AtomicWatch).
2731
* Do not introduce artificial timeouts in tests because they are a source of flakiness. Let the test hang and make the user run go tests with explicit
2832
timeout instead.

‎sei-tendermint/autobahn/types/opt.go‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ func ProposalOpt[P any, T interface{ Proposal() P }](mv utils.Option[T]) utils.O
2020
return utils.None[P]()
2121
}
2222

23-
// NextIndexOpt defaults to 0.
24-
func NextIndexOpt[T interface{ Index() RoadIndex }](mv utils.Option[T]) RoadIndex {
25-
if v, ok := mv.Get(); ok {
26-
return v.Index() + 1
27-
}
28-
return 0
29-
}
30-
3123
// NextViewOpt defaults to {0,0}.
3224
func NextViewOpt[T interface{ View() View }](mv utils.Option[T]) View {
3325
if v, ok := mv.Get(); ok {

‎sei-tendermint/autobahn/types/proposal.go‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,19 @@ func (v View) Next() View {
129129

130130
// ConsensusSpec is the durable CommitQC tip paired with the epoch of the
131131
// RoadIndex that follows it. CommitQC is None before the first tip; until then
132-
// Epoch is genesis epoch 0 (and FirstBlock is the next global block). Consensus
133-
// advances with a spec verbatim.
132+
// Epoch is genesis epoch 0. Consensus advances with a spec verbatim.
134133
type ConsensusSpec struct {
135134
CommitQC utils.Option[*CommitQC]
136135
Epoch *Epoch
137136
}
138137

139-
// Index is the RoadIndex of the next view: CommitQC.Index()+1, or 0 if there is no tip.
138+
// Index is the RoadIndex of the next view: CommitQC.Index()+1, or the start of
139+
// the genesis epoch if there is no tip.
140140
func (s ConsensusSpec) Index() RoadIndex {
141-
return NextIndexOpt(s.CommitQC)
141+
if qc, ok := s.CommitQC.Get(); ok {
142+
return qc.Index() + 1
143+
}
144+
return s.Epoch.RoadRange().First
142145
}
143146

144147
// ViewSpec is ConsensusSpec plus the TimeoutQC for the current view number.
@@ -152,7 +155,8 @@ type ViewSpec struct {
152155
}
153156

154157
// NextGlobalBlock returns the first global block number expected in the next proposal.
155-
// CommitQC is None only at global block 0 (genesis), in which case it returns Epoch[0].FirstBlock.
158+
// CommitQC is None only before the first global block, in which case it returns
159+
// the genesis epoch's first block.
156160
// For all other views, including the first view of a non-genesis epoch, CommitQC is present and it returns CommitQC.GlobalRange().Next.
157161
func (vs *ViewSpec) NextGlobalBlock() GlobalBlockNumber {
158162
if cQC, ok := vs.CommitQC.Get(); ok {

‎sei-tendermint/autobahn/types/proposal_test.go‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require"
99
)
1010

11-
// genFreshEpoch returns an epoch whose road range is OpenRoadRange (so road index 0
12-
// is always valid for a ViewSpec with no CommitQC) but whose epoch index and first
13-
// block are randomised to prevent tests from silently passing on zero-value defaults.
11+
// genFreshEpoch returns an epoch whose first road, first block, and epoch index
12+
// are randomized independently to prevent tests from silently passing on
13+
// zero-value defaults or coupling the two offsets.
1414
func genFreshEpoch(rng utils.Rng, committee *Committee) *Epoch {
15+
first := RoadIndex(rng.Uint64()%1000000) + 1
1516
return NewEpoch(
1617
GenEpochIndex(rng),
17-
OpenRoadRange(),
18+
RoadRange{First: first, Next: utils.Max[RoadIndex]()},
1819
time.Time{},
1920
committee,
2021
GlobalBlockNumber(rng.Uint64()%1000000)+1,
@@ -60,6 +61,16 @@ func makeCommitQCFromProposal(keys []SecretKey, fp *FullProposal) *CommitQC {
6061
return NewCommitQC(votes)
6162
}
6263

64+
func TestViewSpecIndependentGenesisOrigins(t *testing.T) {
65+
rng := utils.TestRng()
66+
committee, _ := GenCommittee(rng, 4)
67+
ep := NewEpoch(0, RoadRange{First: 7, Next: 108_000}, time.Time{}, committee, 11)
68+
vs := ViewSpec{ConsensusSpec: ConsensusSpec{Epoch: ep}}
69+
70+
require.Equal(t, RoadIndex(7), vs.Index())
71+
require.Equal(t, GlobalBlockNumber(11), vs.NextGlobalBlock())
72+
}
73+
6374
func TestProposalVerifyRejectsEmptyTipcut(t *testing.T) {
6475
rng := utils.TestRng()
6576
committee, _ := GenCommittee(rng, 4)
@@ -120,7 +131,6 @@ func TestProposalBlockTimestampStrictlyMonotone(t *testing.T) {
120131
rng := utils.TestRng()
121132
committee, keys := GenCommittee(rng, 4)
122133
ep := genFreshEpoch(rng, committee)
123-
firstBlock := ep.FirstBlock()
124134
vs0 := ViewSpec{ConsensusSpec: ConsensusSpec{Epoch: ep}}
125135
proposer0 := leaderKey(committee, keys, vs0.View())
126136
lane := committee.Lane(proposer0.Public()).OrPanic("missing lane")
@@ -134,8 +144,8 @@ func TestProposalBlockTimestampStrictlyMonotone(t *testing.T) {
134144
))
135145
p0 := firstProposal.Proposal().Msg()
136146
gr0 := p0.GlobalRange()
137-
require.Equal(t, firstBlock, gr0.First)
138-
require.Equal(t, firstBlock+3, gr0.Next)
147+
require.Equal(t, ep.FirstBlock(), gr0.First)
148+
require.Equal(t, ep.FirstBlock()+3, gr0.Next)
139149
first0 := p0.BlockTimestamp(gr0.First).OrPanic("missing first block timestamp")
140150
second0 := p0.BlockTimestamp(gr0.First + 1).OrPanic("missing second block timestamp")
141151
third0 := p0.BlockTimestamp(gr0.First + 2).OrPanic("missing third block timestamp")
@@ -666,10 +676,11 @@ func TestProposalVerifyRejectsLaneRangeLongerThanMaxLaneRangeInProposal(t *testi
666676
rng := utils.TestRng()
667677
committee, keys := GenCommittee(rng, 4)
668678
ep := genFreshEpoch(rng, committee)
669-
lane := committee.Lane(leaderKey(committee, keys, View{}).Public()).OrPanic("missing lane")
679+
view := View{Index: ep.RoadRange().First, EpochIndex: ep.EpochIndex()}
680+
lane := committee.Lane(leaderKey(committee, keys, view).Public()).OrPanic("missing lane")
670681
// Bypass NewProposal's check by constructing the proposal directly.
671682
oversized := newProposal(
672-
View{},
683+
view,
673684
time.Now(),
674685
[]*LaneRange{NewLaneRange(lane, 0, utils.Some(NewBlock(lane, MaxLaneRangeInProposal, GenBlockHeaderHash(rng), GenPayload(rng)).Header()))},
675686
ep.FirstBlock(),
@@ -749,7 +760,7 @@ func TestProposalAtView(t *testing.T) {
749760
func TestProposalVerifyValidReproposal(t *testing.T) {
750761
rng := utils.TestRng()
751762
committee, keys := GenCommittee(rng, 4)
752-
// Build a proposal at view (0, 0) with one lane block so sum(lane.First) > 0.
763+
// Build a proposal at the first road with one lane block so sum(lane.First) > 0.
753764
// firstBlock > 0 ensures a reproposal bug that passes GlobalRange().First
754765
// (= sum(lane.First)+firstBlock) instead of firstBlock would be caught.
755766
ep := genFreshEpoch(rng, committee)
@@ -770,12 +781,12 @@ func TestProposalVerifyValidReproposal(t *testing.T) {
770781
// Timeout at view (0, 0) with the PrepareQC → forces reproposal at (0, 1).
771782
var timeoutVotes []*FullTimeoutVote
772783
for _, k := range keys {
773-
timeoutVotes = append(timeoutVotes, NewFullTimeoutVote(k, View{Index: 0, Number: 0, EpochIndex: ep.EpochIndex()}, utils.Some(prepareQC)))
784+
timeoutVotes = append(timeoutVotes, NewFullTimeoutVote(k, View{Index: ep.RoadRange().First, Number: 0, EpochIndex: ep.EpochIndex()}, utils.Some(prepareQC)))
774785
}
775786
timeoutQC := NewTimeoutQC(timeoutVotes)
776787

777788
vs1 := ViewSpec{ConsensusSpec: ConsensusSpec{Epoch: ep}, TimeoutQC: utils.Some(timeoutQC)}
778-
require.Equal(t, View{Index: 0, Number: 1, EpochIndex: ep.EpochIndex()}, vs1.View())
789+
require.Equal(t, View{Index: ep.RoadRange().First, Number: 1, EpochIndex: ep.EpochIndex()}, vs1.View())
779790

780791
leader1 := leaderKey(committee, keys, vs1.View())
781792
reproposal := utils.OrPanic1(NewProposal(leader1, vs1, time.Now(), oneLaneQCMap(rng, committee, keys, vs1)))
@@ -804,7 +815,7 @@ func TestProposalVerifyRejectsReproposalWithUnnecessaryData(t *testing.T) {
804815

805816
var timeoutVotes []*FullTimeoutVote
806817
for _, k := range keys {
807-
timeoutVotes = append(timeoutVotes, NewFullTimeoutVote(k, View{Index: 0, Number: 0, EpochIndex: ep.EpochIndex()}, utils.Some(prepareQC)))
818+
timeoutVotes = append(timeoutVotes, NewFullTimeoutVote(k, View{Index: ep.RoadRange().First, Number: 0, EpochIndex: ep.EpochIndex()}, utils.Some(prepareQC)))
808819
}
809820
timeoutQC := NewTimeoutQC(timeoutVotes)
810821

@@ -843,7 +854,7 @@ func TestProposalVerifyRejectsReproposalHashMismatch(t *testing.T) {
843854

844855
var timeoutVotes []*FullTimeoutVote
845856
for _, k := range keys {
846-
timeoutVotes = append(timeoutVotes, NewFullTimeoutVote(k, View{Index: 0, Number: 0, EpochIndex: ep.EpochIndex()}, utils.Some(prepareQC)))
857+
timeoutVotes = append(timeoutVotes, NewFullTimeoutVote(k, View{Index: ep.RoadRange().First, Number: 0, EpochIndex: ep.EpochIndex()}, utils.Some(prepareQC)))
847858
}
848859
timeoutQC := NewTimeoutQC(timeoutVotes)
849860

‎sei-tendermint/autobahn/types/timeout.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ func (m *TimeoutQC) Verify(ep *Epoch, prev utils.Option[*CommitQC]) error {
157157
}
158158
c := ep.Committee()
159159
// Check that the TimeoutQC is from the correct consensus instance.
160-
if got, want := view.Index, NextIndexOpt(prev); got != want {
160+
want := ep.RoadRange().First
161+
if qc, ok := prev.Get(); ok {
162+
want = qc.Index() + 1
163+
}
164+
if got := view.Index; got != want {
161165
return fmt.Errorf("timeoutQC.View().Index = %v, want %v", got, want)
162166
}
163167
// Verify the signatures.

‎sei-tendermint/internal/autobahn/autobahn.proto‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ message FullTimeoutVote {
205205
message PersistedInner {
206206
reserved "commit_vote", "prepare_vote", "commit_qc";
207207
reserved 4, 5, 1;
208-
// Index is the current view RoadIndex (ConsensusSpec.Index; 0 at genesis).
208+
// Index is the current view RoadIndex (ConsensusSpec.Index).
209209
optional uint64 index = 9;
210210
optional PrepareQC prepare_qc = 2;
211211
optional TimeoutQC timeout_qc = 3;

‎sei-tendermint/internal/autobahn/avail/inner.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ func (i *inner) advanceEpoch(ep *types.Epoch) {
239239
// advanceTarget is the epoch to install next: the epoch of the road following
240240
// the durable tip, or applied+1 while that road is still inside the applied
241241
// epoch.
242-
func (i *inner) advanceTarget() types.EpochIndex {
242+
func (i *inner) advanceTarget(registry *epoch.Registry) types.EpochIndex {
243243
next := i.applied().EpochIndex() + 1
244244
tip, ok := i.persistedCommitQC.Load().Get()
245245
if !ok {
246246
return next
247247
}
248-
return max(next, epoch.IndexForRoad(tip.Index()+1))
248+
return max(next, registry.IndexForRoad(tip.Index()+1))
249249
}
250250

251251
// canAdvanceEpoch reports whether the applied epoch is sealed and its prune leash is

‎sei-tendermint/internal/autobahn/avail/inner_test.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ func TestRestoreInner_Empty(t *testing.T) {
205205
i, err := restoreInner(newTestDataState(&data.Config{Registry: registry}), &loadedState{})
206206
require.NoError(t, err)
207207

208-
require.Equal(t, types.RoadIndex(0), i.roads.first)
209-
require.Equal(t, types.RoadIndex(0), i.roads.next)
208+
require.Equal(t, registry.FirstRoad(), i.roads.first)
209+
require.Equal(t, registry.FirstRoad(), i.roads.next)
210210
_, ok := i.persistedCommitQC.Load().Get()
211211
require.False(t, ok)
212212
require.NotNil(t, i.nextBlockToPersist)
@@ -460,10 +460,10 @@ func TestRestoreInner_LoadedCommitQCs(t *testing.T) {
460460
}
461461
inner, err := restoreInner(ds, &loadedState{commitQCs: qcs})
462462
require.NoError(t, err)
463-
require.Equal(t, types.RoadIndex(0), inner.roads.first)
464-
require.Equal(t, types.RoadIndex(3), inner.roads.next)
463+
require.Equal(t, registry.FirstRoad(), inner.roads.first)
464+
require.Equal(t, registry.FirstRoad()+3, inner.roads.next)
465465
for i, qc := range qcs {
466-
require.NoError(t, utils.TestDiff(qc, inner.roads.q[types.RoadIndex(i)].commitQC))
466+
require.NoError(t, utils.TestDiff(qc, inner.roads.q[registry.FirstRoad()+types.RoadIndex(i)].commitQC))
467467
}
468468
require.NoError(t, utils.TestDiff(utils.Some(qcs[2]), inner.persistedCommitQC.Load()))
469469
require.Equal(t, int64(qcs[2].Index()), gathered(t, "tendermint_internal_autobahn_avail_commit_road_index", nil))
@@ -514,7 +514,7 @@ func TestRefreshConsensusSpec_WithholdsTipUntilNextViewEpochApplied(t *testing.T
514514
ep0 := registry.MustEpoch(0)
515515
ep1 := registry.MustEpoch(1)
516516

517-
last := epoch.LastRoad(0)
517+
last := registry.LastRoad(0)
518518
qcPrev := types.BuildCommitQC(ep0, keys, utils.Some(tipLink(ep0, keys[0], last-2)), nil)
519519
qcLast := types.BuildCommitQC(ep0, keys, utils.Some(qcPrev), nil)
520520
require.Equal(t, last-1, qcPrev.Index())
@@ -555,7 +555,7 @@ func TestPrune_LeavesAppliedToEpochAdvance(t *testing.T) {
555555
require.Equal(t, types.EpochIndex(0), i.applied().EpochIndex())
556556

557557
prev := types.NewCommitQC([]*types.Signed[*types.CommitVote]{
558-
types.Sign(keys[0], types.NewCommitVote(types.ProposalAt(ep1, types.View{Index: epoch.LastRoad(1), Number: 0}, ep1.FirstBlock()))),
558+
types.Sign(keys[0], types.NewCommitVote(types.ProposalAt(ep1, types.View{Index: registry.LastRoad(1), Number: 0}, ep1.FirstBlock()))),
559559
})
560560
qc := types.BuildCommitQC(ep2, keys, utils.Some(prev), nil)
561561
i.prune(data.Anchor{

‎sei-tendermint/internal/autobahn/avail/state.go‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ type restoredQC struct {
196196
ep *types.Epoch
197197
}
198198

199-
// restorePlan is the applied epoch, first RoadIndex (0 or Anchor+1), and
199+
// restorePlan is the applied epoch, first RoadIndex (genesis or Anchor+1), and
200200
// verified CommitQCs for construction. The epoch is genesis when there is no
201201
// tip, otherwise nextViewEpoch of the persisted tip (last restored QC, or the
202202
// Anchor QC when the queue is empty).
@@ -206,7 +206,7 @@ func restorePlan(ds *data.State, loaded *loadedState) (*types.Epoch, types.RoadI
206206
if err != nil {
207207
return nil, 0, nil, fmt.Errorf("genesis epoch 0: %w", err)
208208
}
209-
var first types.RoadIndex
209+
first := genesis.RoadRange().First
210210
tip := utils.None[restoredQC]()
211211
if anchor, ok := ds.Anchor().Load().Get(); ok {
212212
first = anchor.CommitQC.Index() + 1
@@ -704,7 +704,7 @@ func (s *State) ProduceLocalBlock(lane types.LaneID, n types.BlockNumber, payloa
704704

705705
// Task inserting CommitQCs and local blocks to data state.
706706
func (s *State) runPushQC(ctx context.Context) error {
707-
for n := types.RoadIndex(0); ; n = max(n+1, s.First()) {
707+
for n := s.First(); ; n = max(n+1, s.First()) {
708708
epoch, qc, err := s.fullCommitQC(ctx, n)
709709
if err != nil {
710710
if errors.Is(err, types.ErrPruned) {
@@ -741,7 +741,7 @@ func (s *State) runPushQC(ctx context.Context) error {
741741

742742
// Task inserting AppQCs to data state.
743743
func (s *State) runPushAppQC(ctx context.Context) error {
744-
for n := types.RoadIndex(0); ; n = max(n+1, s.First()) {
744+
for n := s.First(); ; n = max(n+1, s.First()) {
745745
appQC, err := s.appQC(ctx, n)
746746
if err != nil {
747747
if errors.Is(err, types.ErrPruned) {
@@ -773,17 +773,18 @@ func (s *State) runEvict(ctx context.Context) error {
773773

774774
// runEpochAdvance is the sole writer of the applied epoch after construction.
775775
func (s *State) runEpochAdvance(ctx context.Context) error {
776+
registry := s.data.Registry()
776777
for {
777778
var next types.EpochIndex
778779
for inner, ctrl := range s.inner.Lock() {
779780
if err := ctrl.WaitUntil(ctx, func() bool {
780-
next = inner.advanceTarget()
781+
next = inner.advanceTarget(registry)
781782
return next > inner.applied().EpochIndex()+1 || inner.canAdvanceEpoch()
782783
}); err != nil {
783784
return err
784785
}
785786
}
786-
ep, err := s.data.Registry().WaitForEpoch(ctx, next)
787+
ep, err := registry.WaitForEpoch(ctx, next)
787788
if err != nil {
788789
if !errors.Is(err, types.ErrPruned) {
789790
return err
@@ -792,7 +793,7 @@ func (s *State) runEpochAdvance(ctx context.Context) error {
792793
// names a different one.
793794
for inner, ctrl := range s.inner.Lock() {
794795
if err := ctrl.WaitUntil(ctx, func() bool {
795-
return inner.advanceTarget() != next
796+
return inner.advanceTarget(registry) != next
796797
}); err != nil {
797798
return err
798799
}
@@ -803,7 +804,7 @@ func (s *State) runEpochAdvance(ctx context.Context) error {
803804
// The tip may have moved while WaitForEpoch was parked. Installing
804805
// next now would pair it with a tip whose next road is in a later
805806
// epoch, so recompute instead.
806-
if inner.advanceTarget() != next {
807+
if inner.advanceTarget(registry) != next {
807808
break
808809
}
809810
inner.advanceEpoch(ep)
@@ -920,7 +921,10 @@ func (s *State) collectPersistBatch(ctx context.Context) (*persistBatch, error)
920921
for inner, ctrl := range s.inner.Lock() {
921922
// Start after the durable tip, clamped to roads.first after prune has
922923
// dropped the prefix (and possibly jumped the empty-queue tip to the Anchor).
923-
next := types.NextIndexOpt(inner.persistedCommitQC.Load())
924+
next := inner.roads.first
925+
if qc, ok := inner.persistedCommitQC.Load().Get(); ok {
926+
next = qc.Index() + 1
927+
}
924928
if err := ctrl.WaitUntil(ctx, func() bool {
925929
for lane, q := range inner.blocks {
926930
if inner.nextBlockToPersist[lane] < q.next {

0 commit comments

Comments
 (0)