Skip to content

Commit 6a3d192

Browse files
committed
rpc: Tidy up local references (see commit message)
Organize local variables/references such that: 1. There is always a `ChainstateManager` reference before any `LOCK(cs_main)`. 2. NodeContext references are used with Ensure*() functions introduced in previous commit where appropriate to avoid duplicate assertions.
1 parent 038854f commit 6a3d192

File tree

4 files changed

+64
-43
lines changed

4 files changed

+64
-43
lines changed

src/rest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ static bool rest_headers(const std::any& context,
180180
std::vector<const CBlockIndex *> headers;
181181
headers.reserve(count);
182182
{
183-
LOCK(cs_main);
184183
ChainstateManager& chainman = EnsureAnyChainman(context);
184+
LOCK(cs_main);
185185
tip = chainman.ActiveChain().Tip();
186186
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
187187
while (pindex != nullptr && chainman.ActiveChain().Contains(pindex)) {
@@ -250,8 +250,8 @@ static bool rest_block(const std::any& context,
250250
CBlockIndex* pblockindex = nullptr;
251251
CBlockIndex* tip = nullptr;
252252
{
253-
LOCK(cs_main);
254253
ChainstateManager& chainman = EnsureAnyChainman(context);
254+
LOCK(cs_main);
255255
tip = chainman.ActiveChain().Tip();
256256
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
257257
if (!pblockindex) {
@@ -641,8 +641,9 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
641641

642642
CBlockIndex* pblockindex = nullptr;
643643
{
644+
ChainstateManager& chainman = EnsureAnyChainman(context);
644645
LOCK(cs_main);
645-
const CChain& active_chain = EnsureAnyChainman(context).ActiveChain();
646+
const CChain& active_chain = chainman.ActiveChain();
646647
if (blockheight > active_chain.Height()) {
647648
return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
648649
}

src/rpc/blockchain.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ static RPCHelpMan getblockcount()
210210
},
211211
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
212212
{
213+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
213214
LOCK(cs_main);
214-
return EnsureAnyChainman(request.context).ActiveChain().Height();
215+
return chainman.ActiveChain().Height();
215216
},
216217
};
217218
}
@@ -229,8 +230,9 @@ static RPCHelpMan getbestblockhash()
229230
},
230231
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
231232
{
233+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
232234
LOCK(cs_main);
233-
return EnsureAnyChainman(request.context).ActiveChain().Tip()->GetBlockHash().GetHex();
235+
return chainman.ActiveChain().Tip()->GetBlockHash().GetHex();
234236
},
235237
};
236238
}
@@ -410,8 +412,9 @@ static RPCHelpMan getdifficulty()
410412
},
411413
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
412414
{
415+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
413416
LOCK(cs_main);
414-
return GetDifficulty(EnsureAnyChainman(request.context).ActiveChain().Tip());
417+
return GetDifficulty(chainman.ActiveChain().Tip());
415418
},
416419
};
417420
}
@@ -775,8 +778,9 @@ static RPCHelpMan getblockhash()
775778
},
776779
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
777780
{
781+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
778782
LOCK(cs_main);
779-
const CChain& active_chain = EnsureAnyChainman(request.context).ActiveChain();
783+
const CChain& active_chain = chainman.ActiveChain();
780784

781785
int nHeight = request.params[0].get_int();
782786
if (nHeight < 0 || nHeight > active_chain.Height())
@@ -835,8 +839,8 @@ static RPCHelpMan getblockheader()
835839
const CBlockIndex* pblockindex;
836840
const CBlockIndex* tip;
837841
{
838-
LOCK(cs_main);
839842
ChainstateManager& chainman = EnsureAnyChainman(request.context);
843+
LOCK(cs_main);
840844
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
841845
tip = chainman.ActiveChain().Tip();
842846
}
@@ -960,8 +964,8 @@ static RPCHelpMan getblock()
960964
const CBlockIndex* pblockindex;
961965
const CBlockIndex* tip;
962966
{
963-
LOCK(cs_main);
964967
ChainstateManager& chainman = EnsureAnyChainman(request.context);
968+
LOCK(cs_main);
965969
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
966970
tip = chainman.ActiveChain().Tip();
967971

@@ -1003,8 +1007,8 @@ static RPCHelpMan pruneblockchain()
10031007
if (!fPruneMode)
10041008
throw JSONRPCError(RPC_MISC_ERROR, "Cannot prune blocks because node is not in prune mode.");
10051009

1006-
LOCK(cs_main);
10071010
ChainstateManager& chainman = EnsureAnyChainman(request.context);
1011+
LOCK(cs_main);
10081012

10091013
int heightParam = request.params[0].get_int();
10101014
if (heightParam < 0)
@@ -1086,7 +1090,9 @@ static RPCHelpMan gettxoutsetinfo()
10861090
UniValue ret(UniValue::VOBJ);
10871091

10881092
CCoinsStats stats;
1089-
CChainState& active_chainstate = EnsureAnyChainman(request.context).ActiveChainstate();
1093+
NodeContext& node = EnsureAnyNodeContext(request.context);
1094+
ChainstateManager& chainman = EnsureChainman(node);
1095+
CChainState& active_chainstate = chainman.ActiveChainstate();
10901096
active_chainstate.ForceFlushStateToDisk();
10911097

10921098
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
@@ -1098,7 +1104,6 @@ static RPCHelpMan gettxoutsetinfo()
10981104
coins_view = &active_chainstate.CoinsDB();
10991105
blockman = &active_chainstate.m_blockman;
11001106
}
1101-
NodeContext& node = EnsureAnyNodeContext(request.context);
11021107
if (GetUTXOStats(coins_view, *blockman, stats, hash_type, node.rpc_interruption_point)) {
11031108
ret.pushKV("height", (int64_t)stats.nHeight);
11041109
ret.pushKV("bestblock", stats.hashBlock.GetHex());
@@ -1158,6 +1163,8 @@ static RPCHelpMan gettxout()
11581163
},
11591164
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
11601165
{
1166+
NodeContext& node = EnsureAnyNodeContext(request.context);
1167+
ChainstateManager& chainman = EnsureChainman(node);
11611168
LOCK(cs_main);
11621169

11631170
UniValue ret(UniValue::VOBJ);
@@ -1170,11 +1177,11 @@ static RPCHelpMan gettxout()
11701177
fMempool = request.params[2].get_bool();
11711178

11721179
Coin coin;
1173-
CChainState& active_chainstate = EnsureAnyChainman(request.context).ActiveChainstate();
1180+
CChainState& active_chainstate = chainman.ActiveChainstate();
11741181
CCoinsViewCache* coins_view = &active_chainstate.CoinsTip();
11751182

11761183
if (fMempool) {
1177-
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
1184+
const CTxMemPool& mempool = EnsureMemPool(node);
11781185
LOCK(mempool.cs);
11791186
CCoinsViewMemPool view(coins_view, mempool);
11801187
if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
@@ -1224,9 +1231,10 @@ static RPCHelpMan verifychain()
12241231
const int check_level(request.params[0].isNull() ? DEFAULT_CHECKLEVEL : request.params[0].get_int());
12251232
const int check_depth{request.params[1].isNull() ? DEFAULT_CHECKBLOCKS : request.params[1].get_int()};
12261233

1234+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
12271235
LOCK(cs_main);
12281236

1229-
CChainState& active_chainstate = EnsureAnyChainman(request.context).ActiveChainstate();
1237+
CChainState& active_chainstate = chainman.ActiveChainstate();
12301238
return CVerifyDB().VerifyDB(Params(), active_chainstate, &active_chainstate.CoinsTip(), check_level, check_depth);
12311239
},
12321240
};
@@ -1353,8 +1361,8 @@ RPCHelpMan getblockchaininfo()
13531361
},
13541362
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
13551363
{
1356-
LOCK(cs_main);
13571364
ChainstateManager& chainman = EnsureAnyChainman(request.context);
1365+
LOCK(cs_main);
13581366

13591367
const CBlockIndex* tip = chainman.ActiveChain().Tip();
13601368
CHECK_NONFATAL(tip);
@@ -1893,8 +1901,8 @@ static RPCHelpMan getblockstats()
18931901
},
18941902
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
18951903
{
1896-
LOCK(cs_main);
18971904
ChainstateManager& chainman = EnsureAnyChainman(request.context);
1905+
LOCK(cs_main);
18981906

18991907
CBlockIndex* pindex;
19001908
if (request.params[0].isNum()) {
@@ -2295,16 +2303,16 @@ static RPCHelpMan scantxoutset()
22952303
int64_t count = 0;
22962304
std::unique_ptr<CCoinsViewCursor> pcursor;
22972305
CBlockIndex* tip;
2306+
NodeContext& node = EnsureAnyNodeContext(request.context);
22982307
{
2308+
ChainstateManager& chainman = EnsureChainman(node);
22992309
LOCK(cs_main);
2300-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
23012310
chainman.ActiveChainstate().ForceFlushStateToDisk();
23022311
pcursor = std::unique_ptr<CCoinsViewCursor>(chainman.ActiveChainstate().CoinsDB().Cursor());
23032312
CHECK_NONFATAL(pcursor);
23042313
tip = chainman.ActiveChain().Tip();
23052314
CHECK_NONFATAL(tip);
23062315
}
2307-
NodeContext& node = EnsureAnyNodeContext(request.context);
23082316
bool res = FindScriptPubKey(g_scan_progress, g_should_abort_scan, count, pcursor.get(), needles, coins, node.rpc_interruption_point);
23092317
result.pushKV("success", res);
23102318
result.pushKV("txouts", count);
@@ -2377,8 +2385,9 @@ static RPCHelpMan getblockfilter()
23772385
const CBlockIndex* block_index;
23782386
bool block_was_connected;
23792387
{
2388+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
23802389
LOCK(cs_main);
2381-
block_index = EnsureAnyChainman(request.context).m_blockman.LookupBlockIndex(block_hash);
2390+
block_index = chainman.m_blockman.LookupBlockIndex(block_hash);
23822391
if (!block_index) {
23832392
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
23842393
}

src/rpc/mining.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ static RPCHelpMan getnetworkhashps()
100100
},
101101
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
102102
{
103+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
103104
LOCK(cs_main);
104-
const CChain& active_chain = EnsureAnyChainman(request.context).ActiveChain();
105-
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1, active_chain);
105+
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1, chainman.ActiveChain());
106106
},
107107
};
108108
}
@@ -235,8 +235,9 @@ static RPCHelpMan generatetodescriptor()
235235
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
236236
}
237237

238-
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
239-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
238+
NodeContext& node = EnsureAnyNodeContext(request.context);
239+
const CTxMemPool& mempool = EnsureMemPool(node);
240+
ChainstateManager& chainman = EnsureChainman(node);
240241

241242
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
242243
},
@@ -280,8 +281,9 @@ static RPCHelpMan generatetoaddress()
280281
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: Invalid address");
281282
}
282283

283-
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
284-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
284+
NodeContext& node = EnsureAnyNodeContext(request.context);
285+
const CTxMemPool& mempool = EnsureMemPool(node);
286+
ChainstateManager& chainman = EnsureChainman(node);
285287

286288
CScript coinbase_script = GetScriptForDestination(destination);
287289

@@ -329,7 +331,8 @@ static RPCHelpMan generateblock()
329331
coinbase_script = GetScriptForDestination(destination);
330332
}
331333

332-
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
334+
NodeContext& node = EnsureAnyNodeContext(request.context);
335+
const CTxMemPool& mempool = EnsureMemPool(node);
333336

334337
std::vector<CTransactionRef> txs;
335338
const auto raw_txs_or_txids = request.params[1].get_array();
@@ -358,7 +361,7 @@ static RPCHelpMan generateblock()
358361
CChainParams chainparams(Params());
359362
CBlock block;
360363

361-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
364+
ChainstateManager& chainman = EnsureChainman(node);
362365
{
363366
LOCK(cs_main);
364367

@@ -424,9 +427,11 @@ static RPCHelpMan getmininginfo()
424427
},
425428
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
426429
{
430+
NodeContext& node = EnsureAnyNodeContext(request.context);
431+
const CTxMemPool& mempool = EnsureMemPool(node);
432+
ChainstateManager& chainman = EnsureChainman(node);
427433
LOCK(cs_main);
428-
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
429-
const CChain& active_chain = EnsureAnyChainman(request.context).ActiveChain();
434+
const CChain& active_chain = chainman.ActiveChain();
430435

431436
UniValue obj(UniValue::VOBJ);
432437
obj.pushKV("blocks", (int)active_chain.Height());
@@ -595,8 +600,9 @@ static RPCHelpMan getblocktemplate()
595600
},
596601
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
597602
{
603+
NodeContext& node = EnsureAnyNodeContext(request.context);
604+
ChainstateManager& chainman = EnsureChainman(node);
598605
LOCK(cs_main);
599-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
600606

601607
std::string strMode = "template";
602608
UniValue lpval = NullUniValue;
@@ -663,7 +669,6 @@ static RPCHelpMan getblocktemplate()
663669
if (strMode != "template")
664670
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
665671

666-
NodeContext& node = EnsureAnyNodeContext(request.context);
667672
if(!node.connman)
668673
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
669674

@@ -678,7 +683,7 @@ static RPCHelpMan getblocktemplate()
678683
}
679684

680685
static unsigned int nTransactionsUpdatedLast;
681-
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
686+
const CTxMemPool& mempool = EnsureMemPool(node);
682687

683688
if (!lpval.isNull())
684689
{

src/rpc/rawtransaction.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static RPCHelpMan getrawtransaction()
158158
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
159159
{
160160
const NodeContext& node = EnsureAnyNodeContext(request.context);
161-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
161+
ChainstateManager& chainman = EnsureChainman(node);
162162

163163
bool in_active_chain = true;
164164
uint256 hash = ParseHashV(request.params[0], "parameter 1");
@@ -350,9 +350,9 @@ static RPCHelpMan verifytxoutproof()
350350
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot)
351351
return res;
352352

353+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
353354
LOCK(cs_main);
354355

355-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
356356
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(merkleBlock.header.GetHash());
357357
if (!pindex || !chainman.ActiveChain().Contains(pindex) || pindex->nTx == 0) {
358358
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
@@ -678,10 +678,11 @@ static RPCHelpMan combinerawtransaction()
678678
CCoinsView viewDummy;
679679
CCoinsViewCache view(&viewDummy);
680680
{
681-
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
682-
LOCK(cs_main);
683-
LOCK(mempool.cs);
684-
CCoinsViewCache &viewChain = EnsureAnyChainman(request.context).ActiveChainstate().CoinsTip();
681+
NodeContext& node = EnsureAnyNodeContext(request.context);
682+
const CTxMemPool& mempool = EnsureMemPool(node);
683+
ChainstateManager& chainman = EnsureChainman(node);
684+
LOCK2(cs_main, mempool.cs);
685+
CCoinsViewCache &viewChain = chainman.ActiveChainstate().CoinsTip();
685686
CCoinsViewMemPool viewMempool(&viewChain, mempool);
686687
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
687688

@@ -943,7 +944,9 @@ static RPCHelpMan testmempoolaccept()
943944
DEFAULT_MAX_RAW_TX_FEE_RATE :
944945
CFeeRate(AmountFromValue(request.params[1]));
945946

946-
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
947+
NodeContext& node = EnsureAnyNodeContext(request.context);
948+
949+
CTxMemPool& mempool = EnsureMemPool(node);
947950
int64_t virtual_size = GetVirtualTransactionSize(*tx);
948951
CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
949952

@@ -952,7 +955,8 @@ static RPCHelpMan testmempoolaccept()
952955
result_0.pushKV("txid", tx->GetHash().GetHex());
953956
result_0.pushKV("wtxid", tx->GetWitnessHash().GetHex());
954957

955-
const MempoolAcceptResult accept_result = WITH_LOCK(cs_main, return AcceptToMemoryPool(EnsureAnyChainman(request.context).ActiveChainstate(), mempool, std::move(tx),
958+
ChainstateManager& chainman = EnsureChainman(node);
959+
const MempoolAcceptResult accept_result = WITH_LOCK(cs_main, return AcceptToMemoryPool(chainman.ActiveChainstate(), mempool, std::move(tx),
956960
false /* bypass_limits */, /* test_accept */ true));
957961

958962
// Only return the fee and vsize if the transaction would pass ATMP.
@@ -1601,9 +1605,11 @@ static RPCHelpMan utxoupdatepsbt()
16011605
CCoinsView viewDummy;
16021606
CCoinsViewCache view(&viewDummy);
16031607
{
1604-
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
1608+
NodeContext& node = EnsureAnyNodeContext(request.context);
1609+
const CTxMemPool& mempool = EnsureMemPool(node);
1610+
ChainstateManager& chainman = EnsureChainman(node);
16051611
LOCK2(cs_main, mempool.cs);
1606-
CCoinsViewCache &viewChain = EnsureAnyChainman(request.context).ActiveChainstate().CoinsTip();
1612+
CCoinsViewCache &viewChain = chainman.ActiveChainstate().CoinsTip();
16071613
CCoinsViewMemPool viewMempool(&viewChain, mempool);
16081614
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
16091615

0 commit comments

Comments
 (0)