mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
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: utACKfaea56400d
jonasschnelli: utACKfaea56400d
Tree-SHA512: 005d378cda1e6024e9f5142f99a8adbefe202cd7bfeaafee55eb909e8990a3790aa27fcf5dd16119cc9afe9dc8bd30f660de40233316781669be166bac3018e7
This commit is contained in:
commit
94daebf327
2 changed files with 33 additions and 15 deletions
|
@ -93,6 +93,9 @@ static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* b
|
||||||
|
|
||||||
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex)
|
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);
|
UniValue result(UniValue::VOBJ);
|
||||||
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
|
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
|
||||||
const CBlockIndex* pnext;
|
const CBlockIndex* pnext;
|
||||||
|
@ -119,6 +122,9 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
|
||||||
|
|
||||||
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
|
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);
|
UniValue result(UniValue::VOBJ);
|
||||||
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
|
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
|
||||||
const CBlockIndex* pnext;
|
const CBlockIndex* pnext;
|
||||||
|
@ -824,9 +830,7 @@ static CBlock GetBlockChecked(const CBlockIndex* pblockindex)
|
||||||
|
|
||||||
static UniValue getblock(const JSONRPCRequest& request)
|
static UniValue getblock(const JSONRPCRequest& request)
|
||||||
{
|
{
|
||||||
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
|
const RPCHelpMan help{"getblock",
|
||||||
throw std::runtime_error(
|
|
||||||
RPCHelpMan{"getblock",
|
|
||||||
"\nIf verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
|
"\nIf verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
|
||||||
"If verbosity is 1, returns an Object with information about block <hash>.\n"
|
"If verbosity is 1, returns an Object with information about block <hash>.\n"
|
||||||
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n",
|
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n",
|
||||||
|
@ -878,9 +882,11 @@ static UniValue getblock(const JSONRPCRequest& request)
|
||||||
HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
|
HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
|
||||||
+ HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
|
+ HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
|
||||||
},
|
},
|
||||||
}.ToString());
|
};
|
||||||
|
|
||||||
LOCK(cs_main);
|
if (request.fHelp || !help.IsValidNumArgs(request.params.size())) {
|
||||||
|
throw std::runtime_error(help.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
uint256 hash(ParseHashV(request.params[0], "blockhash"));
|
uint256 hash(ParseHashV(request.params[0], "blockhash"));
|
||||||
|
|
||||||
|
@ -892,12 +898,20 @@ static UniValue getblock(const JSONRPCRequest& request)
|
||||||
verbosity = request.params[1].get_bool() ? 1 : 0;
|
verbosity = request.params[1].get_bool() ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CBlockIndex* pblockindex = LookupBlockIndex(hash);
|
CBlock block;
|
||||||
|
const CBlockIndex* pblockindex;
|
||||||
|
const CBlockIndex* tip;
|
||||||
|
{
|
||||||
|
LOCK(cs_main);
|
||||||
|
pblockindex = LookupBlockIndex(hash);
|
||||||
|
tip = chainActive.Tip();
|
||||||
|
|
||||||
if (!pblockindex) {
|
if (!pblockindex) {
|
||||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const CBlock block = GetBlockChecked(pblockindex);
|
block = GetBlockChecked(pblockindex);
|
||||||
|
}
|
||||||
|
|
||||||
if (verbosity <= 0)
|
if (verbosity <= 0)
|
||||||
{
|
{
|
||||||
|
@ -907,7 +921,7 @@ static UniValue getblock(const JSONRPCRequest& request)
|
||||||
return strHex;
|
return strHex;
|
||||||
}
|
}
|
||||||
|
|
||||||
return blockToJSON(block, chainActive.Tip(), pblockindex, verbosity >= 2);
|
return blockToJSON(block, tip, pblockindex, verbosity >= 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CCoinsStats
|
struct CCoinsStats
|
||||||
|
|
|
@ -5,9 +5,13 @@
|
||||||
#ifndef BITCOIN_RPC_BLOCKCHAIN_H
|
#ifndef BITCOIN_RPC_BLOCKCHAIN_H
|
||||||
#define BITCOIN_RPC_BLOCKCHAIN_H
|
#define BITCOIN_RPC_BLOCKCHAIN_H
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <amount.h>
|
#include <amount.h>
|
||||||
|
#include <sync.h>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
extern RecursiveMutex cs_main;
|
||||||
|
|
||||||
class CBlock;
|
class CBlock;
|
||||||
class CBlockIndex;
|
class CBlockIndex;
|
||||||
|
@ -28,7 +32,7 @@ double GetDifficulty(const CBlockIndex* blockindex);
|
||||||
void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
|
void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
|
||||||
|
|
||||||
/** Block description to JSON */
|
/** 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 */
|
/** Mempool information to JSON */
|
||||||
UniValue MempoolInfoToJSON(const CTxMemPool& pool);
|
UniValue MempoolInfoToJSON(const CTxMemPool& pool);
|
||||||
|
@ -37,7 +41,7 @@ UniValue MempoolInfoToJSON(const CTxMemPool& pool);
|
||||||
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false);
|
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false);
|
||||||
|
|
||||||
/** Block header to JSON */
|
/** 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 */
|
/** Used by getblockstats to get feerates at different percentiles by weight */
|
||||||
void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector<std::pair<CAmount, int64_t>>& scores, int64_t total_weight);
|
void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector<std::pair<CAmount, int64_t>>& scores, int64_t total_weight);
|
||||||
|
|
Loading…
Add table
Reference in a new issue