2017-04-17 19:55:43 +02:00
|
|
|
// Copyright (c) 2018 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-04-07 09:42:02 +02:00
|
|
|
#include <interfaces/node.h>
|
2017-04-17 19:55:43 +02:00
|
|
|
|
2017-04-17 22:02:44 +02:00
|
|
|
#include <addrdb.h>
|
2017-04-18 00:56:44 +02:00
|
|
|
#include <amount.h>
|
2017-04-17 21:37:36 +02:00
|
|
|
#include <chain.h>
|
2017-04-17 19:55:43 +02:00
|
|
|
#include <chainparams.h>
|
|
|
|
#include <init.h>
|
2017-05-30 21:55:17 +02:00
|
|
|
#include <interfaces/chain.h>
|
2018-04-07 09:42:02 +02:00
|
|
|
#include <interfaces/handler.h>
|
|
|
|
#include <interfaces/wallet.h>
|
2017-04-17 20:23:14 +02:00
|
|
|
#include <net.h>
|
2017-04-17 21:57:19 +02:00
|
|
|
#include <net_processing.h>
|
2017-04-17 20:23:14 +02:00
|
|
|
#include <netaddress.h>
|
|
|
|
#include <netbase.h>
|
2017-04-18 01:46:08 +02:00
|
|
|
#include <policy/feerate.h>
|
|
|
|
#include <policy/fees.h>
|
|
|
|
#include <policy/policy.h>
|
2017-04-17 21:37:36 +02:00
|
|
|
#include <primitives/block.h>
|
2017-04-17 22:38:51 +02:00
|
|
|
#include <rpc/server.h>
|
2017-04-17 19:55:43 +02:00
|
|
|
#include <scheduler.h>
|
2018-05-16 21:17:40 +02:00
|
|
|
#include <shutdown.h>
|
2017-04-17 21:37:36 +02:00
|
|
|
#include <sync.h>
|
|
|
|
#include <txmempool.h>
|
2017-04-17 19:55:43 +02:00
|
|
|
#include <ui_interface.h>
|
2018-10-23 00:51:11 +02:00
|
|
|
#include <util/system.h>
|
2017-04-17 21:37:36 +02:00
|
|
|
#include <validation.h>
|
2017-04-17 19:55:43 +02:00
|
|
|
#include <warnings.h>
|
|
|
|
|
2017-04-17 21:10:47 +02:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config/bitcoin-config.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-17 21:37:36 +02:00
|
|
|
#include <atomic>
|
2017-04-17 22:38:51 +02:00
|
|
|
#include <univalue.h>
|
2017-04-17 19:55:43 +02:00
|
|
|
|
2018-09-12 16:05:00 +02:00
|
|
|
class CWallet;
|
2018-09-22 01:53:25 +02:00
|
|
|
fs::path GetWalletDir();
|
|
|
|
std::vector<fs::path> ListWalletDir();
|
2018-09-12 16:05:00 +02:00
|
|
|
std::vector<std::shared_ptr<CWallet>> GetWallets();
|
|
|
|
|
2018-04-07 09:42:02 +02:00
|
|
|
namespace interfaces {
|
2018-09-12 16:05:00 +02:00
|
|
|
|
|
|
|
class Wallet;
|
|
|
|
|
2017-04-17 19:55:43 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class NodeImpl : public Node
|
|
|
|
{
|
2017-05-30 21:55:17 +02:00
|
|
|
public:
|
|
|
|
NodeImpl() { m_interfaces.chain = MakeChain(); }
|
2018-04-29 01:40:51 +02:00
|
|
|
bool parseParameters(int argc, const char* const argv[], std::string& error) override
|
2017-04-17 19:55:43 +02:00
|
|
|
{
|
2018-04-29 01:40:51 +02:00
|
|
|
return gArgs.ParseParameters(argc, argv, error);
|
2017-04-17 19:55:43 +02:00
|
|
|
}
|
2018-07-30 09:25:25 +02:00
|
|
|
bool readConfigFiles(std::string& error) override { return gArgs.ReadConfigFiles(error, true); }
|
2017-04-17 20:23:14 +02:00
|
|
|
bool softSetArg(const std::string& arg, const std::string& value) override { return gArgs.SoftSetArg(arg, value); }
|
|
|
|
bool softSetBoolArg(const std::string& arg, bool value) override { return gArgs.SoftSetBoolArg(arg, value); }
|
2017-04-17 19:55:43 +02:00
|
|
|
void selectParams(const std::string& network) override { SelectParams(network); }
|
2018-10-07 14:11:36 +02:00
|
|
|
uint64_t getAssumedBlockchainSize() override { return Params().AssumedBlockchainSize(); }
|
|
|
|
uint64_t getAssumedChainStateSize() override { return Params().AssumedChainStateSize(); }
|
2018-03-23 22:14:39 +01:00
|
|
|
std::string getNetwork() override { return Params().NetworkIDString(); }
|
2017-04-17 19:55:43 +02:00
|
|
|
void initLogging() override { InitLogging(); }
|
|
|
|
void initParameterInteraction() override { InitParameterInteraction(); }
|
|
|
|
std::string getWarnings(const std::string& type) override { return GetWarnings(type); }
|
2018-04-11 22:02:01 +02:00
|
|
|
uint32_t getLogCategories() override { return g_logger->GetCategoryMask(); }
|
2017-04-17 19:55:43 +02:00
|
|
|
bool baseInitialize() override
|
|
|
|
{
|
|
|
|
return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() &&
|
|
|
|
AppInitLockDataDirectory();
|
|
|
|
}
|
2017-05-30 21:55:17 +02:00
|
|
|
bool appInitMain() override { return AppInitMain(m_interfaces); }
|
2017-04-17 19:55:43 +02:00
|
|
|
void appShutdown() override
|
|
|
|
{
|
|
|
|
Interrupt();
|
2017-05-30 21:55:17 +02:00
|
|
|
Shutdown(m_interfaces);
|
2017-04-17 19:55:43 +02:00
|
|
|
}
|
|
|
|
void startShutdown() override { StartShutdown(); }
|
2017-04-17 20:33:47 +02:00
|
|
|
bool shutdownRequested() override { return ShutdownRequested(); }
|
2017-04-17 20:23:14 +02:00
|
|
|
void mapPort(bool use_upnp) override
|
|
|
|
{
|
|
|
|
if (use_upnp) {
|
|
|
|
StartMapPort();
|
|
|
|
} else {
|
|
|
|
InterruptMapPort();
|
|
|
|
StopMapPort();
|
|
|
|
}
|
|
|
|
}
|
2018-04-28 22:54:58 +02:00
|
|
|
void setupServerArgs() override { return SetupServerArgs(); }
|
2017-04-17 20:23:14 +02:00
|
|
|
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
|
2017-04-17 21:37:36 +02:00
|
|
|
size_t getNodeCount(CConnman::NumConnections flags) override
|
|
|
|
{
|
|
|
|
return g_connman ? g_connman->GetNodeCount(flags) : 0;
|
|
|
|
}
|
2017-04-17 21:57:19 +02:00
|
|
|
bool getNodesStats(NodesStats& stats) override
|
|
|
|
{
|
|
|
|
stats.clear();
|
|
|
|
|
|
|
|
if (g_connman) {
|
|
|
|
std::vector<CNodeStats> stats_temp;
|
|
|
|
g_connman->GetNodeStats(stats_temp);
|
|
|
|
|
|
|
|
stats.reserve(stats_temp.size());
|
|
|
|
for (auto& node_stats_temp : stats_temp) {
|
|
|
|
stats.emplace_back(std::move(node_stats_temp), false, CNodeStateStats());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to retrieve the CNodeStateStats for each node.
|
|
|
|
TRY_LOCK(::cs_main, lockMain);
|
|
|
|
if (lockMain) {
|
|
|
|
for (auto& node_stats : stats) {
|
|
|
|
std::get<1>(node_stats) =
|
|
|
|
GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-17 22:02:44 +02:00
|
|
|
bool getBanned(banmap_t& banmap) override
|
|
|
|
{
|
2017-10-05 19:10:58 +02:00
|
|
|
if (g_banman) {
|
|
|
|
g_banman->GetBanned(banmap);
|
2017-04-17 22:02:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-17 22:38:51 +02:00
|
|
|
bool ban(const CNetAddr& net_addr, BanReason reason, int64_t ban_time_offset) override
|
|
|
|
{
|
2017-10-05 19:10:58 +02:00
|
|
|
if (g_banman) {
|
|
|
|
g_banman->Ban(net_addr, reason, ban_time_offset);
|
2017-04-17 22:38:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool unban(const CSubNet& ip) override
|
|
|
|
{
|
2017-10-05 19:10:58 +02:00
|
|
|
if (g_banman) {
|
|
|
|
g_banman->Unban(ip);
|
2017-04-17 22:38:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-05 00:25:34 +02:00
|
|
|
bool disconnect(const CNetAddr& net_addr) override
|
|
|
|
{
|
|
|
|
if (g_connman) {
|
|
|
|
return g_connman->DisconnectNode(net_addr);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-17 22:38:51 +02:00
|
|
|
bool disconnect(NodeId id) override
|
|
|
|
{
|
|
|
|
if (g_connman) {
|
|
|
|
return g_connman->DisconnectNode(id);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-17 21:37:36 +02:00
|
|
|
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();
|
|
|
|
}
|
2017-04-17 21:44:10 +02:00
|
|
|
return GuessVerificationProgress(Params().TxData(), tip);
|
2017-04-17 21:37:36 +02:00
|
|
|
}
|
|
|
|
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(); }
|
2017-04-18 00:56:44 +02:00
|
|
|
CAmount getMaxTxFee() override { return ::maxTxFee; }
|
2017-04-18 01:46:08 +02:00
|
|
|
CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) override
|
|
|
|
{
|
|
|
|
FeeCalculation fee_calc;
|
|
|
|
CFeeRate result = ::feeEstimator.estimateSmartFee(num_blocks, &fee_calc, conservative);
|
|
|
|
if (returned_target) {
|
|
|
|
*returned_target = fee_calc.returnedTarget;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
CFeeRate getDustRelayFee() override { return ::dustRelayFee; }
|
2017-04-17 22:38:51 +02:00
|
|
|
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
|
|
|
|
{
|
|
|
|
JSONRPCRequest req;
|
|
|
|
req.params = params;
|
|
|
|
req.strMethod = command;
|
|
|
|
req.URI = uri;
|
|
|
|
return ::tableRPC.execute(req);
|
|
|
|
}
|
|
|
|
std::vector<std::string> listRpcCommands() override { return ::tableRPC.listCommands(); }
|
|
|
|
void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) override { RPCSetTimerInterfaceIfUnset(iface); }
|
|
|
|
void rpcUnsetTimerInterface(RPCTimerInterface* iface) override { RPCUnsetTimerInterface(iface); }
|
2017-04-18 22:42:30 +02:00
|
|
|
bool getUnspentOutput(const COutPoint& output, Coin& coin) override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
|
|
|
return ::pcoinsTip->GetCoin(output, coin);
|
|
|
|
}
|
2018-09-22 01:53:25 +02:00
|
|
|
std::string getWalletDir() override
|
|
|
|
{
|
|
|
|
return GetWalletDir().string();
|
|
|
|
}
|
|
|
|
std::vector<std::string> listWalletDir() override
|
|
|
|
{
|
|
|
|
std::vector<std::string> paths;
|
|
|
|
for (auto& path : ListWalletDir()) {
|
|
|
|
paths.push_back(path.string());
|
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
}
|
2017-04-18 00:56:44 +02:00
|
|
|
std::vector<std::unique_ptr<Wallet>> getWallets() override
|
|
|
|
{
|
|
|
|
std::vector<std::unique_ptr<Wallet>> wallets;
|
2018-05-22 17:18:07 +02:00
|
|
|
for (const std::shared_ptr<CWallet>& wallet : GetWallets()) {
|
|
|
|
wallets.emplace_back(MakeWallet(wallet));
|
2017-04-18 00:56:44 +02:00
|
|
|
}
|
|
|
|
return wallets;
|
|
|
|
}
|
2017-04-17 19:55:43 +02:00
|
|
|
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
|
|
|
|
{
|
2018-07-11 14:00:15 +02:00
|
|
|
return MakeHandler(::uiInterface.InitMessage_connect(fn));
|
2017-04-17 19:55:43 +02:00
|
|
|
}
|
2017-04-17 20:33:47 +02:00
|
|
|
std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) override
|
|
|
|
{
|
2018-07-11 14:00:15 +02:00
|
|
|
return MakeHandler(::uiInterface.ThreadSafeMessageBox_connect(fn));
|
2017-04-17 20:33:47 +02:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleQuestion(QuestionFn fn) override
|
|
|
|
{
|
2018-07-11 14:00:15 +02:00
|
|
|
return MakeHandler(::uiInterface.ThreadSafeQuestion_connect(fn));
|
2017-04-17 20:33:47 +02:00
|
|
|
}
|
2017-04-17 21:10:47 +02:00
|
|
|
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
|
|
|
|
{
|
2018-07-11 14:00:15 +02:00
|
|
|
return MakeHandler(::uiInterface.ShowProgress_connect(fn));
|
2017-04-17 21:10:47 +02:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override
|
|
|
|
{
|
2018-09-12 16:05:00 +02:00
|
|
|
return MakeHandler(::uiInterface.LoadWallet_connect([fn](std::shared_ptr<CWallet> wallet) { fn(MakeWallet(wallet)); }));
|
2017-04-17 21:10:47 +02:00
|
|
|
}
|
2017-04-17 21:37:36 +02:00
|
|
|
std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
|
|
|
|
{
|
2018-07-11 14:00:15 +02:00
|
|
|
return MakeHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn));
|
2017-04-17 21:37:36 +02:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override
|
|
|
|
{
|
2018-07-11 14:00:15 +02:00
|
|
|
return MakeHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn));
|
2017-04-17 21:37:36 +02:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override
|
|
|
|
{
|
2018-07-11 14:00:15 +02:00
|
|
|
return MakeHandler(::uiInterface.NotifyAlertChanged_connect(fn));
|
2017-04-17 21:37:36 +02:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override
|
|
|
|
{
|
2018-07-11 14:00:15 +02:00
|
|
|
return MakeHandler(::uiInterface.BannedListChanged_connect(fn));
|
2017-04-17 21:37:36 +02:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
|
|
|
|
{
|
2018-07-11 14:00:15 +02:00
|
|
|
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](bool initial_download, const CBlockIndex* block) {
|
2017-04-17 21:37:36 +02:00
|
|
|
fn(initial_download, block->nHeight, block->GetBlockTime(),
|
2017-04-17 21:44:10 +02:00
|
|
|
GuessVerificationProgress(Params().TxData(), block));
|
2017-04-17 21:37:36 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
|
|
|
|
{
|
|
|
|
return MakeHandler(
|
2018-07-11 14:00:15 +02:00
|
|
|
::uiInterface.NotifyHeaderTip_connect([fn](bool initial_download, const CBlockIndex* block) {
|
2017-04-17 21:37:36 +02:00
|
|
|
fn(initial_download, block->nHeight, block->GetBlockTime(),
|
2017-04-17 21:44:10 +02:00
|
|
|
GuessVerificationProgress(Params().TxData(), block));
|
2017-04-17 21:37:36 +02:00
|
|
|
}));
|
|
|
|
}
|
2017-05-30 21:55:17 +02:00
|
|
|
InitInterfaces m_interfaces;
|
2017-04-17 19:55:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<Node> MakeNode() { return MakeUnique<NodeImpl>(); }
|
|
|
|
|
2018-04-07 09:42:02 +02:00
|
|
|
} // namespace interfaces
|