Skip to content

Cache Cadence lane predecessors for constant-time ASERT header validation #103

Closed

Description

@kiwidream

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

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 nBits as the current walk.
  • Cover both lanes, the anchor boundary, side branches, reorgs, and index reload.
  • Verify HeadersSyncState initializes 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

  1. added
    enhancement
    on Jul 11, 2026
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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions