@@ -66,22 +66,21 @@ using interfaces::Node;
66
66
using interfaces::WalletLoader;
67
67
68
68
namespace node {
69
+ // All members of the classes in this namespace are intentionally public, as the
70
+ // classes themselves are private.
69
71
namespace {
70
72
#ifdef ENABLE_EXTERNAL_SIGNER
71
73
class ExternalSignerImpl : public interfaces ::ExternalSigner
72
74
{
73
75
public:
74
76
ExternalSignerImpl (::ExternalSigner signer) : m_signer(std::move(signer)) {}
75
77
std::string getName () override { return m_signer.m_name ; }
76
- private:
77
78
::ExternalSigner m_signer;
78
79
};
79
80
#endif
80
81
81
82
class NodeImpl : public Node
82
83
{
83
- private:
84
- ChainstateManager& chainman () { return *Assert (m_context->chainman ); }
85
84
public:
86
85
explicit NodeImpl (NodeContext& context) { setContext (&context); }
87
86
void initLogging () override { InitLogging (*Assert (m_context->args )); }
@@ -288,12 +287,7 @@ class NodeImpl : public Node
288
287
}
289
288
double getVerificationProgress () override
290
289
{
291
- const CBlockIndex* tip;
292
- {
293
- LOCK (::cs_main);
294
- tip = chainman ().ActiveChain ().Tip ();
295
- }
296
- return GuessVerificationProgress (chainman ().GetParams ().TxData (), tip);
290
+ return GuessVerificationProgress (chainman ().GetParams ().TxData (), WITH_LOCK (::cs_main, return chainman ().ActiveChain ().Tip ()));
297
291
}
298
292
bool isInitialBlockDownload () override {
299
293
return chainman ().ActiveChainstate ().IsInitialBlockDownload ();
@@ -389,6 +383,7 @@ class NodeImpl : public Node
389
383
{
390
384
m_context = context;
391
385
}
386
+ ChainstateManager& chainman () { return *Assert (m_context->chainman ); }
392
387
NodeContext* m_context{nullptr };
393
388
};
394
389
@@ -501,40 +496,28 @@ class RpcHandlerImpl : public Handler
501
496
502
497
class ChainImpl : public Chain
503
498
{
504
- private:
505
- ChainstateManager& chainman () { return *Assert (m_node.chainman ); }
506
499
public:
507
500
explicit ChainImpl (NodeContext& node) : m_node(node) {}
508
501
std::optional<int > getHeight () override
509
502
{
510
- LOCK (::cs_main);
511
- const CChain& active = chainman ().ActiveChain ();
512
- int height = active.Height ();
513
- if (height >= 0 ) {
514
- return height;
515
- }
516
- return std::nullopt;
503
+ const int height{WITH_LOCK (::cs_main, return chainman ().ActiveChain ().Height ())};
504
+ return height >= 0 ? std::optional{height} : std::nullopt;
517
505
}
518
506
uint256 getBlockHash (int height) override
519
507
{
520
508
LOCK (::cs_main);
521
- const CChain& active = chainman ().ActiveChain ();
522
- CBlockIndex* block = active[height];
523
- assert (block);
524
- return block->GetBlockHash ();
509
+ return Assert (chainman ().ActiveChain ()[height])->GetBlockHash ();
525
510
}
526
511
bool haveBlockOnDisk (int height) override
527
512
{
528
513
LOCK (::cs_main);
529
- const CChain& active = chainman ().ActiveChain ();
530
- CBlockIndex* block = active[height];
514
+ const CBlockIndex* block{chainman ().ActiveChain ()[height]};
531
515
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0 ) && block->nTx > 0 ;
532
516
}
533
517
CBlockLocator getTipLocator () override
534
518
{
535
519
LOCK (::cs_main);
536
- const CChain& active = chainman ().ActiveChain ();
537
- return active.GetLocator ();
520
+ return chainman ().ActiveChain ().GetLocator ();
538
521
}
539
522
CBlockLocator getActiveChainLocator (const uint256& block_hash) override
540
523
{
@@ -546,17 +529,15 @@ class ChainImpl : public Chain
546
529
std::optional<int > findLocatorFork (const CBlockLocator& locator) override
547
530
{
548
531
LOCK (::cs_main);
549
- const CChainState& active = chainman ().ActiveChainstate ();
550
- if (const CBlockIndex* fork = active.FindForkInGlobalIndex (locator)) {
532
+ if (const CBlockIndex* fork = chainman ().ActiveChainstate ().FindForkInGlobalIndex (locator)) {
551
533
return fork->nHeight ;
552
534
}
553
535
return std::nullopt;
554
536
}
555
537
bool findBlock (const uint256& hash, const FoundBlock& block) override
556
538
{
557
539
WAIT_LOCK (cs_main, lock);
558
- const CChain& active = chainman ().ActiveChain ();
559
- return FillBlock (chainman ().m_blockman .LookupBlockIndex (hash), block, lock, active);
540
+ return FillBlock (chainman ().m_blockman .LookupBlockIndex (hash), block, lock, chainman ().ActiveChain ());
560
541
}
561
542
bool findFirstBlockWithTimeAndHeight (int64_t min_time, int min_height, const FoundBlock& block) override
562
543
{
@@ -578,11 +559,10 @@ class ChainImpl : public Chain
578
559
bool findAncestorByHash (const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
579
560
{
580
561
WAIT_LOCK (cs_main, lock);
581
- const CChain& active = chainman ().ActiveChain ();
582
562
const CBlockIndex* block = chainman ().m_blockman .LookupBlockIndex (block_hash);
583
563
const CBlockIndex* ancestor = chainman ().m_blockman .LookupBlockIndex (ancestor_hash);
584
564
if (block && ancestor && block->GetAncestor (ancestor->nHeight ) != ancestor) ancestor = nullptr ;
585
- return FillBlock (ancestor, ancestor_out, lock, active );
565
+ return FillBlock (ancestor, ancestor_out, lock, chainman (). ActiveChain () );
586
566
}
587
567
bool findCommonAncestor (const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
588
568
{
@@ -722,11 +702,7 @@ class ChainImpl : public Chain
722
702
}
723
703
void waitForNotificationsIfTipChanged (const uint256& old_tip) override
724
704
{
725
- if (!old_tip.IsNull ()) {
726
- LOCK (::cs_main);
727
- const CChain& active = chainman ().ActiveChain ();
728
- if (old_tip == active.Tip ()->GetBlockHash ()) return ;
729
- }
705
+ if (!old_tip.IsNull () && old_tip == WITH_LOCK (::cs_main, return chainman ().ActiveChain ().Tip ()->GetBlockHash ())) return ;
730
706
SyncWithValidationInterfaceQueue ();
731
707
}
732
708
std::unique_ptr<Handler> handleRpc (const CRPCCommand& command) override
@@ -782,6 +758,7 @@ class ChainImpl : public Chain
782
758
}
783
759
784
760
NodeContext* context () override { return &m_node; }
761
+ ChainstateManager& chainman () { return *Assert (m_node.chainman ); }
785
762
NodeContext& m_node;
786
763
};
787
764
} // namespace
0 commit comments