Skip to content

Commit

Permalink
rpc: Avoid getchaintxstats invalid results
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFalke committed Mar 25, 2024
1 parent c122318 commit 2ece999
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1637,9 +1637,13 @@ static RPCHelpMan getchaintxstats()
{RPCResult::Type::STR_HEX, "window_final_block_hash", "The hash of the final block in the window"},
{RPCResult::Type::NUM, "window_final_block_height", "The height of the final block in the window."},
{RPCResult::Type::NUM, "window_block_count", "Size of the window in number of blocks"},
{RPCResult::Type::NUM, "window_tx_count", /*optional=*/true, "The number of transactions in the window. Only returned if \"window_block_count\" is > 0"},
{RPCResult::Type::NUM, "window_tx_count", /*optional=*/true,
"The number of transactions in the window. "
"Only returned if \"window_block_count\" is > 0 and if txcount is non-zero for the start end end of the window."},
{RPCResult::Type::NUM, "window_interval", /*optional=*/true, "The elapsed time in the window in seconds. Only returned if \"window_block_count\" is > 0"},
{RPCResult::Type::NUM, "txrate", /*optional=*/true, "The average rate of transactions per second in the window. Only returned if \"window_interval\" is > 0"},
{RPCResult::Type::NUM, "txrate", /*optional=*/true,
"The average rate of transactions per second in the window. "
"Only returned if \"window_interval\" is > 0 and if window_tx_count exists."},
}},
RPCExamples{
HelpExampleCli("getchaintxstats", "")
Expand Down Expand Up @@ -1680,7 +1684,9 @@ static RPCHelpMan getchaintxstats()

const CBlockIndex& past_block{*CHECK_NONFATAL(pindex->GetAncestor(pindex->nHeight - blockcount))};
const int64_t nTimeDiff{pindex->GetMedianTimePast() - past_block.GetMedianTimePast()};
const int nTxDiff = pindex->nChainTx - past_block.nChainTx;
const auto window_tx_count{
(pindex->nChainTx != 0 && past_block.nChainTx != 0) ? std::optional{pindex->nChainTx - past_block.nChainTx} : std::nullopt,
};

UniValue ret(UniValue::VOBJ);
ret.pushKV("time", (int64_t)pindex->nTime);
Expand All @@ -1689,10 +1695,12 @@ static RPCHelpMan getchaintxstats()
ret.pushKV("window_final_block_height", pindex->nHeight);
ret.pushKV("window_block_count", blockcount);
if (blockcount > 0) {
ret.pushKV("window_tx_count", nTxDiff);
if (window_tx_count) {
ret.pushKV("window_tx_count", *window_tx_count);
}
ret.pushKV("window_interval", nTimeDiff);
if (nTimeDiff > 0) {
ret.pushKV("txrate", ((double)nTxDiff) / nTimeDiff);
if (nTimeDiff > 0 && window_tx_count) {
ret.pushKV("txrate", double(*window_tx_count) / nTimeDiff);
}
}

Expand Down
2 changes: 0 additions & 2 deletions test/sanitizer_suppressions/ubsan
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ unsigned-integer-overflow:CCoinsViewCache::Uncache
unsigned-integer-overflow:CompressAmount
unsigned-integer-overflow:DecompressAmount
unsigned-integer-overflow:crypto/
unsigned-integer-overflow:getchaintxstats*
unsigned-integer-overflow:MurmurHash3
unsigned-integer-overflow:CBlockPolicyEstimator::processBlockTx
unsigned-integer-overflow:TxConfirmStats::EstimateMedianVal
Expand All @@ -62,7 +61,6 @@ implicit-integer-sign-change:CBlockPolicyEstimator::processBlockTx
implicit-integer-sign-change:SetStdinEcho
implicit-integer-sign-change:compressor.h
implicit-integer-sign-change:crypto/
implicit-integer-sign-change:getchaintxstats*
implicit-integer-sign-change:TxConfirmStats::removeTx
implicit-integer-sign-change:prevector.h
implicit-integer-sign-change:verify_flags
Expand Down

0 comments on commit 2ece999

Please sign in to comment.