rpc: generateblock to allow multiple outputs with custom payouts

bitcoin/bitcoin: PR #32468

1 minor finding · 7 suggestions

Findings

Link

[minor] · generateblock publishes an incorrect output schema

With show_hidden=true, the OpenRPC schema excludes supported strings and custom-reward arrays and permits rejected objects; no in-tree consumer validating requests against this schema was found.

Location: src/rpc/mining.cpp:347 (head).

Details and suggested fix

The unchecked ARR declaration takes OpenRPCArgSchema's array-or-object fallback, while its only inner STR makes the array string-only. The handler instead accepts a string or an array containing strings and single-pair custom-reward objects, and rejects a top-level object. Unlike the base STR declaration, this misdescribes even the existing single-address form. Add schema support for a string-or-array outer union and string-or-single-pair-object array items, then assert the parameter schema through getopenrpcinfo(True).


Optional improvements

Link

[suggestion] · Fee accounting is undocumented for the two transaction modes

Automatic selection pays selected transaction fees, while explicit lists retain subsidy-only payouts, but the revised generateblock help and release note omit that distinction.

Location: src/rpc/mining.cpp:338 (base).

Details and suggested fix

An explicit list is appended after a use_mempool=false template has fixed the reward. Fetching those txids from the mempool does not run assembler fee accounting. Omitting transactions instead rebuilds the template with selection enabled, whose selected fees enter total_reward. A custom allocation above the subsidy can therefore succeed automatically yet fail when the same selected txids are supplied explicitly. Replace the removed fee sentence with mode-specific help and release-note wording: automatic selection distributes subsidy plus selected fees, while an explicit array distributes only the subsidy.


Link

[suggestion] · Custom amounts do not fix the final recipient payouts

Custom amounts are base allocations rather than fixed payouts; in mempool mode, expressing an exact recipient payment requires predicting the selected fees.

Location: src/rpc/mining.cpp:516 (head).

Details and suggested fix

The loop adds an equal share of total_reward - total_value to every entry, including those with an amount. The mixed-output test deliberately gives the 8 BTC entry 13.66666667 BTC. Thus [{A: X}, B] does not mean “pay A exactly X and give B the rest”, despite the help's “fixed amount” wording. Automatic selection also makes the remainder depend on selected fees. If exact-recipient simulation is required, leave specified amounts unchanged and split the remainder only among unspecified outputs, with a defined policy when every amount is specified. Otherwise, preserve the tested additive rule but call the amounts base allocations in the help and release note, and add a mixed-output example.


Link

[suggestion] · The new tests do not check fee-bearing coinbase totals

The new rpc_generate.py cases verify transaction selection but not fee-bearing payout totals, so distributing only the subsidy in mempool mode would leave their assertions passing.

Location: test/functional/rpc_generate.py:150 (head).

Details and suggested fix

The ten-transaction mempool case checks only transaction count, and the weight and sigops cases check selection and limits. All payout-value assertions use transactions=[]. Reading the reward from the initial no-mempool template instead of the selected template could therefore silently omit fees; successful block submission would not catch that because underclaiming is consensus-valid. Add a transaction with a known odd-satoshi fee and generate unsubmitted blocks at the same tip in both modes with two payouts. Assert subsidy plus fee and the correct remainder distribution in automatic mode, and subsidy only with an explicit transaction list.


Link

[suggestion] · Custom-reward tests cannot distinguish the recipients

In the multi-output tests, address, address2 and address3 all name one script, so the custom-reward assertions cannot detect correct amounts assigned to the wrong recipient.

Location: test/functional/rpc_generate.py:72 (head).

Details and suggested fix

MiniWallet.get_address() returns the stored address, not a fresh destination. Reusing the first recipient's script for later custom-reward objects would therefore preserve the asserted amounts and output positions and still pass. The witness-prefix case uses distinct scripts, but only through string entries. Obtain address2 and address3 from wallets with distinct tag_name values, retain the original wallet for funding, and keep the ordered address/value assertions.


Link

[suggestion] · The over-allocation test does not exercise the cumulative guard

The one-entry rejection case would also pass with a per-entry guard, leaving cumulative custom-reward over-allocation untested.

Location: test/functional/rpc_generate.py:248 (head).

Details and suggested fix

The guard correctly rejects a running total above total_reward, but [{address: 51*COIN}] only tests an individually excessive amount. A per-entry comparison would accept two 15 BTC entries against a 25 BTC reward. The resulting negative remainder budget would reduce both outputs to 12.5 BTC, producing a valid block rather than the requested parameter error. Add [{address: 15*COIN}, {address2: 15*COIN}] alongside the early 25 BTC payout checks, before the 300-block parallel-mining section, and expect RPC error -8 with Sum of custom rewards exceeds the total block reward.


Link

[suggestion] · The multi-output commit leaves its reservation invariants unexplained

The 94c5b8df message omits payout-reservation and commitment-ordering details, while their focused tests arrive later in ed6fed31, making the implementation harder to validate commit by commit.

Location: src/rpc/mining.cpp:438 at 94c5b8df11d2 (commit) (commit).

Details and suggested fix

547e7adc7bfd0228c463de534b0901316bbb9912 introduces automatic mempool selection but retains the unconditional fee disclaimer. In 94c5b8df11d2a8f424357aeb092e5fb3f432bafb, the message says the reward was “previously burned”, although the base paid coinbase_output_script, and does not explain the new weight/sigops reservation or the ordering of RegenerateCommitments before payout installation. The focused resource-limit and witness-prefix tests arrive in ed6fed3177c013482945b32e7415b80036504c7d.

Expand the multi-output commit's message to explain these invariants and correct the base-behavior description. Move test_generateblock_sigops, test_generateblock_weight, and the witness-prefix case into that implementation commit, and update the fee wording in the earlier automatic-selection commit. Keep payout reservation atomic with the multi-payout mempool path: landing it later could select transactions without reserving the installed payouts' weight and sigops, causing intermediate RPC calls to fail TestBlockValidity.


Link

[suggestion] · The weight test never exercises coinbase output-count growth

rpc_generate.py never reaches the 253-output CompactSize boundary, so dropping output_count_increase would leave the current weight assertions passing.

Location: src/rpc/mining.cpp:470 (head).

Details and suggested fix

The weight cases use 64 and 100 payouts, giving 65 and 101 coinbase outputs after the witness commitment. Both counts occupy one byte, so neither exercises output_count_increase. With 252 payouts plus the commitment, the count occupies three bytes and needs another 8 WU. Omitting that reservation could admit transactions that exceed the configured -blockmaxweight by up to 7 WU, while remaining below the consensus maximum. Add a 252-payout case with a raised, tightly tuned weight limit and a known-weight mempool transaction that fits only if those 8 WU are ignored. Assert that it remains unmined and that the block respects the configured limit. The current production calculation is correct.


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

Earlier discussion exposed a tradeoff between realistic miner economics and keeping a testing RPC simple. The fee-collection proposal sought subsidy-plus-fees behavior matching mainnet miners; objections questioned the testing use case and highlighted the need for prevout-based accounting. That proposal was closed, but the inspected record does not establish a permanent prohibition on fee collection or settle payout semantics for a newly added automatic-selection mode.

Historical noncollection explains the explicit-list behavior and its implementation cost. It is not, by itself, evidence that the new mempool mode must discard fees, nor that all caller-selected transactions must begin collecting them.

Source 1 · Source 2 · Source 3 · Source 4