@@ -2,7 +2,6 @@ package avail
22
33import (
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.
10414type inner struct {
10515 nextCommitQCToPersist types.RoadIndex // first road whose CommitQC is not persisted
@@ -118,10 +28,16 @@ type inner struct {
11828}
11929
12030type 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
13854func 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