2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2018-2022 The Bitcoin Core developers
|
2017-04-17 13:55:43 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-04-17 16:02:44 -04:00
|
|
|
#include <addrdb.h>
|
2017-10-05 16:40:43 -04:00
|
|
|
#include <banman.h>
|
2022-08-23 16:10:59 +02:00
|
|
|
#include <blockfilter.h>
|
2017-04-17 15:37:36 -04:00
|
|
|
#include <chain.h>
|
2017-04-17 13:55:43 -04:00
|
|
|
#include <chainparams.h>
|
2023-03-23 12:23:29 +01:00
|
|
|
#include <common/args.h>
|
2020-07-03 04:01:23 +10:00
|
|
|
#include <deploymentstatus.h>
|
2021-05-13 16:19:44 +02:00
|
|
|
#include <external_signer.h>
|
2022-08-23 16:10:59 +02:00
|
|
|
#include <index/blockfilterindex.h>
|
2017-04-17 13:55:43 -04:00
|
|
|
#include <init.h>
|
2017-05-30 15:55:17 -04:00
|
|
|
#include <interfaces/chain.h>
|
2018-04-07 03:42:02 -04:00
|
|
|
#include <interfaces/handler.h>
|
2020-11-24 10:13:23 -05:00
|
|
|
#include <interfaces/node.h>
|
2018-04-07 03:42:02 -04:00
|
|
|
#include <interfaces/wallet.h>
|
2022-11-30 10:37:13 +00:00
|
|
|
#include <kernel/chain.h>
|
|
|
|
#include <kernel/mempool_entry.h>
|
2023-07-29 13:06:01 +02:00
|
|
|
#include <logging.h>
|
2020-02-06 19:00:26 +02:00
|
|
|
#include <mapport.h>
|
2017-04-17 14:23:14 -04:00
|
|
|
#include <net.h>
|
2017-04-17 15:57:19 -04:00
|
|
|
#include <net_processing.h>
|
2017-04-17 14:23:14 -04:00
|
|
|
#include <netaddress.h>
|
|
|
|
#include <netbase.h>
|
2021-04-02 20:42:05 +02:00
|
|
|
#include <node/blockstorage.h>
|
2020-11-24 10:13:23 -05:00
|
|
|
#include <node/coin.h>
|
2019-09-17 18:28:03 -04:00
|
|
|
#include <node/context.h>
|
2022-06-14 10:38:51 +02:00
|
|
|
#include <node/interface_ui.h>
|
2022-03-09 11:23:22 +00:00
|
|
|
#include <node/mini_miner.h>
|
2022-11-30 10:37:13 +00:00
|
|
|
#include <node/transaction.h>
|
2017-04-17 19:46:08 -04:00
|
|
|
#include <policy/feerate.h>
|
|
|
|
#include <policy/fees.h>
|
2020-11-24 10:13:23 -05:00
|
|
|
#include <policy/policy.h>
|
|
|
|
#include <policy/rbf.h>
|
2019-04-02 14:14:58 -04:00
|
|
|
#include <policy/settings.h>
|
2017-04-17 15:37:36 -04:00
|
|
|
#include <primitives/block.h>
|
2020-11-24 10:13:23 -05:00
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <rpc/protocol.h>
|
2017-04-17 16:38:51 -04:00
|
|
|
#include <rpc/server.h>
|
2019-05-24 17:14:16 -04:00
|
|
|
#include <support/allocators/secure.h>
|
2017-04-17 15:37:36 -04:00
|
|
|
#include <sync.h>
|
|
|
|
#include <txmempool.h>
|
2020-11-24 10:13:23 -05:00
|
|
|
#include <uint256.h>
|
|
|
|
#include <univalue.h>
|
2020-05-28 09:48:30 -04:00
|
|
|
#include <util/check.h>
|
2023-11-24 19:28:14 +01:00
|
|
|
#include <util/result.h>
|
2023-07-07 17:32:54 -04:00
|
|
|
#include <util/signalinterrupt.h>
|
2020-04-11 18:48:04 +03:00
|
|
|
#include <util/translation.h>
|
2017-04-17 15:37:36 -04:00
|
|
|
#include <validation.h>
|
2020-11-24 10:13:23 -05:00
|
|
|
#include <validationinterface.h>
|
2017-04-17 13:55:43 -04:00
|
|
|
#include <warnings.h>
|
|
|
|
|
2017-04-17 15:10:47 -04:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config/bitcoin-config.h>
|
|
|
|
#endif
|
|
|
|
|
2020-12-01 00:36:36 +01:00
|
|
|
#include <any>
|
2020-11-24 10:13:23 -05:00
|
|
|
#include <memory>
|
2021-03-15 11:59:05 +08:00
|
|
|
#include <optional>
|
2020-11-24 10:13:23 -05:00
|
|
|
#include <utility>
|
2019-11-11 10:53:03 -05:00
|
|
|
|
2021-04-02 20:42:05 +02:00
|
|
|
#include <boost/signals2/signal.hpp>
|
|
|
|
|
2020-11-24 10:13:23 -05:00
|
|
|
using interfaces::BlockTip;
|
2020-11-24 10:13:23 -05:00
|
|
|
using interfaces::Chain;
|
|
|
|
using interfaces::FoundBlock;
|
2020-11-24 10:13:23 -05:00
|
|
|
using interfaces::Handler;
|
2022-10-12 10:42:38 -04:00
|
|
|
using interfaces::MakeSignalHandler;
|
2020-11-24 10:13:23 -05:00
|
|
|
using interfaces::Node;
|
2021-12-22 13:44:55 -05:00
|
|
|
using interfaces::WalletLoader;
|
2017-04-17 13:55:43 -04:00
|
|
|
|
2020-11-24 10:13:23 -05:00
|
|
|
namespace node {
|
2022-07-20 17:17:34 +02:00
|
|
|
// All members of the classes in this namespace are intentionally public, as the
|
|
|
|
// classes themselves are private.
|
2020-11-24 10:13:23 -05:00
|
|
|
namespace {
|
2021-06-16 14:49:09 -04:00
|
|
|
#ifdef ENABLE_EXTERNAL_SIGNER
|
|
|
|
class ExternalSignerImpl : public interfaces::ExternalSigner
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExternalSignerImpl(::ExternalSigner signer) : m_signer(std::move(signer)) {}
|
|
|
|
std::string getName() override { return m_signer.m_name; }
|
|
|
|
::ExternalSigner m_signer;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-04-17 13:55:43 -04:00
|
|
|
class NodeImpl : public Node
|
|
|
|
{
|
2017-05-30 15:55:17 -04:00
|
|
|
public:
|
2017-12-05 15:57:12 -05:00
|
|
|
explicit NodeImpl(NodeContext& context) { setContext(&context); }
|
2023-03-10 12:39:13 +01:00
|
|
|
void initLogging() override { InitLogging(args()); }
|
|
|
|
void initParameterInteraction() override { InitParameterInteraction(args()); }
|
2020-06-10 11:44:48 +03:00
|
|
|
bilingual_str getWarnings() override { return GetWarnings(true); }
|
2023-06-08 12:16:23 -03:00
|
|
|
int getExitStatus() override { return Assert(m_context)->exit_status.load(); }
|
2019-01-25 15:54:49 -05:00
|
|
|
uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); }
|
2017-04-17 13:55:43 -04:00
|
|
|
bool baseInitialize() override
|
|
|
|
{
|
2023-05-20 10:51:17 -03:00
|
|
|
if (!AppInitBasicSetup(args(), Assert(context())->exit_status)) return false;
|
2023-05-04 12:07:26 +01:00
|
|
|
if (!AppInitParameterInteraction(args())) return false;
|
2022-05-25 14:31:54 -04:00
|
|
|
|
|
|
|
m_context->kernel = std::make_unique<kernel::Context>();
|
2022-05-27 16:47:05 -04:00
|
|
|
if (!AppInitSanityChecks(*m_context->kernel)) return false;
|
2022-05-25 14:31:54 -04:00
|
|
|
|
|
|
|
if (!AppInitLockDataDirectory()) return false;
|
|
|
|
if (!AppInitInterfaces(*m_context)) return false;
|
|
|
|
|
|
|
|
return true;
|
2017-04-17 13:55:43 -04:00
|
|
|
}
|
2020-05-19 15:13:17 +02:00
|
|
|
bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override
|
2019-09-17 18:28:03 -04:00
|
|
|
{
|
2023-06-08 12:16:23 -03:00
|
|
|
if (AppInitMain(*m_context, tip_info)) return true;
|
|
|
|
// Error during initialization, set exit status before continue
|
|
|
|
m_context->exit_status.store(EXIT_FAILURE);
|
|
|
|
return false;
|
2019-09-17 18:28:03 -04:00
|
|
|
}
|
2017-04-17 13:55:43 -04:00
|
|
|
void appShutdown() override
|
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
Interrupt(*m_context);
|
|
|
|
Shutdown(*m_context);
|
2017-04-17 13:55:43 -04:00
|
|
|
}
|
2020-03-27 20:29:20 +02:00
|
|
|
void startShutdown() override
|
|
|
|
{
|
2023-07-07 17:32:54 -04:00
|
|
|
if (!(*Assert(Assert(m_context)->shutdown))()) {
|
|
|
|
LogPrintf("Error: failed to send shutdown signal\n");
|
|
|
|
}
|
2020-03-27 20:29:20 +02:00
|
|
|
// Stop RPC for clean shutdown if any of waitfor* commands is executed.
|
2023-03-10 12:39:13 +01:00
|
|
|
if (args().GetBoolArg("-server", false)) {
|
2020-03-27 20:29:20 +02:00
|
|
|
InterruptRPC();
|
|
|
|
StopRPC();
|
|
|
|
}
|
|
|
|
}
|
2023-07-07 17:32:54 -04:00
|
|
|
bool shutdownRequested() override { return ShutdownRequested(*Assert(m_context)); };
|
2019-04-29 15:29:00 -04:00
|
|
|
bool isSettingIgnored(const std::string& name) override
|
|
|
|
{
|
|
|
|
bool ignored = false;
|
2023-05-24 16:18:59 +02:00
|
|
|
args().LockSettings([&](common::Settings& settings) {
|
|
|
|
if (auto* options = common::FindKey(settings.command_line_options, name)) {
|
2019-04-29 15:29:00 -04:00
|
|
|
ignored = !options->empty();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ignored;
|
|
|
|
}
|
2023-05-24 16:18:59 +02:00
|
|
|
common::SettingsValue getPersistentSetting(const std::string& name) override { return args().GetPersistentSetting(name); }
|
|
|
|
void updateRwSetting(const std::string& name, const common::SettingsValue& value) override
|
2019-04-29 15:29:00 -04:00
|
|
|
{
|
2023-05-24 16:18:59 +02:00
|
|
|
args().LockSettings([&](common::Settings& settings) {
|
2019-04-29 15:29:00 -04:00
|
|
|
if (value.isNull()) {
|
|
|
|
settings.rw_settings.erase(name);
|
|
|
|
} else {
|
|
|
|
settings.rw_settings[name] = value;
|
|
|
|
}
|
|
|
|
});
|
2023-03-10 12:39:13 +01:00
|
|
|
args().WriteSettingsFile();
|
2019-04-29 15:29:00 -04:00
|
|
|
}
|
2023-05-24 16:18:59 +02:00
|
|
|
void forceSetting(const std::string& name, const common::SettingsValue& value) override
|
2019-04-29 15:29:00 -04:00
|
|
|
{
|
2023-05-24 16:18:59 +02:00
|
|
|
args().LockSettings([&](common::Settings& settings) {
|
2019-04-29 15:29:00 -04:00
|
|
|
if (value.isNull()) {
|
|
|
|
settings.forced_settings.erase(name);
|
|
|
|
} else {
|
|
|
|
settings.forced_settings[name] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-05-16 14:37:00 -04:00
|
|
|
void resetSettings() override
|
|
|
|
{
|
2023-03-10 12:39:13 +01:00
|
|
|
args().WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true);
|
2023-05-24 16:18:59 +02:00
|
|
|
args().LockSettings([&](common::Settings& settings) {
|
2022-05-16 14:37:00 -04:00
|
|
|
settings.rw_settings.clear();
|
|
|
|
});
|
2023-03-10 12:39:13 +01:00
|
|
|
args().WriteSettingsFile();
|
2022-05-16 14:37:00 -04:00
|
|
|
}
|
2020-02-23 02:12:19 +02:00
|
|
|
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
|
2021-11-08 17:34:32 +01:00
|
|
|
bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); }
|
2019-10-16 17:37:19 +00:00
|
|
|
size_t getNodeCount(ConnectionDirection flags) override
|
2017-04-17 15:37:36 -04:00
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;
|
2017-04-17 15:37:36 -04:00
|
|
|
}
|
2017-04-17 15:57:19 -04:00
|
|
|
bool getNodesStats(NodesStats& stats) override
|
|
|
|
{
|
|
|
|
stats.clear();
|
|
|
|
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->connman) {
|
2017-04-17 15:57:19 -04:00
|
|
|
std::vector<CNodeStats> stats_temp;
|
2020-05-28 16:35:15 -04:00
|
|
|
m_context->connman->GetNodeStats(stats_temp);
|
2017-04-17 15:57:19 -04: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 17:39:54 +01: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 15:57:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-17 16:02:44 -04: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 16:02:44 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-10 17:11:38 -07:00
|
|
|
bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) override
|
2017-04-17 16:38:51 -04: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 16:38:51 -04: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 16:38:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-05 15:57:12 -05:00
|
|
|
bool disconnectByAddress(const CNetAddr& net_addr) override
|
2017-10-04 18:25:34 -04:00
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->connman) {
|
|
|
|
return m_context->connman->DisconnectNode(net_addr);
|
2017-10-04 18:25:34 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-05 15:57:12 -05:00
|
|
|
bool disconnectById(NodeId id) override
|
2017-04-17 16:38:51 -04:00
|
|
|
{
|
2020-05-28 16:35:15 -04:00
|
|
|
if (m_context->connman) {
|
|
|
|
return m_context->connman->DisconnectNode(id);
|
2017-04-17 16:38:51 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-16 14:49:09 -04:00
|
|
|
std::vector<std::unique_ptr<interfaces::ExternalSigner>> listExternalSigners() override
|
2019-10-29 20:53:49 +01:00
|
|
|
{
|
2021-05-13 15:23:19 +02:00
|
|
|
#ifdef ENABLE_EXTERNAL_SIGNER
|
2019-10-29 20:53:49 +01:00
|
|
|
std::vector<ExternalSigner> signers = {};
|
2023-03-10 12:39:13 +01:00
|
|
|
const std::string command = args().GetArg("-signer", "");
|
2021-06-16 14:49:09 -04:00
|
|
|
if (command == "") return {};
|
2023-04-17 22:20:59 +02:00
|
|
|
ExternalSigner::Enumerate(command, signers, Params().GetChainTypeString());
|
2021-06-16 14:49:09 -04:00
|
|
|
std::vector<std::unique_ptr<interfaces::ExternalSigner>> result;
|
2023-03-26 20:17:55 +01:00
|
|
|
result.reserve(signers.size());
|
2021-06-16 14:49:09 -04:00
|
|
|
for (auto& signer : signers) {
|
|
|
|
result.emplace_back(std::make_unique<ExternalSignerImpl>(std::move(signer)));
|
|
|
|
}
|
|
|
|
return result;
|
2021-05-13 15:23:19 +02:00
|
|
|
#else
|
2021-06-23 13:33:18 +02:00
|
|
|
// This result is indistinguishable from a successful call that returns
|
2021-05-13 15:23:19 +02:00
|
|
|
// no signers. For the current GUI this doesn't matter, because the wallet
|
|
|
|
// creation dialog disables the external signer checkbox in both
|
|
|
|
// cases. The return type could be changed to std::optional<std::vector>
|
|
|
|
// (or something that also includes error messages) if this distinction
|
|
|
|
// becomes important.
|
|
|
|
return {};
|
|
|
|
#endif // ENABLE_EXTERNAL_SIGNER
|
2019-10-29 20:53:49 +01:00
|
|
|
}
|
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 15:37:36 -04:00
|
|
|
bool getHeaderTip(int& height, int64_t& block_time) override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2022-03-18 12:35:52 -04:00
|
|
|
auto best_header = chainman().m_best_header;
|
2020-12-24 16:18:46 -05:00
|
|
|
if (best_header) {
|
|
|
|
height = best_header->nHeight;
|
|
|
|
block_time = best_header->GetBlockTime();
|
2017-04-17 15:37:36 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int getNumBlocks() override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2021-03-24 14:54:29 -04:00
|
|
|
return chainman().ActiveChain().Height();
|
2017-04-17 15:37:36 -04:00
|
|
|
}
|
2020-01-23 19:31:16 -03:00
|
|
|
uint256 getBestBlockHash() override
|
|
|
|
{
|
2020-11-04 17:18:09 -05:00
|
|
|
const CBlockIndex* tip = WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip());
|
2022-05-18 18:36:31 +02:00
|
|
|
return tip ? tip->GetBlockHash() : chainman().GetParams().GenesisBlock().GetHash();
|
2020-01-23 19:31:16 -03:00
|
|
|
}
|
2017-04-17 15:37:36 -04:00
|
|
|
int64_t getLastBlockTime() override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2021-03-24 14:54:29 -04:00
|
|
|
if (chainman().ActiveChain().Tip()) {
|
|
|
|
return chainman().ActiveChain().Tip()->GetBlockTime();
|
2017-04-17 15:37:36 -04:00
|
|
|
}
|
2022-05-18 18:36:31 +02:00
|
|
|
return chainman().GetParams().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
|
2017-04-17 15:37:36 -04:00
|
|
|
}
|
|
|
|
double getVerificationProgress() override
|
|
|
|
{
|
2022-07-27 13:21:08 +02:00
|
|
|
return GuessVerificationProgress(chainman().GetParams().TxData(), WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()));
|
2017-04-17 15:37:36 -04:00
|
|
|
}
|
2023-08-04 16:43:39 -04:00
|
|
|
bool isInitialBlockDownload() override
|
|
|
|
{
|
|
|
|
return chainman().IsInitialBlockDownload();
|
2020-12-18 14:24:55 -05:00
|
|
|
}
|
2023-01-03 13:09:55 +01:00
|
|
|
bool isLoadingBlocks() override { return chainman().m_blockman.LoadingBlocks(); }
|
2017-04-17 15:37:36 -04:00
|
|
|
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 15:37:36 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 16:35:15 -04:00
|
|
|
bool getNetworkActive() override { return m_context->connman && m_context->connman->GetNetworkActive(); }
|
2022-07-21 13:47:36 +02:00
|
|
|
CFeeRate getDustRelayFee() override
|
|
|
|
{
|
|
|
|
if (!m_context->mempool) return CFeeRate{DUST_RELAY_TX_FEE};
|
|
|
|
return m_context->mempool->m_dust_relay_feerate;
|
|
|
|
}
|
2017-04-17 16:38:51 -04:00
|
|
|
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
|
|
|
|
{
|
2021-04-02 13:35:01 -04:00
|
|
|
JSONRPCRequest req;
|
|
|
|
req.context = m_context;
|
2017-04-17 16:38:51 -04: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); }
|
2022-09-12 11:09:06 -04:00
|
|
|
std::optional<Coin> getUnspentOutput(const COutPoint& output) override
|
2017-04-18 16:42:30 -04:00
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2022-09-12 11:09:06 -04:00
|
|
|
Coin coin;
|
|
|
|
if (chainman().ActiveChainstate().CoinsTip().GetCoin(output, coin)) return coin;
|
|
|
|
return {};
|
2017-04-18 16:42:30 -04:00
|
|
|
}
|
2021-11-12 15:20:53 -05:00
|
|
|
TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string& err_string) override
|
|
|
|
{
|
|
|
|
return BroadcastTransaction(*m_context, std::move(tx), err_string, max_tx_fee, /*relay=*/ true, /*wait_callback=*/ false);
|
|
|
|
}
|
2021-12-22 13:44:55 -05:00
|
|
|
WalletLoader& walletLoader() override
|
2017-04-17 18:56:44 -04:00
|
|
|
{
|
2021-12-22 13:44:55 -05:00
|
|
|
return *Assert(m_context->wallet_loader);
|
2019-05-24 17:14:16 -04:00
|
|
|
}
|
2017-04-17 13:55:43 -04:00
|
|
|
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::uiInterface.InitMessage_connect(fn));
|
2017-04-17 13:55:43 -04:00
|
|
|
}
|
2017-04-17 14:33:47 -04:00
|
|
|
std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::uiInterface.ThreadSafeMessageBox_connect(fn));
|
2017-04-17 14:33:47 -04:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleQuestion(QuestionFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::uiInterface.ThreadSafeQuestion_connect(fn));
|
2017-04-17 14:33:47 -04:00
|
|
|
}
|
2017-04-17 15:10:47 -04:00
|
|
|
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::uiInterface.ShowProgress_connect(fn));
|
2017-04-17 15:10:47 -04:00
|
|
|
}
|
2021-08-30 21:04:06 -04:00
|
|
|
std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::uiInterface.InitWallet_connect(fn));
|
2021-08-30 21:04:06 -04:00
|
|
|
}
|
2017-04-17 15:37:36 -04:00
|
|
|
std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn));
|
2017-04-17 15:37:36 -04:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn));
|
2017-04-17 15:37:36 -04:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::uiInterface.NotifyAlertChanged_connect(fn));
|
2017-04-17 15:37:36 -04:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::uiInterface.BannedListChanged_connect(fn));
|
2017-04-17 15:37:36 -04:00
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(::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 15:44:10 -04:00
|
|
|
GuessVerificationProgress(Params().TxData(), block));
|
2017-04-17 15:37:36 -04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
|
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return MakeSignalHandler(
|
2022-08-13 14:21:12 -04:00
|
|
|
::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, int64_t height, int64_t timestamp, bool presync) {
|
|
|
|
fn(sync_state, BlockTip{(int)height, timestamp, uint256{}}, presync);
|
2017-04-17 15:37:36 -04:00
|
|
|
}));
|
|
|
|
}
|
2020-05-28 16:35:15 -04:00
|
|
|
NodeContext* context() override { return m_context; }
|
|
|
|
void setContext(NodeContext* context) override
|
|
|
|
{
|
|
|
|
m_context = context;
|
|
|
|
}
|
2023-03-10 12:39:13 +01:00
|
|
|
ArgsManager& args() { return *Assert(Assert(m_context)->args); }
|
2022-07-20 17:17:34 +02:00
|
|
|
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
|
2020-05-28 16:35:15 -04:00
|
|
|
NodeContext* m_context{nullptr};
|
2017-04-17 13:55:43 -04:00
|
|
|
};
|
2020-11-24 10:13:23 -05:00
|
|
|
|
2023-02-18 15:49:41 +01:00
|
|
|
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active, const BlockManager& blockman)
|
2020-11-24 10:13:23 -05: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;
|
2022-08-07 20:56:17 -04:00
|
|
|
if (block.m_locator) { *block.m_locator = GetLocator(index); }
|
2023-02-18 15:49:41 +01:00
|
|
|
if (block.m_next_block) FillBlock(active[index->nHeight] == index ? active[index->nHeight + 1] : nullptr, *block.m_next_block, lock, active, blockman);
|
2020-11-24 10:13:23 -05:00
|
|
|
if (block.m_data) {
|
|
|
|
REVERSE_LOCK(lock);
|
2023-02-18 15:49:41 +01:00
|
|
|
if (!blockman.ReadBlockFromDisk(*block.m_data, *index)) block.m_data->SetNull();
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
2020-12-10 13:31:48 -05:00
|
|
|
block.found = true;
|
2020-11-24 10:13:23 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationsProxy : public CValidationInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit NotificationsProxy(std::shared_ptr<Chain::Notifications> notifications)
|
|
|
|
: m_notifications(std::move(notifications)) {}
|
|
|
|
virtual ~NotificationsProxy() = default;
|
2023-11-03 17:04:30 +01:00
|
|
|
void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence) override
|
2020-11-24 10:13:23 -05:00
|
|
|
{
|
2023-11-03 17:04:30 +01:00
|
|
|
m_notifications->transactionAddedToMempool(tx.info.m_tx);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override
|
|
|
|
{
|
2022-12-26 06:17:05 -03:00
|
|
|
m_notifications->transactionRemovedFromMempool(tx, reason);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
2019-09-23 13:54:21 -04:00
|
|
|
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
|
2020-11-24 10:13:23 -05:00
|
|
|
{
|
2019-09-23 13:54:21 -04:00
|
|
|
m_notifications->blockConnected(role, kernel::MakeBlockInfo(index, block.get()));
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
|
|
|
|
{
|
2022-01-13 07:55:18 -05:00
|
|
|
m_notifications->blockDisconnected(kernel::MakeBlockInfo(index, block.get()));
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
void UpdatedBlockTip(const CBlockIndex* index, const CBlockIndex* fork_index, bool is_ibd) override
|
|
|
|
{
|
|
|
|
m_notifications->updatedBlockTip();
|
|
|
|
}
|
2019-09-23 13:54:21 -04:00
|
|
|
void ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator) override {
|
|
|
|
m_notifications->chainStateFlushed(role, locator);
|
|
|
|
}
|
2020-11-24 10:13:23 -05:00
|
|
|
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"];
|
2022-05-13 12:32:59 +02:00
|
|
|
if (code.isNum() && code.getInt<int>() == RPC_WALLET_NOT_FOUND) {
|
2020-11-24 10:13:23 -05:00
|
|
|
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) {}
|
2021-03-15 10:41:30 +08:00
|
|
|
std::optional<int> getHeight() override
|
2020-11-24 10:13:23 -05:00
|
|
|
{
|
2022-07-27 13:21:08 +02:00
|
|
|
const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())};
|
|
|
|
return height >= 0 ? std::optional{height} : std::nullopt;
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
uint256 getBlockHash(int height) override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
2022-07-27 13:21:08 +02:00
|
|
|
return Assert(chainman().ActiveChain()[height])->GetBlockHash();
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
bool haveBlockOnDisk(int height) override
|
|
|
|
{
|
2022-04-29 19:10:57 -03:00
|
|
|
LOCK(::cs_main);
|
2022-07-27 13:21:08 +02:00
|
|
|
const CBlockIndex* block{chainman().ActiveChain()[height]};
|
2020-11-24 10:13:23 -05:00
|
|
|
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
|
|
|
|
}
|
|
|
|
CBlockLocator getTipLocator() override
|
|
|
|
{
|
2022-04-29 19:10:57 -03:00
|
|
|
LOCK(::cs_main);
|
2022-07-27 13:21:08 +02:00
|
|
|
return chainman().ActiveChain().GetLocator();
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
2022-04-29 19:10:57 -03:00
|
|
|
CBlockLocator getActiveChainLocator(const uint256& block_hash) override
|
|
|
|
{
|
|
|
|
LOCK(::cs_main);
|
|
|
|
const CBlockIndex* index = chainman().m_blockman.LookupBlockIndex(block_hash);
|
2022-08-07 20:56:17 -04:00
|
|
|
return GetLocator(index);
|
2022-04-29 19:10:57 -03:00
|
|
|
}
|
2021-03-15 10:41:30 +08:00
|
|
|
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
|
2020-11-24 10:13:23 -05:00
|
|
|
{
|
2022-04-29 19:10:57 -03:00
|
|
|
LOCK(::cs_main);
|
2022-07-27 13:21:08 +02:00
|
|
|
if (const CBlockIndex* fork = chainman().ActiveChainstate().FindForkInGlobalIndex(locator)) {
|
2020-11-24 10:13:23 -05:00
|
|
|
return fork->nHeight;
|
|
|
|
}
|
2021-03-15 10:41:30 +08:00
|
|
|
return std::nullopt;
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
2022-08-23 16:10:59 +02:00
|
|
|
bool hasBlockFilterIndex(BlockFilterType filter_type) override
|
|
|
|
{
|
|
|
|
return GetBlockFilterIndex(filter_type) != nullptr;
|
|
|
|
}
|
|
|
|
std::optional<bool> blockFilterMatchesAny(BlockFilterType filter_type, const uint256& block_hash, const GCSFilter::ElementSet& filter_set) override
|
|
|
|
{
|
|
|
|
const BlockFilterIndex* block_filter_index{GetBlockFilterIndex(filter_type)};
|
|
|
|
if (!block_filter_index) return std::nullopt;
|
|
|
|
|
|
|
|
BlockFilter filter;
|
|
|
|
const CBlockIndex* index{WITH_LOCK(::cs_main, return chainman().m_blockman.LookupBlockIndex(block_hash))};
|
|
|
|
if (index == nullptr || !block_filter_index->LookupFilter(index, filter)) return std::nullopt;
|
|
|
|
return filter.GetFilter().MatchAny(filter_set);
|
|
|
|
}
|
2020-11-24 10:13:23 -05:00
|
|
|
bool findBlock(const uint256& hash, const FoundBlock& block) override
|
|
|
|
{
|
|
|
|
WAIT_LOCK(cs_main, lock);
|
2023-02-18 15:49:41 +01:00
|
|
|
return FillBlock(chainman().m_blockman.LookupBlockIndex(hash), block, lock, chainman().ActiveChain(), chainman().m_blockman);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override
|
|
|
|
{
|
|
|
|
WAIT_LOCK(cs_main, lock);
|
2022-07-19 10:00:44 +02:00
|
|
|
const CChain& active = chainman().ActiveChain();
|
2023-02-18 15:49:41 +01:00
|
|
|
return FillBlock(active.FindEarliestAtLeast(min_time, min_height), block, lock, active, chainman().m_blockman);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out) override
|
|
|
|
{
|
|
|
|
WAIT_LOCK(cs_main, lock);
|
2022-07-19 10:00:44 +02:00
|
|
|
const CChain& active = chainman().ActiveChain();
|
|
|
|
if (const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
|
2020-11-24 10:13:23 -05:00
|
|
|
if (const CBlockIndex* ancestor = block->GetAncestor(ancestor_height)) {
|
2023-02-18 15:49:41 +01:00
|
|
|
return FillBlock(ancestor, ancestor_out, lock, active, chainman().m_blockman);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-18 15:49:41 +01:00
|
|
|
return FillBlock(nullptr, ancestor_out, lock, active, chainman().m_blockman);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
|
|
|
|
{
|
|
|
|
WAIT_LOCK(cs_main, lock);
|
2022-07-19 10:00:44 +02:00
|
|
|
const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash);
|
|
|
|
const CBlockIndex* ancestor = chainman().m_blockman.LookupBlockIndex(ancestor_hash);
|
2020-11-24 10:13:23 -05:00
|
|
|
if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;
|
2023-02-18 15:49:41 +01:00
|
|
|
return FillBlock(ancestor, ancestor_out, lock, chainman().ActiveChain(), chainman().m_blockman);
|
2020-11-24 10:13:23 -05: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);
|
2022-07-19 10:00:44 +02:00
|
|
|
const CChain& active = chainman().ActiveChain();
|
|
|
|
const CBlockIndex* block1 = chainman().m_blockman.LookupBlockIndex(block_hash1);
|
|
|
|
const CBlockIndex* block2 = chainman().m_blockman.LookupBlockIndex(block_hash2);
|
2020-11-24 10:13:23 -05:00
|
|
|
const CBlockIndex* ancestor = block1 && block2 ? LastCommonAncestor(block1, block2) : nullptr;
|
|
|
|
// Using & instead of && below to avoid short circuiting and leaving
|
refactor: cast bool to int to silence compiler warning
This fixes -Wbitwise-instead-of-logical compiler warnings:
node/interfaces.cpp:544:16: warning: use of bitwise '&' with boolean operands [-Wbitwise-instead-of-logical]
return FillBlock(ancestor, ancestor_out, lock, active) & FillBlock(block1, block1_out, lock, active) & FillBlock(block2, block2_out, lock, active);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&&
node/interfaces.cpp:544:16: note: cast one or both operands to int to silence this warning
node/interfaces.cpp:544:16: warning: use of bitwise '&' with boolean operands [-Wbitwise-instead-of-logical]
return FillBlock(ancestor, ancestor_out, lock, active) & FillBlock(block1, block1_out, lock, active) & FillBlock(block2, block2_out, lock, active);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&&
node/interfaces.cpp:544:16: note: cast one or both operands to int to silence this warning
2 warnings generated.
A similar change was recently made to libsecp in commit 16d13221
for the same reason.
2021-11-22 12:08:24 +01:00
|
|
|
// output uninitialized. Cast bool to int to avoid -Wbitwise-instead-of-logical
|
|
|
|
// compiler warnings.
|
2023-02-18 15:49:41 +01:00
|
|
|
return int{FillBlock(ancestor, ancestor_out, lock, active, chainman().m_blockman)} &
|
|
|
|
int{FillBlock(block1, block1_out, lock, active, chainman().m_blockman)} &
|
|
|
|
int{FillBlock(block2, block2_out, lock, active, chainman().m_blockman)};
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); }
|
|
|
|
double guessVerificationProgress(const uint256& block_hash) override
|
|
|
|
{
|
2022-04-29 19:10:57 -03:00
|
|
|
LOCK(::cs_main);
|
2022-05-18 18:36:31 +02:00
|
|
|
return GuessVerificationProgress(chainman().GetParams().TxData(), chainman().m_blockman.LookupBlockIndex(block_hash));
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
2021-03-15 10:41:30 +08:00
|
|
|
bool hasBlocks(const uint256& block_hash, int min_height, std::optional<int> max_height) override
|
2020-11-24 10:13:23 -05:00
|
|
|
{
|
|
|
|
// 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);
|
2022-03-02 15:42:57 +10:00
|
|
|
if (const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
|
2020-11-24 10:13:23 -05:00
|
|
|
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);
|
|
|
|
}
|
2020-05-01 17:36:17 +02:00
|
|
|
bool isInMempool(const uint256& txid) override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return false;
|
|
|
|
LOCK(m_node.mempool->cs);
|
2021-10-20 16:41:45 +01:00
|
|
|
return m_node.mempool->exists(GenTxid::Txid(txid));
|
2020-05-01 17:36:17 +02:00
|
|
|
}
|
2020-11-24 10:13:23 -05:00
|
|
|
bool hasDescendantsInMempool(const uint256& txid) override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return false;
|
|
|
|
LOCK(m_node.mempool->cs);
|
2023-11-02 15:50:45 +01:00
|
|
|
const auto entry{m_node.mempool->GetEntry(Txid::FromUint256(txid))};
|
|
|
|
if (entry == nullptr) return false;
|
|
|
|
return entry->GetCountWithDescendants() > 1;
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
bool broadcastTransaction(const CTransactionRef& tx,
|
|
|
|
const CAmount& max_tx_fee,
|
|
|
|
bool relay,
|
|
|
|
std::string& err_string) override
|
|
|
|
{
|
2022-04-02 16:01:40 +01:00
|
|
|
const TransactionError err = BroadcastTransaction(m_node, tx, err_string, max_tx_fee, relay, /*wait_callback=*/false);
|
2020-11-24 10:13:23 -05:00
|
|
|
// 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;
|
|
|
|
}
|
2019-10-18 04:02:50 +00:00
|
|
|
void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants, size_t* ancestorsize, CAmount* ancestorfees) override
|
2020-11-24 10:13:23 -05:00
|
|
|
{
|
|
|
|
ancestors = descendants = 0;
|
|
|
|
if (!m_node.mempool) return;
|
2019-10-18 04:02:50 +00:00
|
|
|
m_node.mempool->GetTransactionAncestry(txid, ancestors, descendants, ancestorsize, ancestorfees);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
2022-03-09 11:23:22 +00:00
|
|
|
|
2023-09-26 13:47:05 -04:00
|
|
|
std::map<COutPoint, CAmount> calculateIndividualBumpFees(const std::vector<COutPoint>& outpoints, const CFeeRate& target_feerate) override
|
2022-03-09 11:23:22 +00:00
|
|
|
{
|
|
|
|
if (!m_node.mempool) {
|
|
|
|
std::map<COutPoint, CAmount> bump_fees;
|
|
|
|
for (const auto& outpoint : outpoints) {
|
2023-10-04 13:53:40 +02:00
|
|
|
bump_fees.emplace(outpoint, 0);
|
2022-03-09 11:23:22 +00:00
|
|
|
}
|
|
|
|
return bump_fees;
|
|
|
|
}
|
|
|
|
return MiniMiner(*m_node.mempool, outpoints).CalculateBumpFees(target_feerate);
|
|
|
|
}
|
|
|
|
|
2023-09-26 13:47:05 -04:00
|
|
|
std::optional<CAmount> calculateCombinedBumpFee(const std::vector<COutPoint>& outpoints, const CFeeRate& target_feerate) override
|
2022-03-09 11:23:22 +00:00
|
|
|
{
|
|
|
|
if (!m_node.mempool) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return MiniMiner(*m_node.mempool, outpoints).CalculateTotalBumpFees(target_feerate);
|
|
|
|
}
|
2020-11-24 10:13:23 -05:00
|
|
|
void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) override
|
|
|
|
{
|
2022-05-18 14:43:59 -04:00
|
|
|
const CTxMemPool::Limits default_limits{};
|
|
|
|
|
|
|
|
const CTxMemPool::Limits& limits{m_node.mempool ? m_node.mempool->m_limits : default_limits};
|
|
|
|
|
|
|
|
limit_ancestor_count = limits.ancestor_count;
|
|
|
|
limit_descendant_count = limits.descendant_count;
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
2023-11-24 19:28:14 +01:00
|
|
|
util::Result<void> checkChainLimits(const CTransactionRef& tx) override
|
2020-11-24 10:13:23 -05:00
|
|
|
{
|
2023-11-24 19:28:14 +01:00
|
|
|
if (!m_node.mempool) return {};
|
2020-11-24 10:13:23 -05:00
|
|
|
LockPoints lp;
|
2023-06-05 09:15:31 +10:00
|
|
|
CTxMemPoolEntry entry(tx, 0, 0, 0, 0, false, 0, lp);
|
2020-11-24 10:13:23 -05:00
|
|
|
LOCK(m_node.mempool->cs);
|
2023-11-24 19:28:14 +01:00
|
|
|
return m_node.mempool->CheckPackageLimits({tx}, entry.GetTxSize());
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
|
|
|
|
{
|
2020-07-28 19:12:50 +02:00
|
|
|
if (!m_node.fee_estimator) return {};
|
|
|
|
return m_node.fee_estimator->estimateSmartFee(num_blocks, calc, conservative);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
unsigned int estimateMaxBlocks() override
|
|
|
|
{
|
2020-07-28 19:12:50 +02:00
|
|
|
if (!m_node.fee_estimator) return 0;
|
|
|
|
return m_node.fee_estimator->HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
CFeeRate mempoolMinFee() override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return {};
|
2021-10-28 13:46:19 -04:00
|
|
|
return m_node.mempool->GetMinFee();
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
2022-07-21 11:40:22 +02:00
|
|
|
CFeeRate relayMinFee() override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return CFeeRate{DEFAULT_MIN_RELAY_TX_FEE};
|
|
|
|
return m_node.mempool->m_min_relay_feerate;
|
|
|
|
}
|
|
|
|
CFeeRate relayIncrementalFee() override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return CFeeRate{DEFAULT_INCREMENTAL_RELAY_FEE};
|
|
|
|
return m_node.mempool->m_incremental_relay_feerate;
|
|
|
|
}
|
2022-07-21 13:47:36 +02:00
|
|
|
CFeeRate relayDustFee() override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return CFeeRate{DUST_RELAY_TX_FEE};
|
|
|
|
return m_node.mempool->m_dust_relay_feerate;
|
|
|
|
}
|
2020-11-24 10:13:23 -05:00
|
|
|
bool havePruned() override
|
|
|
|
{
|
2022-04-29 19:10:57 -03:00
|
|
|
LOCK(::cs_main);
|
2022-07-19 10:00:44 +02:00
|
|
|
return chainman().m_blockman.m_have_pruned;
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
2023-01-03 13:03:43 +01:00
|
|
|
bool isReadyToBroadcast() override { return !chainman().m_blockman.LoadingBlocks() && !isInitialBlockDownload(); }
|
|
|
|
bool isInitialBlockDownload() override
|
|
|
|
{
|
2023-08-04 16:43:39 -04:00
|
|
|
return chainman().IsInitialBlockDownload();
|
2020-12-18 14:30:28 -05:00
|
|
|
}
|
2023-07-07 17:32:54 -04:00
|
|
|
bool shutdownRequested() override { return ShutdownRequested(m_node); }
|
2020-11-24 10:13:23 -05:00
|
|
|
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
|
|
|
|
{
|
2021-03-10 17:28:08 +08:00
|
|
|
return std::make_unique<NotificationsHandlerImpl>(std::move(notifications));
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
void waitForNotificationsIfTipChanged(const uint256& old_tip) override
|
|
|
|
{
|
2022-07-27 13:21:08 +02:00
|
|
|
if (!old_tip.IsNull() && old_tip == WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()->GetBlockHash())) return;
|
2020-11-24 10:13:23 -05:00
|
|
|
SyncWithValidationInterfaceQueue();
|
|
|
|
}
|
|
|
|
std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override
|
|
|
|
{
|
2021-03-10 17:28:08 +08:00
|
|
|
return std::make_unique<RpcHandlerImpl>(command);
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2023-09-07 19:16:57 +10:00
|
|
|
bool rpcSerializationWithoutWitness() override { return RPCSerializationWithoutWitness(); }
|
2023-05-24 16:18:59 +02:00
|
|
|
common::SettingsValue getSetting(const std::string& name) override
|
2017-12-05 15:57:12 -05:00
|
|
|
{
|
2023-03-10 12:39:13 +01:00
|
|
|
return args().GetSetting(name);
|
2017-12-05 15:57:12 -05:00
|
|
|
}
|
2023-05-24 16:18:59 +02:00
|
|
|
std::vector<common::SettingsValue> getSettingsList(const std::string& name) override
|
2017-12-05 15:57:12 -05:00
|
|
|
{
|
2023-03-10 12:39:13 +01:00
|
|
|
return args().GetSettingsList(name);
|
2017-12-05 15:57:12 -05:00
|
|
|
}
|
2023-05-24 16:18:59 +02:00
|
|
|
common::SettingsValue getRwSetting(const std::string& name) override
|
2020-11-24 10:13:23 -05:00
|
|
|
{
|
2023-05-24 16:18:59 +02:00
|
|
|
common::SettingsValue result;
|
|
|
|
args().LockSettings([&](const common::Settings& settings) {
|
|
|
|
if (const common::SettingsValue* value = common::FindKey(settings.rw_settings, name)) {
|
2020-11-24 10:13:23 -05:00
|
|
|
result = *value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2023-05-24 16:18:59 +02:00
|
|
|
bool updateRwSetting(const std::string& name, const common::SettingsValue& value, bool write) override
|
2020-11-24 10:13:23 -05:00
|
|
|
{
|
2023-05-24 16:18:59 +02:00
|
|
|
args().LockSettings([&](common::Settings& settings) {
|
2020-11-24 10:13:23 -05:00
|
|
|
if (value.isNull()) {
|
|
|
|
settings.rw_settings.erase(name);
|
|
|
|
} else {
|
|
|
|
settings.rw_settings[name] = value;
|
|
|
|
}
|
|
|
|
});
|
2023-03-10 12:39:13 +01:00
|
|
|
return !write || args().WriteSettingsFile();
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
void requestMempoolTransactions(Notifications& notifications) override
|
|
|
|
{
|
|
|
|
if (!m_node.mempool) return;
|
|
|
|
LOCK2(::cs_main, m_node.mempool->cs);
|
2023-11-09 17:51:20 +01:00
|
|
|
for (const CTxMemPoolEntry& entry : m_node.mempool->entryAll()) {
|
2022-12-26 06:14:24 -03:00
|
|
|
notifications.transactionAddedToMempool(entry.GetSharedTx());
|
2020-11-24 10:13:23 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-29 17:10:11 -04:00
|
|
|
bool hasAssumedValidChain() override
|
|
|
|
{
|
2022-07-19 10:00:44 +02:00
|
|
|
return chainman().IsSnapshotActive();
|
2019-03-29 17:10:11 -04:00
|
|
|
}
|
|
|
|
|
2022-01-13 07:57:54 -05:00
|
|
|
NodeContext* context() override { return &m_node; }
|
2023-03-10 12:39:13 +01:00
|
|
|
ArgsManager& args() { return *Assert(m_node.args); }
|
2022-07-20 17:17:34 +02:00
|
|
|
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
|
2020-11-24 10:13:23 -05:00
|
|
|
NodeContext& m_node;
|
|
|
|
};
|
2017-04-17 13:55:43 -04:00
|
|
|
} // namespace
|
2020-11-24 10:13:23 -05:00
|
|
|
} // namespace node
|
2017-04-17 13:55:43 -04:00
|
|
|
|
2020-11-24 10:13:23 -05:00
|
|
|
namespace interfaces {
|
2021-11-12 10:06:00 -05:00
|
|
|
std::unique_ptr<Node> MakeNode(node::NodeContext& context) { return std::make_unique<node::NodeImpl>(context); }
|
|
|
|
std::unique_ptr<Chain> MakeChain(node::NodeContext& context) { return std::make_unique<node::ChainImpl>(context); }
|
2018-04-07 03:42:02 -04:00
|
|
|
} // namespace interfaces
|