From 4bedfd702ad878645c51bea6ee8ce40d8c0bd3da Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 27 Jul 2022 13:21:08 +0200 Subject: refactor: remove unneeded temporaries in node/interfaces, simplify code - make the code easier to read and understand - improve performance by avoiding unnecessary move operations - the cleaner, simpler, and easier to read the code is, the better chance the compiler has at implementing it well --- src/node/interfaces.cpp | 42 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 32 deletions(-) (limited to 'src/node') diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index b5a6374fcb..699dca0a73 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -287,12 +287,7 @@ public: } double getVerificationProgress() override { - const CBlockIndex* tip; - { - LOCK(::cs_main); - tip = chainman().ActiveChain().Tip(); - } - return GuessVerificationProgress(chainman().GetParams().TxData(), tip); + return GuessVerificationProgress(chainman().GetParams().TxData(), WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip())); } bool isInitialBlockDownload() override { return chainman().ActiveChainstate().IsInitialBlockDownload(); @@ -505,34 +500,24 @@ public: explicit ChainImpl(NodeContext& node) : m_node(node) {} std::optional getHeight() override { - LOCK(::cs_main); - const CChain& active = chainman().ActiveChain(); - int height = active.Height(); - if (height >= 0) { - return height; - } - return std::nullopt; + const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())}; + return height >= 0 ? std::optional{height} : std::nullopt; } uint256 getBlockHash(int height) override { LOCK(::cs_main); - const CChain& active = chainman().ActiveChain(); - CBlockIndex* block = active[height]; - assert(block); - return block->GetBlockHash(); + return Assert(chainman().ActiveChain()[height])->GetBlockHash(); } bool haveBlockOnDisk(int height) override { LOCK(::cs_main); - const CChain& active = chainman().ActiveChain(); - CBlockIndex* block = active[height]; + const CBlockIndex* block{chainman().ActiveChain()[height]}; return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0; } CBlockLocator getTipLocator() override { LOCK(::cs_main); - const CChain& active = chainman().ActiveChain(); - return active.GetLocator(); + return chainman().ActiveChain().GetLocator(); } CBlockLocator getActiveChainLocator(const uint256& block_hash) override { @@ -544,8 +529,7 @@ public: std::optional findLocatorFork(const CBlockLocator& locator) override { LOCK(::cs_main); - const CChainState& active = chainman().ActiveChainstate(); - if (const CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) { + if (const CBlockIndex* fork = chainman().ActiveChainstate().FindForkInGlobalIndex(locator)) { return fork->nHeight; } return std::nullopt; @@ -553,8 +537,7 @@ public: bool findBlock(const uint256& hash, const FoundBlock& block) override { WAIT_LOCK(cs_main, lock); - const CChain& active = chainman().ActiveChain(); - return FillBlock(chainman().m_blockman.LookupBlockIndex(hash), block, lock, active); + return FillBlock(chainman().m_blockman.LookupBlockIndex(hash), block, lock, chainman().ActiveChain()); } bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override { @@ -576,11 +559,10 @@ public: bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override { WAIT_LOCK(cs_main, lock); - const CChain& active = chainman().ActiveChain(); const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash); const CBlockIndex* ancestor = chainman().m_blockman.LookupBlockIndex(ancestor_hash); if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr; - return FillBlock(ancestor, ancestor_out, lock, active); + return FillBlock(ancestor, ancestor_out, lock, chainman().ActiveChain()); } bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override { @@ -720,11 +702,7 @@ public: } void waitForNotificationsIfTipChanged(const uint256& old_tip) override { - if (!old_tip.IsNull()) { - LOCK(::cs_main); - const CChain& active = chainman().ActiveChain(); - if (old_tip == active.Tip()->GetBlockHash()) return; - } + if (!old_tip.IsNull() && old_tip == WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()->GetBlockHash())) return; SyncWithValidationInterfaceQueue(); } std::unique_ptr handleRpc(const CRPCCommand& command) override -- cgit v1.2.3