Skip to content

Bound a peer's claimed difficulty before a snapshot walk #216

Closed

Description

@SDL-A1

Migrated from SergioDemianLerner/rustock#216 (pull request by @SDL-A1, 2026-09-29).
Merged: yes. The commits are already in this repository's history.
Original: SergioDemianLerner/rustock#216

Snapshot sync commits to a full header walk — about 25 minutes and 10 GB on a mainnet chain — on the strength of a checkpoint the peer offered, and establishes the claimed cumulative difficulty only at the end of it. This reaches that verdict up front.

The gate

A sampling phase sits between the peer's offer and the walk. It asks for one header every 768 blocks above the shipped checkpoint, verifies each, and bounds the work the blocks between them can carry from the retarget rule — which changes difficulty by a fixed fraction of the parent's per block, with nothing a miner controls affecting that magnitude.

  • a claim above the ceiling is refused and the peer charged
  • a peer that will not answer its samples is refused too, so silence is not a way through

Ordinary sync is deliberately left alone

It verifies headers in order and checks proof of work unconditionally, so a fabricated chain fails at the first header rather than the millionth — there is nothing for a gate to save. A peer overstating the difficulty of a genuine chain costs nothing either: ordinary sync derives total difficulty by accumulation and never stores the claim.

The numbers behind that decision:

gate cost 341 samples + 69 skeletons = 410 requests, ~25 s at rskj's 16.7 req/s
following a chain 6,014 headers/s (measured: 9.25 M in 25 m 38 s)
break-even ~148,000 blocks
fresh node 1,543 s to walk against 25 s to gate — 60x

Snapshot sync only runs when executed == 0, so it is always on the paying side of that.

The checkpoint is configuration

Option<DifficultyCheckpoint> — None on any chain without one, where the walk runs exactly as before. Only mainnet ships one. An earlier draft referenced the mainnet constant unconditionally, which would have gated testnet against a number meaningless there.

The divisor for the sampled window is the post-RSKIP156 value by construction, since the window sits above a checkpoint far above that activation. It is still configuration rather than a literal, because a wrong value there is a wrong verdict about a peer.

Tests

712 pass. Two new session tests: that a configured checkpoint actually reaches SamplingClaim before the walk, and that a chain without one is not gated against a constant that does not apply to it.

The first of those caught a real wiring bug — the phase was being set and then immediately overwritten by the walk's own transition, so the gate would have been silently dead.

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions