Skip to content

A segbuild reader must abort on a missing node, never report it as absent #398

Open

Description

@SDL-A1

TrieStore::get returns Option<Vec<u8>>, so a segbuild reader cannot tell these apart:

  • the key is genuinely absent from the state — a legitimate answer the consensus rules branch on
  • the node is absent from this store — the segment never captured it

Reading the second as the first is unsound, and silently so. Worse, it is asymmetric: only code that reads state the segment-builder never read can hit it, so it always looks like a regression in the newer code. That asymmetry is what made a replay of #374 look like a mainnet consensus divergence at #8,922,011 (docs/trie-segments-design.md §6.9d) — and it defeated a pre/post A/B, because the older code never performs the read at all and so is immune by construction.

A passing run of the previous version is not a control here. That is the trap to close.

What is needed

1. Segments must record negative reads. A marker for "this key was read at build time and was genuinely absent", so re-execution can distinguish a true absence from a gap. Without it, no reader can be sound on its own.

2. The reader must abort, not continue. An unmarked miss is an unknown-unknown and must stop the run — never be reported as absence.

3. The abort message must name both explanations, because the wrong one is the intuitive one:

ABORT: node <hash> not present in segment <name> while executing block #<n>

This is NOT evidence of a consensus bug in the code under test. Two
explanations, and they are not distinguishable from here:

  (a) the code under test reads state the segment never captured, because
      the code that BUILT this segment never read it -- an incomplete
      segment, not a wrong execution;
  (b) the code that built this segment was itself wrong, and the state it
      captured does not match the chain.

Re-run against a complete store (--fallback /mnt/import/rustock-trie, or
the archival trie) before concluding anything about consensus.

Interim mitigation, already in place

replay_ram and reexec_all take --fallback <dir>: segment first, complete trie on miss, counted and logged with the block number. That makes the right answer available and measures the gap, but it does not enforce the distinction, and it trusts the fallback store to be complete. The markers are what make a segment trustworthy on its own.

Related: #397 (repair today's holes).

Activity

  1. SDL-A1 commented on Nov 20, 2026

    @SDL-A1
    CollaboratorAuthor

    Working through the mechanics, I think this needs no markers in the segments — and the fix is smaller and more local than the issue as filed suggests.

    A node-hash lookup can never legitimately miss

    TrieStore::get(hash) is not a key lookup. It is called with a hash read out of a parent node. If a parent references child H, then H exists in the chain's state by construction — the trie is content-addressed, so there is no such thing as "that node is legitimately not there".

    Legitimate absence is expressed differently: the walk descends through nodes that are present and diverges from the path, returning "no value at this key". That requires no missing node at all.

    So the two cases the issue wants distinguished are already distinguishable without recording anything at build time:

    meaning
    walk diverges from the path key genuinely absent — a valid answer
    store.get(child_hash) returns None the store is incomplete or corrupt — always an error

    The only lookup where None is a legitimate answer is the top-level root ("do you hold this state root?"), which is how replay_bench already detects an unseedable range.

    Where the unsoundness actually lives

    crates/trie/src/node.rs converts a store miss into absence in several places:

    • line 119 — let data = store.get(h.as_slice())?; — ? on an Option turns a missing node into None
    • line 138 — same pattern
    • lines 554–558 — a missing value node (long values are stored by hash) returns None
    • line 210 — if let Some(data) = store.get(...) silently skips the node on a miss

    MissingTrieNode exists (crates/execution/src/database.rs:40) and is raised on some paths — which is why a broken shard mapping surfaced as MissingTrieNode during this work — but the trie walk itself does not raise it at the sites above. That asymmetry is exactly why the pegout-index gap read as a clean in_index=false with no error, and why #374 appeared to break mainnet consensus.

    What to change

    1. Make those sites return an error rather than None — a missing referenced node is corruption. The signature change ripples, which is the bulk of the work, and it is in consensus-critical code so it wants the full re-execution as its regression test (now 11.3 h, 187 blk/s, --fallback available).
    2. Keep the top-level root lookup as an Option: "do you hold this root" is a real question with a real negative answer.
    3. Carry the two-explanation text from the issue body into that error, because the intuitive reading of it will still be wrong:
    node <hash> referenced by its parent is absent from the store, while
    executing block #<n>
    
    A referenced node cannot be legitimately absent -- the trie is
    content-addressed -- so this is the store, not the chain. Two causes,
    not distinguishable from here:
      (a) this store is incomplete: it never captured the node (a segbuild
          segment holds only what its build touched);
      (b) the code that built this store was wrong.
    Re-run against a complete store before concluding anything about
    consensus.
    

    Why this matters more than the repair of #397: that fixed the gaps one replay exercised. This makes any future gap announce itself instead of silently changing the answer — and the failure is asymmetric, so it will always look like a regression in whatever code is newest.

    Not implemented here. It touches crates/trie on a consensus path, and it wants its own change, review and replay rather than being folded into this investigation.

  2. SDL-A1 commented on Nov 23, 2026

    @SDL-A1
    CollaboratorAuthor

    Part 1 is being dropped, and the reason is worth recording

    Part 1 proposed that segments record a negative marker — "this key was read at build time and was genuinely absent" — so a reader could tell a true absence from a gap.

    The marker is unnecessary. The distinction is already in the data and is being discarded.

    NodeRef::resolve returns None for two different facts:

    NodeRef::Empty => None,                    // the parent says there is NO child
    NodeRef::Hash(h) => { store.get(h)? ... }  // the parent NAMES a child; the store lacks it

    and TrieNode::find propagates both through ? as "key not found".

    But the two are structurally distinguishable at the moment of the miss:

    • NodeRef::Empty is a genuine absence, proven by the trie itself.
    • NodeRef::Hash(h) with a store miss can never be a genuine absence. The parent commits to that child's hash, so the node exists in the real trie. If the store does not have it, the store is incomplete. That is always a gap.

    So no marker is needed to tell them apart.

    What this changes

    Part 1 as filed Dropping it
    Changes the segment format yes no
    Helps the 106 existing segments no — only ones built after yes, immediately
    Must land before a testnet segment set (#411) yes no
    Cost rebuild or re-tag every segment none

    Part 1's benefit is close to zero and its cost is a format change that invalidates every segment already built.

    What is still being done

    Parts 2 and 3 unchanged — abort on a hash-referenced node the store does not have, with a message naming both explanations — plus making the miss distinguishable where it occurs rather than inferring it afterwards.

    This delivers the whole of this issue's goal on every existing segment. It is also what would have identified segment 026's gap at #3,812,411 on 2026-11-22 immediately, instead of it presenting as STATE ROOT MISMATCH and being investigated as a consensus divergence for a day — the exact failure mode this issue was filed to prevent.

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