Skip to content

Commit 4bedfd7

Browse files
committed
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
1 parent b27ba16 commit 4bedfd7

File tree

1 file changed

+10
-32
lines changed

1 file changed

+10
-32
lines changed

src/node/interfaces.cpp

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,7 @@ class NodeImpl : public Node
287287
}
288288
double getVerificationProgress() override
289289
{
290-
const CBlockIndex* tip;
291-
{
292-
LOCK(::cs_main);
293-
tip = chainman().ActiveChain().Tip();
294-
}
295-
return GuessVerificationProgress(chainman().GetParams().TxData(), tip);
290+
return GuessVerificationProgress(chainman().GetParams().TxData(), WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()));
296291
}
297292
bool isInitialBlockDownload() override {
298293
return chainman().ActiveChainstate().IsInitialBlockDownload();
@@ -505,34 +500,24 @@ class ChainImpl : public Chain
505500
explicit ChainImpl(NodeContext& node) : m_node(node) {}
506501
std::optional<int> getHeight() override
507502
{
508-
LOCK(::cs_main);
509-
const CChain& active = chainman().ActiveChain();
510-
int height = active.Height();
511-
if (height >= 0) {
512-
return height;
513-
}
514-
return std::nullopt;
503+
const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())};
504+
return height >= 0 ? std::optional{height} : std::nullopt;
515505
}
516506
uint256 getBlockHash(int height) override
517507
{
518508
LOCK(::cs_main);
519-
const CChain& active = chainman().ActiveChain();
520-
CBlockIndex* block = active[height];
521-
assert(block);
522-
return block->GetBlockHash();
509+
return Assert(chainman().ActiveChain()[height])->GetBlockHash();
523510
}
524511
bool haveBlockOnDisk(int height) override
525512
{
526513
LOCK(::cs_main);
527-
const CChain& active = chainman().ActiveChain();
528-
CBlockIndex* block = active[height];
514+
const CBlockIndex* block{chainman().ActiveChain()[height]};
529515
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
530516
}
531517
CBlockLocator getTipLocator() override
532518
{
533519
LOCK(::cs_main);
534-
const CChain& active = chainman().ActiveChain();
535-
return active.GetLocator();
520+
return chainman().ActiveChain().GetLocator();
536521
}
537522
CBlockLocator getActiveChainLocator(const uint256& block_hash) override
538523
{
@@ -544,17 +529,15 @@ class ChainImpl : public Chain
544529
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
545530
{
546531
LOCK(::cs_main);
547-
const CChainState& active = chainman().ActiveChainstate();
548-
if (const CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) {
532+
if (const CBlockIndex* fork = chainman().ActiveChainstate().FindForkInGlobalIndex(locator)) {
549533
return fork->nHeight;
550534
}
551535
return std::nullopt;
552536
}
553537
bool findBlock(const uint256& hash, const FoundBlock& block) override
554538
{
555539
WAIT_LOCK(cs_main, lock);
556-
const CChain& active = chainman().ActiveChain();
557-
return FillBlock(chainman().m_blockman.LookupBlockIndex(hash), block, lock, active);
540+
return FillBlock(chainman().m_blockman.LookupBlockIndex(hash), block, lock, chainman().ActiveChain());
558541
}
559542
bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override
560543
{
@@ -576,11 +559,10 @@ class ChainImpl : public Chain
576559
bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
577560
{
578561
WAIT_LOCK(cs_main, lock);
579-
const CChain& active = chainman().ActiveChain();
580562
const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash);
581563
const CBlockIndex* ancestor = chainman().m_blockman.LookupBlockIndex(ancestor_hash);
582564
if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;
583-
return FillBlock(ancestor, ancestor_out, lock, active);
565+
return FillBlock(ancestor, ancestor_out, lock, chainman().ActiveChain());
584566
}
585567
bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
586568
{
@@ -720,11 +702,7 @@ class ChainImpl : public Chain
720702
}
721703
void waitForNotificationsIfTipChanged(const uint256& old_tip) override
722704
{
723-
if (!old_tip.IsNull()) {
724-
LOCK(::cs_main);
725-
const CChain& active = chainman().ActiveChain();
726-
if (old_tip == active.Tip()->GetBlockHash()) return;
727-
}
705+
if (!old_tip.IsNull() && old_tip == WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()->GetBlockHash())) return;
728706
SyncWithValidationInterfaceQueue();
729707
}
730708
std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override

0 commit comments

Comments
 (0)