Skip to content

Commit 12ca448

Browse files
committed
Start using init makeNode, makeChain, etc methods
Use interfaces::Init::make* methods instead of interfaces::Make* functions, so interfaces can be constructed differently in different executables without having to change any code. (So for example bitcoin-gui can make an interfaces::Node pointer that communicates with a bitcoin-node subprocess, while bitcoin-qt can make an interfaces::Node pointer that starts node code in the same process.)
1 parent 1704bbf commit 12ca448

File tree

15 files changed

+61
-21
lines changed

15 files changed

+61
-21
lines changed

src/Makefile.qt.include

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,14 @@ bitcoin_qt_libtoolflags = $(AM_LIBTOOLFLAGS) --tag CXX
335335

336336
qt_bitcoin_qt_CPPFLAGS = $(bitcoin_qt_cppflags)
337337
qt_bitcoin_qt_CXXFLAGS = $(bitcoin_qt_cxxflags)
338-
qt_bitcoin_qt_SOURCES = $(bitcoin_qt_sources)
338+
qt_bitcoin_qt_SOURCES = $(bitcoin_qt_sources) init/bitcoind.cpp
339339
qt_bitcoin_qt_LDADD = $(bitcoin_qt_ldadd)
340340
qt_bitcoin_qt_LDFLAGS = $(bitcoin_qt_ldflags)
341341
qt_bitcoin_qt_LIBTOOLFLAGS = $(bitcoin_qt_libtoolflags)
342342

343343
bitcoin_gui_CPPFLAGS = $(bitcoin_qt_cppflags)
344344
bitcoin_gui_CXXFLAGS = $(bitcoin_qt_cxxflags)
345-
bitcoin_gui_SOURCES = $(bitcoin_qt_sources)
345+
bitcoin_gui_SOURCES = $(bitcoin_qt_sources) init/bitcoind.cpp
346346
bitcoin_gui_LDADD = $(bitcoin_qt_ldadd)
347347
bitcoin_gui_LDFLAGS = $(bitcoin_qt_ldflags)
348348
bitcoin_gui_LIBTOOLFLAGS = $(bitcoin_qt_libtoolflags)

src/Makefile.qttest.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ qt_test_test_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_
2828
$(QT_INCLUDES) $(QT_TEST_INCLUDES)
2929

3030
qt_test_test_bitcoin_qt_SOURCES = \
31+
init/bitcoind.cpp \
3132
qt/test/apptests.cpp \
3233
qt/test/rpcnestedtests.cpp \
3334
qt/test/test_main.cpp \

src/dummywallet.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
#include <util/system.h>
66
#include <walletinitinterface.h>
77

8+
class ArgsManager;
89
class CWallet;
910

1011
namespace interfaces {
1112
class Chain;
1213
class Handler;
1314
class Wallet;
15+
class WalletClient;
1416
}
1517

1618
class DummyWalletInit : public WalletInitInterface {
@@ -63,4 +65,9 @@ std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet)
6365
throw std::logic_error("Wallet function called in non-wallet build.");
6466
}
6567

68+
std::unique_ptr<WalletClient> MakeWalletClient(Chain& chain, ArgsManager& args)
69+
{
70+
throw std::logic_error("Wallet function called in non-wallet build.");
71+
}
72+
6673
} // namespace interfaces

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <index/txindex.h>
2626
#include <init/common.h>
2727
#include <interfaces/chain.h>
28+
#include <interfaces/init.h>
2829
#include <interfaces/node.h>
2930
#include <mapport.h>
3031
#include <miner.h>
@@ -1054,7 +1055,7 @@ bool AppInitLockDataDirectory()
10541055

10551056
bool AppInitInterfaces(NodeContext& node)
10561057
{
1057-
node.chain = interfaces::MakeChain(node);
1058+
node.chain = node.init->makeChain();
10581059
// Create client interfaces for wallets that are supposed to be loaded
10591060
// according to -wallet and -disablewallet options. This only constructs
10601061
// the interfaces, it doesn't load wallet data. Wallets actually get loaded

src/init/bitcoin-node.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <interfaces/chain.h>
56
#include <interfaces/echo.h>
67
#include <interfaces/init.h>
78
#include <interfaces/ipc.h>
9+
#include <interfaces/node.h>
10+
#include <interfaces/wallet.h>
811
#include <node/context.h>
912

1013
#include <memory>
@@ -22,6 +25,12 @@ class BitcoinNodeInit : public interfaces::Init
2225
{
2326
m_node.init = this;
2427
}
28+
std::unique_ptr<interfaces::Node> makeNode() override { return interfaces::MakeNode(m_node); }
29+
std::unique_ptr<interfaces::Chain> makeChain() override { return interfaces::MakeChain(m_node); }
30+
std::unique_ptr<interfaces::WalletClient> makeWalletClient(interfaces::Chain& chain) override
31+
{
32+
return MakeWalletClient(chain, *Assert(m_node.args));
33+
}
2534
std::unique_ptr<interfaces::Echo> makeEcho() override { return interfaces::MakeEcho(); }
2635
interfaces::Ipc* ipc() override { return m_ipc.get(); }
2736
NodeContext& m_node;

src/init/bitcoind.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <interfaces/chain.h>
6+
#include <interfaces/echo.h>
57
#include <interfaces/init.h>
8+
#include <interfaces/node.h>
9+
#include <interfaces/wallet.h>
610
#include <node/context.h>
711

812
#include <memory>
@@ -16,6 +20,13 @@ class BitcoindInit : public interfaces::Init
1620
{
1721
m_node.init = this;
1822
}
23+
std::unique_ptr<interfaces::Node> makeNode() override { return interfaces::MakeNode(m_node); }
24+
std::unique_ptr<interfaces::Chain> makeChain() override { return interfaces::MakeChain(m_node); }
25+
std::unique_ptr<interfaces::WalletClient> makeWalletClient(interfaces::Chain& chain) override
26+
{
27+
return MakeWalletClient(chain, *Assert(m_node.args));
28+
}
29+
std::unique_ptr<interfaces::Echo> makeEcho() override { return interfaces::MakeEcho(); }
1930
NodeContext& m_node;
2031
};
2132
} // namespace

src/interfaces/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class Node
232232
};
233233

234234
//! Return implementation of Node interface.
235-
std::unique_ptr<Node> MakeNode(NodeContext* context = nullptr);
235+
std::unique_ptr<Node> MakeNode(NodeContext& context);
236236

237237
//! Block tip (could be a header or not, depends on the subscribed signal).
238238
struct BlockTip {

src/node/interfaces.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class NodeImpl : public Node
7070
private:
7171
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
7272
public:
73-
explicit NodeImpl(NodeContext* context) { setContext(context); }
73+
explicit NodeImpl(NodeContext& context) { setContext(&context); }
7474
void initLogging() override { InitLogging(*Assert(m_context->args)); }
7575
void initParameterInteraction() override { InitParameterInteraction(*Assert(m_context->args)); }
7676
bilingual_str getWarnings() override { return GetWarnings(true); }
@@ -725,6 +725,6 @@ class ChainImpl : public Chain
725725
} // namespace node
726726

727727
namespace interfaces {
728-
std::unique_ptr<Node> MakeNode(NodeContext* context) { return std::make_unique<node::NodeImpl>(context); }
728+
std::unique_ptr<Node> MakeNode(NodeContext& context) { return std::make_unique<node::NodeImpl>(context); }
729729
std::unique_ptr<Chain> MakeChain(NodeContext& context) { return std::make_unique<node::ChainImpl>(context); }
730730
} // namespace interfaces

src/qt/bitcoin.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <init.h>
3131
#include <interfaces/handler.h>
32+
#include <interfaces/init.h>
3233
#include <interfaces/node.h>
3334
#include <node/context.h>
3435
#include <node/ui_interface.h>
@@ -276,10 +277,10 @@ void BitcoinApplication::createSplashScreen(const NetworkStyle *networkStyle)
276277
connect(this, &BitcoinApplication::requestedShutdown, m_splash, &QWidget::close);
277278
}
278279

279-
void BitcoinApplication::setNode(interfaces::Node& node)
280+
void BitcoinApplication::createNode(interfaces::Init& init)
280281
{
281282
assert(!m_node);
282-
m_node = &node;
283+
m_node = init.makeNode();
283284
if (optionsModel) optionsModel->setNode(*m_node);
284285
if (m_splash) m_splash->setNode(*m_node);
285286
}
@@ -458,11 +459,13 @@ int GuiMain(int argc, char* argv[])
458459
util::WinCmdLineArgs winArgs;
459460
std::tie(argc, argv) = winArgs.get();
460461
#endif
461-
SetupEnvironment();
462-
util::ThreadSetInternalName("main");
463462

464463
NodeContext node_context;
465-
std::unique_ptr<interfaces::Node> node = interfaces::MakeNode(&node_context);
464+
int unused_exit_status;
465+
std::unique_ptr<interfaces::Init> init = interfaces::MakeNodeInit(node_context, argc, argv, unused_exit_status);
466+
467+
SetupEnvironment();
468+
util::ThreadSetInternalName("main");
466469

467470
// Subscribe to global signals from core
468471
boost::signals2::scoped_connection handler_message_box = ::uiInterface.ThreadSafeMessageBox_connect(noui_ThreadSafeMessageBox);
@@ -621,7 +624,7 @@ int GuiMain(int argc, char* argv[])
621624
if (gArgs.GetBoolArg("-splash", DEFAULT_SPLASHSCREEN) && !gArgs.GetBoolArg("-min", false))
622625
app.createSplashScreen(networkStyle.data());
623626

624-
app.setNode(*node);
627+
app.createNode(*init);
625628

626629
int rv = EXIT_SUCCESS;
627630
try

src/qt/bitcoin.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class PlatformStyle;
2424
class SplashScreen;
2525
class WalletController;
2626
class WalletModel;
27+
namespace interfaces {
28+
class Init;
29+
} // namespace interfaces
2730

2831

2932
/** Class encapsulating Bitcoin Core startup and shutdown.
@@ -73,6 +76,8 @@ class BitcoinApplication: public QApplication
7376
void createWindow(const NetworkStyle *networkStyle);
7477
/// Create splash screen
7578
void createSplashScreen(const NetworkStyle *networkStyle);
79+
/// Create or spawn node
80+
void createNode(interfaces::Init& init);
7681
/// Basic initialization, before starting initialization/shutdown thread. Return true on success.
7782
bool baseInitialize();
7883

@@ -91,7 +96,6 @@ class BitcoinApplication: public QApplication
9196
void setupPlatformStyle();
9297

9398
interfaces::Node& node() const { assert(m_node); return *m_node; }
94-
void setNode(interfaces::Node& node);
9599

96100
public Q_SLOTS:
97101
void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info);
@@ -125,7 +129,7 @@ public Q_SLOTS:
125129
const PlatformStyle *platformStyle;
126130
std::unique_ptr<QWidget> shutdownWindow;
127131
SplashScreen* m_splash = nullptr;
128-
interfaces::Node* m_node = nullptr;
132+
std::unique_ptr<interfaces::Node> m_node;
129133

130134
void startThread();
131135
};

0 commit comments

Comments
 (0)