Skip to content

Commit 23e4983

Browse files
fix: use stability window for slot threshold
- Replace hard-coded blockfetchBatchSlotThreshold with dynamic calculation - Use correct security parameters based on current era: * Byron era: K parameter from ByronGenesis.ProtocolConsts.K * Shelley+ eras: SecurityParam from ShelleyGenesis.SecurityParam - Update stability window calculation in both chainsync and validation logic - Calculate stability window as 3k/f for Shelley+ eras, k for Byron era - Ensures blockfetch operations respect protocol-defined stability window Fixes the TODO to calculate slot threshold from protocol params Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent e51acd3 commit 23e4983

File tree

4 files changed

+1569
-38
lines changed

4 files changed

+1569
-38
lines changed

ledger/chainsync.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"encoding/hex"
1919
"errors"
2020
"fmt"
21-
"math/big"
2221
"slices"
2322
"time"
2423

@@ -36,9 +35,8 @@ const (
3635
// This prevents us exceeding the configured recv queue size in the block-fetch protocol
3736
blockfetchBatchSize = 500
3837

39-
// TODO: calculate from protocol params
40-
// Number of slots from upstream tip to stop doing blockfetch batches
41-
blockfetchBatchSlotThreshold = 2500 * 20
38+
// Default/fallback slot threshold for blockfetch batches
39+
blockfetchBatchSlotThresholdDefault = 2500 * 20
4240

4341
// Timeout for updates on a blockfetch operation. This is based on a 2s BatchStart
4442
// and a 2s Block timeout for blockfetch
@@ -141,8 +139,10 @@ func (ls *LedgerState) handleEventChainsyncBlockHeader(e ChainsyncEvent) error {
141139
}
142140
// Wait for additional block headers before fetching block bodies if we're
143141
// far enough out from upstream tip
142+
// Use security window as slot threshold if available
143+
slotThreshold := ls.calculateStabilityWindow()
144144
if e.Point.Slot < e.Tip.Point.Slot &&
145-
(e.Tip.Point.Slot-e.Point.Slot > blockfetchBatchSlotThreshold) &&
145+
(e.Tip.Point.Slot-e.Point.Slot > slotThreshold) &&
146146
(headerCount+1) < allowedHeaderCount {
147147
return nil
148148
}
@@ -267,18 +267,7 @@ func (ls *LedgerState) calculateEpochNonce(
267267
return genesisHashBytes, nil
268268
}
269269
// Calculate stability window
270-
byronGenesis := ls.config.CardanoNodeConfig.ByronGenesis()
271-
shelleyGenesis := ls.config.CardanoNodeConfig.ShelleyGenesis()
272-
if byronGenesis == nil || shelleyGenesis == nil {
273-
return nil, errors.New("could not get genesis config")
274-
}
275-
stabilityWindow := new(big.Rat).Quo(
276-
big.NewRat(
277-
int64(3*byronGenesis.ProtocolConsts.K),
278-
1,
279-
),
280-
shelleyGenesis.ActiveSlotsCoeff.Rat,
281-
).Num().Uint64()
270+
stabilityWindow := ls.calculateStabilityWindow()
282271
var stabilityWindowStartSlot uint64
283272
if epochStartSlot > stabilityWindow {
284273
stabilityWindowStartSlot = epochStartSlot - stabilityWindow

0 commit comments

Comments
 (0)