Skip to content

Commit 71e0d90

Browse files
ryanofskyjnewbery
authored andcommitted
Remove direct bitcoin calls from qt/bitcoin.cpp
1 parent ea73b84 commit 71e0d90

File tree

6 files changed

+215
-47
lines changed

6 files changed

+215
-47
lines changed

src/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ BITCOIN_CORE_H = \
104104
httpserver.h \
105105
indirectmap.h \
106106
init.h \
107+
interface/handler.h \
108+
interface/node.h \
107109
key.h \
108110
key_io.h \
109111
keystore.h \
@@ -357,6 +359,8 @@ libbitcoin_util_a_SOURCES = \
357359
compat/glibcxx_sanity.cpp \
358360
compat/strnlen.cpp \
359361
fs.cpp \
362+
interface/handler.cpp \
363+
interface/node.cpp \
360364
random.cpp \
361365
rpc/protocol.cpp \
362366
rpc/util.cpp \

src/interface/handler.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2018 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 <interface/handler.h>
6+
7+
#include <util.h>
8+
9+
#include <boost/signals2/connection.hpp>
10+
#include <memory>
11+
#include <utility>
12+
13+
namespace interface {
14+
namespace {
15+
16+
class HandlerImpl : public Handler
17+
{
18+
public:
19+
HandlerImpl(boost::signals2::connection connection) : m_connection(std::move(connection)) {}
20+
21+
void disconnect() override { m_connection.disconnect(); }
22+
23+
boost::signals2::scoped_connection m_connection;
24+
};
25+
26+
} // namespace
27+
28+
std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection)
29+
{
30+
return MakeUnique<HandlerImpl>(std::move(connection));
31+
}
32+
33+
} // namespace interface

src/interface/handler.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2018 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+
#ifndef BITCOIN_INTERFACE_HANDLER_H
6+
#define BITCOIN_INTERFACE_HANDLER_H
7+
8+
#include <memory>
9+
10+
namespace boost {
11+
namespace signals2 {
12+
class connection;
13+
} // namespace signals2
14+
} // namespace boost
15+
16+
namespace interface {
17+
18+
//! Generic interface for managing an event handler or callback function
19+
//! registered with another interface. Has a single disconnect method to cancel
20+
//! the registration and prevent any future notifications.
21+
class Handler
22+
{
23+
public:
24+
virtual ~Handler() {}
25+
26+
//! Disconnect the handler.
27+
virtual void disconnect() = 0;
28+
};
29+
30+
//! Return handler wrapping a boost signal connection.
31+
std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection);
32+
33+
} // namespace interface
34+
35+
#endif // BITCOIN_INTERFACE_HANDLER_H

src/interface/node.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2018 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 <interface/node.h>
6+
7+
#include <chainparams.h>
8+
#include <init.h>
9+
#include <interface/handler.h>
10+
#include <scheduler.h>
11+
#include <ui_interface.h>
12+
#include <util.h>
13+
#include <warnings.h>
14+
15+
#include <boost/thread/thread.hpp>
16+
17+
namespace interface {
18+
namespace {
19+
20+
class NodeImpl : public Node
21+
{
22+
void parseParameters(int argc, const char* const argv[]) override
23+
{
24+
gArgs.ParseParameters(argc, argv);
25+
}
26+
void readConfigFile(const std::string& conf_path) override { gArgs.ReadConfigFile(conf_path); }
27+
void selectParams(const std::string& network) override { SelectParams(network); }
28+
void initLogging() override { InitLogging(); }
29+
void initParameterInteraction() override { InitParameterInteraction(); }
30+
std::string getWarnings(const std::string& type) override { return GetWarnings(type); }
31+
bool baseInitialize() override
32+
{
33+
return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() &&
34+
AppInitLockDataDirectory();
35+
}
36+
bool appInitMain() override { return AppInitMain(); }
37+
void appShutdown() override
38+
{
39+
Interrupt();
40+
Shutdown();
41+
}
42+
void startShutdown() override { StartShutdown(); }
43+
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
44+
{
45+
return MakeHandler(::uiInterface.InitMessage.connect(fn));
46+
}
47+
};
48+
49+
} // namespace
50+
51+
std::unique_ptr<Node> MakeNode() { return MakeUnique<NodeImpl>(); }
52+
53+
} // namespace interface

src/interface/node.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2018 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+
#ifndef BITCOIN_INTERFACE_NODE_H
6+
#define BITCOIN_INTERFACE_NODE_H
7+
8+
#include <functional>
9+
#include <memory>
10+
#include <string>
11+
12+
namespace interface {
13+
14+
class Handler;
15+
16+
//! Top-level interface for a bitcoin node (bitcoind process).
17+
class Node
18+
{
19+
public:
20+
virtual ~Node() {}
21+
22+
//! Set command line arguments.
23+
virtual void parseParameters(int argc, const char* const argv[]) = 0;
24+
25+
//! Load settings from configuration file.
26+
virtual void readConfigFile(const std::string& conf_path) = 0;
27+
28+
//! Choose network parameters.
29+
virtual void selectParams(const std::string& network) = 0;
30+
31+
//! Init logging.
32+
virtual void initLogging() = 0;
33+
34+
//! Init parameter interaction.
35+
virtual void initParameterInteraction() = 0;
36+
37+
//! Get warnings.
38+
virtual std::string getWarnings(const std::string& type) = 0;
39+
40+
//! Initialize app dependencies.
41+
virtual bool baseInitialize() = 0;
42+
43+
//! Start node.
44+
virtual bool appInitMain() = 0;
45+
46+
//! Stop node.
47+
virtual void appShutdown() = 0;
48+
49+
//! Start shutdown.
50+
virtual void startShutdown() = 0;
51+
52+
//! Register handler for init messages.
53+
using InitMessageFn = std::function<void(const std::string& message)>;
54+
virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
55+
};
56+
57+
//! Return implementation of Node interface.
58+
std::unique_ptr<Node> MakeNode();
59+
60+
} // namespace interface
61+
62+
#endif // BITCOIN_INTERFACE_NODE_H

0 commit comments

Comments
 (0)