Skip to content

Commit 7bfb100

Browse files
instagibbsstevenroose
authored andcommitted
expose CBlockIndex::nTx in getblock(header)
1 parent 9644d66 commit 7bfb100

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

qa/rpc-tests/pruning.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,17 @@ def has_block(index):
269269
# should not prune because chain tip of node 3 (995) < PruneAfterHeight (1000)
270270
assert_raises_message(JSONRPCException, "Blockchain is too short for pruning", node.pruneblockchain, height(500))
271271

272+
# Save block transaction count before pruning, assert value
273+
block1_details = node.getblock(node.getblockhash(1))
274+
assert_equal(block1_details["nTx"], len(block1_details["tx"]))
275+
272276
# mine 6 blocks so we are at height 1001 (i.e., above PruneAfterHeight)
273277
node.generate(6)
274278
assert_equal(node.getblockchaininfo()["blocks"], 1001)
275279

280+
# Pruned block should still know the number of transactions
281+
assert_equal(node.getblockheader(node.getblockhash(1))["nTx"], block1_details["nTx"])
282+
276283
# negative heights should raise an exception
277284
assert_raises_message(JSONRPCException, "Negative", node.pruneblockchain, -10)
278285

src/rpc/blockchain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
6262
result.push_back(Pair("merkleroot", blockindex->hashMerkleRoot.GetHex()));
6363
result.push_back(Pair("time", (int64_t)blockindex->nTime));
6464
result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast()));
65+
result.push_back(Pair("nTx", (uint64_t)blockindex->nTx));
6566
result.push_back(Pair("signblock_witness_asm", ScriptToAsmStr(blockindex->proof.solution)));
6667
result.push_back(Pair("signblock_witness_hex", HexStr(blockindex->proof.solution)));
6768

@@ -104,6 +105,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
104105
result.push_back(Pair("tx", txs));
105106
result.push_back(Pair("time", block.GetBlockTime()));
106107
result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast()));
108+
result.push_back(Pair("nTx", (int64_t)blockindex->nTx));
107109
result.push_back(Pair("signblock_witness_asm", ScriptToAsmStr(blockindex->proof.solution)));
108110
result.push_back(Pair("signblock_witness_hex", HexStr(blockindex->proof.solution)));
109111

@@ -605,8 +607,7 @@ UniValue getblockheader(const JSONRPCRequest& request)
605607
" \"merkleroot\" : \"xxxx\", (string) The merkle root\n"
606608
" \"time\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
607609
" \"mediantime\" : ttt, (numeric) The median block time in seconds since epoch (Jan 1 1970 GMT)\n"
608-
" \"signblock_witness_asm\":\"asm\", (string) scriptSig for block signing (asm)'\n"
609-
" \"signblock_witness_hex\":\"hex\", (string) scriptSig for block signing (hex)'\n"
610+
" \"nTx\" : n, (numeric) The number of transactions in the block.\n"
610611
" \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n"
611612
" \"nextblockhash\" : \"hash\", (string) The hash of the next block\n"
612613
"}\n"
@@ -691,8 +692,7 @@ static UniValue getblock(const JSONRPCRequest& request)
691692
" ],\n"
692693
" \"time\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
693694
" \"mediantime\" : ttt, (numeric) The median block time in seconds since epoch (Jan 1 1970 GMT)\n"
694-
" \"signblock_witness_asm\":\"asm\", (string) scriptSig for block signing (asm)'\n"
695-
" \"signblock_witness_hex\":\"hex\", (string) scriptSig for block signing (hex)'\n"
695+
" \"nTx\" : n, (numeric) The number of transactions in the block.\n"
696696
" \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n"
697697
" \"nextblockhash\" : \"hash\" (string) The hash of the next block\n"
698698
"}\n"

0 commit comments

Comments
 (0)