Skip to content

Commit dd427dd

Browse files
committed
refactor: Add thread safety annotations to Mempool{Info}ToJSON()
1 parent 7e37329 commit dd427dd

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
@@ -502,6 +502,8 @@ static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPool
502502

503503
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
504504
{
505+
AssertLockNotHeld(pool.cs);
506+
505507
if (verbose) {
506508
if (include_mempool_sequence) {
507509
throw JSONRPCError(RPC_INVALID_PARAMETER, "Verbose results cannot contain mempool sequence values.");
@@ -1477,6 +1479,8 @@ static RPCHelpMan getchaintips()
14771479

14781480
UniValue MempoolInfoToJSON(const CTxMemPool& pool)
14791481
{
1482+
AssertLockNotHeld(pool.cs);
1483+
14801484
// Make sure this call is atomic in the pool.
14811485
LOCK(pool.cs);
14821486
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>
@@ -15,7 +16,6 @@ extern RecursiveMutex cs_main;
1516

1617
class CBlock;
1718
class CBlockIndex;
18-
class CTxMemPool;
1919
class ChainstateManager;
2020
class UniValue;
2121
struct NodeContext;
@@ -40,10 +40,10 @@ void RPCNotifyBlockChange(const CBlockIndex*);
4040
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false) LOCKS_EXCLUDED(cs_main);
4141

4242
/** Mempool information to JSON */
43-
UniValue MempoolInfoToJSON(const CTxMemPool& pool);
43+
UniValue MempoolInfoToJSON(const CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(!pool.cs);
4444

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

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

0 commit comments

Comments
 (0)