Skip to content

Commit 3336676

Browse files
committed
Fix getchaintxstats()
1. Calculate nblocks more adaptive. If not specify nblocks-parameter, illegal parameter error will happen when target block height is below blocks for 1 month. To avoid this error, set default nblocks to min(blocks for 1 month, target block's height - 1) And allowing 0 so that this RPC works good even if target block is genesis block or 1st block. 2. Correct error message. nblocks accepts [0 .. block's height -1] . so fix as following: "Invalid block count: should be between 0 and the block's height - 1" 3. Add check 0-divide. If nTimeDiff = 0 then returns {... "txrate":} and bitcoin-cli cannot handle the response. To avoid this error, do not return "txrate" if nTimeDiff = 0. 4. Add following 3 elements to the return object. 1) 'window_block_count' : Size of the window in number of blocks. 2) 'window_tx_count' : The number of transactions in the window. 3) 'window_interval' : The elapsed time in the window. They clarify how 'txrate' is calculated. 2) and 3) are returned only if 'window_block_count' is a positive value. 5. Improve help text for 'time' as following. 'The timestamp for the final block in the window in UNIX format.
1 parent 2c9f5ec commit 3336676

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/rpc/blockchain.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,9 +1477,12 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
14771477
"2. \"blockhash\" (string, optional) The hash of the block that ends the window.\n"
14781478
"\nResult:\n"
14791479
"{\n"
1480-
" \"time\": xxxxx, (numeric) The timestamp for the statistics in UNIX format.\n"
1481-
" \"txcount\": xxxxx, (numeric) The total number of transactions in the chain up to that point.\n"
1482-
" \"txrate\": x.xx, (numeric) The average rate of transactions per second in the window.\n"
1480+
" \"time\": xxxxx, (numeric) The timestamp for the final block in the window in UNIX format.\n"
1481+
" \"txcount\": xxxxx, (numeric) The total number of transactions in the chain up to that point.\n"
1482+
" \"window_block_count\": xxxxx, (numeric) Size of the window in number of blocks.\n"
1483+
" \"window_tx_count\": xxxxx, (numeric) The number of transactions in the window. Only returned if \"window_block_count\" is > 0.\n"
1484+
" \"window_interval\": xxxxx, (numeric) The elapsed time in the window in seconds. Only returned if \"window_block_count\" is > 0.\n"
1485+
" \"txrate\": x.xx, (numeric) The average rate of transactions per second in the window. Only returned if \"window_interval\" is > 0.\n"
14831486
"}\n"
14841487
"\nExamples:\n"
14851488
+ HelpExampleCli("getchaintxstats", "")
@@ -1489,10 +1492,6 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
14891492
const CBlockIndex* pindex;
14901493
int blockcount = 30 * 24 * 60 * 60 / Params().GetConsensus().nPowTargetSpacing; // By default: 1 month
14911494

1492-
if (!request.params[0].isNull()) {
1493-
blockcount = request.params[0].get_int();
1494-
}
1495-
14961495
bool havehash = !request.params[1].isNull();
14971496
uint256 hash;
14981497
if (havehash) {
@@ -1515,8 +1514,14 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
15151514
}
15161515
}
15171516

1518-
if (blockcount < 1 || blockcount >= pindex->nHeight) {
1519-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block count: should be between 1 and the block's height");
1517+
if (request.params[0].isNull()) {
1518+
blockcount = std::max(0, std::min(blockcount, pindex->nHeight - 1));
1519+
} else {
1520+
blockcount = request.params[0].get_int();
1521+
1522+
if (blockcount < 0 || (blockcount > 0 && blockcount >= pindex->nHeight)) {
1523+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block count: should be between 0 and the block's height - 1");
1524+
}
15201525
}
15211526

15221527
const CBlockIndex* pindexPast = pindex->GetAncestor(pindex->nHeight - blockcount);
@@ -1526,7 +1531,14 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
15261531
UniValue ret(UniValue::VOBJ);
15271532
ret.push_back(Pair("time", (int64_t)pindex->nTime));
15281533
ret.push_back(Pair("txcount", (int64_t)pindex->nChainTx));
1529-
ret.push_back(Pair("txrate", ((double)nTxDiff) / nTimeDiff));
1534+
ret.push_back(Pair("window_block_count", blockcount));
1535+
if (blockcount > 0) {
1536+
ret.push_back(Pair("window_tx_count", nTxDiff));
1537+
ret.push_back(Pair("window_interval", nTimeDiff));
1538+
if (nTimeDiff > 0) {
1539+
ret.push_back(Pair("txrate", ((double)nTxDiff) / nTimeDiff));
1540+
}
1541+
}
15301542

15311543
return ret;
15321544
}

0 commit comments

Comments
 (0)