Skip to content

Commit 228b086

Browse files
committed
Merge #12083: Improve getchaintxstats test coverage
57e6786 qa: Improve getchaintxstats functional test (João Barbosa) 501b439 rpc: Refactor blockhash parse in getchaintxstats (João Barbosa) Pull request description: Tree-SHA512: 61dec5cb68122998df7ec7b5239830f3caf0fe7185c107a66f27653ab2531a800db19a09050671b6fa8dbb5b53181da861eb31199c79d8635f246ccfa0d10efd
2 parents d3f4dd3 + 57e6786 commit 228b086

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/rpc/blockchain.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,25 +1542,19 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
15421542
const CBlockIndex* pindex;
15431543
int blockcount = 30 * 24 * 60 * 60 / Params().GetConsensus().nPowTargetSpacing; // By default: 1 month
15441544

1545-
bool havehash = !request.params[1].isNull();
1546-
uint256 hash;
1547-
if (havehash) {
1548-
hash = uint256S(request.params[1].get_str());
1549-
}
1550-
1551-
{
1545+
if (request.params[1].isNull()) {
15521546
LOCK(cs_main);
1553-
if (havehash) {
1554-
auto it = mapBlockIndex.find(hash);
1555-
if (it == mapBlockIndex.end()) {
1556-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1557-
}
1558-
pindex = it->second;
1559-
if (!chainActive.Contains(pindex)) {
1560-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block is not in main chain");
1561-
}
1562-
} else {
1563-
pindex = chainActive.Tip();
1547+
pindex = chainActive.Tip();
1548+
} else {
1549+
uint256 hash = uint256S(request.params[1].get_str());
1550+
LOCK(cs_main);
1551+
auto it = mapBlockIndex.find(hash);
1552+
if (it == mapBlockIndex.end()) {
1553+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1554+
}
1555+
pindex = it->second;
1556+
if (!chainActive.Contains(pindex)) {
1557+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block is not in main chain");
15641558
}
15651559
}
15661560

test/functional/rpc_blockchain.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ def _test_getblockchaininfo(self):
102102
def _test_getchaintxstats(self):
103103
self.log.info("Test getchaintxstats")
104104

105+
# Test `getchaintxstats` invalid extra parameters
106+
assert_raises_rpc_error(-1, 'getchaintxstats', self.nodes[0].getchaintxstats, 0, '', 0)
107+
108+
# Test `getchaintxstats` invalid `nblocks`
109+
assert_raises_rpc_error(-1, "JSON value is not an integer as expected", self.nodes[0].getchaintxstats, '')
110+
assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, -1)
111+
assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, self.nodes[0].getblockcount())
112+
113+
# Test `getchaintxstats` invalid `blockhash`
114+
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[0].getchaintxstats, blockhash=0)
115+
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getchaintxstats, blockhash='0')
116+
blockhash = self.nodes[0].getblockhash(200)
117+
self.nodes[0].invalidateblock(blockhash)
118+
assert_raises_rpc_error(-8, "Block is not in main chain", self.nodes[0].getchaintxstats, blockhash=blockhash)
119+
self.nodes[0].reconsiderblock(blockhash)
120+
105121
chaintxstats = self.nodes[0].getchaintxstats(1)
106122
# 200 txs plus genesis tx
107123
assert_equal(chaintxstats['txcount'], 201)
@@ -133,8 +149,6 @@ def _test_getchaintxstats(self):
133149
assert('window_interval' not in chaintxstats)
134150
assert('txrate' not in chaintxstats)
135151

136-
assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, 201)
137-
138152
def _test_gettxoutsetinfo(self):
139153
node = self.nodes[0]
140154
res = node.gettxoutsetinfo()

0 commit comments

Comments
 (0)