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.
|
|
|
|
|
2017-04-17 17:02:44 -03:00
|
|
|
#include <addrdb.h>
|
2017-10-05 17:40:43 -03:00
|
|
|
#include <banman.h>
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <boost/signals2/signal.hpp>
|
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>
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <interfaces/node.h>
|
2018-04-07 04:42:02 -03:00
|
|
|
#include <interfaces/wallet.h>
|
2020-02-06 14:00:26 -03:00
|
|
|
#include <mapport.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>
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <node/coin.h>
|
2019-09-17 19:28:03 -03:00
|
|
|
#include <node/context.h>
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <node/transaction.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>
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <policy/policy.h>
|
|
|
|
#include <policy/rbf.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>
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <rpc/protocol.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>
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <timedata.h>
|
2017-04-17 16:37:36 -03:00
|
|
|
#include <txmempool.h>
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <uint256.h>
|
|
|
|
#include <univalue.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>
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <validationinterface.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
|
|
|
|
|
2020-11-24 12:13:23 -03:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2019-11-11 12:53:03 -03:00
|
|
|
|
2020-11-24 12:13:23 -03:00
|
|
|
using interfaces::BlockTip;
|
2020-11-24 12:13:23 -03:00
|
|
|
using interfaces::Chain;
|
|
|
|
using interfaces::FoundBlock;
|
2020-11-24 12:13:23 -03:00
|
|
|
using interfaces::Handler;
|
|
|
|
using interfaces::MakeHandler;
|
|
|
|
using interfaces::Node;
|
|
|
|
using interfaces::WalletClient;
|
2017-04-17 14:55:43 -03:00
|
|
|
|
2020-11-24 12:13:23 -03:00
|
|
|
namespace node {
|
|
|
|
namespace {
|
2017-04-17 14:55:43 -03:00
|
|
|
class NodeImpl : public Node
|
|
|
|
{
|
2017-05-30 15:55:17 -04:00
|
|
|
public:
|
2020-11-29 15:33:22 -03:00
|
|
|
explicit 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(); }
|
2020-02-22 19:10:48 -03:00
|
|
|
void mapPort(bool use_upnp) override { StartMapPort(use_upnp); }
|
2017-04-17 15:23:14 -03:00
|
|
|
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.
|
2020-08-24 12:39:54 -04:00
|
|
|
if (m_context->peerman) {
|
|
|
|
TRY_LOCK(::cs_main, lockMain);
|
|
|
|
if (lockMain) {
|
|
|
|
for (auto& node_stats : stats) {
|
|
|
|
std::get<1>(node_stats) =
|
|
|
|
m_context->peerman->GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));
|
|
|
|
}
|
2017-04-17 16:57:19 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 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
|
|
|
};
|
2020-11-24 12:13:23 -03:00
|
|
|
|
2020-07-20 09:49:10 -04:00
|
|
|
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active)
|
2020-11-24 12:13:23 -03:00
|
|
|
{
|
|
|
|
if (!index) return false;
|
|
|
|
if (block.m_hash) *block.m_hash = index->GetBlockHash();
|
|
|
|
if (block.m_height) *block.m_height = index->nHeight;
|
|
|
|
if (block.m_time) *block.m_time = index->GetBlockTime();
|
|
|
|
if (block.m_max_time) *block.m_max_time = index->GetBlockTimeMax();
|
|
|
|
if (block.m_mtp_time) *block.m_mtp_time = index->GetMedianTimePast();
|
2020-07-20 09:49:10 -04:00
|
|
|
if (block.m_in_active_chain) *block.m_in_active_chain = active[index->nHeight] == index;
|
|
|
|
if (block.m_next_block) FillBlock(active[index->nHeight] == index ? active[index->nHeight + 1] : nullptr, *block.m_next_block, lock, active);
|
2020-11-24 12:13:23 -03:00
|
|
|
if (block.m_data) {
|
|
|
|
REVERSE_LOCK(lock);
|
|
|
|
if (!ReadBlockFromDisk(*block.m_data, index, Params().GetConsensus())) block.m_data->SetNull();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationsProxy : public CValidationInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit NotificationsProxy(std::shared_ptr<Chain::Notifications> notifications)
|
|
|
|
: m_notifications(std::move(notifications)) {}
|
|
|
|
virtual ~NotificationsProxy() = default;
|
|
|
|
void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) override
|
|
|
|
{
|
|
|
|
m_notifications->transactionAddedToMempool(tx, mempool_sequence);
|
|
|
|
}
|
|
|
|
void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override
|
|
|
|
{
|
|
|
|
m_notifications->transactionRemovedFromMempool(tx, reason, mempool_sequence);
|
|
|
|
}
|
|
|
|
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
|
|
|
|
{
|
|
|
|
m_notifications->blockConnected(*block, index->nHeight);
|
|
|
|
}
|
|
|
|
void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
|
|
|
|
{
|
|
|
|
m_notifications->blockDisconnected(*block, index->nHeight);
|
|
|
|
}
|
|
|
|
void UpdatedBlockTip(const CBlockIndex* index, const CBlockIndex* fork_index, bool is_ibd) override
|
|
|
|
{
|
|
|
|
m_notifications->updatedBlockTip();
|
|
|
|
}
|
|
|
|
void ChainStateFlushed(const CBlockLocator& locator) override { m_notifications->chainStateFlushed(locator); }
|
|
|
|
std::shared_ptr<Chain::Notifications> m_notifications;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NotificationsHandlerImpl : public Handler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit NotificationsHandlerImpl(std::shared_ptr<Chain::Notifications> notifications)
|
|
|
|
: m_proxy(std::make_shared<NotificationsProxy>(std::move(notifications)))
|
|
|
|
{
|
|
|
|
RegisterSharedValidationInterface(m_proxy);
|
|
|
|
}
|
|
|
|
~NotificationsHandlerImpl() override { disconnect(); }
|
|
|
|
void disconnect() override
|
|
|
|
{
|
|
|
|
if (m_proxy) {
|
|
|
|
UnregisterSharedValidationInterface(m_proxy);
|
|
|
|
m_proxy.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::shared_ptr<NotificationsProxy> m_proxy;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RpcHandlerImpl : public Handler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit RpcHandlerImpl(const CRPCCommand& command) : m_command(command), m_wrapped_command(&command)
|
|
|
|
{
|
|
|
|
m_command.actor = [this](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
|
|
|
|
if (!m_wrapped_command) return false;
|
|
|
|
try {
|
|
|
|
return m_wrapped_command->actor(request, result, last_handler);
|
|
|
|
} catch (const UniValue& e) {
|
|
|
|
// If this is not the last handler and a wallet not found
|
|
|
|
// exception was thrown, return false so the next handler can
|
|
|
|
// try to handle the request. Otherwise, reraise the exception.
|
|
|
|
if (!last_handler) {
|
|
|
|
const UniValue& code = e["code"];
|
|
|
|
if (code.isNum() && code.get_int() == RPC_WALLET_NOT_FOUND) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
::tableRPC.appendCommand(m_command.name, &m_command);
|
|
|
|
}
|
|
|
|
|
|
|
|
void disconnect() final
|
|
|
|
{
|
|
|
|
if (m_wrapped_command) {
|
|
|
|
m_wrapped_command = nullptr;
|
|
|
|
::tableRPC.removeCommand(m_command.name, &m_command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~RpcHandlerImpl() override { disconnect(); }
|
|
|
|
|
|
|
|
CRPCCommand m_command;
|
|
|
|
const CRPCCommand* m_wrapped_command;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ChainImpl : public Chain
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ChainImpl(NodeContext& node) : m_node(node) {}
|
|
|
|
Optional<int> getHeight() override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
|
|
|
int height = active.Height();
|
2020-11-24 12:13:23 -03:00
|
|
|
if (height >= 0) {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
uint256 getBlockHash(int height) override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
|
|
|
CBlockIndex* block = active[height];
|
2020-11-24 12:13:23 -03:00
|
|
|
assert(block);
|
|
|
|
return block->GetBlockHash();
|
|
|
|
}
|
|
|
|
bool haveBlockOnDisk(int height) override
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
|
|
|
CBlockIndex* block = active[height];
|
2020-11-24 12:13:23 -03:00
|
|
|
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
|
|
|
|
}
|
|
|
|
CBlockLocator getTipLocator() override
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
|
|
|
return active.GetLocator();
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
bool checkFinalTx(const CTransaction& tx) override
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
return CheckFinalTx(tx);
|
|
|
|
}
|
|
|
|
Optional<int> findLocatorFork(const CBlockLocator& locator) override
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
|
|
|
if (CBlockIndex* fork = FindForkInGlobalIndex(active, locator)) {
|
2020-11-24 12:13:23 -03:00
|
|
|
return fork->nHeight;
|
|
|
|
}
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
bool findBlock(const uint256& hash, const FoundBlock& block) override
|
|
|
|
{
|
|
|
|
WAIT_LOCK(cs_main, lock);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
|
|
|
return FillBlock(LookupBlockIndex(hash), block, lock, active);
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override
|
|
|
|
{
|
|
|
|
WAIT_LOCK(cs_main, lock);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
|
|
|
return FillBlock(active.FindEarliestAtLeast(min_time, min_height), block, lock, active);
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out) override
|
|
|
|
{
|
|
|
|
WAIT_LOCK(cs_main, lock);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
2020-11-24 12:13:23 -03:00
|
|
|
if (const CBlockIndex* block = LookupBlockIndex(block_hash)) {
|
|
|
|
if (const CBlockIndex* ancestor = block->GetAncestor(ancestor_height)) {
|
2020-07-20 09:49:10 -04:00
|
|
|
return FillBlock(ancestor, ancestor_out, lock, active);
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 09:49:10 -04:00
|
|
|
return FillBlock(nullptr, ancestor_out, lock, active);
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
|
|
|
|
{
|
|
|
|
WAIT_LOCK(cs_main, lock);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
2020-11-24 12:13:23 -03:00
|
|
|
const CBlockIndex* block = LookupBlockIndex(block_hash);
|
|
|
|
const CBlockIndex* ancestor = LookupBlockIndex(ancestor_hash);
|
|
|
|
if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;
|
2020-07-20 09:49:10 -04:00
|
|
|
return FillBlock(ancestor, ancestor_out, lock, active);
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
|
|
|
|
{
|
|
|
|
WAIT_LOCK(cs_main, lock);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
2020-11-24 12:13:23 -03:00
|
|
|
const CBlockIndex* block1 = LookupBlockIndex(block_hash1);
|
|
|
|
const CBlockIndex* block2 = LookupBlockIndex(block_hash2);
|
|
|
|
const CBlockIndex* ancestor = block1 && block2 ? LastCommonAncestor(block1, block2) : nullptr;
|
|
|
|
// Using & instead of && below to avoid short circuiting and leaving
|
|
|
|
// output uninitialized.
|
2020-07-20 09:49:10 -04:00
|
|
|
return FillBlock(ancestor, ancestor_out, lock, active) & FillBlock(block1, block1_out, lock, active) & FillBlock(block2, block2_out, lock, active);
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); }
|
|
|
|
double guessVerificationProgress(const uint256& block_hash) override
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
return GuessVerificationProgress(Params().TxData(), LookupBlockIndex(block_hash));
|
|
|
|
}
|
|
|
|
bool hasBlocks(const uint256& block_hash, int min_height, Optional<int> max_height) override
|
|
|
|
{
|
|
|
|
// hasBlocks returns true if all ancestors of block_hash in specified
|
|
|
|
// range have block data (are not pruned), false if any ancestors in
|
|
|
|
// specified range are missing data.
|
|
|
|
//
|
|
|
|
// For simplicity and robustness, min_height and max_height are only
|
|
|
|
// used to limit the range, and passing min_height that's too low or
|
|
|
|
// max_height that's too high will not crash or change the result.
|
|
|
|
LOCK(::cs_main);
|
|
|
|
if (CBlockIndex* block = LookupBlockIndex(block_hash)) {
|
|
|
|
if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height);
|
|
|
|
for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) {
|
|
|
|
// Check pprev to not segfault if min_height is too low
|
|
|
|
if (block->nHeight <= min_height || !block->pprev) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RBFTransactionState isRBFOptIn(const CTransaction& tx) override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return IsRBFOptInEmptyMempool(tx);
|
|
|
|
LOCK(m_node.mempool->cs);
|
|
|
|
return IsRBFOptIn(tx, *m_node.mempool);
|
|
|
|
}
|
|
|
|
bool hasDescendantsInMempool(const uint256& txid) override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return false;
|
|
|
|
LOCK(m_node.mempool->cs);
|
|
|
|
auto it = m_node.mempool->GetIter(txid);
|
|
|
|
return it && (*it)->GetCountWithDescendants() > 1;
|
|
|
|
}
|
|
|
|
bool broadcastTransaction(const CTransactionRef& tx,
|
|
|
|
const CAmount& max_tx_fee,
|
|
|
|
bool relay,
|
|
|
|
std::string& err_string) override
|
|
|
|
{
|
|
|
|
const TransactionError err = BroadcastTransaction(m_node, tx, err_string, max_tx_fee, relay, /*wait_callback*/ false);
|
|
|
|
// Chain clients only care about failures to accept the tx to the mempool. Disregard non-mempool related failures.
|
|
|
|
// Note: this will need to be updated if BroadcastTransactions() is updated to return other non-mempool failures
|
|
|
|
// that Chain clients do not need to know about.
|
|
|
|
return TransactionError::OK == err;
|
|
|
|
}
|
|
|
|
void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) override
|
|
|
|
{
|
|
|
|
ancestors = descendants = 0;
|
|
|
|
if (!m_node.mempool) return;
|
|
|
|
m_node.mempool->GetTransactionAncestry(txid, ancestors, descendants);
|
|
|
|
}
|
|
|
|
void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) override
|
|
|
|
{
|
|
|
|
limit_ancestor_count = gArgs.GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
|
|
|
|
limit_descendant_count = gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
|
|
|
|
}
|
|
|
|
bool checkChainLimits(const CTransactionRef& tx) override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return true;
|
|
|
|
LockPoints lp;
|
|
|
|
CTxMemPoolEntry entry(tx, 0, 0, 0, false, 0, lp);
|
|
|
|
CTxMemPool::setEntries ancestors;
|
|
|
|
auto limit_ancestor_count = gArgs.GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
|
|
|
|
auto limit_ancestor_size = gArgs.GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT) * 1000;
|
|
|
|
auto limit_descendant_count = gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
|
|
|
|
auto limit_descendant_size = gArgs.GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT) * 1000;
|
|
|
|
std::string unused_error_string;
|
|
|
|
LOCK(m_node.mempool->cs);
|
|
|
|
return m_node.mempool->CalculateMemPoolAncestors(
|
|
|
|
entry, ancestors, limit_ancestor_count, limit_ancestor_size,
|
|
|
|
limit_descendant_count, limit_descendant_size, unused_error_string);
|
|
|
|
}
|
|
|
|
CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
|
|
|
|
{
|
2020-07-28 13:12:50 -04:00
|
|
|
if (!m_node.fee_estimator) return {};
|
|
|
|
return m_node.fee_estimator->estimateSmartFee(num_blocks, calc, conservative);
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
unsigned int estimateMaxBlocks() override
|
|
|
|
{
|
2020-07-28 13:12:50 -04:00
|
|
|
if (!m_node.fee_estimator) return 0;
|
|
|
|
return m_node.fee_estimator->HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
CFeeRate mempoolMinFee() override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return {};
|
|
|
|
return m_node.mempool->GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
|
|
|
|
}
|
|
|
|
CFeeRate relayMinFee() override { return ::minRelayTxFee; }
|
|
|
|
CFeeRate relayIncrementalFee() override { return ::incrementalRelayFee; }
|
|
|
|
CFeeRate relayDustFee() override { return ::dustRelayFee; }
|
|
|
|
bool havePruned() override
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
return ::fHavePruned;
|
|
|
|
}
|
|
|
|
bool isReadyToBroadcast() override { return !::fImporting && !::fReindex && !isInitialBlockDownload(); }
|
|
|
|
bool isInitialBlockDownload() override { return ::ChainstateActive().IsInitialBlockDownload(); }
|
|
|
|
bool shutdownRequested() override { return ShutdownRequested(); }
|
|
|
|
int64_t getAdjustedTime() override { return GetAdjustedTime(); }
|
|
|
|
void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }
|
|
|
|
void initWarning(const bilingual_str& message) override { InitWarning(message); }
|
|
|
|
void initError(const bilingual_str& message) override { InitError(message); }
|
|
|
|
void showProgress(const std::string& title, int progress, bool resume_possible) override
|
|
|
|
{
|
|
|
|
::uiInterface.ShowProgress(title, progress, resume_possible);
|
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) override
|
|
|
|
{
|
|
|
|
return MakeUnique<NotificationsHandlerImpl>(std::move(notifications));
|
|
|
|
}
|
|
|
|
void waitForNotificationsIfTipChanged(const uint256& old_tip) override
|
|
|
|
{
|
|
|
|
if (!old_tip.IsNull()) {
|
|
|
|
LOCK(::cs_main);
|
2020-07-20 09:49:10 -04:00
|
|
|
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
|
|
|
if (old_tip == active.Tip()->GetBlockHash()) return;
|
2020-11-24 12:13:23 -03:00
|
|
|
}
|
|
|
|
SyncWithValidationInterfaceQueue();
|
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override
|
|
|
|
{
|
|
|
|
return MakeUnique<RpcHandlerImpl>(command);
|
|
|
|
}
|
|
|
|
bool rpcEnableDeprecated(const std::string& method) override { return IsDeprecatedRPCEnabled(method); }
|
|
|
|
void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) override
|
|
|
|
{
|
|
|
|
RPCRunLater(name, std::move(fn), seconds);
|
|
|
|
}
|
|
|
|
int rpcSerializationFlags() override { return RPCSerializationFlags(); }
|
|
|
|
util::SettingsValue getRwSetting(const std::string& name) override
|
|
|
|
{
|
|
|
|
util::SettingsValue result;
|
|
|
|
gArgs.LockSettings([&](const util::Settings& settings) {
|
|
|
|
if (const util::SettingsValue* value = util::FindKey(settings.rw_settings, name)) {
|
|
|
|
result = *value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
bool updateRwSetting(const std::string& name, const util::SettingsValue& value) override
|
|
|
|
{
|
|
|
|
gArgs.LockSettings([&](util::Settings& settings) {
|
|
|
|
if (value.isNull()) {
|
|
|
|
settings.rw_settings.erase(name);
|
|
|
|
} else {
|
|
|
|
settings.rw_settings[name] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return gArgs.WriteSettingsFile();
|
|
|
|
}
|
|
|
|
void requestMempoolTransactions(Notifications& notifications) override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return;
|
|
|
|
LOCK2(::cs_main, m_node.mempool->cs);
|
|
|
|
for (const CTxMemPoolEntry& entry : m_node.mempool->mapTx) {
|
|
|
|
notifications.transactionAddedToMempool(entry.GetSharedTx(), 0 /* mempool_sequence */);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NodeContext& m_node;
|
|
|
|
};
|
2017-04-17 14:55:43 -03:00
|
|
|
} // namespace
|
2020-11-24 12:13:23 -03:00
|
|
|
} // namespace node
|
2017-04-17 14:55:43 -03:00
|
|
|
|
2020-11-24 12:13:23 -03:00
|
|
|
namespace interfaces {
|
|
|
|
std::unique_ptr<Node> MakeNode(NodeContext* context) { return MakeUnique<node::NodeImpl>(context); }
|
2020-11-24 12:13:23 -03:00
|
|
|
std::unique_ptr<Chain> MakeChain(NodeContext& context) { return MakeUnique<node::ChainImpl>(context); }
|
2018-04-07 04:42:02 -03:00
|
|
|
} // namespace interfaces
|