mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-19 05:12:03 +02:00
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.
This commit is contained in:
parent
038854f31e
commit
6a3d192020
@ -180,8 +180,8 @@ static bool rest_headers(const std::any& context,
|
||||
std::vector<const CBlockIndex *> headers;
|
||||
headers.reserve(count);
|
||||
{
|
||||
LOCK(cs_main);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(context);
|
||||
LOCK(cs_main);
|
||||
tip = chainman.ActiveChain().Tip();
|
||||
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
|
||||
while (pindex != nullptr && chainman.ActiveChain().Contains(pindex)) {
|
||||
@ -250,8 +250,8 @@ static bool rest_block(const std::any& context,
|
||||
CBlockIndex* pblockindex = nullptr;
|
||||
CBlockIndex* tip = nullptr;
|
||||
{
|
||||
LOCK(cs_main);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(context);
|
||||
LOCK(cs_main);
|
||||
tip = chainman.ActiveChain().Tip();
|
||||
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
|
||||
if (!pblockindex) {
|
||||
@ -641,8 +641,9 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
|
||||
|
||||
CBlockIndex* pblockindex = nullptr;
|
||||
{
|
||||
ChainstateManager& chainman = EnsureAnyChainman(context);
|
||||
LOCK(cs_main);
|
||||
const CChain& active_chain = EnsureAnyChainman(context).ActiveChain();
|
||||
const CChain& active_chain = chainman.ActiveChain();
|
||||
if (blockheight > active_chain.Height()) {
|
||||
return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
|
||||
}
|
||||
|
@ -210,8 +210,9 @@ static RPCHelpMan getblockcount()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
return EnsureAnyChainman(request.context).ActiveChain().Height();
|
||||
return chainman.ActiveChain().Height();
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -229,8 +230,9 @@ static RPCHelpMan getbestblockhash()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
return EnsureAnyChainman(request.context).ActiveChain().Tip()->GetBlockHash().GetHex();
|
||||
return chainman.ActiveChain().Tip()->GetBlockHash().GetHex();
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -410,8 +412,9 @@ static RPCHelpMan getdifficulty()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
return GetDifficulty(EnsureAnyChainman(request.context).ActiveChain().Tip());
|
||||
return GetDifficulty(chainman.ActiveChain().Tip());
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -775,8 +778,9 @@ static RPCHelpMan getblockhash()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
const CChain& active_chain = EnsureAnyChainman(request.context).ActiveChain();
|
||||
const CChain& active_chain = chainman.ActiveChain();
|
||||
|
||||
int nHeight = request.params[0].get_int();
|
||||
if (nHeight < 0 || nHeight > active_chain.Height())
|
||||
@ -835,8 +839,8 @@ static RPCHelpMan getblockheader()
|
||||
const CBlockIndex* pblockindex;
|
||||
const CBlockIndex* tip;
|
||||
{
|
||||
LOCK(cs_main);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
|
||||
tip = chainman.ActiveChain().Tip();
|
||||
}
|
||||
@ -960,8 +964,8 @@ static RPCHelpMan getblock()
|
||||
const CBlockIndex* pblockindex;
|
||||
const CBlockIndex* tip;
|
||||
{
|
||||
LOCK(cs_main);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
|
||||
tip = chainman.ActiveChain().Tip();
|
||||
|
||||
@ -1003,8 +1007,8 @@ static RPCHelpMan pruneblockchain()
|
||||
if (!fPruneMode)
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Cannot prune blocks because node is not in prune mode.");
|
||||
|
||||
LOCK(cs_main);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
|
||||
int heightParam = request.params[0].get_int();
|
||||
if (heightParam < 0)
|
||||
@ -1086,7 +1090,9 @@ static RPCHelpMan gettxoutsetinfo()
|
||||
UniValue ret(UniValue::VOBJ);
|
||||
|
||||
CCoinsStats stats;
|
||||
CChainState& active_chainstate = EnsureAnyChainman(request.context).ActiveChainstate();
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
CChainState& active_chainstate = chainman.ActiveChainstate();
|
||||
active_chainstate.ForceFlushStateToDisk();
|
||||
|
||||
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
|
||||
@ -1098,7 +1104,6 @@ static RPCHelpMan gettxoutsetinfo()
|
||||
coins_view = &active_chainstate.CoinsDB();
|
||||
blockman = &active_chainstate.m_blockman;
|
||||
}
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
if (GetUTXOStats(coins_view, *blockman, stats, hash_type, node.rpc_interruption_point)) {
|
||||
ret.pushKV("height", (int64_t)stats.nHeight);
|
||||
ret.pushKV("bestblock", stats.hashBlock.GetHex());
|
||||
@ -1158,6 +1163,8 @@ static RPCHelpMan gettxout()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
LOCK(cs_main);
|
||||
|
||||
UniValue ret(UniValue::VOBJ);
|
||||
@ -1170,11 +1177,11 @@ static RPCHelpMan gettxout()
|
||||
fMempool = request.params[2].get_bool();
|
||||
|
||||
Coin coin;
|
||||
CChainState& active_chainstate = EnsureAnyChainman(request.context).ActiveChainstate();
|
||||
CChainState& active_chainstate = chainman.ActiveChainstate();
|
||||
CCoinsViewCache* coins_view = &active_chainstate.CoinsTip();
|
||||
|
||||
if (fMempool) {
|
||||
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||
LOCK(mempool.cs);
|
||||
CCoinsViewMemPool view(coins_view, mempool);
|
||||
if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
|
||||
@ -1224,9 +1231,10 @@ static RPCHelpMan verifychain()
|
||||
const int check_level(request.params[0].isNull() ? DEFAULT_CHECKLEVEL : request.params[0].get_int());
|
||||
const int check_depth{request.params[1].isNull() ? DEFAULT_CHECKBLOCKS : request.params[1].get_int()};
|
||||
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
|
||||
CChainState& active_chainstate = EnsureAnyChainman(request.context).ActiveChainstate();
|
||||
CChainState& active_chainstate = chainman.ActiveChainstate();
|
||||
return CVerifyDB().VerifyDB(Params(), active_chainstate, &active_chainstate.CoinsTip(), check_level, check_depth);
|
||||
},
|
||||
};
|
||||
@ -1353,8 +1361,8 @@ RPCHelpMan getblockchaininfo()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
LOCK(cs_main);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
|
||||
const CBlockIndex* tip = chainman.ActiveChain().Tip();
|
||||
CHECK_NONFATAL(tip);
|
||||
@ -1893,8 +1901,8 @@ static RPCHelpMan getblockstats()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
LOCK(cs_main);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
|
||||
CBlockIndex* pindex;
|
||||
if (request.params[0].isNum()) {
|
||||
@ -2295,16 +2303,16 @@ static RPCHelpMan scantxoutset()
|
||||
int64_t count = 0;
|
||||
std::unique_ptr<CCoinsViewCursor> pcursor;
|
||||
CBlockIndex* tip;
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
{
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
LOCK(cs_main);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
chainman.ActiveChainstate().ForceFlushStateToDisk();
|
||||
pcursor = std::unique_ptr<CCoinsViewCursor>(chainman.ActiveChainstate().CoinsDB().Cursor());
|
||||
CHECK_NONFATAL(pcursor);
|
||||
tip = chainman.ActiveChain().Tip();
|
||||
CHECK_NONFATAL(tip);
|
||||
}
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
bool res = FindScriptPubKey(g_scan_progress, g_should_abort_scan, count, pcursor.get(), needles, coins, node.rpc_interruption_point);
|
||||
result.pushKV("success", res);
|
||||
result.pushKV("txouts", count);
|
||||
@ -2377,8 +2385,9 @@ static RPCHelpMan getblockfilter()
|
||||
const CBlockIndex* block_index;
|
||||
bool block_was_connected;
|
||||
{
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
block_index = EnsureAnyChainman(request.context).m_blockman.LookupBlockIndex(block_hash);
|
||||
block_index = chainman.m_blockman.LookupBlockIndex(block_hash);
|
||||
if (!block_index) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
|
||||
}
|
||||
|
@ -100,9 +100,9 @@ static RPCHelpMan getnetworkhashps()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
const CChain& active_chain = EnsureAnyChainman(request.context).ActiveChain();
|
||||
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1, active_chain);
|
||||
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1, chainman.ActiveChain());
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -235,8 +235,9 @@ static RPCHelpMan generatetodescriptor()
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
|
||||
}
|
||||
|
||||
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
|
||||
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
|
||||
},
|
||||
@ -280,8 +281,9 @@ static RPCHelpMan generatetoaddress()
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: Invalid address");
|
||||
}
|
||||
|
||||
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
|
||||
CScript coinbase_script = GetScriptForDestination(destination);
|
||||
|
||||
@ -329,7 +331,8 @@ static RPCHelpMan generateblock()
|
||||
coinbase_script = GetScriptForDestination(destination);
|
||||
}
|
||||
|
||||
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||
|
||||
std::vector<CTransactionRef> txs;
|
||||
const auto raw_txs_or_txids = request.params[1].get_array();
|
||||
@ -358,7 +361,7 @@ static RPCHelpMan generateblock()
|
||||
CChainParams chainparams(Params());
|
||||
CBlock block;
|
||||
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
{
|
||||
LOCK(cs_main);
|
||||
|
||||
@ -424,9 +427,11 @@ static RPCHelpMan getmininginfo()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
LOCK(cs_main);
|
||||
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||
const CChain& active_chain = EnsureAnyChainman(request.context).ActiveChain();
|
||||
const CChain& active_chain = chainman.ActiveChain();
|
||||
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
obj.pushKV("blocks", (int)active_chain.Height());
|
||||
@ -595,8 +600,9 @@ static RPCHelpMan getblocktemplate()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
LOCK(cs_main);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
|
||||
std::string strMode = "template";
|
||||
UniValue lpval = NullUniValue;
|
||||
@ -663,7 +669,6 @@ static RPCHelpMan getblocktemplate()
|
||||
if (strMode != "template")
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
|
||||
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
if(!node.connman)
|
||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||
|
||||
@ -678,7 +683,7 @@ static RPCHelpMan getblocktemplate()
|
||||
}
|
||||
|
||||
static unsigned int nTransactionsUpdatedLast;
|
||||
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||
|
||||
if (!lpval.isNull())
|
||||
{
|
||||
|
@ -158,7 +158,7 @@ static RPCHelpMan getrawtransaction()
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
const NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
|
||||
bool in_active_chain = true;
|
||||
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
||||
@ -350,9 +350,9 @@ static RPCHelpMan verifytxoutproof()
|
||||
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot)
|
||||
return res;
|
||||
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
LOCK(cs_main);
|
||||
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(merkleBlock.header.GetHash());
|
||||
if (!pindex || !chainman.ActiveChain().Contains(pindex) || pindex->nTx == 0) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
|
||||
@ -678,10 +678,11 @@ static RPCHelpMan combinerawtransaction()
|
||||
CCoinsView viewDummy;
|
||||
CCoinsViewCache view(&viewDummy);
|
||||
{
|
||||
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||
LOCK(cs_main);
|
||||
LOCK(mempool.cs);
|
||||
CCoinsViewCache &viewChain = EnsureAnyChainman(request.context).ActiveChainstate().CoinsTip();
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
LOCK2(cs_main, mempool.cs);
|
||||
CCoinsViewCache &viewChain = chainman.ActiveChainstate().CoinsTip();
|
||||
CCoinsViewMemPool viewMempool(&viewChain, mempool);
|
||||
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
|
||||
|
||||
@ -943,7 +944,9 @@ static RPCHelpMan testmempoolaccept()
|
||||
DEFAULT_MAX_RAW_TX_FEE_RATE :
|
||||
CFeeRate(AmountFromValue(request.params[1]));
|
||||
|
||||
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
|
||||
CTxMemPool& mempool = EnsureMemPool(node);
|
||||
int64_t virtual_size = GetVirtualTransactionSize(*tx);
|
||||
CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
|
||||
|
||||
@ -952,7 +955,8 @@ static RPCHelpMan testmempoolaccept()
|
||||
result_0.pushKV("txid", tx->GetHash().GetHex());
|
||||
result_0.pushKV("wtxid", tx->GetWitnessHash().GetHex());
|
||||
|
||||
const MempoolAcceptResult accept_result = WITH_LOCK(cs_main, return AcceptToMemoryPool(EnsureAnyChainman(request.context).ActiveChainstate(), mempool, std::move(tx),
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
const MempoolAcceptResult accept_result = WITH_LOCK(cs_main, return AcceptToMemoryPool(chainman.ActiveChainstate(), mempool, std::move(tx),
|
||||
false /* bypass_limits */, /* test_accept */ true));
|
||||
|
||||
// Only return the fee and vsize if the transaction would pass ATMP.
|
||||
@ -1601,9 +1605,11 @@ static RPCHelpMan utxoupdatepsbt()
|
||||
CCoinsView viewDummy;
|
||||
CCoinsViewCache view(&viewDummy);
|
||||
{
|
||||
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
|
||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
const CTxMemPool& mempool = EnsureMemPool(node);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
LOCK2(cs_main, mempool.cs);
|
||||
CCoinsViewCache &viewChain = EnsureAnyChainman(request.context).ActiveChainstate().CoinsTip();
|
||||
CCoinsViewCache &viewChain = chainman.ActiveChainstate().CoinsTip();
|
||||
CCoinsViewMemPool viewMempool(&viewChain, mempool);
|
||||
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user