-
Notifications
You must be signed in to change notification settings - Fork 37.8k
rpc: Remove cs_main lock from blockToJSON and blockheaderToJSON #12151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,10 +58,7 @@ static CUpdatedBlock latestblock; | |
*/ | ||
double GetDifficulty(const CBlockIndex* blockindex) | ||
{ | ||
if (blockindex == nullptr) | ||
{ | ||
return 1.0; | ||
} | ||
assert(blockindex); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In rpc code, assert should be replaced with throw JSONRPCError? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is a usage error, should be a programatic error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you could avoid the assert by passing in a const reference? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure but out of scope here and I'm happy to submit that refactor in a separate PR. |
||
|
||
int nShift = (blockindex->nBits >> 24) & 0xff; | ||
double dDiff = | ||
|
@@ -81,15 +78,22 @@ double GetDifficulty(const CBlockIndex* blockindex) | |
return dDiff; | ||
} | ||
|
||
UniValue blockheaderToJSON(const CBlockIndex* blockindex) | ||
static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next) | ||
{ | ||
next = tip->GetAncestor(blockindex->nHeight + 1); | ||
if (next && next->pprev == blockindex) { | ||
return tip->nHeight - blockindex->nHeight + 1; | ||
} | ||
next = nullptr; | ||
return blockindex == tip ? 1 : -1; | ||
} | ||
|
||
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex) | ||
{ | ||
AssertLockHeld(cs_main); | ||
UniValue result(UniValue::VOBJ); | ||
result.pushKV("hash", blockindex->GetBlockHash().GetHex()); | ||
int confirmations = -1; | ||
// Only report confirmations if the block is on the main chain | ||
if (chainActive.Contains(blockindex)) | ||
confirmations = chainActive.Height() - blockindex->nHeight + 1; | ||
const CBlockIndex* pnext; | ||
int confirmations = ComputeNextBlockAndDepth(tip, blockindex, pnext); | ||
result.pushKV("confirmations", confirmations); | ||
result.pushKV("height", blockindex->nHeight); | ||
result.pushKV("version", blockindex->nVersion); | ||
|
@@ -105,21 +109,17 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex) | |
|
||
if (blockindex->pprev) | ||
result.pushKV("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()); | ||
CBlockIndex *pnext = chainActive.Next(blockindex); | ||
if (pnext) | ||
result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex()); | ||
return result; | ||
} | ||
|
||
UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails) | ||
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails) | ||
{ | ||
AssertLockHeld(cs_main); | ||
UniValue result(UniValue::VOBJ); | ||
result.pushKV("hash", blockindex->GetBlockHash().GetHex()); | ||
int confirmations = -1; | ||
// Only report confirmations if the block is on the main chain | ||
if (chainActive.Contains(blockindex)) | ||
confirmations = chainActive.Height() - blockindex->nHeight + 1; | ||
const CBlockIndex* pnext; | ||
int confirmations = ComputeNextBlockAndDepth(tip, blockindex, pnext); | ||
result.pushKV("confirmations", confirmations); | ||
result.pushKV("strippedsize", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS)); | ||
result.pushKV("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION)); | ||
|
@@ -151,7 +151,6 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx | |
|
||
if (blockindex->pprev) | ||
result.pushKV("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()); | ||
CBlockIndex *pnext = chainActive.Next(blockindex); | ||
if (pnext) | ||
result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex()); | ||
return result; | ||
|
@@ -747,7 +746,7 @@ static UniValue getblockheader(const JSONRPCRequest& request) | |
return strHex; | ||
} | ||
|
||
return blockheaderToJSON(pblockindex); | ||
return blockheaderToJSON(chainActive.Tip(), pblockindex); | ||
} | ||
|
||
static CBlock GetBlockChecked(const CBlockIndex* pblockindex) | ||
|
@@ -848,7 +847,7 @@ static UniValue getblock(const JSONRPCRequest& request) | |
return strHex; | ||
} | ||
|
||
return blockToJSON(block, pblockindex, verbosity >= 2); | ||
return blockToJSON(block, chainActive.Tip(), pblockindex, verbosity >= 2); | ||
} | ||
|
||
struct CCoinsStats | ||
|
@@ -1121,7 +1120,7 @@ static UniValue verifychain(const JSONRPCRequest& request) | |
} | ||
|
||
/** Implementation of IsSuperMajority with better feedback */ | ||
static UniValue SoftForkMajorityDesc(int version, CBlockIndex* pindex, const Consensus::Params& consensusParams) | ||
static UniValue SoftForkMajorityDesc(int version, const CBlockIndex* pindex, const Consensus::Params& consensusParams) | ||
{ | ||
UniValue rv(UniValue::VOBJ); | ||
bool activated = false; | ||
|
@@ -1141,7 +1140,7 @@ static UniValue SoftForkMajorityDesc(int version, CBlockIndex* pindex, const Con | |
return rv; | ||
} | ||
|
||
static UniValue SoftForkDesc(const std::string &name, int version, CBlockIndex* pindex, const Consensus::Params& consensusParams) | ||
static UniValue SoftForkDesc(const std::string &name, int version, const CBlockIndex* pindex, const Consensus::Params& consensusParams) | ||
{ | ||
UniValue rv(UniValue::VOBJ); | ||
rv.pushKV("id", name); | ||
|
@@ -1247,20 +1246,21 @@ UniValue getblockchaininfo(const JSONRPCRequest& request) | |
|
||
LOCK(cs_main); | ||
|
||
const CBlockIndex* tip = chainActive.Tip(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to change these unless you're gonna actually kill the cs_main held for the whole time, I'd think, no (and little reason to, its not like its being held for an extended period)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This moves up tip variable declared below. |
||
UniValue obj(UniValue::VOBJ); | ||
obj.pushKV("chain", Params().NetworkIDString()); | ||
obj.pushKV("blocks", (int)chainActive.Height()); | ||
obj.pushKV("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1); | ||
obj.pushKV("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()); | ||
obj.pushKV("difficulty", (double)GetDifficulty(chainActive.Tip())); | ||
obj.pushKV("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast()); | ||
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip())); | ||
obj.pushKV("bestblockhash", tip->GetBlockHash().GetHex()); | ||
obj.pushKV("difficulty", (double)GetDifficulty(tip)); | ||
obj.pushKV("mediantime", (int64_t)tip->GetMedianTimePast()); | ||
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), tip)); | ||
obj.pushKV("initialblockdownload", IsInitialBlockDownload()); | ||
obj.pushKV("chainwork", chainActive.Tip()->nChainWork.GetHex()); | ||
obj.pushKV("chainwork", tip->nChainWork.GetHex()); | ||
obj.pushKV("size_on_disk", CalculateCurrentUsage()); | ||
obj.pushKV("pruned", fPruneMode); | ||
if (fPruneMode) { | ||
CBlockIndex* block = chainActive.Tip(); | ||
const CBlockIndex* block = tip; | ||
assert(block); | ||
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) { | ||
block = block->pprev; | ||
|
@@ -1277,7 +1277,6 @@ UniValue getblockchaininfo(const JSONRPCRequest& request) | |
} | ||
|
||
const Consensus::Params& consensusParams = Params().GetConsensus(); | ||
CBlockIndex* tip = chainActive.Tip(); | ||
UniValue softforks(UniValue::VARR); | ||
UniValue bip9_softforks(UniValue::VOBJ); | ||
softforks.push_back(SoftForkDesc("bip34", 2, tip, consensusParams)); | ||
|
Uh oh!
There was an error while loading. Please reload this page.