Merge bitcoin/bitcoin#34512: rpc: add coinbase_tx field to getblock

e0463b4e8c25f8a5fe10999f2821e7b221d2e40a rpc: add coinbase_tx field to getblock (Sjors Provoost)

Pull request description:

  This adds a `coinbase_tx` field to the `getblock` RPC result, starting at verbosity level 1. It contains only fields guaranteed to be small, i.e. not the outputs.

  Initial motivation for this was to more efficiently scan for BIP54 compliance. Without this change, it requires verbosity level 2 to get the coinbase, which makes such scan very slow. See https://github.com/bitcoin-inquisition/bitcoin/pull/99#issuecomment-3852370506.

  Adding these fields should be useful in general though and hardly makes the verbosity 1 result longer.

  ```
  bitcoin rpc help getblock

  getblock "blockhash" ( verbosity )

  If verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.
  If verbosity is 1, returns an Object with information about block <hash>.
  If verbosity is 2, returns an Object with information about block <hash> and information about each transaction.
  ...
  Result (for verbosity = 1):
  {                                 (json object)
    "hash" : "hex",                 (string) the block hash (same as provided)
    "confirmations" : n,            (numeric) The number of confirmations, or -1 if the block is not on the main chain
    "size" : n,                     (numeric) The block size
    "strippedsize" : n,             (numeric) The block size excluding witness data
    "weight" : n,                   (numeric) The block weight as defined in BIP 141
    "coinbase_tx" : {               (json object) Coinbase transaction metadata
      "version" : n,                (numeric) The coinbase transaction version
      "locktime" : n,               (numeric) The coinbase transaction's locktime (nLockTime)
      "sequence" : n,               (numeric) The coinbase input's sequence number (nSequence)
      "coinbase" : "hex",         (string) The coinbase input's script
      "witness" : "hex"             (string, optional) The coinbase input's first (and only) witness stack element, if present
    },
    "height" : n,                   (numeric) The block height or index
    "version" : n,                  (numeric) The block version
  ...
  ```

  ```
  bitcoin rpc getblock 000000000000000000013c986f9aebe800a78454c835ccd07ecae2650bfad3f6 1
  ```

  ```json
  {
    "hash": "000000000000000000013c986f9aebe800a78454c835ccd07ecae2650bfad3f6",
    "confirmations": 2,
    "height": 935113,
    "version": 561807360,
    "...": "...",
    "weight": 3993624,
    "coinbase_tx": {
      "version": 2,
      "locktime": 0,
      "sequence": 4294967295,
      "coinbase": "03c9440e04307c84692f466f756e6472792055534120506f6f6c202364726f70676f6c642ffabe6d6d9a8624235259d3680c972b0dd42fa3fe1c45c5e5ae5a96fe10c182bda17080e70100000000000000184b17d3f138020000000000",
      "witness": "0000000000000000000000000000000000000000000000000000000000000000"
    },
    "tx": [
      "70eb053340c7978c5aa1b34d75e1ba9f9d1879c09896317f306f30c243536b62",
      "5bcf8ed2900cb70721e808b8977898e47f2c9001fcee83c3ccd29e51c7775dcd",
      "3f1991771aef846d7bb379d2931cccc04e8421a630ec9f52d22449d028d2e7f4",
      "..."
    ]
  }
  ```

ACKs for top commit:
  sedited:
    Re-ACK e0463b4e8c25f8a5fe10999f2821e7b221d2e40a
  darosior:
    re-utACK e0463b4e8c25f8a5fe10999f2821e7b221d2e40a

Tree-SHA512: 1b3e7111e6a0edffde8619c49b3e9bca833c8e90e416defc66811bd56dd00d45b69a84c8fd9715508f4e6515f77ac4fb5c59868ab997ae111017c78c05b74ba3
This commit is contained in:
merge-script 2026-02-19 20:31:33 +01:00
commit 37e449dcc7
No known key found for this signature in database
GPG Key ID: 9B79B45691DB4173
3 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,8 @@
Updated RPCs
------------
- The `getblock` RPC now returns a `coinbase_tx` object at verbosity levels 1, 2,
and 3. It contains `version`, `locktime`, `sequence`, `coinbase` and
`witness`. This allows for efficiently querying coinbase
transaction properties without fetching the full transaction data at
verbosity 2+. (#34512)

View File

@ -181,6 +181,24 @@ UniValue blockheaderToJSON(const CBlockIndex& tip, const CBlockIndex& blockindex
return result;
}
/** Serialize coinbase transaction metadata */
UniValue coinbaseTxToJSON(const CTransaction& coinbase_tx)
{
CHECK_NONFATAL(!coinbase_tx.vin.empty());
const CTxIn& vin_0{coinbase_tx.vin[0]};
UniValue coinbase_tx_obj(UniValue::VOBJ);
coinbase_tx_obj.pushKV("version", coinbase_tx.version);
coinbase_tx_obj.pushKV("locktime", coinbase_tx.nLockTime);
coinbase_tx_obj.pushKV("sequence", vin_0.nSequence);
coinbase_tx_obj.pushKV("coinbase", HexStr(vin_0.scriptSig));
const auto& witness_stack{vin_0.scriptWitness.stack};
if (!witness_stack.empty()) {
CHECK_NONFATAL(witness_stack.size() == 1);
coinbase_tx_obj.pushKV("witness", HexStr(witness_stack[0]));
}
return coinbase_tx_obj;
}
UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIndex& tip, const CBlockIndex& blockindex, TxVerbosity verbosity, const uint256 pow_limit)
{
UniValue result = blockheaderToJSON(tip, blockindex, pow_limit);
@ -188,6 +206,10 @@ UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIn
result.pushKV("strippedsize", ::GetSerializeSize(TX_NO_WITNESS(block)));
result.pushKV("size", ::GetSerializeSize(TX_WITH_WITNESS(block)));
result.pushKV("weight", ::GetBlockWeight(block));
CHECK_NONFATAL(!block.vtx.empty());
result.pushKV("coinbase_tx", coinbaseTxToJSON(*block.vtx[0]));
UniValue txs(UniValue::VARR);
txs.reserve(block.vtx.size());
@ -760,6 +782,14 @@ static RPCHelpMan getblock()
{RPCResult::Type::NUM, "size", "The block size"},
{RPCResult::Type::NUM, "strippedsize", "The block size excluding witness data"},
{RPCResult::Type::NUM, "weight", "The block weight as defined in BIP 141"},
{RPCResult::Type::OBJ, "coinbase_tx", "Coinbase transaction metadata",
{
{RPCResult::Type::NUM, "version", "The coinbase transaction version"},
{RPCResult::Type::NUM, "locktime", "The coinbase transaction's locktime (nLockTime)"},
{RPCResult::Type::NUM, "sequence", "The coinbase input's sequence number (nSequence)"},
{RPCResult::Type::STR_HEX, "coinbase", "The coinbase input's script"},
{RPCResult::Type::STR_HEX, "witness", /*optional=*/true, "The coinbase input's first (and only) witness stack element, if present"},
}},
{RPCResult::Type::NUM, "height", "The block height or index"},
{RPCResult::Type::NUM, "version", "The block version"},
{RPCResult::Type::STR_HEX, "versionHex", "The block version formatted in hexadecimal"},

View File

@ -646,6 +646,26 @@ class BlockchainTest(BitcoinTestFramework):
self.wallet.send_self_transfer(fee_rate=fee_per_kb, from_node=node)
blockhash = self.generate(node, 1)[0]
def assert_coinbase_metadata(hash, verbosity):
block = node.getblock(hash, verbosity)
coinbase_tx = node.getblock(hash, 2)["tx"][0]
expected_keys = {"version", "locktime", "sequence", "coinbase"}
if "txinwitness" in coinbase_tx["vin"][0]:
expected_keys.add("witness")
assert_equal(set(block["coinbase_tx"].keys()), expected_keys)
assert_equal(block["coinbase_tx"]["version"], coinbase_tx["version"])
assert_equal(block["coinbase_tx"]["locktime"], coinbase_tx["locktime"])
assert_equal(block["coinbase_tx"]["sequence"], coinbase_tx["vin"][0]["sequence"])
assert_equal(block["coinbase_tx"]["coinbase"], coinbase_tx["vin"][0]["coinbase"])
witness_stack = coinbase_tx["vin"][0].get("txinwitness")
if witness_stack is None:
assert "witness" not in block["coinbase_tx"]
else:
assert_equal(block["coinbase_tx"]["witness"], witness_stack[0])
def assert_hexblock_hashes(verbosity):
block = node.getblock(blockhash, verbosity)
assert_equal(blockhash, hash256(bytes.fromhex(block[:160]))[::-1].hex())
@ -692,6 +712,9 @@ class BlockchainTest(BitcoinTestFramework):
assert_fee_not_in_block(blockhash, 1)
assert_fee_not_in_block(blockhash, True)
self.log.info("Test getblock coinbase metadata fields")
assert_coinbase_metadata(blockhash, 1)
self.log.info('Test that getblock with verbosity 2 and 3 includes expected fee')
assert_fee_in_block(blockhash, 2)
assert_fee_in_block(blockhash, 3)