Skip to content

Commit 09b1ef2

Browse files
committed
refactor: Add thread safety annotations to Mempool{Info}ToJSON()
This change avoids locking CTxMemPool::cs recursively in Mempool{Info}ToJSON() functions.
1 parent 5c4911e commit 09b1ef2

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/bench/rpc_mempool.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& poo
1818
static void RpcMempool(benchmark::Bench& bench)
1919
{
2020
CTxMemPool pool;
21+
{
2122
LOCK2(cs_main, pool.cs);
2223

2324
for (int i = 0; i < 1000; ++i) {
@@ -31,6 +32,7 @@ static void RpcMempool(benchmark::Bench& bench)
3132
const CTransactionRef tx_r{MakeTransactionRef(tx)};
3233
AddTx(tx_r, /* fee */ i, pool);
3334
}
35+
}
3436

3537
bench.run([&] {
3638
(void)MempoolToJSON(pool, /*verbose*/ true);

src/rpc/blockchain.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPool
512512

513513
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
514514
{
515+
AssertLockNotHeld(pool.cs);
516+
515517
if (verbose) {
516518
if (include_mempool_sequence) {
517519
throw JSONRPCError(RPC_INVALID_PARAMETER, "Verbose results cannot contain mempool sequence values.");
@@ -1487,6 +1489,8 @@ static RPCHelpMan getchaintips()
14871489

14881490
UniValue MempoolInfoToJSON(const CTxMemPool& pool)
14891491
{
1492+
AssertLockNotHeld(pool.cs);
1493+
14901494
// Make sure this call is atomic in the pool.
14911495
LOCK(pool.cs);
14921496
UniValue ret(UniValue::VOBJ);

src/rpc/blockchain.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <amount.h>
99
#include <sync.h>
10+
#include <txmempool.h>
1011

1112
#include <stdint.h>
1213
#include <vector>
@@ -16,7 +17,6 @@ extern RecursiveMutex cs_main;
1617
class CBlock;
1718
class CBlockIndex;
1819
class CBlockPolicyEstimator;
19-
class CTxMemPool;
2020
class ChainstateManager;
2121
class UniValue;
2222
struct NodeContext;
@@ -41,10 +41,10 @@ void RPCNotifyBlockChange(const CBlockIndex*);
4141
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false) LOCKS_EXCLUDED(cs_main);
4242

4343
/** Mempool information to JSON */
44-
UniValue MempoolInfoToJSON(const CTxMemPool& pool);
44+
UniValue MempoolInfoToJSON(const CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(!pool.cs);
4545

4646
/** Mempool to JSON */
47-
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false, bool include_mempool_sequence = false);
47+
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false, bool include_mempool_sequence = false) EXCLUSIVE_LOCKS_REQUIRED(!pool.cs);
4848

4949
/** Block header to JSON */
5050
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex) LOCKS_EXCLUDED(cs_main);

0 commit comments

Comments
 (0)