Skip to content

Commit 7d51560

Browse files
committed
Merge bitcoin/bitcoin#26298: refactor: Move src/interfaces/*.cpp files to libbitcoin_common.a
b19c412 refactor: Rename ambiguous interfaces::MakeHandler functions (Ryan Ofsky) dd6e8bd build: remove BOOST_CPPFLAGS from libbitcoin_util (fanquake) 82e272a refactor: Move src/interfaces/*.cpp files to libbitcoin_common.a (Ryan Ofsky) Pull request description: These belong in `libbitcoin_common.a`, not `libbitcoin_util.a`, because they aren't general-purpose utilities, they just contain some common glue code that is used by both the node and the wallet. Another reason not to include these in `libbitcoin_util.a` is to prevent them from being used by the kernel library. Also rename ambiguous `MakeHandler` functions to `MakeCleanupHandler` and `MakeSignalHandler`. Cleanup function handler was introduced after boost signals handler, so original naming didn't make much sense. This just contains a move-only commit, and a rename commit. There are no actual code or behavior changes. This PR is an alternative to #26293, and solves the same issue of removing a boost dependency from the _util_ library. The advantages of this PR compared to #26293 are that it keeps the source directory structure more flat, and it avoids having to change #includes all over the codebase. ACKs for top commit: hebasto: ACK b19c412 Tree-SHA512: b3a1d33eedceda7ad852c6d6f35700159d156d96071e59acae2bc325467fef81476f860a8855ea39cf3ea706a1df2a341f34fb2dcb032c31a3b0e9cf14103b6a
2 parents 272fb0a + b19c412 commit 7d51560

File tree

10 files changed

+88
-116
lines changed

10 files changed

+88
-116
lines changed

src/Makefile.am

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ libbitcoin_common_a_SOURCES = \
633633
chainparams.cpp \
634634
coins.cpp \
635635
common/bloom.cpp \
636+
common/interfaces.cpp \
636637
common/run_command.cpp \
637638
compressor.cpp \
638639
core_read.cpp \
@@ -671,16 +672,13 @@ endif
671672
#
672673

673674
# util #
674-
libbitcoin_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
675+
libbitcoin_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
675676
libbitcoin_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
676677
libbitcoin_util_a_SOURCES = \
677678
support/lockedpool.cpp \
678679
chainparamsbase.cpp \
679680
clientversion.cpp \
680681
fs.cpp \
681-
interfaces/echo.cpp \
682-
interfaces/handler.cpp \
683-
interfaces/init.cpp \
684682
logging.cpp \
685683
random.cpp \
686684
randomenv.cpp \

src/common/interfaces.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <interfaces/echo.h>
6+
#include <interfaces/handler.h>
7+
8+
#include <boost/signals2/connection.hpp>
9+
#include <memory>
10+
#include <utility>
11+
12+
namespace common {
13+
namespace {
14+
class CleanupHandler : public interfaces::Handler
15+
{
16+
public:
17+
explicit CleanupHandler(std::function<void()> cleanup) : m_cleanup(std::move(cleanup)) {}
18+
~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
19+
void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
20+
std::function<void()> m_cleanup;
21+
};
22+
23+
class SignalHandler : public interfaces::Handler
24+
{
25+
public:
26+
explicit SignalHandler(boost::signals2::connection connection) : m_connection(std::move(connection)) {}
27+
28+
void disconnect() override { m_connection.disconnect(); }
29+
30+
boost::signals2::scoped_connection m_connection;
31+
};
32+
33+
class EchoImpl : public interfaces::Echo
34+
{
35+
public:
36+
std::string echo(const std::string& echo) override { return echo; }
37+
};
38+
} // namespace
39+
} // namespace common
40+
41+
namespace interfaces {
42+
std::unique_ptr<Handler> MakeCleanupHandler(std::function<void()> cleanup)
43+
{
44+
return std::make_unique<common::CleanupHandler>(std::move(cleanup));
45+
}
46+
47+
std::unique_ptr<Handler> MakeSignalHandler(boost::signals2::connection connection)
48+
{
49+
return std::make_unique<common::SignalHandler>(std::move(connection));
50+
}
51+
52+
std::unique_ptr<Echo> MakeEcho() { return std::make_unique<common::EchoImpl>(); }
53+
} // namespace interfaces

src/interfaces/echo.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/interfaces/handler.cpp

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/interfaces/handler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class Handler
2929
};
3030

3131
//! Return handler wrapping a boost signal connection.
32-
std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection);
32+
std::unique_ptr<Handler> MakeSignalHandler(boost::signals2::connection connection);
3333

3434
//! Return handler wrapping a cleanup function.
35-
std::unique_ptr<Handler> MakeHandler(std::function<void()> cleanup);
35+
std::unique_ptr<Handler> MakeCleanupHandler(std::function<void()> cleanup);
3636

3737
} // namespace interfaces
3838

src/interfaces/init.cpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/interfaces/init.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
#ifndef BITCOIN_INTERFACES_INIT_H
66
#define BITCOIN_INTERFACES_INIT_H
77

8+
#include <interfaces/chain.h>
9+
#include <interfaces/echo.h>
10+
#include <interfaces/node.h>
11+
#include <interfaces/wallet.h>
12+
813
#include <memory>
914

1015
namespace node {
1116
struct NodeContext;
1217
} // namespace node
1318

1419
namespace interfaces {
15-
class Chain;
16-
class Echo;
1720
class Ipc;
18-
class Node;
19-
class WalletLoader;
2021

2122
//! Initial interface created when a process is first started, and used to give
2223
//! and get access to other interfaces (Node, Chain, Wallet, etc).
@@ -29,11 +30,11 @@ class Init
2930
{
3031
public:
3132
virtual ~Init() = default;
32-
virtual std::unique_ptr<Node> makeNode();
33-
virtual std::unique_ptr<Chain> makeChain();
34-
virtual std::unique_ptr<WalletLoader> makeWalletLoader(Chain& chain);
35-
virtual std::unique_ptr<Echo> makeEcho();
36-
virtual Ipc* ipc();
33+
virtual std::unique_ptr<Node> makeNode() { return nullptr; }
34+
virtual std::unique_ptr<Chain> makeChain() { return nullptr; }
35+
virtual std::unique_ptr<WalletLoader> makeWalletLoader(Chain& chain) { return nullptr; }
36+
virtual std::unique_ptr<Echo> makeEcho() { return nullptr; }
37+
virtual Ipc* ipc() { return nullptr; }
3738
};
3839

3940
//! Return implementation of Init interface for the node process. If the argv

src/node/interfaces.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ using interfaces::BlockTip;
6464
using interfaces::Chain;
6565
using interfaces::FoundBlock;
6666
using interfaces::Handler;
67-
using interfaces::MakeHandler;
67+
using interfaces::MakeSignalHandler;
6868
using interfaces::Node;
6969
using interfaces::WalletLoader;
7070

@@ -336,50 +336,50 @@ class NodeImpl : public Node
336336
}
337337
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
338338
{
339-
return MakeHandler(::uiInterface.InitMessage_connect(fn));
339+
return MakeSignalHandler(::uiInterface.InitMessage_connect(fn));
340340
}
341341
std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) override
342342
{
343-
return MakeHandler(::uiInterface.ThreadSafeMessageBox_connect(fn));
343+
return MakeSignalHandler(::uiInterface.ThreadSafeMessageBox_connect(fn));
344344
}
345345
std::unique_ptr<Handler> handleQuestion(QuestionFn fn) override
346346
{
347-
return MakeHandler(::uiInterface.ThreadSafeQuestion_connect(fn));
347+
return MakeSignalHandler(::uiInterface.ThreadSafeQuestion_connect(fn));
348348
}
349349
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
350350
{
351-
return MakeHandler(::uiInterface.ShowProgress_connect(fn));
351+
return MakeSignalHandler(::uiInterface.ShowProgress_connect(fn));
352352
}
353353
std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) override
354354
{
355-
return MakeHandler(::uiInterface.InitWallet_connect(fn));
355+
return MakeSignalHandler(::uiInterface.InitWallet_connect(fn));
356356
}
357357
std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
358358
{
359-
return MakeHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn));
359+
return MakeSignalHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn));
360360
}
361361
std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override
362362
{
363-
return MakeHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn));
363+
return MakeSignalHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn));
364364
}
365365
std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override
366366
{
367-
return MakeHandler(::uiInterface.NotifyAlertChanged_connect(fn));
367+
return MakeSignalHandler(::uiInterface.NotifyAlertChanged_connect(fn));
368368
}
369369
std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override
370370
{
371-
return MakeHandler(::uiInterface.BannedListChanged_connect(fn));
371+
return MakeSignalHandler(::uiInterface.BannedListChanged_connect(fn));
372372
}
373373
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
374374
{
375-
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
375+
return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
376376
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
377377
GuessVerificationProgress(Params().TxData(), block));
378378
}));
379379
}
380380
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
381381
{
382-
return MakeHandler(
382+
return MakeSignalHandler(
383383
::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, int64_t height, int64_t timestamp, bool presync) {
384384
fn(sync_state, BlockTip{(int)height, timestamp, uint256{}}, presync);
385385
}));

src/wallet/interfaces.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
using interfaces::Chain;
3737
using interfaces::FoundBlock;
3838
using interfaces::Handler;
39-
using interfaces::MakeHandler;
39+
using interfaces::MakeSignalHandler;
4040
using interfaces::Wallet;
4141
using interfaces::WalletAddress;
4242
using interfaces::WalletBalances;
@@ -486,34 +486,34 @@ class WalletImpl : public Wallet
486486
bool isLegacy() override { return m_wallet->IsLegacy(); }
487487
std::unique_ptr<Handler> handleUnload(UnloadFn fn) override
488488
{
489-
return MakeHandler(m_wallet->NotifyUnload.connect(fn));
489+
return MakeSignalHandler(m_wallet->NotifyUnload.connect(fn));
490490
}
491491
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
492492
{
493-
return MakeHandler(m_wallet->ShowProgress.connect(fn));
493+
return MakeSignalHandler(m_wallet->ShowProgress.connect(fn));
494494
}
495495
std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) override
496496
{
497-
return MakeHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); }));
497+
return MakeSignalHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); }));
498498
}
499499
std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) override
500500
{
501-
return MakeHandler(m_wallet->NotifyAddressBookChanged.connect(
501+
return MakeSignalHandler(m_wallet->NotifyAddressBookChanged.connect(
502502
[fn](const CTxDestination& address, const std::string& label, bool is_mine,
503503
const std::string& purpose, ChangeType status) { fn(address, label, is_mine, purpose, status); }));
504504
}
505505
std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) override
506506
{
507-
return MakeHandler(m_wallet->NotifyTransactionChanged.connect(
507+
return MakeSignalHandler(m_wallet->NotifyTransactionChanged.connect(
508508
[fn](const uint256& txid, ChangeType status) { fn(txid, status); }));
509509
}
510510
std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) override
511511
{
512-
return MakeHandler(m_wallet->NotifyWatchonlyChanged.connect(fn));
512+
return MakeSignalHandler(m_wallet->NotifyWatchonlyChanged.connect(fn));
513513
}
514514
std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) override
515515
{
516-
return MakeHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn));
516+
return MakeSignalHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn));
517517
}
518518
CWallet* wallet() override { return m_wallet.get(); }
519519

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ std::unique_ptr<interfaces::Handler> HandleLoadWallet(WalletContext& context, Lo
171171
{
172172
LOCK(context.wallets_mutex);
173173
auto it = context.wallet_load_fns.emplace(context.wallet_load_fns.end(), std::move(load_wallet));
174-
return interfaces::MakeHandler([&context, it] { LOCK(context.wallets_mutex); context.wallet_load_fns.erase(it); });
174+
return interfaces::MakeCleanupHandler([&context, it] { LOCK(context.wallets_mutex); context.wallet_load_fns.erase(it); });
175175
}
176176

177177
void NotifyWalletLoaded(WalletContext& context, const std::shared_ptr<CWallet>& wallet)

0 commit comments

Comments
 (0)