@@ -66,22 +66,21 @@ using interfaces::Node;
6666using interfaces::WalletLoader;
6767
6868namespace node {
69+ // All members of the classes in this namespace are intentionally public, as the
70+ // classes themselves are private.
6971namespace {
7072#ifdef ENABLE_EXTERNAL_SIGNER
7173class ExternalSignerImpl : public interfaces ::ExternalSigner
7274{
7375public:
7476 ExternalSignerImpl (::ExternalSigner signer) : m_signer(std::move(signer)) {}
7577 std::string getName () override { return m_signer.m_name ; }
76- private:
7778 ::ExternalSigner m_signer;
7879};
7980#endif
8081
8182class NodeImpl : public Node
8283{
83- private:
84- ChainstateManager& chainman () { return *Assert (m_context->chainman ); }
8584public:
8685 explicit NodeImpl (NodeContext& context) { setContext (&context); }
8786 void initLogging () override { InitLogging (*Assert (m_context->args )); }
@@ -288,12 +287,7 @@ class NodeImpl : public Node
288287 }
289288 double getVerificationProgress () override
290289 {
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 ()));
297291 }
298292 bool isInitialBlockDownload () override {
299293 return chainman ().ActiveChainstate ().IsInitialBlockDownload ();
@@ -389,6 +383,7 @@ class NodeImpl : public Node
389383 {
390384 m_context = context;
391385 }
386+ ChainstateManager& chainman () { return *Assert (m_context->chainman ); }
392387 NodeContext* m_context{nullptr };
393388};
394389
@@ -501,40 +496,28 @@ class RpcHandlerImpl : public Handler
501496
502497class ChainImpl : public Chain
503498{
504- private:
505- ChainstateManager& chainman () { return *Assert (m_node.chainman ); }
506499public:
507500 explicit ChainImpl (NodeContext& node) : m_node(node) {}
508501 std::optional<int > getHeight () override
509502 {
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 ;
517505 }
518506 uint256 getBlockHash (int height) override
519507 {
520508 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 ();
525510 }
526511 bool haveBlockOnDisk (int height) override
527512 {
528513 LOCK (::cs_main);
529- const CChain& active = chainman ().ActiveChain ();
530- CBlockIndex* block = active[height];
514+ const CBlockIndex* block{chainman ().ActiveChain ()[height]};
531515 return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0 ) && block->nTx > 0 ;
532516 }
533517 CBlockLocator getTipLocator () override
534518 {
535519 LOCK (::cs_main);
536- const CChain& active = chainman ().ActiveChain ();
537- return active.GetLocator ();
520+ return chainman ().ActiveChain ().GetLocator ();
538521 }
539522 CBlockLocator getActiveChainLocator (const uint256& block_hash) override
540523 {
@@ -546,17 +529,15 @@ class ChainImpl : public Chain
546529 std::optional<int > findLocatorFork (const CBlockLocator& locator) override
547530 {
548531 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)) {
551533 return fork->nHeight ;
552534 }
553535 return std::nullopt ;
554536 }
555537 bool findBlock (const uint256& hash, const FoundBlock& block) override
556538 {
557539 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 ());
560541 }
561542 bool findFirstBlockWithTimeAndHeight (int64_t min_time, int min_height, const FoundBlock& block) override
562543 {
@@ -578,11 +559,10 @@ class ChainImpl : public Chain
578559 bool findAncestorByHash (const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
579560 {
580561 WAIT_LOCK (cs_main, lock);
581- const CChain& active = chainman ().ActiveChain ();
582562 const CBlockIndex* block = chainman ().m_blockman .LookupBlockIndex (block_hash);
583563 const CBlockIndex* ancestor = chainman ().m_blockman .LookupBlockIndex (ancestor_hash);
584564 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 () );
586566 }
587567 bool findCommonAncestor (const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
588568 {
@@ -722,11 +702,7 @@ class ChainImpl : public Chain
722702 }
723703 void waitForNotificationsIfTipChanged (const uint256& old_tip) override
724704 {
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 ;
730706 SyncWithValidationInterfaceQueue ();
731707 }
732708 std::unique_ptr<Handler> handleRpc (const CRPCCommand& command) override
@@ -782,6 +758,7 @@ class ChainImpl : public Chain
782758 }
783759
784760 NodeContext* context () override { return &m_node; }
761+ ChainstateManager& chainman () { return *Assert (m_node.chainman ); }
785762 NodeContext& m_node;
786763};
787764} // namespace
0 commit comments