Repository navigation
Description
Summary
GetNextWorkRequired performs a linear pprev walk to find the previous block in the incoming header's Cadence lane. If one lane has been starved for a long interval, validating a header for that lane costs O(distance to the last same-lane block), up to O(chain height).
An invalid header can reach this lookup after satisfying only its claimed proof-of-work target. An attacker can claim mainnet powLimit, produce the corresponding inexpensive proof of work, and force the predecessor walk before rejection at the contextual nBits equality check.
This is a low-severity asymmetric CPU hardening issue, not a chain split or proof-of-work bypass.
Verified on
origin/1.0.0 at 988756471aeecdf4463c04be49da2b7b89a98c21.
Source trace
GetPreviousSameTypeBlockadvances throughpprevone entry at a time until it finds the requested lane or reaches the ASERT anchor:src/pow.cpp#L220-L230.- Cadence
GetNextWorkRequiredinvokes that walk for every header after activation:src/pow.cpp#L327-L355. ContextualCheckBlockHeadercomputes the required target before comparing it with the incomingnBits:src/validation.cpp#L4556-L4565.- The earlier context-free header check validates proof of work against the header's claimed
nBits:src/validation.cpp#L4283-L4289. - Mainnet
powLimitis0000ffff..., approximately 16 bits of expected work:src/kernel/chainparams.cpp#L136. CBlockIndexstores cumulativenAuxPow, but no same-lane predecessor link:src/chain.h#L170-L216.HeadersSyncState::MakeHeaderDifficultyStateindependently walkspprevuntil it finds both lanes, and is called twice during construction and again when redownload begins:src/headerssync.cpp#L55-L93,src/headerssync.cpp#L265.
The attack cost is therefore roughly one powLimit header proof per forced contextual-validation scan. The scan length depends on honest chain history: normally it is short, but there is no consensus rule that prevents a lane from remaining unused for a long interval. Low-work headers synchronization contains the same starvation-dependent history lookup and should share the optimized index rather than retain a second linear path.
Expected behavior
Computing the required target for either Cadence lane should be O(1), independent of lane starvation length. Initializing headers-sync difficulty state from any known branch should use the same constant-time lane lookup.
Recommended fix
Cache the most recent permissionless and AuxPoW predecessors in or alongside CBlockIndex when an index entry is created. GetNextWorkRequired and HeadersSyncState::MakeHeaderDifficultyState can then select the appropriate pointers directly.
The cache must be reconstructible deterministically when loading the block index and must behave correctly across side branches and reorgs. A per-index pointer is preferable to a global active-chain cache because difficulty is also validated on competing branches.
If adding two pointers to every index entry is undesirable, use an equivalent branch-local indexing structure with proven O(1) or O(log n) lookup.
Regression and benchmark coverage
- Build a synthetic chain with a long run of one lane followed by a header for the other lane.
- Assert the optimized lookup returns the same predecessor and
nBitsas the current walk. - Cover both lanes, the anchor boundary, side branches, reorgs, and index reload.
- Verify
HeadersSyncStateinitializes both lane histories from the cached predecessors without a fallback linear walk. - Add a benchmark that compares contextual validation and headers-sync initialization at starvation distances of 1, 1,000, and 100,000 and demonstrates distance-independent behavior after the change.
Acceptance criteria
- Same-lane predecessor lookup is no longer linear in starvation distance.
- No starvation-dependent lane-predecessor walk remains in contextual header validation or headers-sync initialization.
- Difficulty results remain byte-for-byte identical on existing ASERT/Cadence test vectors.
- Cached state is correct on forks, after reorg, and after restart/reindex.
- A performance regression test or benchmark covers a highly starved lane in both affected paths.
Activity