Detect transaction problems early during deserialization

bitcoin/bitcoin: PR #36906

1 minor finding · 5 suggestions

Findings

Link

[minor] · resize() eagerly constructs elements from truncated transactions

On a synced transaction-relaying node, a roughly 50-byte tx payload now constructs 100,000 outputs before failing; the extra work is bounded, and its throughput impact is unmeasured.

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

Details and suggested fix

One minimal input followed by a count of 100,000 outputs and no output data charges 3,600,220 WU, below the 3,999,676 WU P2P budget. vout.resize() constructs every claimed output before the first read fails. Base already reserved capacity, but constructed only the first output before reading it. The same eager construction applies to vin. The failure occurs before transaction tracking, and the exception handler only logs it, so the peer can repeat the message. Memory remains bounded and is released on failure.

Keep the caps and minimum-weight precharges, but use reserve() followed by emplace_back() immediately before reading each element. This preserves the allocation bound while making construction grow with the data actually received. A large-count truncated-input test can guard that distinction.


Optional improvements

Link

[suggestion] · Overweight-block tests stop before consensus validation

The changed P2P assertions no longer guard the unchanged consensus size and weight rejections, which remain necessary for unbudgeted submitblock and blocks reconstructed from already-known transactions.

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

Details and suggested fix

test_witness_block_size previously expected bad-blk-weight at MAX_BLOCK_WEIGHT + 1; feature_block.py's b24 expected bad-blk-length at MAX_BLOCK_WEIGHT + 4. Both now fail with Weight limit exceeded before those validation checks run. Removing either validation rejection would therefore leave the replacement P2P assertion satisfied. RPC decoding has no weight budget, and compact-block reconstruction combines transaction references without charging a new aggregate budget.

Keep the new P2P assertions and also submit the same fixtures through submitblock, expecting their former validation errors. Submit the witness fixture before shrinking it. Submitting b24 also restores its invalid-parent setup for the following b25 step.


Link

[suggestion] · Budget tests do not enforce precharging before element reads

The complete-encoding tests would still pass if minimum-weight charges moved after element construction, leaving the early-rejection invariant untested.

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

Details and suggested fix

The budget assertions check accounting or eventual rejection on complete encodings. too_many_inputs_or_outputs uses truncated data but no budget, and the transaction fuzzer first requires an unbudgeted decode. Add input-count and output-count cases below the unconditional caps whose minimum weight exceeds the remaining budget, and omit the corresponding element data. Require Weight limit exceeded rather than end of data, and assert that the corresponding destination vector remains empty. These cases distinguish the intended precharge from an equally accurate charge after construction; retain the full-data accounting tests alongside them.


Link

[suggestion] · The compact-block rejection test is introduced in a temporary form

The reject_empty commit introduces implicit compact-block parameters and a test that the next commit immediately rewrites; preparing the parameter plumbing first would make the behavioral change easier to validate.

Location: src/blockencodings.h:69 at 9de186b5271f (commit) (commit).

Details and suggested fix

After the count-cap commit 039f5ce69d04ef61e2f5f2c1bbabd5b973cedf50, 9de186b5271fe3f7a9edad1740d86ea4a5db7998 adds reject_empty, hard-codes TX_WITH_WITNESS_P2P in BlockTransactions and PrefilledTransaction, and adds EmptyTransactionDeserializationTest using those implicit parameters. The refactor a0356ec9493bf419d7c2318e9948cf49397a4e33 then removes both wrappers and rewrites that new test with explicit parameters. Reviewers must validate two versions of the same parameter selection and test, including an intermediate policy applied to every deserialization of these types.

Move the refactor before the rejection commit, initially passing TX_WITH_WITNESS at all callers, including receive and deserialize-fuzz sites. Then let the rejection commit switch the receive and P2P-deserialization fuzz sites to TX_WITH_WITNESS_P2P and add the explicit-parameter test once. The final tree stays unchanged. The reordered commits have not been built.


Link

[suggestion] · Regenerated compact-block responses lack witness assertions

The non-cached compact-block response correctly uses TX_WITH_WITNESS, but the existing functional tests would not detect replacing it with TX_NO_WITNESS.

Location: src/net_processing.cpp:2717 (head).

Details and suggested fix

test_compactblock_construction compares prefilled wtxid_hex values for an announcement and a getdata request for the newly mined tip. That request hits the cache. test_compactblocks_not_at_tip reaches regeneration but only waits for a response. Since PrefilledTransaction now uses caller-selected parameters, TX_NO_WITNESS here would strip the prefilled coinbase witness without failing those assertions. Extend the construction test by mining another block, waiting for its announcement, clearing the recorded announcement, and requesting the original witness-bearing block with MSG_CMPCT_BLOCK. Run check_compactblock_construction_from_block on that reply. The extra block replaces the cache while the original remains within MAX_CMPCTBLOCK_DEPTH.


Link

[suggestion] · RPC count-limit changes lack a release note

RPC decoding now rejects transactions above 24,390 inputs or 111,111 outputs, changing testmempoolaccept from a rejection result to error -22; the undocumented change affects only transactions that cannot be valid.

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

Details and suggested fix

The caps apply regardless of TransactionSerParams, so both DecodeHexTx modes enforce them before RPC validation. The added mempool_accept.py assertion expects -22 with TX decode failed where base returned allowed: false with bad-txns-oversize. Add a short release note listing the caps and changed error behavior. Distinguish these unconditional limits from the empty-transaction rejection and aggregate-weight budget enabled only for P2P.


Coverage details

Background

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

Unresolved disagreements

The inspected historical issue #36576 leaves the BIP152 treatment of null transactions and entirely empty blocks disputed. Its author questioned disconnecting for these cases because the specification emphasizes commitments and valid chain extension; a reply argued that committing to a transaction presupposes an actual transaction, and that zero transactions cannot define a coherent block merkle root.

This is directly relevant to moving empty-transaction failures into deserialization for compact messages. The issue also identifies the competing resource concern: declining to disconnect could lead to requesting a full block already known to be unwanted. The inspected discussion contains no resolution, and Core's own pre-forwarding CheckBlock behavior does not by itself settle another implementation's protocol obligations.

Source 1 · Source 2