Skip to content

Commit 501b439

Browse files
committed
rpc: Refactor blockhash parse in getchaintxstats
1 parent 3fa556a commit 501b439

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
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

0 commit comments

Comments
 (0)