Repository navigation
Description
Migrated from SergioDemianLerner/rustock#256 (issue by @SDL-A1, 2026-10-04).
Original: SergioDemianLerner/rustock#256
The bug
ceiling_for bounds the work between two sampled blocks from their header
difficulties. The quantity it must bound is cumulative difficulty, which in
RSK is the header difficulty plus every uncle's.
Nothing in max_work_between or ceiling_for accounts for uncles. The ceiling
is therefore a bound on header-difficulty work, compared against a claim that
includes uncle work.
Same class as #240: a calculation that silently assumed work is the sum of
header difficulties.
Measured, over the shipped sample window (#9,020,000 → #9,049,952)
| header-difficulty work | 214,613,100,898,110,116,704,167,168 |
| real work (uncles included) | 325,633,107,595,286,328,239,037,789 |
| uncles add | +51.7% |
| the ceiling's bound on that window | 360,165,496,948,679,897,859,306,829 |
| bound ÷ header-only | 1.68× — what the slack figures measured |
| bound ÷ real | 1.106× |
It holds today, on a 10.6% margin, and only because the retarget slack at
768-block spacing happens to exceed what uncles add. Nothing guards that
margin, and nothing states the dependency.
Why this is worse than it looks
The tightness table in #230 was measured before total difficulty counted
uncles, so every figure in it is against header-only work. Re-read against real
work:
| spacing | bound ÷ header-only | bound ÷ real | |
|---|---|---|---|
| 192 | 1.13× | 0.745× | rejects every honest peer |
| 384 | 1.28× | 0.845× | rejects every honest peer |
| 768 | 1.68× | 1.104× | holds |
That table recommends tighter spacing as better — tighter bound, more samples.
Acting on it breaks the gate completely: the ceiling falls below the honest
chain's work, every peer is judged Impossible, and a node bounds itself out
of the network. The obvious optimisation is the failure.
A rise in the uncle rate does the same thing without any code change. At the
measured ~1.04 uncles per block the margin is 10.6%; it does not take much of a
shift to consume that.
Fix
max_work_between must bound the uncle contribution, not ignore it. A block
may include at most getUncleListLimit() uncles (10 in rskj's Constants),
and an uncle is a sibling computed from the same parent, so its difficulty sits
within the retarget band of the including block's own.
A sound, loose bound is to scale each block's maximum contribution by
1 + uncleListLimit. That is very generous — 11× — and would make the ceiling
useless at any spacing, so the useful version is tighter: sampled headers carry
uncleCount, so the sampled blocks' contributions can be bounded exactly,
and only the unsampled blocks between them need the loose factor.
Whatever the shape, two things must be true afterwards and are not true now:
- the ceiling must exceed the honest chain's uncle-inclusive work at every
spacing, not only at 768; - a test must assert that, so the next person tightening
SAMPLE_INTERVAL
finds out immediately rather than from peers vanishing.
Related
- Count uncle difficulty, carry uncles on the wire, and freeze them #240 — the same assumption, in the accumulator
- The sampled half of the difficulty gate is built and tested but never enabled #230 — the tightness table this invalidates
- RSKIP: The snapshot sync protocol (informational) rsksmart/RSKIPs#695 — describes the algorithm; the caveat belongs there too
@SDL-A1 commented on 2026-10-04:
Fix design
Worked out in review. Four parts; the first two change sampling, the third
changes the arithmetic, the fourth is process.
1. Sample positions uniformly at random, not on a fixed grid
Today they are checkpoint + k·768 — fully predictable. A peer wanting to
overstate its work mines real uncles at exactly those ~39 heights and fabricates
everywhere else, paying a fraction of a percent of the work it claims.
Draw ~340 positions uniformly at random from the window instead. The peer is
already committed before we choose: it supplied a skeleton of hashes, and
uncle_count and ommers_hash sit inside each header bound by its own proof of
work. It cannot change a block's uncle count after seeing where we look, so it
must either hold the uncles everywhere or be caught where it does not.
This is the security property. It is detection, not estimation — the commitment
is what makes it cheap:
| samples | largest undetected fabrication, at 1e-9 |
|---|---|
| 100 | 18.7% of blocks |
| 340 | 5.9% |
| 1,000 | 2.1% |
| 3,400 | 0.6% |
P(caught) = 1 - (1-f)^k for a cheat touching a fraction f of blocks.
Estimating total work from the same 340 samples instead gives a ±128%
confidence interval — the same requests, a far worse answer, because estimation
throws the commitment away.
2. Then fill any gap wider than 768
Random placement leaves tail gaps, and max_work_between compounds as
(1+1/divisor)^(N/2), so an unbounded gap means an unbounded ceiling. After the
uniform draw, split every gap G > 768 with ceil(G/768) - 1 evenly spaced
samples.
Evenly spaced, not recursively bisected: bisection can only produce
power-of-two subdivisions, so a gap just above 768·2^m costs nearly double for
no benefit.
| gap | needed | bisect | waste |
|---|---|---|---|
| 6,145 | 8 | 15 | 1.9x |
| 12,289 | 16 | 31 | 1.9x |
| 50,000 | 65 | 127 | 2.0x |
It is also one pass of arithmetic rather than a work queue.
The two mechanisms want opposite things and this gives each what it needs: the
retarget bound wants bounded gaps, the detection wants unpredictability.
A fixed grid gives the first and destroys the second; pure random gives the
second and lets the first blow up on a tail gap.
Cost of the fills, measured over 200 trials at k=340:
| window | mean max gap before filling | extra samples |
|---|---|---|
| 1 week (19,960) | 365 | 0.0 |
| today's (29,952) | 564 | 0.1 |
| 4 weeks (79,841) | 1,499 | 13.0 |
| 12 weeks (239,524) | 4,462 | 171.0 |
At a current checkpoint the fills almost never fire.
The fills must not count toward the detection bound. They are not uniform,
so the 5.9% figure rests on the 340 random draws alone. Conflating them would
overstate the guarantee.
3. Bound the uncle contribution instead of ignoring it
- Sampled blocks: exact. The header carries
uncle_count, and an uncle is
a sibling computed from the same parent, so its difficulty sits within the
retarget band. No extra request — RSKIP-698's message is not needed here. - Unsampled blocks: this is what part 1 covers. The consensus cap
(uncleListLimit = 10) is available but useless alone — assuming it
everywhere gives a ceiling 12.2x the honest chain's work, which admits
any liar.
Note there is no consensus bound on how many siblings a height may have;
uncleListLimit bounds only how many a block may reference, and
validateIfUncleWasNeverUsed stops one being counted twice. The real limit on
uncle work is economic — every uncle is a mined block — which is exactly why
unpredictable sampling is the right lever: it forces a peer claiming that work
to have done it.
4. Refresh the checkpoint weekly
The loose part of the bound applies only to unsampled blocks, so a fresher
checkpoint shrinks the problem rather than patching it:
| checkpoint age | worst-case slack, full uncle cap assumed |
|---|---|
| 12 weeks | 26.3% |
| 4 weeks | 9.0% |
| 1 week | 2.3% |
A weekly checkpoint gives a fully conservative bound tighter than today's
accidental 10.6% margin, with no statistics at all. It also keeps part 2 free.
What must be tested
- the ceiling exceeds the honest chain's uncle-inclusive work at every
spacing, not only at 768 — this is what is missing today and what would have
caught the bug; - no two consecutive samples are more than 768 apart after filling;
- the fill count is
ceil(G/768) - 1, not a power of two; - positions differ between runs, so a fixed grid cannot be reintroduced
silently.
@SDL-A1 commented on 2026-10-04:
Implemented in #257, with a correction to the design written up here earlier.
The objection about segments was right, and does not bind. Knowing we have not been lied to about uncles at the sampled heights says nothing about whether a particular 768-block segment runs hot. But the ceiling is a sum over every gap, and the allowance multiplies every gap alike, so the quantity that must be bounded is the population mean. A stretch running hot is paid for by the stretches that do not. That is why the allowance is applied globally rather than per segment.
The objection that we are stuck at 10× was not. With the peer committed to its chain before the draw, the sampled counts are a uniform sample of a fixed population, so a concentration inequality applies adversarially. Empirical Bernstein at δ=1e-9, against measured mainnet counts:
| samples | allowance | vs. the true 1.91 | vs. the 11.0 cap |
|---|---|---|---|
| 340 | 4.20 | 2.20× | 0.38× |
| 1,000 | 2.78 | 1.45× | 0.25× |
| 3,400 | 2.22 | 1.16× | 0.20× |
At the shipped 340 samples the sound allowance is 4.2, not 11 — nearly three times tighter than assuming the cap.
But the precision objection was right, and worse than I had it. The slack is dominated by the range term 3Rln(3/δ)/k, which contributes 1.93 of the 3.20 at k=340 and does not depend on what was observed. uncleListLimit = 10 leaks in through the range whatever the data says; it just decays as 1/k rather than being immovable.
And the lever runs out. Skeleton granularity caps the number of distinct askable heights over the window in the low thousands, so the allowance bottoms out near 1.4× the truth.
Two numbers also needed correcting:
- Uncles add 90.9% to the work over the 276,000 blocks above the checkpoint, not the 51.7% measured over a 30,000-block window. The unfixed bound was therefore 0.88× the real work, not 1.106× — below the honest chain, not above it on a thin margin.
- End to end the fixed ceiling sits 17% above the real chain, not the 0.44% the old tests asserted. A peer can overstate its work by a sixth and be judged plausible. That is the real figure and the tests now assert it.
So the conclusion stands on cost, not on impossibility. A consensus change is the clean fix, and it is a better one than more sampling: RSKIP-699 commits cumulative difficulty to the header extension for +1.22% of header size, which makes the quantity exact and removes the 10.5 GB of uncle headers a header-only sync currently needs purely to add up difficulty.
Activity