Skip to content

Commit faea564

Browse files
author
MarcoFalke
committed
rpc: Add lock annotations to block{,header}ToJSON
1 parent fab00a5 commit faea564

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/rpc/blockchain.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* b
9494
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex)
9595
{
9696
// Serialize passed information without accessing chain state of the active chain!
97+
AssertLockNotHeld(cs_main); // For performance reasons
98+
9799
UniValue result(UniValue::VOBJ);
98100
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
99101
const CBlockIndex* pnext;
@@ -121,6 +123,8 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
121123
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
122124
{
123125
// Serialize passed information without accessing chain state of the active chain!
126+
AssertLockNotHeld(cs_main); // For performance reasons
127+
124128
UniValue result(UniValue::VOBJ);
125129
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
126130
const CBlockIndex* pnext;

src/rpc/blockchain.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
#ifndef BITCOIN_RPC_BLOCKCHAIN_H
66
#define BITCOIN_RPC_BLOCKCHAIN_H
77

8-
#include <vector>
9-
#include <stdint.h>
108
#include <amount.h>
9+
#include <sync.h>
10+
11+
#include <stdint.h>
12+
#include <vector>
13+
14+
extern RecursiveMutex cs_main;
1115

1216
class CBlock;
1317
class CBlockIndex;
@@ -28,7 +32,7 @@ double GetDifficulty(const CBlockIndex* blockindex);
2832
void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
2933

3034
/** Block description to JSON */
31-
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false);
35+
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false) LOCKS_EXCLUDED(cs_main);
3236

3337
/** Mempool information to JSON */
3438
UniValue MempoolInfoToJSON(const CTxMemPool& pool);
@@ -37,7 +41,7 @@ UniValue MempoolInfoToJSON(const CTxMemPool& pool);
3741
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false);
3842

3943
/** Block header to JSON */
40-
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex);
44+
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex) LOCKS_EXCLUDED(cs_main);
4145

4246
/** Used by getblockstats to get feerates at different percentiles by weight */
4347
void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector<std::pair<CAmount, int64_t>>& scores, int64_t total_weight);

0 commit comments

Comments
 (0)