Skip to content

Commit cbd2f70

Browse files
instagibbsfanquake
authored andcommitted
expose CBlockIndex::nTx in getblock(header)
GitHub-Pull: #13451 Rebased-From: 86edf4a
1 parent ce8aa54 commit cbd2f70

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

src/rpc/blockchain.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
105105
result.push_back(Pair("bits", strprintf("%08x", blockindex->nBits)));
106106
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
107107
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
108+
result.push_back(Pair("nTx", (uint64_t)blockindex->nTx));
108109

109110
if (blockindex->pprev)
110111
result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
@@ -150,6 +151,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
150151
result.push_back(Pair("bits", strprintf("%08x", block.nBits)));
151152
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
152153
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
154+
result.push_back(Pair("nTx", (uint64_t)blockindex->nTx));
153155

154156
if (blockindex->pprev)
155157
result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
@@ -679,6 +681,7 @@ UniValue getblockheader(const JSONRPCRequest& request)
679681
" \"bits\" : \"1d00ffff\", (string) The bits\n"
680682
" \"difficulty\" : x.xxx, (numeric) The difficulty\n"
681683
" \"chainwork\" : \"0000...1f3\" (string) Expected number of hashes required to produce the current chain (in hex)\n"
684+
" \"nTx\" : n, (numeric) The number of transactions in the block.\n"
682685
" \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n"
683686
" \"nextblockhash\" : \"hash\", (string) The hash of the next block\n"
684687
"}\n"
@@ -748,6 +751,7 @@ UniValue getblock(const JSONRPCRequest& request)
748751
" \"bits\" : \"1d00ffff\", (string) The bits\n"
749752
" \"difficulty\" : x.xxx, (numeric) The difficulty\n"
750753
" \"chainwork\" : \"xxxx\", (string) Expected number of hashes required to produce the chain up to this block (in hex)\n"
754+
" \"nTx\" : n, (numeric) The number of transactions in the block.\n"
751755
" \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n"
752756
" \"nextblockhash\" : \"hash\" (string) The hash of the next block\n"
753757
"}\n"

test/functional/feature_pruning.py

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

263+
# Save block transaction count before pruning, assert value
264+
block1_details = node.getblock(node.getblockhash(1))
265+
assert_equal(block1_details["nTx"], len(block1_details["tx"]))
266+
263267
# mine 6 blocks so we are at height 1001 (i.e., above PruneAfterHeight)
264268
node.generate(6)
265269
assert_equal(node.getblockchaininfo()["blocks"], 1001)
266270

271+
# Pruned block should still know the number of transactions
272+
assert_equal(node.getblockheader(node.getblockhash(1))["nTx"], block1_details["nTx"])
273+
267274
# negative heights should raise an exception
268275
assert_raises_rpc_error(-8, "Negative", node.pruneblockchain, -10)
269276

test/functional/rpc_blockchain.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def _test_getblockheader(self):
185185
assert_equal(header['confirmations'], 1)
186186
assert_equal(header['previousblockhash'], secondbesthash)
187187
assert_is_hex_string(header['chainwork'])
188+
assert_equal(header['nTx'], 1)
188189
assert_is_hash_string(header['hash'])
189190
assert_is_hash_string(header['previousblockhash'])
190191
assert_is_hash_string(header['merkleroot'])

0 commit comments

Comments
 (0)