utxo split/2 pure connect

willcl-ark/bitcoin: PR #41

6 suggestions

Optional improvements

Link

[suggestion] · The context tests miss the BIP30 re-enforcement boundary

The new tests would not detect removal or an off-by-one change to the existing inclusive BIP30 cutoff at height 1,983,702, although the refactor currently preserves it.

Location: src/test/validation_context_tests.cpp:87 (head).

Details and suggested fix

bip30_above_bip34 tests a candidate at height 227,933, while bip30_follows_the_context supplies enforce_bip30 directly at height 2. Neither exercises height >= BIP34_IMPLIES_BIP30_LIMIT, which overrides the known-BIP34-chain exemption because pre-BIP34 coinbases can encode future heights. Add context-builder assertions on ancestry containing the matching BIP34 block: enforcement should be off at 1,983,701 and on at 1,983,702 and 1,983,703. Use correctly skip-linked ancestry that reaches that block; a direct pprev jump from near the limit does not work because GetAncestor() decrements its walking height by one per pprev step. Also qualify the introductory test comment with the upper limit.


Link

[suggestion] · The context duplicates derived script flags with a permissive default

No current caller with inconsistent flags was found, but omitting script_flags selects SCRIPT_VERIFY_NONE, disabling P2SH, witness and Taproot rules and undercounting P2SH/witness sigops.

Location: src/validation.cpp:2581 (head).

Details and suggested fix

ConnectBlock() takes ctx.script_flags directly for GetTransactionSigOpCost() and CheckInputScripts(), while BIP68 enforcement comes separately from ctx.deployments.csv. The public aggregate permits those values to disagree, and the flag type defaults to zero when its initializer is omitted. Compute the flags with GetBlockScriptFlags(ctx.block_hash, ctx.deployments, ctx.params) in the core and remove the stored field. The factory and test helper already use that expression, so this preserves the required deployment additions and block-hash exceptions while removing the redundant caller obligation. The factory can retain its local consensus-subset check.


Link

[suggestion] · ConnectBlock() leaves the header/context parent invariant unchecked

No current caller with a mismatched parent was found, but the free ConnectBlock() can pass its assertions while using a context and UTXO view for a parent other than block.hashPrevBlock.

Location: src/validation.cpp:2548 (head).

Details and suggested fix

MakeBlockValidationContext() accepts the block hash and prev independently, then derives the height, deployments and ancestor lookup from prev. Supplying the actual block hash with a different parent and a view rooted there satisfies both ctx.block_hash == block.GetHash() and ctx.prev_block_hash == view.GetBestBlock(). The call can therefore evaluate inputs, subsidy and relative locks against the wrong parent without detecting that mismatch. Add assert(block.hashPrevBlock == ctx.prev_block_hash); alongside the existing view assertion. Both parent hashes are null for genesis.


Link

[suggestion] · The new ConnectBlock() contract omits nontransactional view changes

The new free API does not document its existing nontransactional behavior on failure or with just_check, although current callers already isolate the view.

Location: src/validation.h:579 (head).

Details and suggested fix

UpdateCoins() spends and creates coins before the coinbase-amount check and queued-script completion, either of which can make the call return false without reverting those changes. An earlier failure can also leave incomplete undo data, so result.undo is not a general rollback guarantee. With just_check, coin changes still occur; only the best-block update is skipped, as just_check_leaves_best_block explicitly checks. Document both effects and tell callers to use an isolated child cache or resettable overlay, discarding its changes on failure or after a just_check call.


Link

[suggestion] · Commit 3b0789fc mixes consensus decisions with the ConnectBlock() move

Bundling the script-flag and BIP30 extractions with the core move and ordering changes makes it harder to verify that consensus decisions are preserved.

Location: src/validation.cpp:2341 at 3b0789fc6fa0 (commit) (commit).

Details and suggested fix

In 3b0789fc6fa04805c08defc2e2ac121272c24749, deletions from the old Chainstate::ConnectBlock() interleave with additions to EnforceBIP30(). Keep its direct parent, cache-lock commit 6b51e21a75da36fb67f527703b6d766e6aba53d0, first. Split the remaining commit into three steps: the CalculateSequenceLocks()/EvaluateSequenceLocks() and IsBIP30Repeat() overloads; the deployment, script-flag and BIP30 helpers, context builder and ContextualCheck* conversion with validation_context_tests; then the free connect/disconnect functions, node wrapper, best-block/undo ordering and timing changes with validation_core_tests.

The old member can consume ctx.script_flags and ctx.enforce_bip30 in place during the context step, allowing review of the historical script exceptions and inclusive BIP30 limit without the core move in the same diff. Carry declarations, includes and test registration with their users and retain the same final tree. Intermediate builds have not been verified.


Link

[suggestion] · The cache tests miss ValidationCache's reader/writer locking

The new locks match CuckooCache's contract, but no test overlaps ScriptExecutionCached() and CacheScriptExecution() on one cache without an external lock.

Location: src/validation.h:403 (head).

Details and suggested fix

The existing parallel CuckooCache test uses its own mutex and finishes its readers before inserting again. The new core tests use a per-Chain cache and connect inline, so neither exercises the exclusion supplied by ValidationCache itself. Add a workload sharing one cache between an insertion thread and readers using both values of erase, without cs_main or another enclosing mutex. Check misses using disjoint, never-inserted nonzero keys, and reserve guaranteed-hit assertions for controlled synchronized phases because eviction is permitted. Running that workload under TSan can detect removal or weakening of the wrapper's reader/writer exclusion.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

BIP34 does not permanently imply BIP30: a pre-BIP34 coinbase can already begin with the serialized height of a future block. The correction identified candidate heights 209921, 490897, and 1983702. The first remained covered by explicit BIP30 checking; the second's relevant output was already spent, with its spending transaction protected against duplication by another input. Numerous later candidates motivated restoring BIP30 checks from 1983702 onward.

BIP34_IMPLIES_BIP30_LIMIT bounds an optimization whose historical proof has a finite range. The context's enforcement decision must retain the limit independently of BIP34 activation and the known-chain exemption.

Source 1 · Source 2 · Source 3

The historical script exceptions permit broadly enforcing P2SH, WITNESS, and TAPROOT while retaining known-valid buried blocks that fail those flags. The Taproot proposal sought stable flags independent of deployment machinery, simpler testing, protection from future deployment-code mistakes, and additional protection against eclipsed-IBD deep-reorganization attacks. It explicitly acknowledged changed behavior under massive historical reorganizations.

The pure script-flag function must preserve both block-hash exceptions and the separately deployment-controlled additions. These flags intentionally are not a literal replay of every rule's original activation state, and the exception table is not interchangeable with disabling all script checks.

Source 1 · Source 2