mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 14:08:40 +01:00
Merge bitcoin/bitcoin#25651: refactor: make all NodeImpl/ChainImpl/ExternalSignerImpl members public, rm temporaries, simplify
4bedfd702arefactor: remove unneeded temporaries in node/interfaces, simplify code (Jon Atack)b27ba169ebrefactor: 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 ACK4bedfd702a. Changes since last review, applying suggested style & simplifiying first commit. Also avoiding another lock in second commit. Tree-SHA512: 112f7cad5e2838c94c5b79d61328f42fe75fdb97f401ab49eccf696fc2c6a8a0c0ee55ec974c0602acf7423f78bb82e90eb8a0cc531e1d3347f73b7c83685504
This commit is contained in:
@@ -66,6 +66,8 @@ using interfaces::Node;
|
|||||||
using interfaces::WalletLoader;
|
using interfaces::WalletLoader;
|
||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
|
// All members of the classes in this namespace are intentionally public, as the
|
||||||
|
// classes themselves are private.
|
||||||
namespace {
|
namespace {
|
||||||
#ifdef ENABLE_EXTERNAL_SIGNER
|
#ifdef ENABLE_EXTERNAL_SIGNER
|
||||||
class ExternalSignerImpl : public interfaces::ExternalSigner
|
class ExternalSignerImpl : public interfaces::ExternalSigner
|
||||||
@@ -73,15 +75,12 @@ class ExternalSignerImpl : public interfaces::ExternalSigner
|
|||||||
public:
|
public:
|
||||||
ExternalSignerImpl(::ExternalSigner signer) : m_signer(std::move(signer)) {}
|
ExternalSignerImpl(::ExternalSigner signer) : m_signer(std::move(signer)) {}
|
||||||
std::string getName() override { return m_signer.m_name; }
|
std::string getName() override { return m_signer.m_name; }
|
||||||
private:
|
|
||||||
::ExternalSigner m_signer;
|
::ExternalSigner m_signer;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class NodeImpl : public Node
|
class NodeImpl : public Node
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
|
|
||||||
public:
|
public:
|
||||||
explicit NodeImpl(NodeContext& context) { setContext(&context); }
|
explicit NodeImpl(NodeContext& context) { setContext(&context); }
|
||||||
void initLogging() override { InitLogging(*Assert(m_context->args)); }
|
void initLogging() override { InitLogging(*Assert(m_context->args)); }
|
||||||
@@ -288,12 +287,7 @@ public:
|
|||||||
}
|
}
|
||||||
double getVerificationProgress() override
|
double getVerificationProgress() override
|
||||||
{
|
{
|
||||||
const CBlockIndex* tip;
|
return GuessVerificationProgress(chainman().GetParams().TxData(), WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()));
|
||||||
{
|
|
||||||
LOCK(::cs_main);
|
|
||||||
tip = chainman().ActiveChain().Tip();
|
|
||||||
}
|
|
||||||
return GuessVerificationProgress(chainman().GetParams().TxData(), tip);
|
|
||||||
}
|
}
|
||||||
bool isInitialBlockDownload() override {
|
bool isInitialBlockDownload() override {
|
||||||
return chainman().ActiveChainstate().IsInitialBlockDownload();
|
return chainman().ActiveChainstate().IsInitialBlockDownload();
|
||||||
@@ -389,6 +383,7 @@ public:
|
|||||||
{
|
{
|
||||||
m_context = context;
|
m_context = context;
|
||||||
}
|
}
|
||||||
|
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
|
||||||
NodeContext* m_context{nullptr};
|
NodeContext* m_context{nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -501,40 +496,28 @@ public:
|
|||||||
|
|
||||||
class ChainImpl : public Chain
|
class ChainImpl : public Chain
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
|
|
||||||
public:
|
public:
|
||||||
explicit ChainImpl(NodeContext& node) : m_node(node) {}
|
explicit ChainImpl(NodeContext& node) : m_node(node) {}
|
||||||
std::optional<int> getHeight() override
|
std::optional<int> getHeight() override
|
||||||
{
|
{
|
||||||
LOCK(::cs_main);
|
const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())};
|
||||||
const CChain& active = chainman().ActiveChain();
|
return height >= 0 ? std::optional{height} : std::nullopt;
|
||||||
int height = active.Height();
|
|
||||||
if (height >= 0) {
|
|
||||||
return height;
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
}
|
||||||
uint256 getBlockHash(int height) override
|
uint256 getBlockHash(int height) override
|
||||||
{
|
{
|
||||||
LOCK(::cs_main);
|
LOCK(::cs_main);
|
||||||
const CChain& active = chainman().ActiveChain();
|
return Assert(chainman().ActiveChain()[height])->GetBlockHash();
|
||||||
CBlockIndex* block = active[height];
|
|
||||||
assert(block);
|
|
||||||
return block->GetBlockHash();
|
|
||||||
}
|
}
|
||||||
bool haveBlockOnDisk(int height) override
|
bool haveBlockOnDisk(int height) override
|
||||||
{
|
{
|
||||||
LOCK(::cs_main);
|
LOCK(::cs_main);
|
||||||
const CChain& active = chainman().ActiveChain();
|
const CBlockIndex* block{chainman().ActiveChain()[height]};
|
||||||
CBlockIndex* block = active[height];
|
|
||||||
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
|
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
|
||||||
}
|
}
|
||||||
CBlockLocator getTipLocator() override
|
CBlockLocator getTipLocator() override
|
||||||
{
|
{
|
||||||
LOCK(::cs_main);
|
LOCK(::cs_main);
|
||||||
const CChain& active = chainman().ActiveChain();
|
return chainman().ActiveChain().GetLocator();
|
||||||
return active.GetLocator();
|
|
||||||
}
|
}
|
||||||
CBlockLocator getActiveChainLocator(const uint256& block_hash) override
|
CBlockLocator getActiveChainLocator(const uint256& block_hash) override
|
||||||
{
|
{
|
||||||
@@ -546,8 +529,7 @@ public:
|
|||||||
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
|
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
|
||||||
{
|
{
|
||||||
LOCK(::cs_main);
|
LOCK(::cs_main);
|
||||||
const CChainState& active = chainman().ActiveChainstate();
|
if (const CBlockIndex* fork = chainman().ActiveChainstate().FindForkInGlobalIndex(locator)) {
|
||||||
if (const CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) {
|
|
||||||
return fork->nHeight;
|
return fork->nHeight;
|
||||||
}
|
}
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
@@ -555,8 +537,7 @@ public:
|
|||||||
bool findBlock(const uint256& hash, const FoundBlock& block) override
|
bool findBlock(const uint256& hash, const FoundBlock& block) override
|
||||||
{
|
{
|
||||||
WAIT_LOCK(cs_main, lock);
|
WAIT_LOCK(cs_main, lock);
|
||||||
const CChain& active = chainman().ActiveChain();
|
return FillBlock(chainman().m_blockman.LookupBlockIndex(hash), block, lock, chainman().ActiveChain());
|
||||||
return FillBlock(chainman().m_blockman.LookupBlockIndex(hash), block, lock, active);
|
|
||||||
}
|
}
|
||||||
bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override
|
bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override
|
||||||
{
|
{
|
||||||
@@ -578,11 +559,10 @@ public:
|
|||||||
bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
|
bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
|
||||||
{
|
{
|
||||||
WAIT_LOCK(cs_main, lock);
|
WAIT_LOCK(cs_main, lock);
|
||||||
const CChain& active = chainman().ActiveChain();
|
|
||||||
const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash);
|
const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash);
|
||||||
const CBlockIndex* ancestor = chainman().m_blockman.LookupBlockIndex(ancestor_hash);
|
const CBlockIndex* ancestor = chainman().m_blockman.LookupBlockIndex(ancestor_hash);
|
||||||
if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;
|
if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;
|
||||||
return FillBlock(ancestor, ancestor_out, lock, active);
|
return FillBlock(ancestor, ancestor_out, lock, chainman().ActiveChain());
|
||||||
}
|
}
|
||||||
bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
|
bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
|
||||||
{
|
{
|
||||||
@@ -722,11 +702,7 @@ public:
|
|||||||
}
|
}
|
||||||
void waitForNotificationsIfTipChanged(const uint256& old_tip) override
|
void waitForNotificationsIfTipChanged(const uint256& old_tip) override
|
||||||
{
|
{
|
||||||
if (!old_tip.IsNull()) {
|
if (!old_tip.IsNull() && old_tip == WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()->GetBlockHash())) return;
|
||||||
LOCK(::cs_main);
|
|
||||||
const CChain& active = chainman().ActiveChain();
|
|
||||||
if (old_tip == active.Tip()->GetBlockHash()) return;
|
|
||||||
}
|
|
||||||
SyncWithValidationInterfaceQueue();
|
SyncWithValidationInterfaceQueue();
|
||||||
}
|
}
|
||||||
std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override
|
std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override
|
||||||
@@ -782,6 +758,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
NodeContext* context() override { return &m_node; }
|
NodeContext* context() override { return &m_node; }
|
||||||
|
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
|
||||||
NodeContext& m_node;
|
NodeContext& m_node;
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ using interfaces::WalletTxStatus;
|
|||||||
using interfaces::WalletValueMap;
|
using interfaces::WalletValueMap;
|
||||||
|
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
|
// All members of the classes in this namespace are intentionally public, as the
|
||||||
|
// classes themselves are private.
|
||||||
namespace {
|
namespace {
|
||||||
//! Construct wallet tx struct.
|
//! Construct wallet tx struct.
|
||||||
WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
|
WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
|
||||||
|
|||||||
Reference in New Issue
Block a user