Skip to content

Validate witness-pruned -reindex-chainstate -assumevalid before wiping local state #53

Closed

Description

@kiwidream

Summary

On a witness-pruned datadir, -reindex-chainstate -assumevalid=<non-null hash> appears to allow local state mutation before proving the supplied hash is usable for witness-pruned historical replay.

The startup guard currently rejects only a null AssumedValidBlock(). It logs that the hash must be above the witness-pruned range, but source order does not enforce that before snapshot chainstate deletion and coins DB wipe paths can run. The later semantic predicate does check that the hash is known, on the best-header chain, and covers the block being validated, but that check is reached after the destructive startup path.

This is not a claimed consensus-acceptance bug. I did not find evidence that the node accepts an invalid chain. The concern is fail-closed startup/recovery behavior: a typo, unknown hash, off-chain hash, or too-low hash may destroy or invalidate recoverable local state before the node proves the recovery command can work.

Affected Branch/Ref

Reviewed as of 2026-06-29:

Candidate Severity and Why

WARN / candidate P2.

Reason: this appears to be a local destructive recovery-safety issue. A witness-pruned node may wipe chainstate or delete snapshot chainstate before proving the supplied -assumevalid value can actually cover stripped historical witness data. I did not prove consensus divergence or invalid-chain acceptance.

Impact

A witness-pruned operator attempting recovery with an unusable non-null -assumevalid value may lose the existing chainstate and/or snapshot chainstate before the node discovers that historical witness-dependent validation still cannot proceed locally.

This can turn a recoverable startup/configuration error into a more expensive recovery path requiring archive peers, snapshot reload, or fresh sync. The risk is highest for:

  • unknown but syntactically valid hashes
  • hashes not on the best-header chain
  • hashes below or inside the stripped witness range
  • boundary hashes around the last stripped / first unstripped block
  • datadirs with an active assumeutxo snapshot chainstate

Source Evidence

-assumevalid parsing accepts syntactically valid non-null hex without semantic validation:

  • src/node/chainstatemanager_args.cpp:40-45 parses -assumevalid with uint256::FromUserHex() and stores the result.
  • src/test/validation_chainstatemanager_tests.cpp:1221-1232 confirms -assumevalid=0x12 is accepted, while invalid characters and overlong hex are rejected.

The witness-pruned -reindex-chainstate guard rejects only null:

  • src/node/chainstate.cpp:66-78 checks options.wipe_chainstate_db && chainman.m_blockman.m_have_witness_pruned.
  • It returns only when chainman.AssumedValidBlock().IsNull().
  • For any non-null hash, it logs: "The supplied hash must be above the witness-pruned range."
  • I did not find an enforcement of that statement before state mutation.

Snapshot chainstate deletion can happen before this guard:

  • src/node/chainstate.cpp:187-198 detects and deletes an existing snapshot chainstate when options.wipe_chainstate_db is true.
  • This occurs before CompleteChainstateInitialization() runs the witness-pruned guard.
  • src/validation.cpp:6732-6747 implements DeleteSnapshotChainstate().
  • src/validation.cpp:5993-6030 removes the snapshot base-blockhash file and destroys the LevelDB directory.

Coins DB wipe can happen after the non-null-only guard but before semantic validation of the hash:

  • src/node/chainstate.cpp:105-113 calls chainstate->InitCoinsDB(... should_wipe=options.wipe_chainstate_db).
  • src/validation.cpp:1990-2008 passes .wipe_data = should_wipe.
  • src/dbwrapper.cpp:230-233 calls leveldb::DestroyDB() when wipe_data is true.

The semantic check exists, but later:

  • src/validation.cpp:2410-2424 checks whether the assumed-valid block is known, whether m_best_header satisfies minimum work, and whether both the assumed-valid block and best header contain the block being validated as an ancestor.
  • src/validation.cpp:2433-2439 uses that in CanSkipScriptChecks().
  • src/validation.cpp:4826-4835 uses it in NeedsWitnessForValidation().
  • src/validation.cpp:3207-3226, src/validation.cpp:3405-3443, and src/validation.cpp:3513-3522 then route missing witness cases into temporary witness recovery.

That means the predicate needed to reject unknown/off-chain/too-low hashes exists, but the startup path does not appear to run it before destructive local state actions.

Existing Test Coverage

test/functional/feature_witness_pruning.py covers:

  • -txindex incompatible with explicit witness pruning: lines 155-164
  • witness pruning behavior and service flag changes: lines 171-232
  • historical verbose getblock and getrawtransaction refusal: lines 234-252
  • recent unpruned block behavior: lines 254-260
  • witness compaction startup recovery/cleanup: lines 262-287
  • witness pruning depth n-1/n/n+1 storage behavior: lines 289-310
  • P2P stripped/witness block behavior: lines 312-337
  • -txindex refusal on a witness-pruned datadir: lines 344-352
  • -reindex refusal on a witness-pruned datadir: lines 354-362
  • -reindex-chainstate with no -assumevalid refusal: lines 364-373
  • -reindex-chainstate with an above-range -assumevalid succeeds: lines 375-379
  • startup compaction with -reindex-chainstate and an above-range -assumevalid: lines 458-476

Related unit coverage exists for the later semantic gate:

  • src/test/validation_chainstatemanager_tests.cpp:214-254 checks NeedsWitnessForValidation() / script-skip behavior for assumed-valid ancestry and alternate-chain cases.

Missing Boundary Tests

I did not find explicit functional coverage for witness-pruned -reindex-chainstate with:

  • unknown non-null -assumevalid
  • hash below the stripped witness range
  • hash inside the stripped witness range but not covering all stripped blocks
  • exact last stripped block
  • first unstripped block
  • off-best-header-chain hash
  • snapshot chainstate present during refusal
  • assertions that refusal happens before chainstate/ wipe or snapshot chainstate deletion

The exact last-stripped vs first-unstripped cases should pin the intended boundary, especially because compaction is file-granular and because the current semantic predicate treats same-height ancestry as covering the candidate block.

Reproduction Plan / Blocker

I did not run a live repro in this pass; this report is based on source order, tests, and GitHub branch/PR state. The workspace did not have an existing build directory, and I avoided creating build/test artifacts while preparing this draft.

Suggested repro:

  1. Extend test/functional/feature_witness_pruning.py after witness compaction has occurred and before the existing above-range success case.
  2. Stop the witness-pruned node.
  3. Record evidence that chainstate/ and any snapshot chainstate directory exist before the attempted restart.
  4. Restart with -fastprune -reindex-chainstate -assumevalid=<unknown nonzero 64-hex hash>.
  5. Assert the node refuses before any Wiping LevelDB or snapshot deletion log appears, and before the recorded chainstate/snapshot state changes.
  6. Repeat for below-range, inside-range, exact-last-stripped, and first-unstripped hashes.
  7. Add a variant with an active snapshot chainstate to prove refusal does not delete the snapshot before the assumevalid preflight.

On current source, I expect the non-null startup guard to pass and the coins DB wipe path to be reachable before the later witness-validation gate discovers the hash cannot cover stripped history. The exact runtime symptom may be refusal, startup stall, or witness-recovery scheduling depending on peer availability, so the repro should assert mutation ordering rather than only a final process exit string.

Proposed Fix Direction

Add a preflight for witness-pruned -reindex-chainstate before any local state deletion or coins DB wipe.

The preflight should validate that a non-null AssumedValidBlock() is:

  • present in the loaded block index
  • on the best-header chain
  • backed by sufficient chain work
  • high enough to cover every witness-pruned block that would otherwise require witness-dependent validation during chainstate replay

The existing IsAssumedValidAncestor() / NeedsWitnessForValidation() logic is close to the needed semantic check, but startup should run an equivalent check before DeleteSnapshotChainstate() and before InitCoinsDB(... should_wipe=true).

If any pre-refusal mutation is intentionally accepted, document that behavior explicitly and add tests proving the intended mutation and recovery semantics.

Acceptance Criteria

  • Witness-pruned -reindex-chainstate validates the supplied non-null -assumevalid before any coins DB wipe.
  • Snapshot chainstate is not deleted before the same preflight succeeds.
  • Unknown, off-chain, below-range, inside-range, exact-last-stripped, and first-unstripped hashes have explicit functional coverage.
  • Tests assert both the final error/success behavior and that refused cases do not mutate chainstate/ or snapshot chainstate.
  • Existing valid above-range -assumevalid recovery still succeeds.
  • Any accepted pre-refusal mutation is documented and tested.

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