rpc/rest: Take and reuse local Chain/ChainState obj

In all rest/rpc-related modules, if there are multiple calls to
ActiveChain{,State}(), and the calls fall under the same ::cs_main lock,
we can simply take a local reference and use/reuse it instead of calling
ActiveChain{,State}() again and again.
This commit is contained in:
Carl Dong
2021-04-12 21:34:42 -04:00
parent bc3bd36902
commit 586190f0b4
4 changed files with 39 additions and 29 deletions

View File

@@ -182,13 +182,14 @@ static bool rest_headers(const std::any& context,
{
ChainstateManager& chainman = EnsureAnyChainman(context);
LOCK(cs_main);
tip = chainman.ActiveChain().Tip();
CChain& active_chain = chainman.ActiveChain();
tip = active_chain.Tip();
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
while (pindex != nullptr && chainman.ActiveChain().Contains(pindex)) {
while (pindex != nullptr && active_chain.Contains(pindex)) {
headers.push_back(pindex);
if (headers.size() == (unsigned long)count)
break;
pindex = chainman.ActiveChain().Next(pindex);
pindex = active_chain.Next(pindex);
}
}