Detect transaction problems early during deserialization

bitcoin/bitcoin: PR #36906

1 minor finding · 4 suggestions

Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive for their assessments.

Findings

Link

[minor] · Truncated transactions eagerly construct every declared input or output

A tiny truncated peer message now constructs about 24,000 inputs or 111,000 outputs before discovering missing data, rather than one element at base; the timing impact is unmeasured.

Location: src/primitives/transaction.h:273 (head).

Details and suggested fix

A connected peer can send an 88-byte block payload containing a header, a one-transaction count, a version, and an input count of 24,388, then stop. Outside block import, this reaches tx.vin.resize() before any request or block-header validation: 32 + 12 + 24,388 × 164 equals the 3,999,676 weight allowance, and charge() permits equality. One input followed by a large output count similarly constructs about 111,000 CTxOut objects. The old VectorFormatter reserved capacity but constructed and read one element at a time, so it failed on the first missing element instead. The exception handler only logs, allowing repetition without disconnection; ordinary tx relay outside IBD also reaches this path. Use bounded reserve(num) followed by emplace_back() immediately before each read, retaining the count caps and upfront weight charge without constructing the unread tail.


Optional improvements

Link

[suggestion] · Overweight-block tests stop before the validation checks

The changed P2P expectations leave the contextual weight and stripped-size rejection branches without their previous functional coverage, even though RPC block submission still relies on them.

Location: test/functional/p2p_segwit.py:942 (head).

Details and suggested fix

The witness-bearing MAX_BLOCK_WEIGHT + 1 fixture and non-witness b24 now stop at Weight limit exceeded, before reaching their validation checks. The remaining bad-blk-length fixtures use empty vtx vectors, and no test still asserts bad-blk-weight. DecodeHexBlk uses unlimited TX_WITH_WITNESS, so submitblock still depends on validation. P2P blocks with a three-byte transaction count can also pass the transaction-only budget while exceeding MAX_BLOCK_WEIGHT by up to eight weight units. Keep the new P2P assertions and additionally submit these fixtures through submitblock, expecting bad-blk-weight for the witness block and bad-blk-length for b24. The contextual weight check already backs up the stripped-size check, so removing only the latter would not make b24 acceptable.


Link

[suggestion] · Weight-limit tests do not pin rejection before vector construction

The new weight-limit tests check accounting on complete objects, so they do not pin early rejection of truncated transactions whose declared minimum input or output weight exceeds the budget.

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

Details and suggested fix

weight_limited_deserialization and WeightLimitedDeserializationTest provide complete serialized objects and check the final accounting or Weight limit exceeded. Moving the minimum-weight charge after the read loops would preserve those assertions. The truncated cases in too_many_inputs_or_outputs use unlimited TX_WITH_WITNESS, and the fuzz target returns before testing limits when unlimited decoding fails. Add truncated cases declaring counts below the hard caps but above the remaining weight budget: a version and input count without inputs, and one input followed by an output count without outputs. Through a limited ParamsStream, require Weight limit exceeded rather than end of data, and assert that the corresponding destination vector remains empty.


Link

[suggestion] · RPC decoding behavior changes without a release note

For transactions that can never fit in a block, testmempoolaccept now raises -22 instead of returning bad-txns-oversize, but this RPC behavior change has no release note.

Location: test/functional/mempool_accept.py:245 (head).

Details and suggested fix

The new limits of 24,390 inputs and 111,111 outputs apply under both TX_WITH_WITNESS and TX_NO_WITNESS, so neither DecodeHexTx decoding attempt can bypass them. The existing oversized-input test previously expected allowed: false with bad-txns-oversize; the new 24,391-input case instead expects a top-level TX decode failed error. Add a short doc/release-notes-<PR>.md entry describing these decoding caps and the changed RPC result, consistent with the API-change guidance in doc/developer-notes.md.


Link

[suggestion] · Temporary compact-block parameter wiring obscures the reject_empty change

The final tree is correct, but adding hard-coded P2P parameters and rewriting the new test in the next commit creates avoidable review churn.

Location: src/blockencodings.h:69 at 79ac5541b0e3 (commit) (commit).

Details and suggested fix

79ac5541b0e3a1d85a16f2d86def196ada086574 adds reject_empty by hard-coding TX_WITH_WITNESS_P2P inside BlockTransactions and PrefilledTransaction. The compact-block parameter refactor, 482aba265cf4ab2d8a4a07a0ff967094039402f8, immediately removes that wiring and rewrites the new EmptyTransactionDeserializationTest to distinguish the two modes.

Move the parameter refactor directly after the count-cap commit, 093c7d7c4f73137bbd934ed09b1f0492d2bd97dd, using the already available TX_WITH_WITNESS at the compact-block and block-transactions receive sites and in the block_header_and_short_txids_deserialize, prefilled_transaction_deserialize, and blocktransactions_deserialize fuzz targets. Then let the reject_empty commit switch those callers, plus transaction and block reception, to TX_WITH_WITNESS_P2P and add the test once in its final two-mode form. This separates mechanical parameter plumbing from the behavior change while retaining the prerequisite for the shared weight state added by 57a26d1a63091964dc7010f45b7d836e11561bda, with no final-tree change.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.