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

@@ -268,12 +268,13 @@ static RPCHelpMan gettxoutproof()
}
} else {
LOCK(cs_main);
CChainState& active_chainstate = chainman.ActiveChainstate();
// Loop through txids and try to find which block they're in. Exit loop once a block is found.
for (const auto& tx : setTxids) {
const Coin& coin = AccessByTxid(chainman.ActiveChainstate().CoinsTip(), tx);
const Coin& coin = AccessByTxid(active_chainstate.CoinsTip(), tx);
if (!coin.IsSpent()) {
pblockindex = chainman.ActiveChain()[coin.nHeight];
pblockindex = active_chainstate.m_chain[coin.nHeight];
break;
}
}