Skip to content

Commit 0a2649b

Browse files
Carry the certified lane tip on empty LaneRanges (#4396)
## Summary Empty lane ranges were required to carry a zero `LastHash`. A commit copied the height where each lane stopped, but not the hash it stopped on. After prune removed the predecessor from the queue, the next block took its parent from this node's local `last`, or from zero. A producer could extend the lane from any parent, because the agreed `CommitQC` did not say which block the lane had stopped on. An empty range now copies the previous commit's `LastHash`, and a prepare vote is refused when that copy is wrong. Height 0 stays a zero hash. When the queue is empty, the next block must extend that hash. TODO: a block already in the queue may not name that parent, when the cluster certified a different block at the previous height. Switching onto the certified branch would rewrite the lane WAL, so the local block is kept for now. ## Test plan - [x] `go test` for `sei-tendermint/autobahn/types`, `internal/autobahn/avail`, `internal/autobahn/consensus`, and `internal/autobahn/consensus/persist` --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 82c82a1 commit 0a2649b

6 files changed

Lines changed: 402 additions & 80 deletions

File tree

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ func (m *LaneRange) Next() BlockNumber { return m.next }
4444
// Len returns the number of blocks in the range.
4545
func (m *LaneRange) Len() uint64 { return uint64(m.next - m.first) }
4646

47-
// LastHash is the hash of the last block of the range.
48-
// Returns a zero hash for an empty range.
47+
// LastHash is the certified lane tip the next block must extend.
48+
// For a non-empty range it is the hash of block Next()-1.
49+
// For an empty range it is the previous commit's LastHash for this lane,
50+
// or zero when the lane has never been extended.
4951
func (m *LaneRange) LastHash() BlockHeaderHash { return m.lastHash }
5052

5153
// Verify verifies the LaneRange against the committee.
@@ -56,8 +58,8 @@ func (m *LaneRange) Verify(c *Committee) error {
5658
if m.first > m.next {
5759
return fmt.Errorf("invalid range [%v,%v)", m.first, m.next)
5860
}
59-
if m.first == m.next && m.lastHash != (BlockHeaderHash{}) {
60-
return errors.New("non-zero hash for an empty range")
61+
if m.next == 0 && m.lastHash != (BlockHeaderHash{}) {
62+
return errors.New("non-zero hash for height 0")
6163
}
6264
return nil
6365
}
@@ -242,7 +244,7 @@ func (m *Proposal) NextTimestamp() time.Time {
242244
}
243245

244246
// Verify checks epoch binding, lane-range structural validity (bounds, max-length,
245-
// and lane committee membership). Empty tipcuts (no finalized blocks) are rejected;
247+
// lane committee membership, and a zero hash on an empty height-0 range). Empty tipcuts (no finalized blocks) are rejected;
246248
// leaders wait for LaneQCs via WaitForLaneQCs instead. QC-chain continuity
247249
// (matching starts against the previous QC) is only enforced by FullProposal.Verify.
248250
func (m *Proposal) Verify(ep *Epoch) error {
@@ -337,7 +339,8 @@ func buildProposal(
337339
) (*Proposal, error) {
338340
var laneRanges []*LaneRange
339341
for lane := range committee.Lanes().All() {
340-
first := LaneRangeOpt(viewSpec.CommitQC, lane).Next()
342+
prev := LaneRangeOpt(viewSpec.CommitQC, lane)
343+
first := prev.Next()
341344
if lQC, ok := laneQCs[lane]; ok {
342345
if lQC.Header().Lane() != lane {
343346
return nil, fmt.Errorf("laneQC %v for lane %v", lQC.Header().Lane(), lane)
@@ -348,7 +351,7 @@ func buildProposal(
348351
}
349352
laneRanges = append(laneRanges, laneRange)
350353
} else {
351-
laneRanges = append(laneRanges, NewLaneRange(lane, first, utils.None[*BlockHeader]()))
354+
laneRanges = append(laneRanges, &LaneRange{lane: lane, first: first, next: first, lastHash: prev.LastHash()})
352355
}
353356
}
354357
// Normalize the creation timestamp.
@@ -458,10 +461,17 @@ func (m *FullProposal) Verify(vs ViewSpec) error {
458461
// Verify each lane range against the previous commitQC and its laneQC justification.
459462
for lane := range c.Lanes().All() {
460463
r := proposal.LaneRange(lane)
464+
prev := LaneRangeOpt(vs.CommitQC, r.Lane())
461465
// Verify that range matches previous commitQC.
462-
if got, want := r.First(), LaneRangeOpt(vs.CommitQC, r.Lane()).Next(); got != want {
466+
if got, want := r.First(), prev.Next(); got != want {
463467
return fmt.Errorf("laneRange[%v].First() = %v, want %v", r.Lane(), got, want)
464468
}
469+
// An empty range carries the previous commit's LastHash.
470+
if r.Len() == 0 {
471+
if got, want := r.LastHash(), prev.LastHash(); got != want {
472+
return fmt.Errorf("laneRange[%v].LastHash() = %v, want %v", r.Lane(), got, want)
473+
}
474+
}
465475
// Verify that the necessary laneQC is present and valid.
466476
if r.First() < r.Next() {
467477
qc, ok := m.LaneQC(r.Lane())

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,98 @@ func TestProposalVerifyAcceptsNonContiguousImplicitRanges(t *testing.T) {
392392
require.NoError(t, shortFP.Verify(vs))
393393
}
394394

395+
func TestLaneRangeVerifyRejectsHashAtHeightZero(t *testing.T) {
396+
rng := utils.TestRng()
397+
committee, _ := GenCommittee(rng, 4)
398+
lane := committee.Lanes().At(0)
399+
400+
require.Error(t, (&LaneRange{lane: lane, first: 0, next: 0, lastHash: GenBlockHeaderHash(rng)}).Verify(committee))
401+
require.NoError(t, (&LaneRange{lane: lane, first: 0, next: 0}).Verify(committee))
402+
// Past height 0 an empty range may carry a tip. FullProposal.Verify checks that it matches.
403+
require.NoError(t, (&LaneRange{
404+
lane: lane, first: 1, next: 1, lastHash: GenBlockHeaderHash(rng),
405+
}).Verify(committee))
406+
}
407+
408+
func TestProposalEmptyLaneAtHeightZeroHasNoHash(t *testing.T) {
409+
rng := utils.TestRng()
410+
committee, keys := GenCommittee(rng, 4)
411+
ep := genFreshEpoch(rng, committee)
412+
vs := ViewSpec{ConsensusSpec: ConsensusSpec{Epoch: ep}}
413+
proposerKey := leaderKey(committee, keys, vs.View())
414+
415+
fp := utils.OrPanic1(NewProposal(proposerKey, vs, time.Now(), oneLaneQCMap(rng, committee, keys, vs)))
416+
require.NoError(t, fp.Verify(vs))
417+
var idle LaneID
418+
for lane := range committee.Lanes().All() {
419+
r := fp.Proposal().Msg().LaneRange(lane)
420+
if r.Len() == 0 {
421+
require.Equal(t, BlockNumber(0), r.Next())
422+
require.Equal(t, BlockHeaderHash{}, r.LastHash())
423+
idle = lane
424+
}
425+
}
426+
require.NotEqual(t, LaneID{}, idle)
427+
428+
// A lane that has not started must not name a tip. There is no previous
429+
// CommitQC to copy, and a non-zero hash would become the required parent.
430+
var ranges []*LaneRange
431+
for _, r := range fp.Proposal().Msg().laneRanges {
432+
if r.Lane() == idle {
433+
ranges = append(ranges, &LaneRange{lane: idle, first: 0, next: 0, lastHash: GenBlockHeaderHash(rng)})
434+
continue
435+
}
436+
ranges = append(ranges, r)
437+
}
438+
invented := &FullProposal{
439+
proposal: Sign(proposerKey, newProposal(
440+
fp.Proposal().Msg().view, fp.Proposal().Msg().timestamp, ranges, fp.Proposal().Msg().GlobalRange().First)),
441+
laneQCs: fp.laneQCs,
442+
}
443+
require.Error(t, invented.Verify(vs))
444+
}
445+
446+
func TestProposalCopiesEmptyLaneLastHash(t *testing.T) {
447+
rng := utils.TestRng()
448+
committee, keys := GenCommittee(rng, 4)
449+
ep := genFreshEpoch(rng, committee)
450+
vs0 := ViewSpec{ConsensusSpec: ConsensusSpec{Epoch: ep}}
451+
lane0 := committee.Lanes().At(0)
452+
lane1 := committee.Lanes().At(1)
453+
454+
fp0 := utils.OrPanic1(NewProposal(
455+
leaderKey(committee, keys, vs0.View()), vs0, time.Now(), oneLaneQCMap(rng, committee, keys, vs0)))
456+
require.NoError(t, fp0.Verify(vs0))
457+
tip := fp0.Proposal().Msg().LaneRange(lane0).LastHash()
458+
require.NotEqual(t, BlockHeaderHash{}, tip)
459+
460+
cqc := makeCommitQCFromProposal(keys, fp0)
461+
vs1 := ViewSpec{ConsensusSpec: ConsensusSpec{CommitQC: utils.Some(cqc), Epoch: ep}}
462+
// Extend a different lane so lane0's range is empty and must carry tip.
463+
qc1 := makeLaneQC(rng, committee, keys, lane1, 0, BlockHeaderHash{})
464+
fp1 := utils.OrPanic1(NewProposal(
465+
leaderKey(committee, keys, vs1.View()), vs1, time.Now(), map[LaneID]*LaneQC{lane1: qc1}))
466+
require.NoError(t, fp1.Verify(vs1))
467+
empty := fp1.Proposal().Msg().LaneRange(lane0)
468+
require.Equal(t, uint64(0), empty.Len())
469+
require.Equal(t, tip, empty.LastHash())
470+
471+
var ranges []*LaneRange
472+
for _, r := range fp1.Proposal().Msg().laneRanges {
473+
if r.Lane() == lane0 {
474+
ranges = append(ranges, &LaneRange{lane: lane0, first: r.First(), next: r.First(), lastHash: BlockHeaderHash{}})
475+
continue
476+
}
477+
ranges = append(ranges, r)
478+
}
479+
stripped := &FullProposal{
480+
proposal: Sign(leaderKey(committee, keys, vs1.View()), newProposal(
481+
fp1.Proposal().Msg().view, fp1.Proposal().Msg().timestamp, ranges, fp1.Proposal().Msg().GlobalRange().First)),
482+
laneQCs: fp1.laneQCs,
483+
}
484+
require.Error(t, stripped.Verify(vs1))
485+
}
486+
395487
func TestProposalVerifyRejectsLaneRangeFirstMismatch(t *testing.T) {
396488
rng := utils.TestRng()
397489
committee, keys := GenCommittee(rng, 4)

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

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package avail
22

33
import (
44
"fmt"
5+
"log/slog"
56

67
"github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types"
78
"github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/consensus/persist"
@@ -13,8 +14,11 @@ import (
1314
// blockQueue is a per-lane block queue.
1415
type blockQueue struct {
1516
queue[types.BlockNumber, *types.Signed[*types.LaneProposal]]
16-
// last is None, or this node's last pushed proposal at height >= first-1.
17-
last utils.Option[*types.Signed[*types.LaneProposal]]
17+
// localTip is None, or this node's latest pushed proposal, at a height of at least first-1.
18+
localTip utils.Option[*types.Signed[*types.LaneProposal]]
19+
// parentOfFirstLaneBlock is the parent hash of the lane block numbered first.
20+
// Zero before any prune, which is the parent of block 0.
21+
parentOfFirstLaneBlock types.BlockHeaderHash
1822
}
1923

2024
func newBlockQueue() *blockQueue {
@@ -23,27 +27,57 @@ func newBlockQueue() *blockQueue {
2327

2428
func (q *blockQueue) pushBack(p *types.Signed[*types.LaneProposal]) {
2529
q.queue.pushBack(p)
26-
q.last = utils.Some(p)
30+
q.localTip = utils.Some(p)
2731
}
2832

29-
// prune drops [first, newFirst). last is kept when newFirst <= next and
30-
// cleared when newFirst > next.
31-
func (q *blockQueue) prune(newFirst types.BlockNumber) {
33+
// parentHash is the parent the block at q.next must name: the hash of the
34+
// predecessor while it is in the queue, else parentOfFirstLaneBlock (first == next).
35+
func (q *blockQueue) parentHash() types.BlockHeaderHash {
36+
if q.first < q.next {
37+
return q.q[q.next-1].Msg().Block().Header().Hash()
38+
}
39+
return q.parentOfFirstLaneBlock
40+
}
41+
42+
// prune drops [first, newFirst) and records parent as the parent hash of block
43+
// newFirst. A stale newFirst changes nothing. localTip is kept when newFirst <= next
44+
// and cleared when newFirst > next. A block already in the queue at or after
45+
// newFirst stays; it is this lane's local block.
46+
func (q *blockQueue) prune(newFirst types.BlockNumber, parent types.BlockHeaderHash) {
3247
if newFirst <= q.first {
3348
return
3449
}
3550
if newFirst > q.next {
36-
// TODO: seed last from a non-empty LaneRange LastHash at Next()-1.
37-
// Empty ranges carry a zero LastHash, so they cannot replace a local last.
38-
q.last = utils.None[*types.Signed[*types.LaneProposal]]()
51+
q.localTip = utils.None[*types.Signed[*types.LaneProposal]]()
3952
}
4053
q.queue.prune(newFirst)
54+
// TODO: the block at newFirst may not name parent when the cluster certified
55+
// a different block at newFirst-1. Switching to the certified branch would
56+
// rewrite the lane WAL, so the local block is kept for now.
57+
q.logLocalBlock(parent)
58+
q.parentOfFirstLaneBlock = parent
59+
}
60+
61+
// logLocalBlock logs when the block at first does not name parent.
62+
func (q *blockQueue) logLocalBlock(parent types.BlockHeaderHash) {
63+
if q.first >= q.next {
64+
return
65+
}
66+
h := q.q[q.first].Msg().Block().Header()
67+
if h.ParentHash() == parent {
68+
return
69+
}
70+
logger.Error("local block does not extend certified tip",
71+
"lane", h.Lane(),
72+
slog.Uint64("block", uint64(h.BlockNumber())),
73+
"got", h.ParentHash(),
74+
"want", parent)
4175
}
4276

43-
// unpersistedLast returns the last block once it has left the active range and
77+
// unpersistedLocalTip returns localTip once it has left the active range and
4478
// block persistence has not reached it.
45-
func (q *blockQueue) unpersistedLast(nextToPersist types.BlockNumber) utils.Option[*types.Signed[*types.LaneProposal]] {
46-
if p, ok := q.last.Get(); ok {
79+
func (q *blockQueue) unpersistedLocalTip(nextToPersist types.BlockNumber) utils.Option[*types.Signed[*types.LaneProposal]] {
80+
if p, ok := q.localTip.Get(); ok {
4781
if n := p.Msg().Block().Header().BlockNumber(); n < q.first && nextToPersist <= n {
4882
return utils.Some(p)
4983
}
@@ -53,7 +87,7 @@ func (q *blockQueue) unpersistedLast(nextToPersist types.BlockNumber) utils.Opti
5387

5488
// retentionFloor returns the lowest block number the WAL must still hold.
5589
func (q *blockQueue) retentionFloor() types.BlockNumber {
56-
if p, ok := q.last.Get(); ok {
90+
if p, ok := q.localTip.Get(); ok {
5791
return min(q.first, p.Msg().Block().Header().BlockNumber())
5892
}
5993
return q.first
@@ -134,24 +168,29 @@ func (i *inner) restoreBlocks(blocks map[types.LaneID][]persist.LoadedBlock) err
134168
return fmt.Errorf("lane %s: loaded %d blocks exceeds capacity %d", lane, len(bs), BlocksPerLane)
135169
}
136170
if b.Number < q.next {
137-
// Certified. Restore last from the proposal at first-1 when present.
171+
// Certified. Restore localTip from the proposal at first-1 when present.
138172
if b.Number+1 == q.first {
139-
q.last = utils.Some(b.Proposal)
173+
q.localTip = utils.Some(b.Proposal)
140174
}
141175
continue
142176
}
143177
if b.Number != q.next {
144178
return fmt.Errorf("lane %s: non-contiguous persisted blocks: expected %d, got %d", lane, q.next, b.Number)
145179
}
146-
// Parent is checked only inside [first, next). last restored from
147-
// first-1 is for local production, not this check.
180+
// Parent is checked only inside [first, next). The persisted block at
181+
// first is this lane's local block. localTip restored from first-1 is
182+
// for local production, not this check.
183+
frontier := q.first == q.next
148184
if q.first < q.next {
149185
ph := b.Proposal.Msg().Block().Header().ParentHash()
150186
if q.q[q.next-1].Msg().Block().Header().Hash() != ph {
151187
return fmt.Errorf("lane %s: parent hash mismatch at block %d", lane, b.Number)
152188
}
153189
}
154190
q.pushBack(b.Proposal)
191+
if frontier {
192+
q.logLocalBlock(q.parentOfFirstLaneBlock)
193+
}
155194
}
156195
i.nextBlockToPersist[lane] = q.next
157196
}
@@ -298,9 +337,11 @@ func (i *inner) prune(anchor data.Anchor) int {
298337
lr := anchor.CommitQC.LaneRange(lane)
299338
bq := i.blocks[lane]
300339
vq.prune(lr.Next())
301-
bq.prune(lr.Next())
302-
// A lagging cursor stops at retentionFloor so an unflushed last can still
303-
// be written. The cursor is never rewound: already past last means it is on disk.
340+
// LastHash is the parent of block Next(). An empty range carries the
341+
// previous commit's LastHash, so it names the same parent for the same height.
342+
bq.prune(lr.Next(), lr.LastHash())
343+
// A lagging cursor stops at retentionFloor so an unflushed localTip can still
344+
// be written. The cursor is never rewound: already past localTip means it is on disk.
304345
if floor := bq.retentionFloor(); i.nextBlockToPersist[lane] < floor {
305346
i.nextBlockToPersist[lane] = floor
306347
}

0 commit comments

Comments
 (0)