Skip to content

Commit 94faec0

Browse files
authored
removed parent hash from empty LaneRanges (#4805)
Tracking last block hash of each lane in CommitQC lane ranges for the sake of checking parent link is misleading, because it is not required for correctness and sequencing mismatching lane ranges is the core property of autobahn. However it is important for the lane producer to track hash of his own lane to make sure that it is accepted by validators who haven't pruned the parent yet. We accomodate that by changing the pruning height from LaneRange.Next to LaneRange.Next-1 for all lanes. Block producer now broadcasts only persisted blocks to make broadcasting crash-safe.
1 parent dec8f28 commit 94faec0

12 files changed

Lines changed: 340 additions & 770 deletions

File tree

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (m *Proposal) View() View { return m.header.view(m.viewNumber) }
134134
func (m *Proposal) Header() *RoadBlockHeader { return m.header }
135135

136136
// Verify checks epoch binding and lane-range bounds, lengths, committee membership,
137-
// and empty height-0 hashes. FullProposal.Verify additionally checks QC-chain continuity.
137+
// and empty-range hashes. FullProposal.Verify additionally checks QC-chain continuity.
138138
func (m *Proposal) Verify(ep *Epoch) error {
139139
return m.header.Verify(ep)
140140
}
@@ -204,8 +204,7 @@ func buildProposal(
204204
) (*Proposal, error) {
205205
var laneRanges []*LaneRange
206206
for lane := range committee.Lanes().All() {
207-
prev := LaneRangeOpt(viewSpec.CommitQC, lane)
208-
first := prev.Next()
207+
first := LaneRangeOpt(viewSpec.CommitQC, lane).Next()
209208
if lQC, ok := laneQCs[lane]; ok {
210209
if lQC.Header().Lane() != lane {
211210
return nil, fmt.Errorf("laneQC %v for lane %v", lQC.Header().Lane(), lane)
@@ -216,7 +215,12 @@ func buildProposal(
216215
}
217216
laneRanges = append(laneRanges, laneRange)
218217
} else {
219-
laneRanges = append(laneRanges, &LaneRange{lane: lane, first: first, next: first, lastHash: prev.LastHash()})
218+
// Empty ranges carry NO last hash. This is intentional:
219+
// Sole purpose of last hash is to identify the lane blocks in the proposal.
220+
// Sequencing mismatching lane ranges is the core property of autobahn, i.e.
221+
// we do NOT require last hash of range in previous proposal to be parent of the first block
222+
// of the range in next proposal.
223+
laneRanges = append(laneRanges, NewLaneRange(lane, first, utils.None[*BlockHeader]()))
220224
}
221225
}
222226
return &Proposal{viewNumber: viewSpec.View().Number, header: newRoadBlockHeader(viewSpec.View().EpochIndex, viewSpec.View().Index, timestamp, laneRanges)}, nil
@@ -320,17 +324,10 @@ func (m *FullProposal) Verify(vs ViewSpec, viewTimeout time.Duration) error {
320324
// Verify each lane range against the previous commitQC and its laneQC justification.
321325
for lane := range c.Lanes().All() {
322326
r := proposal.Header().LaneRange(lane)
323-
prev := LaneRangeOpt(vs.CommitQC, r.Lane())
324327
// Verify that range matches previous commitQC.
325-
if got, want := r.First(), prev.Next(); got != want {
328+
if got, want := r.First(), LaneRangeOpt(vs.CommitQC, r.Lane()).Next(); got != want {
326329
return fmt.Errorf("laneRange[%v].First() = %v, want %v", r.Lane(), got, want)
327330
}
328-
// An empty range carries the previous commit's LastHash.
329-
if r.Len() == 0 {
330-
if got, want := r.LastHash(), prev.LastHash(); got != want {
331-
return fmt.Errorf("laneRange[%v].LastHash() = %v, want %v", r.Lane(), got, want)
332-
}
333-
}
334331
// Verify that the necessary laneQC is present and valid.
335332
if r.First() < r.Next() {
336333
qc, ok := m.LaneQC(r.Lane())

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

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -450,83 +450,6 @@ func TestProposalVerifyAcceptsNonContiguousImplicitRanges(t *testing.T) {
450450
require.NoError(t, shortFP.Verify(vs, testViewTimeout))
451451
}
452452

453-
func TestProposalEmptyLaneAtHeightZeroHasNoHash(t *testing.T) {
454-
rng := utils.TestRng()
455-
committee, keys := GenCommittee(rng, 4)
456-
ep := genFreshEpoch(rng, committee)
457-
vs := ViewSpec{ConsensusSpec: ConsensusSpec{Epoch: ep}}
458-
proposerKey := leaderKey(committee, keys, vs.View())
459-
460-
fp := utils.OrPanic1(NewProposal(proposerKey, vs, vs.NextTimestamp(), oneLaneQCMap(rng, committee, keys, vs)))
461-
require.NoError(t, fp.Verify(vs, testViewTimeout))
462-
var idle LaneID
463-
for lane := range committee.Lanes().All() {
464-
r := fp.Header().LaneRange(lane)
465-
if r.Len() == 0 {
466-
require.Equal(t, BlockNumber(0), r.Next())
467-
require.Equal(t, BlockHeaderHash{}, r.LastHash())
468-
idle = lane
469-
}
470-
}
471-
require.NotEqual(t, LaneID{}, idle)
472-
473-
// A lane that has not started must not name a tip. There is no previous
474-
// CommitQC to copy.
475-
var ranges []*LaneRange
476-
for _, r := range fp.Proposal().Msg().header.laneRanges {
477-
if r.Lane() == idle {
478-
ranges = append(ranges, &LaneRange{lane: idle, first: 0, next: 0, lastHash: GenBlockHeaderHash(rng)})
479-
continue
480-
}
481-
ranges = append(ranges, r)
482-
}
483-
invented := &FullProposal{
484-
proposal: Sign(proposerKey, &Proposal{viewNumber: fp.Proposal().Msg().View().Number, header: newRoadBlockHeader(fp.Proposal().Msg().View().EpochIndex, fp.Proposal().Msg().View().Index, fp.Proposal().Msg().header.timestamp, ranges)}),
485-
laneQCs: fp.laneQCs,
486-
}
487-
require.Error(t, invented.Verify(vs, testViewTimeout))
488-
}
489-
490-
func TestProposalCopiesEmptyLaneLastHash(t *testing.T) {
491-
rng := utils.TestRng()
492-
committee, keys := GenCommittee(rng, 4)
493-
ep := genFreshEpoch(rng, committee)
494-
vs0 := ViewSpec{ConsensusSpec: ConsensusSpec{Epoch: ep}}
495-
lane0 := committee.Lanes().At(0)
496-
lane1 := committee.Lanes().At(1)
497-
498-
fp0 := utils.OrPanic1(NewProposal(
499-
leaderKey(committee, keys, vs0.View()), vs0, vs0.NextTimestamp(), oneLaneQCMap(rng, committee, keys, vs0)))
500-
require.NoError(t, fp0.Verify(vs0, testViewTimeout))
501-
tip := fp0.Header().LaneRange(lane0).LastHash()
502-
require.NotEqual(t, BlockHeaderHash{}, tip)
503-
504-
cqc := makeCommitQCFromProposal(keys, fp0)
505-
vs1 := ViewSpec{ConsensusSpec: ConsensusSpec{CommitQC: utils.Some(cqc), Epoch: ep}}
506-
// Extend a different lane so lane0's range is empty and must carry tip.
507-
qc1 := makeLaneQC(rng, committee, keys, lane1, 0, BlockHeaderHash{})
508-
fp1 := utils.OrPanic1(NewProposal(
509-
leaderKey(committee, keys, vs1.View()), vs1, vs1.NextTimestamp(), map[LaneID]*LaneQC{lane1: qc1}))
510-
require.NoError(t, fp1.Verify(vs1, testViewTimeout))
511-
empty := fp1.Header().LaneRange(lane0)
512-
require.Equal(t, uint64(0), empty.Len())
513-
require.Equal(t, tip, empty.LastHash())
514-
515-
var ranges []*LaneRange
516-
for _, r := range fp1.Proposal().Msg().header.laneRanges {
517-
if r.Lane() == lane0 {
518-
ranges = append(ranges, &LaneRange{lane: lane0, first: r.First(), next: r.First(), lastHash: BlockHeaderHash{}})
519-
continue
520-
}
521-
ranges = append(ranges, r)
522-
}
523-
stripped := &FullProposal{
524-
proposal: Sign(leaderKey(committee, keys, vs1.View()), &Proposal{viewNumber: fp1.Proposal().Msg().View().Number, header: newRoadBlockHeader(fp1.Proposal().Msg().View().EpochIndex, fp1.Proposal().Msg().View().Index, fp1.Proposal().Msg().header.timestamp, ranges)}),
525-
laneQCs: fp1.laneQCs,
526-
}
527-
require.Error(t, stripped.Verify(vs1, testViewTimeout))
528-
}
529-
530453
func TestProposalVerifyRejectsLaneRangeFirstMismatch(t *testing.T) {
531454
rng := utils.TestRng()
532455
committee, keys := GenCommittee(rng, 4)

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

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

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

5452
// Verify verifies the LaneRange against the committee.
@@ -59,8 +57,8 @@ func (m *LaneRange) Verify(c *Committee) error {
5957
if m.first > m.next {
6058
return fmt.Errorf("invalid range [%v,%v)", m.first, m.next)
6159
}
62-
if m.next == 0 && m.lastHash != (BlockHeaderHash{}) {
63-
return errors.New("non-zero hash for height 0")
60+
if m.first == m.next && m.lastHash != (BlockHeaderHash{}) {
61+
return errors.New("non-zero hash for an empty range")
6462
}
6563
return nil
6664
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,15 @@ func TestSuperBlockMergesTransactionsInHeaderOrder(t *testing.T) {
208208
require.Equal(t, [][]byte{{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0}, {2, 1}}, block.Txs)
209209
}
210210

211-
func TestLaneRangeVerifyRejectsHashAtHeightZero(t *testing.T) {
211+
func TestLaneRangeVerifyRejectsHashOnEmptyRange(t *testing.T) {
212212
rng := utils.TestRng()
213213
committee, _ := GenCommittee(rng, 4)
214214
lane := committee.Lanes().At(0)
215215

216216
require.Error(t, (&LaneRange{lane: lane, first: 0, next: 0, lastHash: GenBlockHeaderHash(rng)}).Verify(committee))
217217
require.NoError(t, (&LaneRange{lane: lane, first: 0, next: 0}).Verify(committee))
218-
// Past height 0 an empty range may carry a tip. FullProposal.Verify checks that it matches.
219-
require.NoError(t, (&LaneRange{
218+
// Empty ranges have a zero hash at every height.
219+
require.Error(t, (&LaneRange{
220220
lane: lane, first: 1, next: 1, lastHash: GenBlockHeaderHash(rng),
221221
}).Verify(committee))
222222
}

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

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

33
import (
44
"fmt"
5-
"log/slog"
65
"time"
76

87
"github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types"
@@ -11,95 +10,6 @@ import (
1110
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
1211
)
1312

14-
// blockQueue is one lane's candidate suffix [first, next).
15-
//
16-
// Blocks inside the suffix are parent-linked, so an honest LaneQC voter for its
17-
// tip has the ancestors back to first. The block at first may name any parent,
18-
// so a cut may splice onto a different branch. A CommitQC that chose another
19-
// hash does not rewrite the suffix: execution fetches the chosen body separately
20-
// and verifies it against the FullCommitQC. Prune, driven by the AppQC anchor,
21-
// drops only heights below the anchor's Next(); a later block stays until its
22-
// own height is pruned. parentOfFirstLaneBlock is the parent ProduceLocalBlock
23-
// uses when the suffix is empty. It does not constrain admission.
24-
type blockQueue struct {
25-
queue[types.BlockNumber, *types.Signed[*types.LaneProposal]]
26-
// localTip is None, or this node's latest pushed proposal, at a height of at least first-1.
27-
localTip utils.Option[*types.Signed[*types.LaneProposal]]
28-
// parentOfFirstLaneBlock is the certified tip at first-1. Zero before any prune.
29-
parentOfFirstLaneBlock types.BlockHeaderHash
30-
}
31-
32-
func newBlockQueue() *blockQueue {
33-
return &blockQueue{queue: *newQueue[types.BlockNumber, *types.Signed[*types.LaneProposal]]()}
34-
}
35-
36-
func (q *blockQueue) pushBack(p *types.Signed[*types.LaneProposal]) {
37-
q.queue.pushBack(p)
38-
q.localTip = utils.Some(p)
39-
if q.Len() == 1 {
40-
q.logLocalBlock()
41-
}
42-
}
43-
44-
// parentHash is the parent ProduceLocalBlock uses for the block at q.next:
45-
// the in-queue predecessor while first < next, else parentOfFirstLaneBlock.
46-
func (q *blockQueue) parentHash() types.BlockHeaderHash {
47-
if q.first < q.next {
48-
return q.q[q.next-1].Msg().Block().Header().Hash()
49-
}
50-
return q.parentOfFirstLaneBlock
51-
}
52-
53-
// prune drops [first, newFirst) and records parent as parentOfFirstLaneBlock.
54-
// A stale newFirst changes nothing. localTip is kept when newFirst <= next and
55-
// cleared when newFirst > next. A block at or after newFirst stays.
56-
func (q *blockQueue) prune(newFirst types.BlockNumber, parent types.BlockHeaderHash) {
57-
if newFirst <= q.first {
58-
return
59-
}
60-
if newFirst > q.next {
61-
q.localTip = utils.None[*types.Signed[*types.LaneProposal]]()
62-
}
63-
q.queue.prune(newFirst)
64-
q.parentOfFirstLaneBlock = parent
65-
q.logLocalBlock()
66-
}
67-
68-
// logLocalBlock logs when the block at first does not name parent.
69-
func (q *blockQueue) logLocalBlock() {
70-
if q.first >= q.next {
71-
return
72-
}
73-
h := q.q[q.first].Msg().Block().Header()
74-
if h.ParentHash() == q.parentOfFirstLaneBlock {
75-
return
76-
}
77-
logger.Info("local block does not extend certified tip",
78-
"lane", h.Lane(),
79-
slog.Uint64("block", uint64(h.BlockNumber())),
80-
"got", h.ParentHash(),
81-
"want", q.parentOfFirstLaneBlock)
82-
}
83-
84-
// unpersistedLocalTip returns localTip once it has left the active range and
85-
// block persistence has not reached it.
86-
func (q *blockQueue) unpersistedLocalTip(nextToPersist types.BlockNumber) utils.Option[*types.Signed[*types.LaneProposal]] {
87-
if p, ok := q.localTip.Get(); ok {
88-
if n := p.Msg().Block().Header().BlockNumber(); n < q.first && nextToPersist <= n {
89-
return utils.Some(p)
90-
}
91-
}
92-
return utils.None[*types.Signed[*types.LaneProposal]]()
93-
}
94-
95-
// retentionFloor returns the lowest block number the WAL must still hold.
96-
func (q *blockQueue) retentionFloor() types.BlockNumber {
97-
if p, ok := q.localTip.Get(); ok {
98-
return min(q.first, p.Msg().Block().Header().BlockNumber())
99-
}
100-
return q.first
101-
}
102-
10313
// inner holds roads and lane states.
10414
type inner struct {
10515
nextCommitQCToPersist types.RoadIndex // first road whose CommitQC is not persisted
@@ -118,10 +28,16 @@ type inner struct {
11828
}
11929

12030
type laneState struct {
121-
blocks *blockQueue
31+
blocks *queue[types.BlockNumber, *types.Signed[*types.LaneProposal]]
12232
votes *queue[types.BlockNumber, *blockVotes]
33+
// hash of the last lane block, used as parent hash ONLY for local lane production, NEVER for validation.
34+
// Resetting the parent hash is a valid producer behavior, but makes other validators
35+
// wait for previous blocks to be pruned before accepting, slowing lane production.
36+
// None on startup (even if last block is not pruned), so that parent hash reset is
37+
// exercised on every restart (instead of being an obscure corner case).
38+
lastHash utils.Option[types.BlockHeaderHash]
12339
// nextBlockToPersist tracks per-lane how far block persistence has progressed.
124-
// RecvBatch only yields blocks below this cursor for voting.
40+
// Proposal and vote subscriptions only yield blocks below this cursor.
12541
// Always initialized (even when persistence is disabled — the no-op persist
12642
// goroutine bumps it immediately). Not persisted to disk: on restart it is
12743
// reconstructed from the blocks already on disk (see newInner).
@@ -130,14 +46,14 @@ type laneState struct {
13046
// inner waiters (PushVote, PushCommitQC, etc.) on setNextBlockToPersist calls.
13147
// Now that blocks are persisted concurrently by lane (one notification per
13248
// lane per batch, not per block), the frequency is lower, but still not
133-
// ideal. Only RecvBatch needs to be notified of cursor changes;
49+
// ideal. Only proposal and vote subscriptions need cursor notifications;
13450
// collectPersistBatch is in the same goroutine and reads it directly.
13551
nextBlockToPersist types.BlockNumber
13652
}
13753

13854
func newLaneState() *laneState {
13955
return &laneState{
140-
blocks: newBlockQueue(),
56+
blocks: newQueue[types.BlockNumber, *types.Signed[*types.LaneProposal]](),
14157
votes: newQueue[types.BlockNumber, *blockVotes](),
14258
}
14359
}
@@ -205,19 +121,16 @@ func (i *inner) restoreBlocks(blocks map[types.LaneID][]persist.LoadedBlock) err
205121
return fmt.Errorf("lane %s: loaded %d blocks exceeds capacity %d", lane, len(blocks[lane]), BlocksPerLane)
206122
}
207123
if b.Number < q.next {
208-
// Certified. Restore localTip from the proposal at first-1 when present.
209-
if b.Number+1 == q.first {
210-
q.localTip = utils.Some(b.Proposal)
211-
}
212124
continue
213125
}
214126
if b.Number != q.next {
215127
return fmt.Errorf("lane %s: non-contiguous persisted blocks: expected %d, got %d", lane, q.next, b.Number)
216128
}
217-
// Parent is checked only inside [first, next). The persisted block at
218-
// first is this lane's local block. localTip restored from first-1 is
219-
// for local production, not this check.
220-
if q.first < q.next {
129+
// We check the parent hash only for the blocks above the anchor, because:
130+
// * node can cast LaneVote for the block of the lane without checking the parent hash,
131+
// in case the previous block was already (executed and) pruned from memory.
132+
// * current WAL implementation is lazily pruning on disk, so old executed blocks might be loaded on startup.
133+
if q.Len() > 0 {
221134
ph := b.Proposal.Msg().Block().Header().ParentHash()
222135
if q.q[q.next-1].Msg().Block().Header().Hash() != ph {
223136
return fmt.Errorf("lane %s: parent hash mismatch at block %d", lane, b.Number)
@@ -334,10 +247,8 @@ func (i *inner) setAnchor(anchor data.Anchor) {
334247
for lane, ls := range i.lanes {
335248
lr := types.LaneRangeOpt(qc, lane)
336249
ls.votes.prune(lr.Next())
337-
ls.blocks.prune(lr.Next(), lr.LastHash())
338-
// A lagging cursor stops at retentionFloor so an unflushed localTip can still
339-
// be written. The cursor is never rewound: already past localTip means it is on disk.
340-
ls.nextBlockToPersist = max(ls.nextBlockToPersist, ls.blocks.retentionFloor())
250+
ls.blocks.prune(lr.Next())
251+
ls.nextBlockToPersist = max(ls.nextBlockToPersist, ls.blocks.first)
341252
}
342253
i.advance()
343254
}

0 commit comments

Comments
 (0)