Repository navigation
Description
Migrated from SergioDemianLerner/rustock#215 (pull request by @SDL-A1, 2026-09-29).
Merged: yes. The commits are already in this repository's history.
Original: SergioDemianLerner/rustock#215
Tightens what the header path accepts, and gives the node a way to bound a peer's advertised total difficulty before downloading from it.
Header acceptance
One invariant now holds: a header is stored only once it has been verified, and verification needs its parent. A header whose parent is not held is refused rather than stored, and the refusal is reported so the peer is charged.
Nothing is lost by refusing. Header data is the most replaceable thing in the system, and skeleton chunks are drained in order (PeerChunkTracker::drain_ready), so an honest peer's headers arrive with their parent already stored. A follow-mode announcement running ahead of its parent is simply re-requested — and that caller ignores the result, so it is not punished for it.
Chunk responses are matched to the skeleton by hash and contiguity rather than by block number alone: a chunk is the answer to a specific question, and a matching height does not establish that it is.
Snapshot sync establishes the checkpoint difficulty
It used to accept the peer's figure and write it as the node's own. The walk already reads every header, so it now sums their difficulty; the claim may exceed that sum by no more than the checkpoint block's own difficulty. A session that fails this charges the peer.
Cumulative-difficulty checkpoints
A checkpoint states the work from genesis to a height, so nothing may claim more for that height.
Unlike a hash checkpoint this has a safe failure direction:
| checkpoint is | hash | cumulative difficulty |
|---|---|---|
| wrong | silent capture | detectable contradiction |
| too generous | no such thing | weak, harmless |
| stale | irrelevant or fatal | merely covers less of the chain |
audit_own_chain is what makes shipping one safe: if this node's own validated chain ever carries more work at that height than the checkpoint permits, that is reported rather than silently resolved either way.
The checkpoint is mainnet #9,020,000, read from this project's own synced node. A test checks the constants against their decimal values — it caught two transcription errors while this was being written, one a difficulty that silently truncated because 7.39e21 does not fit in a u64.
Bounding the window above it
Request one header every 768 blocks, verify it, and use the retarget rule to bound what the blocks between samples can carry. The rule changes difficulty by a fixed fraction of the parent's per block and nothing a miner controls changes that magnitude — the timestamp and uncle count pick only the sign — so a ceiling follows.
ChainSampler computes it; SamplingGate drives the requests as a state machine, so it is testable without the service. Three properties are pinned:
- no verdict before the window is covered — otherwise a peer passes by answering the first few samples and going quiet
- silence is rejection, not patience —
Unsubstantiatedrather than pending forever - honest peers pass — driven end-to-end against a truthful peer over 20,000 blocks
Live: the free half. A peer whose claim the checkpoint alone refutes never has its metadata recorded, and best_peer selects on recorded metadata, so it is never chosen. No requests at all.
Not yet invoked: the sampled half. The gate is built and tested; connecting it needs GateActions issued through the request path and responses routed by request id, which is its own change.
Tests
710 pass across sync, core, storage and networking. Notable ones:
- a chunk of mutually unlinked headers leaves the store exactly as it found it — verified to fail against the previous behaviour
- the sampled bound is never below what a flat chain actually carries — caught a real bug, where a strict rise-then-fall model could not express an unchanged difficulty and returned a bound below the truth for short spans
- two existing tests were updated; one had asserted the previous behaviour as intended
🤖 Generated with Claude Code
Activity