Skip to content

Commit

Permalink
Remove direct bitcoin calls from qt/clientmodel.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanofsky authored and jnewbery committed Apr 4, 2018
1 parent 5fba3af commit fe6f27e
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 143 deletions.
87 changes: 87 additions & 0 deletions src/interface/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

#include <interface/node.h>

#include <chain.h>
#include <chainparams.h>
#include <init.h>
#include <interface/handler.h>
#include <interface/wallet.h>
#include <net.h>
#include <netaddress.h>
#include <netbase.h>
#include <primitives/block.h>
#include <scheduler.h>
#include <sync.h>
#include <txmempool.h>
#include <ui_interface.h>
#include <util.h>
#include <validation.h>
#include <warnings.h>

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

#include <atomic>
#include <boost/thread/thread.hpp>

class CWallet;
Expand Down Expand Up @@ -69,6 +75,56 @@ class NodeImpl : public Node
}
std::string helpMessage(HelpMessageMode mode) override { return HelpMessage(mode); }
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
size_t getNodeCount(CConnman::NumConnections flags) override
{
return g_connman ? g_connman->GetNodeCount(flags) : 0;
}
int64_t getTotalBytesRecv() override { return g_connman ? g_connman->GetTotalBytesRecv() : 0; }
int64_t getTotalBytesSent() override { return g_connman ? g_connman->GetTotalBytesSent() : 0; }
size_t getMempoolSize() override { return ::mempool.size(); }
size_t getMempoolDynamicUsage() override { return ::mempool.DynamicMemoryUsage(); }
bool getHeaderTip(int& height, int64_t& block_time) override
{
LOCK(::cs_main);
if (::pindexBestHeader) {
height = ::pindexBestHeader->nHeight;
block_time = ::pindexBestHeader->GetBlockTime();
return true;
}
return false;
}
int getNumBlocks() override
{
LOCK(::cs_main);
return ::chainActive.Height();
}
int64_t getLastBlockTime() override
{
LOCK(::cs_main);
if (::chainActive.Tip()) {
return ::chainActive.Tip()->GetBlockTime();
}
return Params().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
}
double getVerificationProgress() override
{
const CBlockIndex* tip;
{
LOCK(::cs_main);
tip = ::chainActive.Tip();
}
return GuessVerificationProgress(::Params().TxData(), tip);
}
bool isInitialBlockDownload() override { return IsInitialBlockDownload(); }
bool getReindex() override { return ::fReindex; }
bool getImporting() override { return ::fImporting; }
void setNetworkActive(bool active) override
{
if (g_connman) {
g_connman->SetNetworkActive(active);
}
}
bool getNetworkActive() override { return g_connman && g_connman->GetNetworkActive(); }
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
{
return MakeHandler(::uiInterface.InitMessage.connect(fn));
Expand All @@ -90,6 +146,37 @@ class NodeImpl : public Node
CHECK_WALLET(
return MakeHandler(::uiInterface.LoadWallet.connect([fn](CWallet* wallet) { fn(MakeWallet(*wallet)); })));
}
std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
{
return MakeHandler(::uiInterface.NotifyNumConnectionsChanged.connect(fn));
}
std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override
{
return MakeHandler(::uiInterface.NotifyNetworkActiveChanged.connect(fn));
}
std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override
{
return MakeHandler(::uiInterface.NotifyAlertChanged.connect(fn));
}
std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override
{
return MakeHandler(::uiInterface.BannedListChanged.connect(fn));
}
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
{
return MakeHandler(::uiInterface.NotifyBlockTip.connect([fn](bool initial_download, const CBlockIndex* block) {
fn(initial_download, block->nHeight, block->GetBlockTime(),
GuessVerificationProgress(::Params().TxData(), block));
}));
}
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
{
return MakeHandler(
::uiInterface.NotifyHeaderTip.connect([fn](bool initial_download, const CBlockIndex* block) {
fn(initial_download, block->nHeight, block->GetBlockTime(),
GuessVerificationProgress(::Params().TxData(), block));
}));
}
};

} // namespace
Expand Down
71 changes: 71 additions & 0 deletions src/interface/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#define BITCOIN_INTERFACE_NODE_H

#include <init.h> // For HelpMessageMode
#include <net.h> // For CConnman::NumConnections
#include <netaddress.h> // For Network

#include <functional>
#include <memory>
#include <stddef.h>
#include <stdint.h>
#include <string>

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

//! Get number of connections.
virtual size_t getNodeCount(CConnman::NumConnections flags) = 0;

//! Get total bytes recv.
virtual int64_t getTotalBytesRecv() = 0;

//! Get total bytes sent.
virtual int64_t getTotalBytesSent() = 0;

//! Get mempool size.
virtual size_t getMempoolSize() = 0;

//! Get mempool dynamic usage.
virtual size_t getMempoolDynamicUsage() = 0;

//! Get header tip height and time.
virtual bool getHeaderTip(int& height, int64_t& block_time) = 0;

//! Get num blocks.
virtual int getNumBlocks() = 0;

//! Get last block time.
virtual int64_t getLastBlockTime() = 0;

//! Get verification progress.
virtual double getVerificationProgress() = 0;

//! Is initial block download.
virtual bool isInitialBlockDownload() = 0;

//! Get reindex.
virtual bool getReindex() = 0;

//! Get importing.
virtual bool getImporting() = 0;

//! Set network active.
virtual void setNetworkActive(bool active) = 0;

//! Get network active.
virtual bool getNetworkActive() = 0;

//! Register handler for init messages.
using InitMessageFn = std::function<void(const std::string& message)>;
virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;
Expand All @@ -96,6 +141,32 @@ class Node
//! Register handler for load wallet messages.
using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;

//! Register handler for number of connections changed messages.
using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;

//! Register handler for network active messages.
using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;

//! Register handler for notify alert messages.
using NotifyAlertChangedFn = std::function<void()>;
virtual std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0;

//! Register handler for ban list messages.
using BannedListChangedFn = std::function<void()>;
virtual std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) = 0;

//! Register handler for block tip messages.
using NotifyBlockTipFn =
std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>;
virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;

//! Register handler for header tip messages.
using NotifyHeaderTipFn =
std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>;
virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
};

//! Return implementation of Node interface.
Expand Down
2 changes: 1 addition & 1 deletion src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void BitcoinApplication::initializeResult(bool success)
paymentServer->setOptionsModel(optionsModel);
#endif

clientModel = new ClientModel(optionsModel);
clientModel = new ClientModel(m_node, optionsModel);
window->setClientModel(clientModel);

#ifdef ENABLE_WALLET
Expand Down
8 changes: 3 additions & 5 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
connect(_clientModel, SIGNAL(networkActiveChanged(bool)), this, SLOT(setNetworkActive(bool)));

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

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

QString tooltip;

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

void BitcoinGUI::toggleNetworkActive()
{
if (clientModel) {
clientModel->setNetworkActive(!clientModel->getNetworkActive());
}
m_node.setNetworkActive(!m_node.getNetworkActive());
}

UnitDisplayStatusBarControl::UnitDisplayStatusBarControl(const PlatformStyle *platformStyle) :
Expand Down
Loading

0 comments on commit fe6f27e

Please sign in to comment.