Review report

bitcoin/bitcoin: PR #36806

11 suggestions

Optional improvements

Link

[suggestion] · Height lookup optimization duplicates the sequence-lock read rules

The skip path is safe today, but retaining earlier transactions' heights makes correctness depend on duplicated sequence-lock read rules.

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

Details and suggested fix

prevheights lives across the transaction loop, and resize() preserves existing entries. An active version-2 transaction followed by a version-1 transaction can therefore pass the first transaction's heights to SequenceLocks. The consumer currently returns or clears disabled entries before reading them, but the new caller-side guard separately reproduces those conditions. A later change to only one copy could make relative locks use zero or stale heights. Keep a single shared definition of the height-read rules in consensus/tx_verify and use it from both paths. Preserve the disabled-entry zeroing that CalculateLockPointsAtTip uses when finding the highest sequence-locked input. Merely zero-filling the skipped vector would remove retained data without keeping the rules synchronized.


Link

[suggestion] · Sequence-lock test bypasses the production height-population guard

A change made only to ConnectBlock's skip predicate would leave this unit test unchanged, despite its claim to detect an unsafe widening.

Location: src/test/transaction_tests.cpp:1341 (head).

Details and suggested fix

heights_needed is an independent copy of the production predicate, and every assertion supplies its own height vector directly to SequenceLocks. Replacing only ConnectBlock's any_of with all_of would therefore leave this test unchanged while skipping height population for mixed disabled/active inputs. Keep the invariant test, but narrow its comment and add a post-CSV block-level case through ProcessNewBlock or TestBlockValidity. Use a version-2 transaction with one disabled input and one input whose height lock is not yet satisfied, in both input orders. Assert bad-txns-nonfinal, then acceptance after maturity. Precede it with a transaction taking the skip path in the same block to exercise vector reuse.


Link

[suggestion] · Performance evidence does not isolate the patch or reproduce the threshold

The reported reindex comparison uses different base revisions, and the checked-in duplicate-input benchmark does not reproduce the small-input measurements supporting the 96-input threshold.

Location: src/consensus/tx_check.cpp:59 (head).

Details and suggested fix

The reported 4h14m25s versus 4h11m58s compares v32.1 with master plus this patch, once each. That does not separate the patch's benefit from intervening changes or run-to-run variation. The dedicated DuplicateInputs benchmark constructs a near-block-size transaction, so it exercises sorting rather than the pairwise threshold. Add the parameterized small-input benchmark used for the table, covering distinct, shared, and mixed output indices around 96 inputs. Repeat reindex-chainstate against the same base revision with and without the patch, including -debug=bench timings. Use those results to decide whether each consensus-path optimization merits its added complexity; omit changes that show negligible benefit.


Link

[suggestion] · P2SH counting leaves a test-only parallel implementation

The fused production loop duplicates GetP2SHSigOpCount's gate and contribution, leaving targeted existing assertions on a wrapper that validation no longer calls.

Location: src/consensus/tx_verify.cpp:172 (head).

Details and suggested fix

GetTransactionSigOpCost previously called GetP2SHSigOpCount; its fused loop now repeats the IsPayToScriptHash() check and GetSigOpCount(scriptSig) contribution. The old helper's remaining callers are unit tests and fuzzing. Both paths share the script counter, and the new mixed-input test covers the production wrapper, but future changes to the separate gates or traversals could leave targeted P2SH assertions testing only the unused copy. Remove the whole-transaction helper and migrate its assertions to GetTransactionSigOpCost with SCRIPT_VERIFY_P2SH only. Compare the scaled P2SH contribution after subtracting scaled legacy cost, and retain the expectation that coinbase contributes zero P2SH cost.


Link

[suggestion] · Duplicate-input boundary test never distinguishes equal indices across txids

An index-only duplicate comparison would satisfy this boundary fixture because every generated outpoint uses the same transaction hash.

Location: src/test/transaction_tests.cpp:1622 (head).

Details and suggested fix

make_tx varies only the output index under hash ONE, and injected duplicates copy the complete outpoint. The final acceptance example uses the same layout. Consequently, the hash comparison never determines whether an equal-index pair is distinct, and the sorting branch sees only one hash group. Add a complementary generator with distinct txids and a shared index such as zero at 95, 96, and 97 inputs. Use deterministic non-sorted ordering, including hashes with long common prefixes. Assert acceptance, then copy a complete outpoint to a distant position and assert bad-txns-inputs-duplicate. This exercises the missing dimension on both sides of the new boundary.


Link

[suggestion] · Sequence-lock comment incorrectly gives height-only locks two inactive bounds

Height-only relative locks can return a nonnegative height bound with time -1, rather than the {-1, -1} pair described by the comment.

Location: src/consensus/tx_verify.cpp:109 (head).

Details and suggested fix

The comment includes inputs using height-based locks among transactions returning {-1, -1}. However, an enabled height-based input updates nMinHeight; for example, sequence 1 at coin height 100 produces {100, -1}. The implementation correctly skips only the MTP comparison and still checks lockPair.first < block.nHeight. Explain the optimization in terms of lockPair.second staying -1 unless a time-based constraint raises it, and explicitly retain the distinction that height-only locks still require the height check.


Link

[suggestion] · Legacy-and-witness weighting test has no witness contribution

The weighting fixture checks legacy cost alone, while the mixed-input fixture has zero legacy cost, leaving their simultaneous accumulation untested by these cases.

Location: src/test/sigopcount_tests.cpp:359 (head).

Details and suggested fix

The weighting test spends an OP_TRUE output, so CountWitnessSigOps contributes zero even when witness verification is enabled. Conversely, the mixed-input test's scriptSigs are push-only and its output script is empty, giving zero legacy cost. Returning early whenever legacy cost is nonzero would therefore satisfy both fixtures. Keep the existing legacy-only and coinbase-short-circuit checks, and add a variant retaining the multisig outputs while spending a P2WPKH output with a two-item witness. Assert legacy + 1 with SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, and legacy with P2SH only.


Link

[suggestion] · Mixed relative-lock transactions still look up every disabled input

Finding one active input causes ConnectBlock to fetch heights for disabled inputs that CalculateSequenceLocks overwrites without reading.

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

Details and suggested fix

If the caller-side gate is retained, replace any_of followed by the unconditional fill with one indexed loop that calls AccessCoin only when the input lacks SEQUENCE_LOCKTIME_DISABLE_FLAG. Keep the existing flags/version guard and the vector resize. This supplies every height the consumer can read; disabled entries are still cleared there, and CheckTxInputs has already checked input availability. It removes the pre-scan and avoids disabled-input lookups in mixed transactions. The existing any_of short-circuits, so fewer lookups do not establish a speedup for every input distribution. This simplification also retains the duplicated exemption rule rather than resolving it.


Link

[suggestion] · Mixed-input sigop fixture repeats outpoints and misdescribes its multisig

Repeated input kinds reuse one outpoint despite the uniqueness comment, and the redeem script contributes two accurate sigops rather than the twenty described.

Location: src/test/sigopcount_tests.cpp:321 (head).

Details and suggested fix

Each creation transaction depends only on kind; repeated kinds have identical inputs, outputs, version, and default locktime. They therefore produce the same outpoint, including in the repeated-P2SH orders. AddCoins permits the overwrite because these creation transactions count as coinbase. The helper still counts each input, so the asserted totals remain meaningful, but the fixture does not contain the distinct coins its comment describes. Give each creation transaction a unique locktime derived from its input index. Also correct the redeem-script comment: 1 <pk> <pk> 2 OP_CHECKMULTISIGVERIFY is 1-of-2, and accurate P2SH counting reads the preceding OP_2, yielding two sigops or eight cost units.


Link

[suggestion] · Extreme-bound test describes a nonexistent saturation sentinel

The test comment presents INT64_MAX as an unlimited-relative-lock representation, although CalculateSequenceLocks neither saturates nor produces that value.

Location: src/test/transaction_tests.cpp:1290 (head).

Details and suggested fix

CalculateSequenceLocks computes the time bound from a block-time median plus the masked sequence shifted by nine, minus one. That bounded calculation contains no saturation step or INT64_MAX sentinel. The evaluator assertions themselves are valid, but their rationale describes a representation that does not exist. Keep the assertions and describe them as synthetic extreme bounds exercising EvaluateSequenceLocks comparisons.


Link

[suggestion] · New std::span use lacks its direct header

The expectation lambda relies on a transitive <span> inclusion rather than declaring its dependency directly.

Location: src/test/sigopcount_tests.cpp:264 (head).

Details and suggested fix

The new lambda names std::span<const Kind>, but this test file's standard-library includes contain only <vector>. The developer notes require directly including headers whose definitions a file uses, even when another header currently supplies them. Add #include <span> so changes to indirect dependencies do not affect this use.


Coverage details