aboutsummaryrefslogtreecommitdiff
path: root/src/rest.cpp
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-04-17 17:37:26 +0200
committerMarcoFalke <falke.marco@gmail.com>2021-04-17 17:37:37 +0200
commit0dd7b234895b02bfc512ae83bd77b581c861d6e9 (patch)
treeb9ddbf86936e1db3f84a4346ab3234e6c83b2266 /src/rest.cpp
parent4a1751a929cc5727d4941543baf2238f041b4b70 (diff)
parent586190f0b4740457cb86cba632e3d64e6dfe9b0c (diff)
downloadbitcoin-0dd7b234895b02bfc512ae83bd77b581c861d6e9.tar.xz
Merge #21391: [Bundle 5/n] Prune g_chainman usage in RPC modules
586190f0b4740457cb86cba632e3d64e6dfe9b0c rpc/rest: Take and reuse local Chain/ChainState obj (Carl Dong) bc3bd369027273278a0541f3b991eb71de831aa2 rpc: style: Improve BuriedForkDescPushBack signature (Carl Dong) f99913969f92b8b9cef1b83f5ee8e6a9267b4af0 rpc: Remove unnecessary casting of block height (Carl Dong) 6a3d1920209cded0dae52fb9070a3530d9a4e5fd rpc: Tidy up local references (see commit message) (Carl Dong) 038854f31e3511e8bb6e163305cab0a96783d25b rest/rpc: Remove now-unused old Ensure functions (Carl Dong) 6fb65b49f4d393b091479be5a5df5a0a160cf986 scripted-diff: rest/rpc: Use renamed EnsureAny*() (Carl Dong) 1570c7ee98612366df031bebef9e0468fb57b8a2 rpc: Add renamed EnsureAny*() functions (Carl Dong) 306b1cd3eeb2502904ed4698646d2c86d028aad2 rpc: Add alt Ensure* functions acepting NodeContext (Carl Dong) d7824acdb9b18fe8f151771a83ccae1681f16c66 rest: Use existing NodeContext (Carl Dong) 3f0893479908ca28d6127c8d0ada30737cb830be rest: Pass in NodeContext to rest_block (Carl Dong) 7be0671b950842fc3a17641a4a21501de0a800b5 rpc/rawtx: Use existing NodeContext (Carl Dong) 60dc05afc6f6388c6f86729a0edd7cb69f1748e0 rpc/mining: Use existing NodeContext (Carl Dong) d485e815e2b62dc74a485569d08130dc3ef9ff63 rpc/blockchain: Use existing NodeContext (Carl Dong) d0abf0bf429586e3a5b4c3231fe430dc29695481 rpc/*,rest: Add review-only assertion to EnsureChainman (Carl Dong) cced0f46c9133e0fc6211e987421ad1d9be1a399 miner: Pass in previous CBlockIndex to RegenerateCommitments (Carl Dong) Pull request description: Overall PR: #20158 (tree-wide: De-globalize ChainstateManager) Based on: - [x] #21270 | [Bundle 4/n] Prune g_chainman usage in validation-adjacent modules - [x] #21525 | [Bundle 4.5/n] Followup fixups to bundle 4 Note to reviewers: 1. This bundle may _apparently_ introduce usage of `g_chainman` or `::Chain(state|)Active()` globals, but these are resolved later on in the overall PR. [Commits of overall PR](https://github.com/bitcoin/bitcoin/pull/20158/commits) 2. There may be seemingly obvious local references to `ChainstateManager` or other validation objects which are not being used in callers of the current function in question, this is done intentionally to **_keep each commit centered around one function/method_** to ease review and to make the overall change systematic. We don't assume anything about our callers. Rest assured that once we are considering that particular caller in later commits, we will use the obvious local references. [Commits of overall PR](https://github.com/bitcoin/bitcoin/pull/20158/commits) 3. When changing a function/method that has many callers (e.g. `LookupBlockIndex` with 55 callers), it is sometimes easier (and less error-prone) to use a scripted-diff. When doing so, there will be 3 commits in sequence so that every commit compiles like so: 1. Add `new_function`, make `old_function` a wrapper of `new_function`, divert all calls to `old_function` to `new_function` **in the local module only** 2. Scripted-diff to divert all calls to `old_function` to `new_function` **in the rest of the codebase** 3. Remove `old_function` ACKs for top commit: ryanofsky: Code review ACK 586190f0b4740457cb86cba632e3d64e6dfe9b0c. Since last review, no changes to existing commits, just some simple new commits added: three new commits renaming std::any Ensure functions (scripted diff commit and manual pre/post commits), and one new commit factoring out a repeated `ActiveChain()` call made in a loop. Thanks for the updates! jnewbery: utACK 586190f0b4740457cb86cba632e3d64e6dfe9b0c MarcoFalke: review ACK 586190f0b4740457cb86cba632e3d64e6dfe9b0c 🍯 Tree-SHA512: 64b677fb50141805b55c3f1afe68fcd298f9a071a359bdcd63256d52e334f83e462f31fb3ebee9b630da8f1d912a03a128cfc38179e7aaec29a055744a98478c
Diffstat (limited to 'src/rest.cpp')
-rw-r--r--src/rest.cpp41
1 files changed, 24 insertions, 17 deletions
diff --git a/src/rest.cpp b/src/rest.cpp
index 9d4ac55bbe..d41f374c49 100644
--- a/src/rest.cpp
+++ b/src/rest.cpp
@@ -181,14 +181,16 @@ static bool rest_headers(const std::any& context,
std::vector<const CBlockIndex *> headers;
headers.reserve(count);
{
+ ChainstateManager& chainman = EnsureAnyChainman(context);
LOCK(cs_main);
- tip = ::ChainActive().Tip();
- const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
- while (pindex != nullptr && ::ChainActive().Contains(pindex)) {
+ CChain& active_chain = chainman.ActiveChain();
+ tip = active_chain.Tip();
+ const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
+ while (pindex != nullptr && active_chain.Contains(pindex)) {
headers.push_back(pindex);
if (headers.size() == (unsigned long)count)
break;
- pindex = ::ChainActive().Next(pindex);
+ pindex = active_chain.Next(pindex);
}
}
@@ -232,7 +234,8 @@ static bool rest_headers(const std::any& context,
}
}
-static bool rest_block(HTTPRequest* req,
+static bool rest_block(const std::any& context,
+ HTTPRequest* req,
const std::string& strURIPart,
bool showTxDetails)
{
@@ -249,9 +252,10 @@ static bool rest_block(HTTPRequest* req,
CBlockIndex* pblockindex = nullptr;
CBlockIndex* tip = nullptr;
{
+ ChainstateManager& chainman = EnsureAnyChainman(context);
LOCK(cs_main);
- tip = ::ChainActive().Tip();
- pblockindex = g_chainman.m_blockman.LookupBlockIndex(hash);
+ tip = chainman.ActiveChain().Tip();
+ pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
if (!pblockindex) {
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
}
@@ -298,12 +302,12 @@ static bool rest_block(HTTPRequest* req,
static bool rest_block_extended(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
- return rest_block(req, strURIPart, true);
+ return rest_block(context, req, strURIPart, true);
}
static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
- return rest_block(req, strURIPart, false);
+ return rest_block(context, req, strURIPart, false);
}
// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
@@ -537,6 +541,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
std::string bitmapStringRepresentation;
std::vector<bool> hits;
bitmap.resize((vOutPoints.size() + 7) / 8);
+ ChainstateManager& chainman = EnsureAnyChainman(context);
{
auto process_utxos = [&vOutPoints, &outs, &hits](const CCoinsView& view, const CTxMemPool& mempool) {
for (const COutPoint& vOutPoint : vOutPoints) {
@@ -552,12 +557,12 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
if (!mempool) return false;
// use db+mempool as cache backend in case user likes to query mempool
LOCK2(cs_main, mempool->cs);
- CCoinsViewCache& viewChain = ::ChainstateActive().CoinsTip();
+ CCoinsViewCache& viewChain = chainman.ActiveChainstate().CoinsTip();
CCoinsViewMemPool viewMempool(&viewChain, *mempool);
process_utxos(viewMempool, *mempool);
} else {
LOCK(cs_main); // no need to lock mempool!
- process_utxos(::ChainstateActive().CoinsTip(), CTxMemPool());
+ process_utxos(chainman.ActiveChainstate().CoinsTip(), CTxMemPool());
}
for (size_t i = 0; i < hits.size(); ++i) {
@@ -572,7 +577,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
// serialize data
// use exact same output as mentioned in Bip64
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
- ssGetUTXOResponse << ::ChainActive().Height() << ::ChainActive().Tip()->GetBlockHash() << bitmap << outs;
+ ssGetUTXOResponse << chainman.ActiveChain().Height() << chainman.ActiveChain().Tip()->GetBlockHash() << bitmap << outs;
std::string ssGetUTXOResponseString = ssGetUTXOResponse.str();
req->WriteHeader("Content-Type", "application/octet-stream");
@@ -582,7 +587,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
case RetFormat::HEX: {
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
- ssGetUTXOResponse << ::ChainActive().Height() << ::ChainActive().Tip()->GetBlockHash() << bitmap << outs;
+ ssGetUTXOResponse << chainman.ActiveChain().Height() << chainman.ActiveChain().Tip()->GetBlockHash() << bitmap << outs;
std::string strHex = HexStr(ssGetUTXOResponse) + "\n";
req->WriteHeader("Content-Type", "text/plain");
@@ -595,8 +600,8 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
// pack in some essentials
// use more or less the same output as mentioned in Bip64
- objGetUTXOResponse.pushKV("chainHeight", ::ChainActive().Height());
- objGetUTXOResponse.pushKV("chaintipHash", ::ChainActive().Tip()->GetBlockHash().GetHex());
+ objGetUTXOResponse.pushKV("chainHeight", chainman.ActiveChain().Height());
+ objGetUTXOResponse.pushKV("chaintipHash", chainman.ActiveChain().Tip()->GetBlockHash().GetHex());
objGetUTXOResponse.pushKV("bitmap", bitmapStringRepresentation);
UniValue utxos(UniValue::VARR);
@@ -639,11 +644,13 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
CBlockIndex* pblockindex = nullptr;
{
+ ChainstateManager& chainman = EnsureAnyChainman(context);
LOCK(cs_main);
- if (blockheight > ::ChainActive().Height()) {
+ const CChain& active_chain = chainman.ActiveChain();
+ if (blockheight > active_chain.Height()) {
return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
}
- pblockindex = ::ChainActive()[blockheight];
+ pblockindex = active_chain[blockheight];
}
switch (rf) {
case RetFormat::BINARY: {