rpc: Refactor blockhash parse in getchaintxstats

This commit is contained in:
João Barbosa 2018-01-05 15:26:24 +00:00
parent 3fa556aee2
commit 501b43921c

View File

@ -1542,15 +1542,12 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
const CBlockIndex* pindex; const CBlockIndex* pindex;
int blockcount = 30 * 24 * 60 * 60 / Params().GetConsensus().nPowTargetSpacing; // By default: 1 month int blockcount = 30 * 24 * 60 * 60 / Params().GetConsensus().nPowTargetSpacing; // By default: 1 month
bool havehash = !request.params[1].isNull(); if (request.params[1].isNull()) {
uint256 hash; LOCK(cs_main);
if (havehash) { pindex = chainActive.Tip();
hash = uint256S(request.params[1].get_str()); } else {
} uint256 hash = uint256S(request.params[1].get_str());
{
LOCK(cs_main); LOCK(cs_main);
if (havehash) {
auto it = mapBlockIndex.find(hash); auto it = mapBlockIndex.find(hash);
if (it == mapBlockIndex.end()) { if (it == mapBlockIndex.end()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
@ -1559,9 +1556,6 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
if (!chainActive.Contains(pindex)) { if (!chainActive.Contains(pindex)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block is not in main chain"); throw JSONRPCError(RPC_INVALID_PARAMETER, "Block is not in main chain");
} }
} else {
pindex = chainActive.Tip();
}
} }
assert(pindex != nullptr); assert(pindex != nullptr);