Skip to content

Commit fe6f27e

Browse files
ryanofskyjnewbery
authored andcommitted
Remove direct bitcoin calls from qt/clientmodel.cpp
1 parent 5fba3af commit fe6f27e

File tree

9 files changed

+233
-143
lines changed

9 files changed

+233
-143
lines changed

src/interface/node.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44

55
#include <interface/node.h>
66

7+
#include <chain.h>
78
#include <chainparams.h>
89
#include <init.h>
910
#include <interface/handler.h>
1011
#include <interface/wallet.h>
1112
#include <net.h>
1213
#include <netaddress.h>
1314
#include <netbase.h>
15+
#include <primitives/block.h>
1416
#include <scheduler.h>
17+
#include <sync.h>
18+
#include <txmempool.h>
1519
#include <ui_interface.h>
1620
#include <util.h>
21+
#include <validation.h>
1722
#include <warnings.h>
1823

1924
#if defined(HAVE_CONFIG_H)
@@ -25,6 +30,7 @@
2530
#define CHECK_WALLET(x) throw std::logic_error("Wallet function called in non-wallet build.")
2631
#endif
2732

33+
#include <atomic>
2834
#include <boost/thread/thread.hpp>
2935

3036
class CWallet;
@@ -69,6 +75,56 @@ class NodeImpl : public Node
6975
}
7076
std::string helpMessage(HelpMessageMode mode) override { return HelpMessage(mode); }
7177
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
78+
size_t getNodeCount(CConnman::NumConnections flags) override
79+
{
80+
return g_connman ? g_connman->GetNodeCount(flags) : 0;
81+
}
82+
int64_t getTotalBytesRecv() override { return g_connman ? g_connman->GetTotalBytesRecv() : 0; }
83+
int64_t getTotalBytesSent() override { return g_connman ? g_connman->GetTotalBytesSent() : 0; }
84+
size_t getMempoolSize() override { return ::mempool.size(); }
85+
size_t getMempoolDynamicUsage() override { return ::mempool.DynamicMemoryUsage(); }
86+
bool getHeaderTip(int& height, int64_t& block_time) override
87+
{
88+
LOCK(::cs_main);
89+
if (::pindexBestHeader) {
90+
height = ::pindexBestHeader->nHeight;
91+
block_time = ::pindexBestHeader->GetBlockTime();
92+
return true;
93+
}
94+
return false;
95+
}
96+
int getNumBlocks() override
97+
{
98+
LOCK(::cs_main);
99+
return ::chainActive.Height();
100+
}
101+
int64_t getLastBlockTime() override
102+
{
103+
LOCK(::cs_main);
104+
if (::chainActive.Tip()) {
105+
return ::chainActive.Tip()->GetBlockTime();
106+
}
107+
return Params().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
108+
}
109+
double getVerificationProgress() override
110+
{
111+
const CBlockIndex* tip;
112+
{
113+
LOCK(::cs_main);
114+
tip = ::chainActive.Tip();
115+
}
116+
return GuessVerificationProgress(::Params().TxData(), tip);
117+
}
118+
bool isInitialBlockDownload() override { return IsInitialBlockDownload(); }
119+
bool getReindex() override { return ::fReindex; }
120+
bool getImporting() override { return ::fImporting; }
121+
void setNetworkActive(bool active) override
122+
{
123+
if (g_connman) {
124+
g_connman->SetNetworkActive(active);
125+
}
126+
}
127+
bool getNetworkActive() override { return g_connman && g_connman->GetNetworkActive(); }
72128
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
73129
{
74130
return MakeHandler(::uiInterface.InitMessage.connect(fn));
@@ -90,6 +146,37 @@ class NodeImpl : public Node
90146
CHECK_WALLET(
91147
return MakeHandler(::uiInterface.LoadWallet.connect([fn](CWallet* wallet) { fn(MakeWallet(*wallet)); })));
92148
}
149+
std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
150+
{
151+
return MakeHandler(::uiInterface.NotifyNumConnectionsChanged.connect(fn));
152+
}
153+
std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override
154+
{
155+
return MakeHandler(::uiInterface.NotifyNetworkActiveChanged.connect(fn));
156+
}
157+
std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override
158+
{
159+
return MakeHandler(::uiInterface.NotifyAlertChanged.connect(fn));
160+
}
161+
std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override
162+
{
163+
return MakeHandler(::uiInterface.BannedListChanged.connect(fn));
164+
}
165+
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
166+
{
167+
return MakeHandler(::uiInterface.NotifyBlockTip.connect([fn](bool initial_download, const CBlockIndex* block) {
168+
fn(initial_download, block->nHeight, block->GetBlockTime(),
169+
GuessVerificationProgress(::Params().TxData(), block));
170+
}));
171+
}
172+
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
173+
{
174+
return MakeHandler(
175+
::uiInterface.NotifyHeaderTip.connect([fn](bool initial_download, const CBlockIndex* block) {
176+
fn(initial_download, block->nHeight, block->GetBlockTime(),
177+
GuessVerificationProgress(::Params().TxData(), block));
178+
}));
179+
}
93180
};
94181

95182
} // namespace

src/interface/node.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
#define BITCOIN_INTERFACE_NODE_H
77

88
#include <init.h> // For HelpMessageMode
9+
#include <net.h> // For CConnman::NumConnections
910
#include <netaddress.h> // For Network
1011

1112
#include <functional>
1213
#include <memory>
14+
#include <stddef.h>
15+
#include <stdint.h>
1316
#include <string>
1417

1518
class proxyType;
@@ -73,6 +76,48 @@ class Node
7376
//! Get proxy.
7477
virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
7578

79+
//! Get number of connections.
80+
virtual size_t getNodeCount(CConnman::NumConnections flags) = 0;
81+
82+
//! Get total bytes recv.
83+
virtual int64_t getTotalBytesRecv() = 0;
84+
85+
//! Get total bytes sent.
86+
virtual int64_t getTotalBytesSent() = 0;
87+
88+
//! Get mempool size.
89+
virtual size_t getMempoolSize() = 0;
90+
91+
//! Get mempool dynamic usage.
92+
virtual size_t getMempoolDynamicUsage() = 0;
93+
94+
//! Get header tip height and time.
95+
virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;
96+
97+
//! Get num blocks.
98+
virtual int getNumBlocks() = 0;
99+
100+
//! Get last block time.
101+
virtual int64_t getLastBlockTime() = 0;
102+
103+
//! Get verification progress.
104+
virtual double getVerificationProgress() = 0;
105+
106+
//! Is initial block download.
107+
virtual bool isInitialBlockDownload() = 0;
108+
109+
//! Get reindex.
110+
virtual bool getReindex() = 0;
111+
112+
//! Get importing.
113+
virtual bool getImporting() = 0;
114+
115+
//! Set network active.
116+
virtual void setNetworkActive(bool active) = 0;
117+
118+
//! Get network active.
119+
virtual bool getNetworkActive() = 0;
120+
76121
//! Register handler for init messages.
77122
using InitMessageFn = std::function<void(const std::string& message)>;
78123
virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
@@ -96,6 +141,32 @@ class Node
96141
//! Register handler for load wallet messages.
97142
using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
98143
virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
144+
145+
//! Register handler for number of connections changed messages.
146+
using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
147+
virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
148+
149+
//! Register handler for network active messages.
150+
using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
151+
virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;
152+
153+
//! Register handler for notify alert messages.
154+
using NotifyAlertChangedFn = std::function<void()>;
155+
virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;
156+
157+
//! Register handler for ban list messages.
158+
using BannedListChangedFn = std::function<void()>;
159+
virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;
160+
161+
//! Register handler for block tip messages.
162+
using NotifyBlockTipFn =
163+
std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>;
164+
virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
165+
166+
//! Register handler for header tip messages.
167+
using NotifyHeaderTipFn =
168+
std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>;
169+
virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
99170
};
100171

101172
//! Return implementation of Node interface.

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ void BitcoinApplication::initializeResult(bool success)
460460
paymentServer->setOptionsModel(optionsModel);
461461
#endif
462462

463-
clientModel = new ClientModel(optionsModel);
463+
clientModel = new ClientModel(m_node, optionsModel);
464464
window->setClientModel(clientModel);
465465

466466
#ifdef ENABLE_WALLET

src/qt/bitcoingui.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
493493
connect(_clientModel, SIGNAL(networkActiveChanged(bool)), this, SLOT(setNetworkActive(bool)));
494494

495495
modalOverlay->setKnownBestHeight(_clientModel->getHeaderTipHeight(), QDateTime::fromTime_t(_clientModel->getHeaderTipTime()));
496-
setNumBlocks(_clientModel->getNumBlocks(), _clientModel->getLastBlockDate(), _clientModel->getVerificationProgress(nullptr), false);
496+
setNumBlocks(m_node.getNumBlocks(), QDateTime::fromTime_t(m_node.getLastBlockTime()), m_node.getVerificationProgress(), false);
497497
connect(_clientModel, SIGNAL(numBlocksChanged(int,QDateTime,double,bool)), this, SLOT(setNumBlocks(int,QDateTime,double,bool)));
498498

499499
// Receive and report messages from client model
@@ -751,7 +751,7 @@ void BitcoinGUI::updateNetworkState()
751751

752752
QString tooltip;
753753

754-
if (clientModel->getNetworkActive()) {
754+
if (m_node.getNetworkActive()) {
755755
tooltip = tr("%n active connection(s) to Bitcoin network", "", count) + QString(".<br>") + tr("Click to disable network activity.");
756756
} else {
757757
tooltip = tr("Network activity disabled.") + QString("<br>") + tr("Click to enable network activity again.");
@@ -1230,9 +1230,7 @@ void BitcoinGUI::unsubscribeFromCoreSignals()
12301230

12311231
void BitcoinGUI::toggleNetworkActive()
12321232
{
1233-
if (clientModel) {
1234-
clientModel->setNetworkActive(!clientModel->getNetworkActive());
1235-
}
1233+
m_node.setNetworkActive(!m_node.getNetworkActive());
12361234
}
12371235

12381236
UnitDisplayStatusBarControl::UnitDisplayStatusBarControl(const PlatformStyle *platformStyle) :

0 commit comments

Comments
 (0)