Merge #15932: rpc: Serialize in getblock without cs_main

faea56400d rpc: Add lock annotations to block{,header}ToJSON (MarcoFalke)
fab00a5cb9 rpc: Serialize in getblock without cs_main (MarcoFalke)
fa1c3591ad rpc: Use IsValidNumArgs in getblock (MarcoFalke)

Pull request description:

  No need to hold cs_main when serializing a struct to json

  Fixes: #15925

ACKs for commit faea56:
  jnewbery:
    utACK faea56400d5578023133cf4d1c761cdeb0c3e3da
  jonasschnelli:
    utACK faea56400d5578023133cf4d1c761cdeb0c3e3da

Tree-SHA512: 005d378cda1e6024e9f5142f99a8adbefe202cd7bfeaafee55eb909e8990a3790aa27fcf5dd16119cc9afe9dc8bd30f660de40233316781669be166bac3018e7
This commit is contained in:
MarcoFalke 2019-05-03 08:09:14 -04:00 committed by Vijay Das Manikpuri
parent d43f9d4ae1
commit d5986a1e8f
No known key found for this signature in database
GPG Key ID: DB1D81B01DB7C46E
2 changed files with 37 additions and 16 deletions

View File

@ -124,6 +124,9 @@ static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* b
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex)
{
// Serialize passed information without accessing chain state of the active chain!
AssertLockNotHeld(cs_main); // For performance reasons
UniValue result(UniValue::VOBJ);
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
const CBlockIndex* pnext;
@ -153,6 +156,9 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
{
// Serialize passed information without accessing chain state of the active chain!
AssertLockNotHeld(cs_main); // For performance reasons
UniValue result(UniValue::VOBJ);
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
const CBlockIndex* pnext;
@ -904,12 +910,17 @@ static UniValue getblockheaders(const JSONRPCRequest& request)
},
}.Check(request);
LOCK(cs_main);
std::string strHash = request.params[0].get_str();
uint256 hash(uint256S(strHash));
const CBlockIndex* pblockindex = LookupBlockIndex(hash);
const CBlockIndex* pblockindex;
const CBlockIndex* tip;
{
LOCK(cs_main);
pblockindex = LookupBlockIndex(hash);
tip = ::ChainActive().Tip();
}
if (!pblockindex) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}
@ -943,7 +954,7 @@ static UniValue getblockheaders(const JSONRPCRequest& request)
for (; pblockindex; pblockindex = ::ChainActive().Next(pblockindex))
{
arrHeaders.push_back(blockheaderToJSON(::ChainActive().Tip(), pblockindex));
arrHeaders.push_back(blockheaderToJSON(tip, pblockindex));
if (--nCount <= 0)
break;
}
@ -1120,8 +1131,6 @@ static UniValue getblock(const JSONRPCRequest& request)
},
}.Check(request);
LOCK(cs_main);
std::string strHash = request.params[0].get_str();
uint256 hash(uint256S(strHash));
@ -1133,12 +1142,20 @@ static UniValue getblock(const JSONRPCRequest& request)
verbosity = request.params[1].get_bool() ? 1 : 0;
}
const CBlockIndex* pblockindex = LookupBlockIndex(hash);
if (!pblockindex) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}
CBlock block;
const CBlockIndex* pblockindex;
const CBlockIndex* tip;
{
LOCK(cs_main);
pblockindex = LookupBlockIndex(hash);
tip = ::ChainActive().Tip();
const CBlock block = GetBlockChecked(pblockindex);
if (!pblockindex) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}
block = GetBlockChecked(pblockindex);
}
if (verbosity <= 0)
{
@ -1148,7 +1165,7 @@ static UniValue getblock(const JSONRPCRequest& request)
return strHex;
}
return blockToJSON(block, ::ChainActive().Tip(), pblockindex, verbosity >= 2);
return blockToJSON(block, tip, pblockindex, verbosity >= 2);
}
static UniValue pruneblockchain(const JSONRPCRequest& request)

View File

@ -5,9 +5,13 @@
#ifndef BITCOIN_RPC_BLOCKCHAIN_H
#define BITCOIN_RPC_BLOCKCHAIN_H
#include <vector>
#include <stdint.h>
#include <amount.h>
#include <sync.h>
#include <stdint.h>
#include <vector>
extern RecursiveMutex cs_main;
class CBlock;
class CBlockIndex;
@ -33,7 +37,7 @@ double GetDifficulty(const CBlockIndex* blockindex);
void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
/** Block description to JSON */
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false);
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false) LOCKS_EXCLUDED(cs_main);
/** Mempool information to JSON */
UniValue MempoolInfoToJSON(const CTxMemPool& pool);
@ -42,7 +46,7 @@ UniValue MempoolInfoToJSON(const CTxMemPool& pool);
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false);
/** Block header to JSON */
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex);
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex) LOCKS_EXCLUDED(cs_main);
/** Used by getblockstats to get feerates at different percentiles by weight */
void CalculatePercentilesBySize(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector<std::pair<CAmount, int64_t>>& scores, int64_t total_size);