2020-01-14 16:17:38 -03:00
|
|
|
// Copyright (c) 2018-2020 The Bitcoin Core developers
|
2017-04-17 14:55:43 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-04-07 04:42:02 -03:00
|
|
|
#include <interfaces/node.h>
|
2017-04-17 14:55:43 -03:00
|
|
|
|
2017-04-17 17:02:44 -03:00
|
|
|
#include <addrdb.h>
|
2017-10-05 17:40:43 -03:00
|
|
|
#include <banman.h>
|
2017-04-17 16:37:36 -03:00
|
|
|
#include <chain.h>
|
2017-04-17 14:55:43 -03:00
|
|
|
#include <chainparams.h>
|
|
|
|
#include <init.h>
|
2017-05-30 15:55:17 -04:00
|
|
|
#include <interfaces/chain.h>
|
2018-04-07 04:42:02 -03:00
|
|
|
#include <interfaces/handler.h>
|
|
|
|
#include <interfaces/wallet.h>
|
2017-04-17 15:23:14 -03:00
|
|
|
#include <net.h>
|
2017-04-17 16:57:19 -03:00
|
|
|
#include <net_processing.h>
|
2017-04-17 15:23:14 -03:00
|
|
|
#include <netaddress.h>
|
|
|
|
#include <netbase.h>
|
2019-09-17 19:28:03 -03:00
|
|
|
#include <node/context.h>
|
2020-06-19 18:14:17 -04:00
|
|
|
#include <node/ui_interface.h>
|
2017-04-17 20:46:08 -03:00
|
|
|
#include <policy/feerate.h>
|
|
|
|
#include <policy/fees.h>
|
2019-04-02 15:14:58 -03:00
|
|
|
#include <policy/settings.h>
|
2017-04-17 16:37:36 -03:00
|
|
|
#include <primitives/block.h>
|
2017-04-17 17:38:51 -03:00
|
|
|
#include <rpc/server.h>
|
2018-05-16 15:17:40 -04:00
|
|
|
#include <shutdown.h>
|
2019-05-24 17:14:16 -04:00
|
|
|
#include <support/allocators/secure.h>
|
2017-04-17 16:37:36 -03:00
|
|
|
#include <sync.h>
|
|
|
|
#include <txmempool.h>
|
2020-05-28 09:48:30 -04:00
|
|
|
#include <util/check.h>
|
2020-04-17 11:28:45 -04:00
|
|
|
#include <util/ref.h>
|
2018-10-22 19:51:11 -03:00
|
|
|
#include <util/system.h>
|
2020-04-11 11:48:04 -04:00
|
|
|
#include <util/translation.h>
|
2017-04-17 16:37:36 -03:00
|
|
|
#include <validation.h>
|
2017-04-17 14:55:43 -03:00
|
|
|
#include <warnings.h>
|
|
|
|
|
2017-04-17 16:10:47 -03:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config/bitcoin-config.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-17 17:38:51 -03:00
|
|
|
#include <univalue.h>
|
2017-04-17 14:55:43 -03:00
|
|
|
|
2019-11-11 12:53:03 -03:00
|
|
|
#include <boost/signals2/signal.hpp>
|
|
|
|
|
2018-04-07 04:42:02 -03:00
|
|
|
namespace interfaces {
|
2017-04-17 14:55:43 -03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class NodeImpl : public Node
|
|
|
|
{
|
2017-05-30 15:55:17 -04:00
|
|
|
public:
|
2020-05-28 16:35:15 -04:00
|
|
|
NodeImpl(NodeContext* context) { setContext(context); }
|
2018-08-23 14:42:31 -03:00
|
|
|
void initLogging() override { InitLogging(*Assert(m_context->args)); }
|
|
|
|
void initParameterInteraction() override { InitParameterInteraction(*Assert(m_context->args)); }
|
2020-06-10 04:44:48 -04:00
|
|
|
bilingual_str getWarnings() override { return GetWarnings(true); }
|
2019-01-25 17:54:49 -03:00
|
|
|
uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); }
|
2017-04-17 14:55:43 -03:00
|
|
|
bool baseInitialize() override
|
|
|
|
{
|
2020-08-21 14:13:40 -04:00
|
|
|
return AppInitBasicSetup(gArgs) && AppInitParameterInteraction(gArgs) && AppInitSanityChecks() &&
|
2020-05-29 00:07:18 -04:00
|
|
|
AppInitLockDataDirectory() && AppInitInterfaces(*m_context);
|
2017-04-17 14:55:43 -03:00
|
|
|
}
|
2020-05-19 09:13:17 -04:00
|
|
|
bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override
|
2019-09-17 19:28:03 -03:00
|
|
|
{
|
2020-05-19 09:13:17 -04:00
|
|
|
return AppInitMain(m_context_ref, *m_context, tip_info);
|
2019-09-17 19:28:03 -03:00
|
|
|
}
|
2017-04-17 14:55:43 -03:00
|
|
|
void appShutdown() override
|
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
Interrupt(*m_context);
|
|
|
|
Shutdown(*m_context);
|
2017-04-17 14:55:43 -03:00
|
|
|
}
|
2020-03-27 15:29:20 -03:00
|
|
|
void startShutdown() override
|
|
|
|
{
|
|
|
|
StartShutdown();
|
|
|
|
// Stop RPC for clean shutdown if any of waitfor* commands is executed.
|
|
|
|
if (gArgs.GetBoolArg("-server", false)) {
|
|
|
|
InterruptRPC();
|
|
|
|
StopRPC();
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 15:33:47 -03:00
|
|
|
bool shutdownRequested() override { return ShutdownRequested(); }
|
2017-04-17 15:23:14 -03:00
|
|
|
void mapPort(bool use_upnp) override
|
|
|
|
{
|
|
|
|
if (use_upnp) {
|
|
|
|
StartMapPort();
|
|
|
|
} else {
|
|
|
|
InterruptMapPort();
|
|
|
|
StopMapPort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
|
2017-04-17 16:37:36 -03:00
|
|
|
size_t getNodeCount(CConnman::NumConnections flags) override
|
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
2017-04-17 16:57:19 -03:00
|
|
|
bool getNodesStats(NodesStats& stats) override
|
|
|
|
{
|
|
|
|
stats.clear();
|
|
|
|
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->connman) {
|
2017-04-17 16:57:19 -03:00
|
|
|
std::vector<CNodeStats> stats_temp;
|
2020-05-28 16:35:15 -04:00
|
|
|
m_context->connman->GetNodeStats(stats_temp);
|
2017-04-17 16:57:19 -03:00
|
|
|
|
|
|
|
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 17:02:44 -03:00
|
|
|
bool getBanned(banmap_t& banmap) override
|
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->banman) {
|
|
|
|
m_context->banman->GetBanned(banmap);
|
2017-04-17 17:02:44 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-10 20:11:38 -04:00
|
|
|
bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) override
|
2017-04-17 17:38:51 -03:00
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->banman) {
|
|
|
|
m_context->banman->Ban(net_addr, ban_time_offset);
|
2017-04-17 17:38:51 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool unban(const CSubNet& ip) override
|
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->banman) {
|
|
|
|
m_context->banman->Unban(ip);
|
2017-04-17 17:38:51 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-05 17:57:12 -03:00
|
|
|
bool disconnectByAddress(const CNetAddr& net_addr) override
|
2017-10-04 19:25:34 -03:00
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->connman) {
|
|
|
|
return m_context->connman->DisconnectNode(net_addr);
|
2017-10-04 19:25:34 -03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-05 17:57:12 -03:00
|
|
|
bool disconnectById(NodeId id) override
|
2017-04-17 17:38:51 -03:00
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->connman) {
|
|
|
|
return m_context->connman->DisconnectNode(id);
|
2017-04-17 17:38:51 -03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-28 16:35:15 -04:00
|
|
|
int64_t getTotalBytesRecv() override { return m_context->connman ? m_context->connman->GetTotalBytesRecv() : 0; }
|
|
|
|
int64_t getTotalBytesSent() override { return m_context->connman ? m_context->connman->GetTotalBytesSent() : 0; }
|
|
|
|
size_t getMempoolSize() override { return m_context->mempool ? m_context->mempool->size() : 0; }
|
|
|
|
size_t getMempoolDynamicUsage() override { return m_context->mempool ? m_context->mempool->DynamicMemoryUsage() : 0; }
|
2017-04-17 16:37:36 -03:00
|
|
|
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);
|
2019-03-27 12:14:25 -03:00
|
|
|
return ::ChainActive().Height();
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
2020-01-23 19:31:16 -03:00
|
|
|
uint256 getBestBlockHash() override
|
|
|
|
{
|
|
|
|
const CBlockIndex* tip = WITH_LOCK(::cs_main, return ::ChainActive().Tip());
|
|
|
|
return tip ? tip->GetBlockHash() : Params().GenesisBlock().GetHash();
|
|
|
|
}
|
2017-04-17 16:37:36 -03:00
|
|
|
int64_t getLastBlockTime() override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2019-03-27 12:14:25 -03:00
|
|
|
if (::ChainActive().Tip()) {
|
|
|
|
return ::ChainActive().Tip()->GetBlockTime();
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
|
|
|
return Params().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
|
|
|
|
}
|
|
|
|
double getVerificationProgress() override
|
|
|
|
{
|
|
|
|
const CBlockIndex* tip;
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2019-03-27 12:14:25 -03:00
|
|
|
tip = ::ChainActive().Tip();
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
2017-04-17 16:44:10 -03:00
|
|
|
return GuessVerificationProgress(Params().TxData(), tip);
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
2019-03-27 13:21:50 -03:00
|
|
|
bool isInitialBlockDownload() override { return ::ChainstateActive().IsInitialBlockDownload(); }
|
2017-04-17 16:37:36 -03:00
|
|
|
bool getReindex() override { return ::fReindex; }
|
|
|
|
bool getImporting() override { return ::fImporting; }
|
|
|
|
void setNetworkActive(bool active) override
|
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->connman) {
|
|
|
|
m_context->connman->SetNetworkActive(active);
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 16:35:15 -04:00
|
|
|
bool getNetworkActive() override { return m_context->connman && m_context->connman->GetNetworkActive(); }
|
2017-04-17 20:46:08 -03: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 17:38:51 -03:00
|
|
|
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
|
|
|
|
{
|
2020-04-17 11:28:45 -04:00
|
|
|
JSONRPCRequest req(m_context_ref);
|
2017-04-17 17:38:51 -03:00
|
|
|
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 17:42:30 -03:00
|
|
|
bool getUnspentOutput(const COutPoint& output, Coin& coin) override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2019-07-24 11:45:04 -04:00
|
|
|
return ::ChainstateActive().CoinsTip().GetCoin(output, coin);
|
2017-04-18 17:42:30 -03:00
|
|
|
}
|
2020-05-28 09:48:30 -04:00
|
|
|
WalletClient& walletClient() override
|
2017-04-17 19:56:44 -03:00
|
|
|
{
|
2020-05-28 09:48:30 -04:00
|
|
|
return *Assert(m_context->wallet_client);
|
2019-05-24 17:14:16 -04:00
|
|
|
}
|
2017-04-17 14:55:43 -03:00
|
|
|
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
|
|
|
|
{
|
2018-07-11 08:00:15 -04:00
|
|
|
return MakeHandler(::uiInterface.InitMessage_connect(fn));
|
2017-04-17 14:55:43 -03:00
|
|
|
}
|
2017-04-17 15:33:47 -03:00
|
|
|
std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) override
|
|
|
|
{
|
2018-07-11 08:00:15 -04:00
|
|
|
return MakeHandler(::uiInterface.ThreadSafeMessageBox_connect(fn));
|
2017-04-17 15:33:47 -03:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleQuestion(QuestionFn fn) override
|
|
|
|
{
|
2018-07-11 08:00:15 -04:00
|
|
|
return MakeHandler(::uiInterface.ThreadSafeQuestion_connect(fn));
|
2017-04-17 15:33:47 -03:00
|
|
|
}
|
2017-04-17 16:10:47 -03:00
|
|
|
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
|
|
|
|
{
|
2018-07-11 08:00:15 -04:00
|
|
|
return MakeHandler(::uiInterface.ShowProgress_connect(fn));
|
2017-04-17 16:10:47 -03:00
|
|
|
}
|
2017-04-17 16:37:36 -03:00
|
|
|
std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
|
|
|
|
{
|
2018-07-11 08:00:15 -04:00
|
|
|
return MakeHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn));
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override
|
|
|
|
{
|
2018-07-11 08:00:15 -04:00
|
|
|
return MakeHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn));
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override
|
|
|
|
{
|
2018-07-11 08:00:15 -04:00
|
|
|
return MakeHandler(::uiInterface.NotifyAlertChanged_connect(fn));
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override
|
|
|
|
{
|
2018-07-11 08:00:15 -04:00
|
|
|
return MakeHandler(::uiInterface.BannedListChanged_connect(fn));
|
2017-04-17 16:37:36 -03:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
|
|
|
|
{
|
2020-03-04 15:05:42 -03:00
|
|
|
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
|
2020-01-23 21:34:58 -03:00
|
|
|
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
|
2017-04-17 16:44:10 -03:00
|
|
|
GuessVerificationProgress(Params().TxData(), block));
|
2017-04-17 16:37:36 -03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
|
|
|
|
{
|
|
|
|
return MakeHandler(
|
2020-03-04 15:05:42 -03:00
|
|
|
::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
|
2020-01-23 21:34:58 -03:00
|
|
|
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
|
2019-10-25 10:17:27 -03:00
|
|
|
/* verification progress is unused when a header was received */ 0);
|
2017-04-17 16:37:36 -03:00
|
|
|
}));
|
|
|
|
}
|
2020-05-28 16:35:15 -04:00
|
|
|
NodeContext* context() override { return m_context; }
|
|
|
|
void setContext(NodeContext* context) override
|
|
|
|
{
|
|
|
|
m_context = context;
|
|
|
|
if (context) {
|
|
|
|
m_context_ref.Set(*context);
|
|
|
|
} else {
|
|
|
|
m_context_ref.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NodeContext* m_context{nullptr};
|
|
|
|
util::Ref m_context_ref;
|
2017-04-17 14:55:43 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-05-28 16:35:15 -04:00
|
|
|
std::unique_ptr<Node> MakeNode(NodeContext* context) { return MakeUnique<NodeImpl>(context); }
|
2017-04-17 14:55:43 -03:00
|
|
|
|
2018-04-07 04:42:02 -03:00
|
|
|
} // namespace interfaces
|