Skip to content

Commit facbaf0

Browse files
author
MarcoFalke
committed
rpc: Use mempool from node context instead of global
Currently they are identical, but in the future we might want to turn the mempool into a unique_ptr. Replacing the global with the mempool pointer from the node context simplifies this step.
1 parent b983e7e commit facbaf0

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/rpc/blockchain.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ static UniValue getrawmempool(const JSONRPCRequest& request)
528528
if (!request.params[0].isNull())
529529
fVerbose = request.params[0].get_bool();
530530

531-
return MempoolToJSON(::mempool, fVerbose);
531+
return MempoolToJSON(EnsureMemPool(), fVerbose);
532532
}
533533

534534
static UniValue getmempoolancestors(const JSONRPCRequest& request)
@@ -566,6 +566,7 @@ static UniValue getmempoolancestors(const JSONRPCRequest& request)
566566

567567
uint256 hash = ParseHashV(request.params[0], "parameter 1");
568568

569+
const CTxMemPool& mempool = EnsureMemPool();
569570
LOCK(mempool.cs);
570571

571572
CTxMemPool::txiter it = mempool.mapTx.find(hash);
@@ -591,7 +592,7 @@ static UniValue getmempoolancestors(const JSONRPCRequest& request)
591592
const CTxMemPoolEntry &e = *ancestorIt;
592593
const uint256& _hash = e.GetTx().GetHash();
593594
UniValue info(UniValue::VOBJ);
594-
entryToJSON(::mempool, info, e);
595+
entryToJSON(mempool, info, e);
595596
o.pushKV(_hash.ToString(), info);
596597
}
597598
return o;
@@ -633,6 +634,7 @@ static UniValue getmempooldescendants(const JSONRPCRequest& request)
633634

634635
uint256 hash = ParseHashV(request.params[0], "parameter 1");
635636

637+
const CTxMemPool& mempool = EnsureMemPool();
636638
LOCK(mempool.cs);
637639

638640
CTxMemPool::txiter it = mempool.mapTx.find(hash);
@@ -658,7 +660,7 @@ static UniValue getmempooldescendants(const JSONRPCRequest& request)
658660
const CTxMemPoolEntry &e = *descendantIt;
659661
const uint256& _hash = e.GetTx().GetHash();
660662
UniValue info(UniValue::VOBJ);
661-
entryToJSON(::mempool, info, e);
663+
entryToJSON(mempool, info, e);
662664
o.pushKV(_hash.ToString(), info);
663665
}
664666
return o;
@@ -685,6 +687,7 @@ static UniValue getmempoolentry(const JSONRPCRequest& request)
685687

686688
uint256 hash = ParseHashV(request.params[0], "parameter 1");
687689

690+
const CTxMemPool& mempool = EnsureMemPool();
688691
LOCK(mempool.cs);
689692

690693
CTxMemPool::txiter it = mempool.mapTx.find(hash);
@@ -694,7 +697,7 @@ static UniValue getmempoolentry(const JSONRPCRequest& request)
694697

695698
const CTxMemPoolEntry &e = *it;
696699
UniValue info(UniValue::VOBJ);
697-
entryToJSON(::mempool, info, e);
700+
entryToJSON(mempool, info, e);
698701
return info;
699702
}
700703

@@ -1070,6 +1073,7 @@ UniValue gettxout(const JSONRPCRequest& request)
10701073
CCoinsViewCache* coins_view = &::ChainstateActive().CoinsTip();
10711074

10721075
if (fMempool) {
1076+
const CTxMemPool& mempool = EnsureMemPool();
10731077
LOCK(mempool.cs);
10741078
CCoinsViewMemPool view(coins_view, mempool);
10751079
if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
@@ -1448,7 +1452,7 @@ static UniValue getmempoolinfo(const JSONRPCRequest& request)
14481452
},
14491453
}.Check(request);
14501454

1451-
return MempoolInfoToJSON(::mempool);
1455+
return MempoolInfoToJSON(EnsureMemPool());
14521456
}
14531457

14541458
static UniValue preciousblock(const JSONRPCRequest& request)
@@ -1964,11 +1968,13 @@ static UniValue savemempool(const JSONRPCRequest& request)
19641968
},
19651969
}.Check(request);
19661970

1967-
if (!::mempool.IsLoaded()) {
1971+
const CTxMemPool& mempool = EnsureMemPool();
1972+
1973+
if (!mempool.IsLoaded()) {
19681974
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
19691975
}
19701976

1971-
if (!DumpMempool(::mempool)) {
1977+
if (!DumpMempool(mempool)) {
19721978
throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
19731979
}
19741980

src/rpc/mining.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
244244
}.Check(request);
245245

246246
LOCK(cs_main);
247+
const CTxMemPool& mempool = EnsureMemPool();
247248

248249
UniValue obj(UniValue::VOBJ);
249250
obj.pushKV("blocks", (int)::ChainActive().Height());
@@ -290,7 +291,7 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)
290291
throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.");
291292
}
292293

293-
mempool.PrioritiseTransaction(hash, nAmount);
294+
EnsureMemPool().PrioritiseTransaction(hash, nAmount);
294295
return true;
295296
}
296297

@@ -476,6 +477,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
476477
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is in initial sync and waiting for blocks...");
477478

478479
static unsigned int nTransactionsUpdatedLast;
480+
const CTxMemPool& mempool = EnsureMemPool();
479481

480482
if (!lpval.isNull())
481483
{
@@ -510,7 +512,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
510512
if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout)
511513
{
512514
// Timeout: Check transactions for update
513-
// without holding ::mempool.cs to avoid deadlocks
515+
// without holding the mempool lock to avoid deadlocks
514516
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
515517
break;
516518
checktxtime += std::chrono::seconds(10);

src/rpc/rawtransaction.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ static UniValue combinerawtransaction(const JSONRPCRequest& request)
636636
CCoinsView viewDummy;
637637
CCoinsViewCache view(&viewDummy);
638638
{
639+
const CTxMemPool& mempool = EnsureMemPool();
639640
LOCK(cs_main);
640641
LOCK(mempool.cs);
641642
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
@@ -888,6 +889,7 @@ static UniValue testmempoolaccept(const JSONRPCRequest& request)
888889
max_raw_tx_fee_rate = CFeeRate(AmountFromValue(request.params[1]));
889890
}
890891

892+
CTxMemPool& mempool = EnsureMemPool();
891893
int64_t virtual_size = GetVirtualTransactionSize(*tx);
892894
CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
893895

@@ -1506,6 +1508,7 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
15061508
CCoinsView viewDummy;
15071509
CCoinsViewCache view(&viewDummy);
15081510
{
1511+
const CTxMemPool& mempool = EnsureMemPool();
15091512
LOCK2(cs_main, mempool.cs);
15101513
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
15111514
CCoinsViewMemPool viewMempool(&viewChain, mempool);

0 commit comments

Comments
 (0)