Skip to content

Commit da23320

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25651: refactor: make all NodeImpl/ChainImpl/ExternalSignerImpl members public, rm temporaries, simplify
4bedfd7 refactor: remove unneeded temporaries in node/interfaces, simplify code (Jon Atack) b27ba16 refactor: make all NodeImpl/ChainImpl/ExternalSignerImpl members public (Jon Atack) Pull request description: - Make all `NodeImpl`, `ChainImpl` and `ExternalSignerImpl` class members `public` (and document why), to be consistent in all the `*Impl` classes in `src/node/interfaces.cpp` and `src/wallet/interfaces.cpp` and to help future reviewers and contributors. - Remove unneeded temporaries in `NodeImpl` and `ChainImpl` methods in `src/node/interfaces.cpp` and simplify, to make the code easier to read and understand and to improve performance by avoiding unnecessary move operations. ACKs for top commit: ryanofsky: Code review ACK 4bedfd7. Changes since last review, applying suggested style & simplifiying first commit. Also avoiding another lock in second commit. Tree-SHA512: 112f7cad5e2838c94c5b79d61328f42fe75fdb97f401ab49eccf696fc2c6a8a0c0ee55ec974c0602acf7423f78bb82e90eb8a0cc531e1d3347f73b7c83685504
2 parents f5eadcb + 4bedfd7 commit da23320

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

src/node/interfaces.cpp

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,21 @@ using interfaces::Node;
6666
using interfaces::WalletLoader;
6767

6868
namespace node {
69+
// All members of the classes in this namespace are intentionally public, as the
70+
// classes themselves are private.
6971
namespace {
7072
#ifdef ENABLE_EXTERNAL_SIGNER
7173
class ExternalSignerImpl : public interfaces::ExternalSigner
7274
{
7375
public:
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

8182
class NodeImpl : public Node
8283
{
83-
private:
84-
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
8584
public:
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

502497
class ChainImpl : public Chain
503498
{
504-
private:
505-
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
506499
public:
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

src/wallet/interfaces.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ using interfaces::WalletTxStatus;
4848
using interfaces::WalletValueMap;
4949

5050
namespace wallet {
51+
// All members of the classes in this namespace are intentionally public, as the
52+
// classes themselves are private.
5153
namespace {
5254
//! Construct wallet tx struct.
5355
WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)

0 commit comments

Comments
 (0)