Skip to content

Commit 87d90ef

Browse files
author
MarcoFalke
committed
Merge #11618: rpc: Lock cs_main in blockToJSON/blockheaderToJSON
a9b6ba0 Add missing cs_main locks when calling blockToJSON/blockheaderToJSON (practicalswift) Pull request description: `blockToJSON(...)` and `blockheaderToJSON(...)` read the variable `chainActive` which requires holding the mutex `cs_main`. So does `GetDifficulty(...)`. Tree-SHA512: bfb94f5e3238accbf6a4daddde49d53f1891c38ae9b07e25b3098c485747159258f64bb66a50e147b32beac601de89d9d04ff717b6c4f1460d329c90a53d3333
2 parents 5aeaa9c + a9b6ba0 commit 87d90ef

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/rest.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,11 @@ static bool rest_headers(HTTPRequest* req,
178178
}
179179
case RF_JSON: {
180180
UniValue jsonHeaders(UniValue::VARR);
181-
for (const CBlockIndex *pindex : headers) {
182-
jsonHeaders.push_back(blockheaderToJSON(pindex));
181+
{
182+
LOCK(cs_main);
183+
for (const CBlockIndex *pindex : headers) {
184+
jsonHeaders.push_back(blockheaderToJSON(pindex));
185+
}
183186
}
184187
std::string strJSON = jsonHeaders.write() + "\n";
185188
req->WriteHeader("Content-Type", "application/json");
@@ -239,7 +242,11 @@ static bool rest_block(HTTPRequest* req,
239242
}
240243

241244
case RF_JSON: {
242-
UniValue objBlock = blockToJSON(block, pblockindex, showTxDetails);
245+
UniValue objBlock;
246+
{
247+
LOCK(cs_main);
248+
objBlock = blockToJSON(block, pblockindex, showTxDetails);
249+
}
243250
std::string strJSON = objBlock.write() + "\n";
244251
req->WriteHeader("Content-Type", "application/json");
245252
req->WriteReply(HTTP_OK, strJSON);

src/rpc/blockchain.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ double GetDifficulty(const CBlockIndex* blockindex)
7878

7979
UniValue blockheaderToJSON(const CBlockIndex* blockindex)
8080
{
81+
AssertLockHeld(cs_main);
8182
UniValue result(UniValue::VOBJ);
8283
result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex()));
8384
int confirmations = -1;
@@ -106,6 +107,7 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
106107

107108
UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails)
108109
{
110+
AssertLockHeld(cs_main);
109111
UniValue result(UniValue::VOBJ);
110112
result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex()));
111113
int confirmations = -1;

0 commit comments

Comments
 (0)