From 82e272a109281f750909d1feade784c778d8b592 Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Tue, 11 Oct 2022 23:35:28 -0400 Subject: [PATCH 1/3] refactor: Move src/interfaces/*.cpp files to libbitcoin_common.a These belong in libbitcoin_common.a, not libbitcoin_util.a, because they aren't general-purpose utilities, they just contain common code that is used by both the node and the wallet. Another reason to reason to not include these in libbitcoin_util.a is to prevent them from being used by the kernel library. --- src/Makefile.am | 4 +- .../handler.cpp => common/interfaces.cpp} | 40 +++++++++++-------- src/interfaces/echo.cpp | 18 --------- src/interfaces/init.cpp | 17 -------- src/interfaces/init.h | 19 ++++----- 5 files changed, 35 insertions(+), 63 deletions(-) rename src/{interfaces/handler.cpp => common/interfaces.cpp} (63%) delete mode 100644 src/interfaces/echo.cpp delete mode 100644 src/interfaces/init.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 2e2da54b2d..63a690d49f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -633,6 +633,7 @@ libbitcoin_common_a_SOURCES = \ chainparams.cpp \ coins.cpp \ common/bloom.cpp \ + common/interfaces.cpp \ common/run_command.cpp \ compressor.cpp \ core_read.cpp \ @@ -678,9 +679,6 @@ libbitcoin_util_a_SOURCES = \ chainparamsbase.cpp \ clientversion.cpp \ fs.cpp \ - interfaces/echo.cpp \ - interfaces/handler.cpp \ - interfaces/init.cpp \ logging.cpp \ random.cpp \ randomenv.cpp \ diff --git a/src/interfaces/handler.cpp b/src/common/interfaces.cpp similarity index 63% rename from src/interfaces/handler.cpp rename to src/common/interfaces.cpp index adb7031cbc..289856871d 100644 --- a/src/interfaces/handler.cpp +++ b/src/common/interfaces.cpp @@ -1,17 +1,26 @@ -// Copyright (c) 2018-2021 The Bitcoin Core developers +// Copyright (c) 2021 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include - #include +#include #include -namespace interfaces { +namespace common { namespace { +class CleanupHandler : public interfaces::Handler +{ +public: + explicit CleanupHandler(std::function cleanup) : m_cleanup(std::move(cleanup)) {} + ~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; } + void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; } + std::function m_cleanup; +}; -class HandlerImpl : public Handler +class HandlerImpl : public interfaces::Handler { public: explicit HandlerImpl(boost::signals2::connection connection) : m_connection(std::move(connection)) {} @@ -21,25 +30,24 @@ public: boost::signals2::scoped_connection m_connection; }; -class CleanupHandler : public Handler +class EchoImpl : public interfaces::Echo { public: - explicit CleanupHandler(std::function cleanup) : m_cleanup(std::move(cleanup)) {} - ~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; } - void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; } - std::function m_cleanup; + std::string echo(const std::string& echo) override { return echo; } }; - } // namespace +} // namespace common + +namespace interfaces { +std::unique_ptr MakeHandler(std::function cleanup) +{ + return std::make_unique(std::move(cleanup)); +} std::unique_ptr MakeHandler(boost::signals2::connection connection) { - return std::make_unique(std::move(connection)); -} - -std::unique_ptr MakeHandler(std::function cleanup) -{ - return std::make_unique(std::move(cleanup)); + return std::make_unique(std::move(connection)); } +std::unique_ptr MakeEcho() { return std::make_unique(); } } // namespace interfaces diff --git a/src/interfaces/echo.cpp b/src/interfaces/echo.cpp deleted file mode 100644 index 9bbb42217b..0000000000 --- a/src/interfaces/echo.cpp +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) 2021 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#include - -#include - -namespace interfaces { -namespace { -class EchoImpl : public Echo -{ -public: - std::string echo(const std::string& echo) override { return echo; } -}; -} // namespace -std::unique_ptr MakeEcho() { return std::make_unique(); } -} // namespace interfaces diff --git a/src/interfaces/init.cpp b/src/interfaces/init.cpp deleted file mode 100644 index f0f8aa5fed..0000000000 --- a/src/interfaces/init.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) 2021 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#include -#include -#include -#include -#include - -namespace interfaces { -std::unique_ptr Init::makeNode() { return {}; } -std::unique_ptr Init::makeChain() { return {}; } -std::unique_ptr Init::makeWalletLoader(Chain& chain) { return {}; } -std::unique_ptr Init::makeEcho() { return {}; } -Ipc* Init::ipc() { return nullptr; } -} // namespace interfaces diff --git a/src/interfaces/init.h b/src/interfaces/init.h index 2153076366..5b8f61640e 100644 --- a/src/interfaces/init.h +++ b/src/interfaces/init.h @@ -5,6 +5,11 @@ #ifndef BITCOIN_INTERFACES_INIT_H #define BITCOIN_INTERFACES_INIT_H +#include +#include +#include +#include + #include namespace node { @@ -12,11 +17,7 @@ struct NodeContext; } // namespace node namespace interfaces { -class Chain; -class Echo; class Ipc; -class Node; -class WalletLoader; //! Initial interface created when a process is first started, and used to give //! and get access to other interfaces (Node, Chain, Wallet, etc). @@ -29,11 +30,11 @@ class Init { public: virtual ~Init() = default; - virtual std::unique_ptr makeNode(); - virtual std::unique_ptr makeChain(); - virtual std::unique_ptr makeWalletLoader(Chain& chain); - virtual std::unique_ptr makeEcho(); - virtual Ipc* ipc(); + virtual std::unique_ptr makeNode() { return nullptr; } + virtual std::unique_ptr makeChain() { return nullptr; } + virtual std::unique_ptr makeWalletLoader(Chain& chain) { return nullptr; } + virtual std::unique_ptr makeEcho() { return nullptr; } + virtual Ipc* ipc() { return nullptr; } }; //! Return implementation of Init interface for the node process. If the argv From dd6e8bd71c6025a51d88000caf28121ec00499db Mon Sep 17 00:00:00 2001 From: fanquake Date: Tue, 11 Oct 2022 16:34:48 +0800 Subject: [PATCH 2/3] build: remove BOOST_CPPFLAGS from libbitcoin_util --- src/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile.am b/src/Makefile.am index 63a690d49f..7cd85ce6a8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -672,7 +672,7 @@ endif # # util # -libbitcoin_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS) +libbitcoin_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_util_a_SOURCES = \ support/lockedpool.cpp \ From b19c4124b3c9a15fe3baf8792c55eb26eca51c0f Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Wed, 12 Oct 2022 10:42:38 -0400 Subject: [PATCH 3/3] refactor: Rename ambiguous interfaces::MakeHandler functions --- src/common/interfaces.cpp | 10 +++++----- src/interfaces/handler.h | 4 ++-- src/node/interfaces.cpp | 24 ++++++++++++------------ src/wallet/interfaces.cpp | 16 ++++++++-------- src/wallet/wallet.cpp | 2 +- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/common/interfaces.cpp b/src/common/interfaces.cpp index 289856871d..b9b4f5dded 100644 --- a/src/common/interfaces.cpp +++ b/src/common/interfaces.cpp @@ -20,10 +20,10 @@ public: std::function m_cleanup; }; -class HandlerImpl : public interfaces::Handler +class SignalHandler : public interfaces::Handler { public: - explicit HandlerImpl(boost::signals2::connection connection) : m_connection(std::move(connection)) {} + explicit SignalHandler(boost::signals2::connection connection) : m_connection(std::move(connection)) {} void disconnect() override { m_connection.disconnect(); } @@ -39,14 +39,14 @@ public: } // namespace common namespace interfaces { -std::unique_ptr MakeHandler(std::function cleanup) +std::unique_ptr MakeCleanupHandler(std::function cleanup) { return std::make_unique(std::move(cleanup)); } -std::unique_ptr MakeHandler(boost::signals2::connection connection) +std::unique_ptr MakeSignalHandler(boost::signals2::connection connection) { - return std::make_unique(std::move(connection)); + return std::make_unique(std::move(connection)); } std::unique_ptr MakeEcho() { return std::make_unique(); } diff --git a/src/interfaces/handler.h b/src/interfaces/handler.h index 11baf9dd65..f46f5e04a5 100644 --- a/src/interfaces/handler.h +++ b/src/interfaces/handler.h @@ -29,10 +29,10 @@ public: }; //! Return handler wrapping a boost signal connection. -std::unique_ptr MakeHandler(boost::signals2::connection connection); +std::unique_ptr MakeSignalHandler(boost::signals2::connection connection); //! Return handler wrapping a cleanup function. -std::unique_ptr MakeHandler(std::function cleanup); +std::unique_ptr MakeCleanupHandler(std::function cleanup); } // namespace interfaces diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 212780b259..f557d35b73 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -64,7 +64,7 @@ using interfaces::BlockTip; using interfaces::Chain; using interfaces::FoundBlock; using interfaces::Handler; -using interfaces::MakeHandler; +using interfaces::MakeSignalHandler; using interfaces::Node; using interfaces::WalletLoader; @@ -336,50 +336,50 @@ public: } std::unique_ptr handleInitMessage(InitMessageFn fn) override { - return MakeHandler(::uiInterface.InitMessage_connect(fn)); + return MakeSignalHandler(::uiInterface.InitMessage_connect(fn)); } std::unique_ptr handleMessageBox(MessageBoxFn fn) override { - return MakeHandler(::uiInterface.ThreadSafeMessageBox_connect(fn)); + return MakeSignalHandler(::uiInterface.ThreadSafeMessageBox_connect(fn)); } std::unique_ptr handleQuestion(QuestionFn fn) override { - return MakeHandler(::uiInterface.ThreadSafeQuestion_connect(fn)); + return MakeSignalHandler(::uiInterface.ThreadSafeQuestion_connect(fn)); } std::unique_ptr handleShowProgress(ShowProgressFn fn) override { - return MakeHandler(::uiInterface.ShowProgress_connect(fn)); + return MakeSignalHandler(::uiInterface.ShowProgress_connect(fn)); } std::unique_ptr handleInitWallet(InitWalletFn fn) override { - return MakeHandler(::uiInterface.InitWallet_connect(fn)); + return MakeSignalHandler(::uiInterface.InitWallet_connect(fn)); } std::unique_ptr handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override { - return MakeHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn)); + return MakeSignalHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn)); } std::unique_ptr handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override { - return MakeHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn)); + return MakeSignalHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn)); } std::unique_ptr handleNotifyAlertChanged(NotifyAlertChangedFn fn) override { - return MakeHandler(::uiInterface.NotifyAlertChanged_connect(fn)); + return MakeSignalHandler(::uiInterface.NotifyAlertChanged_connect(fn)); } std::unique_ptr handleBannedListChanged(BannedListChangedFn fn) override { - return MakeHandler(::uiInterface.BannedListChanged_connect(fn)); + return MakeSignalHandler(::uiInterface.BannedListChanged_connect(fn)); } std::unique_ptr handleNotifyBlockTip(NotifyBlockTipFn fn) override { - return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) { + return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) { fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()}, GuessVerificationProgress(Params().TxData(), block)); })); } std::unique_ptr handleNotifyHeaderTip(NotifyHeaderTipFn fn) override { - return MakeHandler( + return MakeSignalHandler( ::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, int64_t height, int64_t timestamp, bool presync) { fn(sync_state, BlockTip{(int)height, timestamp, uint256{}}, presync); })); diff --git a/src/wallet/interfaces.cpp b/src/wallet/interfaces.cpp index 9cf2b677e6..009dd8d55a 100644 --- a/src/wallet/interfaces.cpp +++ b/src/wallet/interfaces.cpp @@ -36,7 +36,7 @@ using interfaces::Chain; using interfaces::FoundBlock; using interfaces::Handler; -using interfaces::MakeHandler; +using interfaces::MakeSignalHandler; using interfaces::Wallet; using interfaces::WalletAddress; using interfaces::WalletBalances; @@ -486,34 +486,34 @@ public: bool isLegacy() override { return m_wallet->IsLegacy(); } std::unique_ptr handleUnload(UnloadFn fn) override { - return MakeHandler(m_wallet->NotifyUnload.connect(fn)); + return MakeSignalHandler(m_wallet->NotifyUnload.connect(fn)); } std::unique_ptr handleShowProgress(ShowProgressFn fn) override { - return MakeHandler(m_wallet->ShowProgress.connect(fn)); + return MakeSignalHandler(m_wallet->ShowProgress.connect(fn)); } std::unique_ptr handleStatusChanged(StatusChangedFn fn) override { - return MakeHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); })); + return MakeSignalHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); })); } std::unique_ptr handleAddressBookChanged(AddressBookChangedFn fn) override { - return MakeHandler(m_wallet->NotifyAddressBookChanged.connect( + return MakeSignalHandler(m_wallet->NotifyAddressBookChanged.connect( [fn](const CTxDestination& address, const std::string& label, bool is_mine, const std::string& purpose, ChangeType status) { fn(address, label, is_mine, purpose, status); })); } std::unique_ptr handleTransactionChanged(TransactionChangedFn fn) override { - return MakeHandler(m_wallet->NotifyTransactionChanged.connect( + return MakeSignalHandler(m_wallet->NotifyTransactionChanged.connect( [fn](const uint256& txid, ChangeType status) { fn(txid, status); })); } std::unique_ptr handleWatchOnlyChanged(WatchOnlyChangedFn fn) override { - return MakeHandler(m_wallet->NotifyWatchonlyChanged.connect(fn)); + return MakeSignalHandler(m_wallet->NotifyWatchonlyChanged.connect(fn)); } std::unique_ptr handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) override { - return MakeHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn)); + return MakeSignalHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn)); } CWallet* wallet() override { return m_wallet.get(); } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 431e970edc..4a7fa3d00e 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -171,7 +171,7 @@ std::unique_ptr HandleLoadWallet(WalletContext& context, Lo { LOCK(context.wallets_mutex); auto it = context.wallet_load_fns.emplace(context.wallet_load_fns.end(), std::move(load_wallet)); - return interfaces::MakeHandler([&context, it] { LOCK(context.wallets_mutex); context.wallet_load_fns.erase(it); }); + return interfaces::MakeCleanupHandler([&context, it] { LOCK(context.wallets_mutex); context.wallet_load_fns.erase(it); }); } void NotifyWalletLoaded(WalletContext& context, const std::shared_ptr& wallet)