!5277odin/sol-scan-latency → develop47362a1c67

Solana scanner waits only the rest of the scan interval

AI-generatedEverything on this page was written by an AI agent from the diff: the summaries, the narration, the diagrams, the flags and the glossary. It can be wrong or incomplete, so check it against the code before you rely on it.

1commits
2files
37lines changed
15lines to read
15s → ≤15sidle wait per cycle

Where the diff is

2-minute explainer

What changed

Old wait

Any lag under the interval slept a full 15 s

lag 10ssleep 15sscan at 25s

New wait

Sleeps interval − lag; same lag ≥ interval gate

lag 10ssleep 5sscan at 15s

Pure helper + table test

scanCoalesceWait returns 0 or interval − lag

lag 0 → 15slag 10s → 5slag 15s → 0lag 40s → 0

Look here first

Short sleeps re-run GetSlot soonerEach loop re-calls GetSlot (and may re-check mimir); a short wait plus a 300ms underestimate can add idle loops, so RPC calls aren't strictly unchanged.sol_scanner.go:256 ↗careful
249249 time.Sleep(s.cfg.Solana.ScanInterval)
250250 return
251251 }
252252
253253 // coalesce scan to configured scan interval
254254 solanaBlockTime := time.Duration(common.SOLChain.ApproximateBlockMilliseconds()) * time.Millisecond
255255 lag := time.Duration(currentSlot-lastSlot) * solanaBlockTime
256−if lag < s.cfg.Solana.ScanInterval {
257− time.Sleep(s.cfg.Solana.ScanInterval)
256+if wait := scanCoalesceWait(lag, s.cfg.Solana.ScanInterval); wait > 0 {
257+ time.Sleep(wait)
258258 return
259259 }
260260
261261 var wg sync.WaitGroup
262262 wg.Add(len(vaultAddrs))
263263
264264 healthy := true
265265 var mu sync.Mutex
266266 setUnhealthy := func() {
267267 mu.Lock()
268268 defer mu.Unlock()
269269 healthy = false
lag rests on 300ms slot estimateElapsed time is slot count × ApproximateBlockMilliseconds; faster slots overstate lag, before and after alike. Constant is consensus-critical, untouched.sol_scanner.go:255 ↗look
246246
247247 // skip if rpc slot is behind last scanned slot
248248 if currentSlot <= lastSlot {
249249 time.Sleep(s.cfg.Solana.ScanInterval)
250250 return
251251 }
252252
253253 // coalesce scan to configured scan interval
254254 solanaBlockTime := time.Duration(common.SOLChain.ApproximateBlockMilliseconds()) * time.Millisecond
255255 lag := time.Duration(currentSlot-lastSlot) * solanaBlockTime
256−if lag < s.cfg.Solana.ScanInterval {
257− time.Sleep(s.cfg.Solana.ScanInterval)
256+if wait := scanCoalesceWait(lag, s.cfg.Solana.ScanInterval); wait > 0 {
257+ time.Sleep(wait)
258258 return
259259 }
260260
261261 var wg sync.WaitGroup
262262 wg.Add(len(vaultAddrs))
263263
264264 healthy := true
265265 var mu sync.Mutex
266266 setUnhealthy := func() {
Behind-RPC branch still sleeps full intervalWhen currentSlot ≤ lastSlot the scanner still sleeps a whole interval; consistent with intent, but untouched by this fix.sol_scanner.go:249 ↗look
240240 if err = s.checkRecentBlocksAndUpdateNetworkFee(s.netFeeSlotMultiple * s.cfg.Solana.FeeUpdateInterval); err != nil {
241241 s.logger.Error().Err(err).Msg("failed to check recent blocks and update network fee")
242242 }
243243 }
244244
245245 lastSlot := s.getLastHeight()
246246
247247 // skip if rpc slot is behind last scanned slot
248248 if currentSlot <= lastSlot {
249249 time.Sleep(s.cfg.Solana.ScanInterval)
250250 return
251251 }
252252
253253 // coalesce scan to configured scan interval
254254 solanaBlockTime := time.Duration(common.SOLChain.ApproximateBlockMilliseconds()) * time.Millisecond
255255 lag := time.Duration(currentSlot-lastSlot) * solanaBlockTime
256−if lag < s.cfg.Solana.ScanInterval {
257− time.Sleep(s.cfg.Solana.ScanInterval)
256+if wait := scanCoalesceWait(lag, s.cfg.Solana.ScanInterval); wait > 0 {
257+ time.Sleep(wait)
258258 return
259259 }
260260
Helper tested, call site notTestScanCoalesceWait covers four lags; the scan() call site needs live RPC and stays uncovered, as before.sol_scanner.go:1150 ↗look
11411141 func convertLamportsToTHORChain(lamports uint64) sdk.Uint {
11421142 lamportsBig := new(big.Int).SetUint64(lamports)
11431143 return sdk.NewUintFromBigInt(lamportsBig.Quo(lamportsBig, big.NewInt(10)))
11441144 }
1145+
1146+// scanCoalesceWait is how long to wait before scanning when the unscanned slots
1147+// span lag: nothing once lag reaches the scan interval, otherwise only the rest
1148+// of the interval. Sleeping a whole interval instead adds up to another interval
1149+// to every observation.
1150+func scanCoalesceWait(lag, interval time.Duration) time.Duration {
1151+ if lag >= interval {
1152+ return 0
1153+ }
1154+ return interval - lag
1155+}

Ask an AI about this change

Chat assistantClaude, ChatGPT, … (reads a link)

Coding agentClaude Code, Cursor, … (reads the repo)

The context file is context.md. Use Copy with context for an AI that can't open links; it pastes the whole file in.

Glossary

ApproximateBlockMilliseconds
Per-chain block-time constant in common; 300 for SOL. Consensus-critical elsewhere, so not changed here. common/chain.go ↗
Bifrost
THORNode's chain-observer daemon; it scans external chains like Solana and reports inbound transactions. bifrost/ ↗
scan_interval
Bifrost Solana config: minimum estimated lag before a scan runs; 15 s by default. config/default.yaml ↗
scanCoalesceWait
New helper: 0 once lag reaches the interval, otherwise interval minus lag. sol_scanner.go ↗
Slot
Solana's block-production unit; lag is estimated as unscanned slots times 300 ms. Solana docs ↗