@@ -287,12 +287,7 @@ class NodeImpl : public Node
287
287
}
288
288
double getVerificationProgress () override
289
289
{
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 ()));
296
291
}
297
292
bool isInitialBlockDownload () override {
298
293
return chainman ().ActiveChainstate ().IsInitialBlockDownload ();
@@ -505,34 +500,24 @@ class ChainImpl : public Chain
505
500
explicit ChainImpl (NodeContext& node) : m_node(node) {}
506
501
std::optional<int > getHeight () override
507
502
{
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;
515
505
}
516
506
uint256 getBlockHash (int height) override
517
507
{
518
508
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 ();
523
510
}
524
511
bool haveBlockOnDisk (int height) override
525
512
{
526
513
LOCK (::cs_main);
527
- const CChain& active = chainman ().ActiveChain ();
528
- CBlockIndex* block = active[height];
514
+ const CBlockIndex* block{chainman ().ActiveChain ()[height]};
529
515
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0 ) && block->nTx > 0 ;
530
516
}
531
517
CBlockLocator getTipLocator () override
532
518
{
533
519
LOCK (::cs_main);
534
- const CChain& active = chainman ().ActiveChain ();
535
- return active.GetLocator ();
520
+ return chainman ().ActiveChain ().GetLocator ();
536
521
}
537
522
CBlockLocator getActiveChainLocator (const uint256& block_hash) override
538
523
{
@@ -544,17 +529,15 @@ class ChainImpl : public Chain
544
529
std::optional<int > findLocatorFork (const CBlockLocator& locator) override
545
530
{
546
531
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)) {
549
533
return fork->nHeight ;
550
534
}
551
535
return std::nullopt;
552
536
}
553
537
bool findBlock (const uint256& hash, const FoundBlock& block) override
554
538
{
555
539
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 ());
558
541
}
559
542
bool findFirstBlockWithTimeAndHeight (int64_t min_time, int min_height, const FoundBlock& block) override
560
543
{
@@ -576,11 +559,10 @@ class ChainImpl : public Chain
576
559
bool findAncestorByHash (const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
577
560
{
578
561
WAIT_LOCK (cs_main, lock);
579
- const CChain& active = chainman ().ActiveChain ();
580
562
const CBlockIndex* block = chainman ().m_blockman .LookupBlockIndex (block_hash);
581
563
const CBlockIndex* ancestor = chainman ().m_blockman .LookupBlockIndex (ancestor_hash);
582
564
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 () );
584
566
}
585
567
bool findCommonAncestor (const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
586
568
{
@@ -720,11 +702,7 @@ class ChainImpl : public Chain
720
702
}
721
703
void waitForNotificationsIfTipChanged (const uint256& old_tip) override
722
704
{
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 ;
728
706
SyncWithValidationInterfaceQueue ();
729
707
}
730
708
std::unique_ptr<Handler> handleRpc (const CRPCCommand& command) override
0 commit comments