Skip to content

Count uncle difficulty, carry uncles on the wire, and freeze them #240

Closed

Description

@SDL-A1

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

Makes rustock and rskj able to snapshot sync from each other, and records what
the runs measured.

Total difficulty counts uncle difficulty

rskj advances total difficulty by header.difficulty + sum(uncle difficulties)
(Block.getCumulativeDifficulty). rustock advanced it by the header difficulty
alone, so our totals drifted below rskj's by whatever the chain absorbed in
uncles, and rskj rejected our snap status with a bare INVALID_BLOCK.

Uncle headers live only in block bodies, so the figure cannot be derived from a
header walk. The freezer therefore keeps them in parallel files
(uncles.cidx, uncles-NNNN.cdat), and --repair chain-work rebuilds both
halves in one pass over the bodies. Existing databases need that pass; it took
7h31m for mainnet's 9,288,885 blocks.

Because the pruner works below head - 8000 while the freezer works below
head - 20000, the pruner would reach every block first and delete the only
copy of the uncles. plan_prune now waits for the freezer.

Difficulties are written in two's complement

rskj writes difficulties with BigInteger.toByteArray() and reads them with
new BigInteger(bytes), so a value whose top bit is set needs a leading
0x00. Canonical RLP strips it, and rskj then refuses the status message as a
negative difficulty and drops the connection mid-decode.

This was latent, not introduced: mainnet's total difficulty would have reached
2^95 unaided. Counting uncles brought the date forward.

rsk/63: headers with their uncles

BLOCK_HEADERS_WITH_UNCLES_REQUEST/_RESPONSE carry each trunk header with
the uncles it references, so a header-only client can compute cumulative work.
The pairing is verified against ommers_hash over the bytes as they arrived,
and requests are gated on the negotiated capability — an rsk/62 peer still
gets the plain message.

Reports

  • docs/cross-client-snap-sync.md — the cross-client results, written for rskj
    developers
  • docs/snap-sync-near-tip.md — the rustock-to-rustock run
  • docs/sync-comparison-rskj-rustock.md — refocused on full sync

Snapshot sync works in both directions. rustock completed in 2h05m to a state
root identical to the server's. rskj accepted the status, the difficulties, the
6,000 blocks and all 918 MB of state, then ran out of heap rebuilding it at
-Xmx3g on a 7.7 GB host.

Workspace green: 1,984 tests.

🤖 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