aboutsummaryrefslogtreecommitdiff
path: root/src/init.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/init.cpp')
-rw-r--r--src/init.cpp543
1 files changed, 274 insertions, 269 deletions
diff --git a/src/init.cpp b/src/init.cpp
index e60feecf10..ab53cb851d 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -3,7 +3,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include <config/bitcoin-config.h> // IWYU pragma: keep
+#include <bitcoin-build-config.h> // IWYU pragma: keep
#include <init.h>
@@ -29,6 +29,7 @@
#include <init/common.h>
#include <interfaces/chain.h>
#include <interfaces/init.h>
+#include <interfaces/ipc.h>
#include <interfaces/mining.h>
#include <interfaces/node.h>
#include <kernel/context.h>
@@ -122,17 +123,19 @@ using node::ApplyArgsManOptions;
using node::BlockManager;
using node::CacheSizes;
using node::CalculateCacheSizes;
+using node::ChainstateLoadResult;
+using node::ChainstateLoadStatus;
using node::DEFAULT_PERSIST_MEMPOOL;
using node::DEFAULT_PRINT_MODIFIED_FEE;
using node::DEFAULT_STOPATHEIGHT;
using node::DumpMempool;
-using node::LoadMempool;
+using node::ImportBlocks;
using node::KernelNotifications;
using node::LoadChainstate;
+using node::LoadMempool;
using node::MempoolPath;
using node::NodeContext;
using node::ShouldPersistMempool;
-using node::ImportBlocks;
using node::VerifyLoadedChainstate;
using util::Join;
using util::ReplaceAll;
@@ -147,11 +150,12 @@ static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
// accessing block files don't count towards the fd_set size limit
// anyway.
-#define MIN_CORE_FILEDESCRIPTORS 0
+#define MIN_LEVELDB_FDS 0
#else
-#define MIN_CORE_FILEDESCRIPTORS 150
+#define MIN_LEVELDB_FDS 150
#endif
+static constexpr int MIN_CORE_FDS = MIN_LEVELDB_FDS + NUM_FDS_MESSAGE_CAPTURE;
static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
/**
@@ -203,7 +207,14 @@ void InitContext(NodeContext& node)
g_shutdown.emplace();
node.args = &gArgs;
- node.shutdown = &*g_shutdown;
+ node.shutdown_signal = &*g_shutdown;
+ node.shutdown_request = [&node] {
+ assert(node.shutdown_signal);
+ if (!(*node.shutdown_signal)()) return false;
+ // Wake any threads that may be waiting for the tip to change.
+ if (node.notifications) WITH_LOCK(node.notifications->m_tip_block_mutex, node.notifications->m_tip_block_cv.notify_all());
+ return true;
+ };
}
//////////////////////////////////////////////////////////////////////////////
@@ -231,7 +242,7 @@ void InitContext(NodeContext& node)
bool ShutdownRequested(node::NodeContext& node)
{
- return bool{*Assert(node.shutdown)};
+ return bool{*Assert(node.shutdown_signal)};
}
#if HAVE_SYSTEM
@@ -296,7 +307,7 @@ void Shutdown(NodeContext& node)
StopTorControl();
- if (node.chainman && node.chainman->m_thread_load.joinable()) node.chainman->m_thread_load.join();
+ if (node.background_init_thread.joinable()) node.background_init_thread.join();
// After everything has been shut down, but before things get flushed, stop the
// the scheduler. After this point, SyncWithValidationInterfaceQueue() should not be called anymore
// as this would prevent the shutdown from completing.
@@ -427,21 +438,7 @@ static void registerSignalHandler(int signal, void(*handler)(int))
}
#endif
-static boost::signals2::connection rpc_notify_block_change_connection;
-static void OnRPCStarted()
-{
- rpc_notify_block_change_connection = uiInterface.NotifyBlockTip_connect(std::bind(RPCNotifyBlockChange, std::placeholders::_2));
-}
-
-static void OnRPCStopped()
-{
- rpc_notify_block_change_connection.disconnect();
- RPCNotifyBlockChange(nullptr);
- g_best_block_cv.notify_all();
- LogDebug(BCLog::RPC, "RPC stopped.\n");
-}
-
-void SetupServerArgs(ArgsManager& argsman)
+void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
{
SetupHelpOptions(argsman);
argsman.AddArg("-help-debug", "Print help message with debugging options and exit", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST); // server-only for now
@@ -488,7 +485,7 @@ void SetupServerArgs(ArgsManager& argsman)
argsman.AddArg("-conf=<file>", strprintf("Specify path to read-only configuration file. Relative paths will be prefixed by datadir location (only useable from command line, not configuration file) (default: %s)", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-dbbatchsize", strprintf("Maximum database write batch size in bytes (default: %u)", nDefaultDbBatchSize), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
- argsman.AddArg("-dbcache=<n>", strprintf("Maximum database cache size <n> MiB (%d to %d, default: %d). In addition, unused mempool memory is shared for this cache (see -maxmempool).", nMinDbCache, nMaxDbCache, nDefaultDbCache), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
+ argsman.AddArg("-dbcache=<n>", strprintf("Maximum database cache size <n> MiB (minimum %d, default: %d). Make sure you have enough RAM. In addition, unused memory allocated to the mempool is shared with this cache (see -maxmempool).", nMinDbCache, nDefaultDbCache), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-includeconf=<file>", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-allowignoredconf", strprintf("For backwards compatibility, treat an unused %s file in the datadir as a warning, not an error.", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-loadblock=<file>", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -569,11 +566,7 @@ void SetupServerArgs(ArgsManager& argsman)
#else
hidden_args.emplace_back("-upnp");
#endif
-#ifdef USE_NATPMP
- argsman.AddArg("-natpmp", strprintf("Use NAT-PMP to map the listening port (default: %u)", DEFAULT_NATPMP), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
-#else
- hidden_args.emplace_back("-natpmp");
-#endif // USE_NATPMP
+ argsman.AddArg("-natpmp", strprintf("Use PCP or NAT-PMP to map the listening port (default: %u)", DEFAULT_NATPMP), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-whitebind=<[permissions@]addr>", "Bind to the given address and add permission flags to the peers connecting to it. "
"Use [host]:port notation for IPv6. Allowed permissions: " + Join(NET_PERMISSIONS_DOC, ", ") + ". "
"Specify multiple permissions separated by commas (default: download,noban,mempool,relay). Can be specified multiple times.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -676,6 +669,9 @@ void SetupServerArgs(ArgsManager& argsman)
argsman.AddArg("-rpcwhitelistdefault", "Sets default behavior for rpc whitelisting. Unless rpcwhitelistdefault is set to 0, if any -rpcwhitelist is set, the rpc server acts as if all rpc users are subject to empty-unless-otherwise-specified whitelists. If rpcwhitelistdefault is set to 1 and no -rpcwhitelist is set, rpc server acts as if all rpc users are subject to empty whitelists.", ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
argsman.AddArg("-rpcworkqueue=<n>", strprintf("Set the depth of the work queue to service RPC calls (default: %d)", DEFAULT_HTTP_WORKQUEUE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::RPC);
argsman.AddArg("-server", "Accept command line and JSON-RPC commands", ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
+ if (can_listen_ipc) {
+ argsman.AddArg("-ipcbind=<address>", "Bind to Unix socket address and listen for incoming connections. Valid address values are \"unix\" to listen on the default path, <datadir>/node.sock, or \"unix:/custom/path\" to specify a custom path. Can be specified multiple times to listen on multiple paths. Default behavior is not to listen on any path. If relative paths are specified, they are interpreted relative to the network data directory. If paths include any parent directory components and the parent directories do not exist, they will be created.", ArgsManager::ALLOW_ANY, OptionsCategory::IPC);
+ }
#if HAVE_DECL_FORK
argsman.AddArg("-daemon", strprintf("Run in the background as a daemon and accept commands (default: %d)", DEFAULT_DAEMON), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -689,21 +685,6 @@ void SetupServerArgs(ArgsManager& argsman)
argsman.AddHiddenArgs(hidden_args);
}
-static bool fHaveGenesis = false;
-static GlobalMutex g_genesis_wait_mutex;
-static std::condition_variable g_genesis_wait_cv;
-
-static void BlockNotifyGenesisWait(const CBlockIndex* pBlockIndex)
-{
- if (pBlockIndex != nullptr) {
- {
- LOCK(g_genesis_wait_mutex);
- fHaveGenesis = true;
- }
- g_genesis_wait_cv.notify_all();
- }
-}
-
#if HAVE_SYSTEM
static void StartupNotify(const ArgsManager& args)
{
@@ -718,9 +699,7 @@ static void StartupNotify(const ArgsManager& args)
static bool AppInitServers(NodeContext& node)
{
const ArgsManager& args = *Assert(node.args);
- RPCServer::OnStarted(&OnRPCStarted);
- RPCServer::OnStopped(&OnRPCStopped);
- if (!InitHTTPServer(*Assert(node.shutdown))) {
+ if (!InitHTTPServer(*Assert(node.shutdown_signal))) {
return false;
}
StartRPC();
@@ -834,9 +813,8 @@ void InitLogging(const ArgsManager& args)
namespace { // Variables internal to initialization process only
int nMaxConnections;
-int nUserMaxConnections;
-int nFD;
-ServiceFlags nLocalServices = ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS);
+int available_fds;
+ServiceFlags g_local_services = ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS);
int64_t peer_connect_timeout;
std::set<BlockFilterType> g_enabled_filter_types;
@@ -951,7 +929,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
// Signal NODE_P2P_V2 if BIP324 v2 transport is enabled.
if (args.GetBoolArg("-v2transport", DEFAULT_V2_TRANSPORT)) {
- nLocalServices = ServiceFlags(nLocalServices | NODE_P2P_V2);
+ g_local_services = ServiceFlags(g_local_services | NODE_P2P_V2);
}
// Signal NODE_COMPACT_FILTERS if peerblockfilters and basic filters index are both enabled.
@@ -960,7 +938,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
return InitError(_("Cannot set -peerblockfilters without -blockfilterindex."));
}
- nLocalServices = ServiceFlags(nLocalServices | NODE_COMPACT_FILTERS);
+ g_local_services = ServiceFlags(g_local_services | NODE_COMPACT_FILTERS);
}
if (args.GetIntArg("-prune", 0)) {
@@ -987,27 +965,33 @@ bool AppInitParameterInteraction(const ArgsManager& args)
return InitError(Untranslated("Cannot set -listen=0 together with -listenonion=1"));
}
- // Make sure enough file descriptors are available
- int nBind = std::max(nUserBind, size_t(1));
- nUserMaxConnections = args.GetIntArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
- nMaxConnections = std::max(nUserMaxConnections, 0);
-
- nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS + MAX_ADDNODE_CONNECTIONS + nBind + NUM_FDS_MESSAGE_CAPTURE);
+ // Make sure enough file descriptors are available. We need to reserve enough FDs to account for the bare minimum,
+ // plus all manual connections and all bound interfaces. Any remainder will be available for connection sockets
-#ifdef USE_POLL
- int fd_max = nFD;
-#else
- int fd_max = FD_SETSIZE;
+ // Number of bound interfaces (we have at least one)
+ int nBind = std::max(nUserBind, size_t(1));
+ // Maximum number of connections with other nodes, this accounts for all types of outbounds and inbounds except for manual
+ int user_max_connection = args.GetIntArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
+ if (user_max_connection < 0) {
+ return InitError(Untranslated("-maxconnections must be greater or equal than zero"));
+ }
+ // Reserve enough FDs to account for the bare minimum, plus any manual connections, plus the bound interfaces
+ int min_required_fds = MIN_CORE_FDS + MAX_ADDNODE_CONNECTIONS + nBind;
+
+ // Try raising the FD limit to what we need (available_fds may be smaller than the requested amount if this fails)
+ available_fds = RaiseFileDescriptorLimit(user_max_connection + min_required_fds);
+ // If we are using select instead of poll, our actual limit may be even smaller
+#ifndef USE_POLL
+ available_fds = std::min(FD_SETSIZE, available_fds);
#endif
+ if (available_fds < min_required_fds)
+ return InitError(strprintf(_("Not enough file descriptors available. %d available, %d required."), available_fds, min_required_fds));
+
// Trim requested connection counts, to fit into system limitations
- // <int> in std::min<int>(...) to work around FreeBSD compilation issue described in #2695
- nMaxConnections = std::max(std::min<int>(nMaxConnections, fd_max - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS - NUM_FDS_MESSAGE_CAPTURE), 0);
- if (nFD < MIN_CORE_FILEDESCRIPTORS)
- return InitError(_("Not enough file descriptors available."));
- nMaxConnections = std::min(nFD - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS - NUM_FDS_MESSAGE_CAPTURE, nMaxConnections);
+ nMaxConnections = std::min(available_fds - min_required_fds, user_max_connection);
- if (nMaxConnections < nUserMaxConnections)
- InitWarning(strprintf(_("Reducing -maxconnections from %d to %d, because of system limitations."), nUserMaxConnections, nMaxConnections));
+ if (nMaxConnections < user_max_connection)
+ InitWarning(strprintf(_("Reducing -maxconnections from %d to %d, because of system limitations."), user_max_connection, nMaxConnections));
// ********************************************************* Step 3: parameter-to-internal-flags
if (auto result{init::SetLoggingCategories(args)}; !result) return InitError(util::ErrorString(result));
@@ -1039,7 +1023,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
SetMockTime(args.GetIntArg("-mocktime", 0)); // SetMockTime(0) is a no-op
if (args.GetBoolArg("-peerbloomfilters", DEFAULT_PEERBLOOMFILTERS))
- nLocalServices = ServiceFlags(nLocalServices | NODE_BLOOM);
+ g_local_services = ServiceFlags(g_local_services | NODE_BLOOM);
if (args.IsArgSet("-test")) {
if (chainparams.GetChainType() != ChainType::REGTEST) {
@@ -1078,6 +1062,13 @@ bool AppInitParameterInteraction(const ArgsManager& args)
if (!blockman_result) {
return InitError(util::ErrorString(blockman_result));
}
+ CTxMemPool::Options mempool_opts{
+ .check_ratio = chainparams.DefaultConsistencyChecks() ? 1 : 0,
+ };
+ auto mempool_result{ApplyArgsManOptions(args, chainparams, mempool_opts)};
+ if (!mempool_result) {
+ return InitError(util::ErrorString(mempool_result));
+ }
}
return true;
@@ -1135,6 +1126,151 @@ bool AppInitInterfaces(NodeContext& node)
return true;
}
+bool CheckHostPortOptions(const ArgsManager& args) {
+ for (const std::string port_option : {
+ "-port",
+ "-rpcport",
+ }) {
+ if (args.IsArgSet(port_option)) {
+ const std::string port = args.GetArg(port_option, "");
+ uint16_t n;
+ if (!ParseUInt16(port, &n) || n == 0) {
+ return InitError(InvalidPortErrMsg(port_option, port));
+ }
+ }
+ }
+
+ for ([[maybe_unused]] const auto& [arg, unix] : std::vector<std::pair<std::string, bool>>{
+ // arg name UNIX socket support
+ {"-i2psam", false},
+ {"-onion", true},
+ {"-proxy", true},
+ {"-rpcbind", false},
+ {"-torcontrol", false},
+ {"-whitebind", false},
+ {"-zmqpubhashblock", true},
+ {"-zmqpubhashtx", true},
+ {"-zmqpubrawblock", true},
+ {"-zmqpubrawtx", true},
+ {"-zmqpubsequence", true},
+ }) {
+ for (const std::string& socket_addr : args.GetArgs(arg)) {
+ std::string host_out;
+ uint16_t port_out{0};
+ if (!SplitHostPort(socket_addr, port_out, host_out)) {
+#ifdef HAVE_SOCKADDR_UN
+ // Allow unix domain sockets for some options e.g. unix:/some/file/path
+ if (!unix || !socket_addr.starts_with(ADDR_PREFIX_UNIX)) {
+ return InitError(InvalidPortErrMsg(arg, socket_addr));
+ }
+#else
+ return InitError(InvalidPortErrMsg(arg, socket_addr));
+#endif
+ }
+ }
+ }
+
+ return true;
+}
+
+// A GUI user may opt to retry once if there is a failure during chainstate initialization.
+// The function therefore has to support re-entry.
+static ChainstateLoadResult InitAndLoadChainstate(
+ NodeContext& node,
+ bool do_reindex,
+ const bool do_reindex_chainstate,
+ CacheSizes& cache_sizes,
+ const ArgsManager& args)
+{
+ const CChainParams& chainparams = Params();
+ CTxMemPool::Options mempool_opts{
+ .check_ratio = chainparams.DefaultConsistencyChecks() ? 1 : 0,
+ .signals = node.validation_signals.get(),
+ };
+ Assert(ApplyArgsManOptions(args, chainparams, mempool_opts)); // no error can happen, already checked in AppInitParameterInteraction
+ bilingual_str mempool_error;
+ node.mempool = std::make_unique<CTxMemPool>(mempool_opts, mempool_error);
+ if (!mempool_error.empty()) {
+ return {ChainstateLoadStatus::FAILURE_FATAL, mempool_error};
+ }
+ LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), mempool_opts.max_size_bytes * (1.0 / 1024 / 1024));
+ ChainstateManager::Options chainman_opts{
+ .chainparams = chainparams,
+ .datadir = args.GetDataDirNet(),
+ .notifications = *node.notifications,
+ .signals = node.validation_signals.get(),
+ };
+ Assert(ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
+ BlockManager::Options blockman_opts{
+ .chainparams = chainman_opts.chainparams,
+ .blocks_dir = args.GetBlocksDirPath(),
+ .notifications = chainman_opts.notifications,
+ };
+ Assert(ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction
+ try {
+ node.chainman = std::make_unique<ChainstateManager>(*Assert(node.shutdown_signal), chainman_opts, blockman_opts);
+ } catch (std::exception& e) {
+ return {ChainstateLoadStatus::FAILURE_FATAL, strprintf(Untranslated("Failed to initialize ChainstateManager: %s"), e.what())};
+ }
+ ChainstateManager& chainman = *node.chainman;
+ // This is defined and set here instead of inline in validation.h to avoid a hard
+ // dependency between validation and index/base, since the latter is not in
+ // libbitcoinkernel.
+ chainman.snapshot_download_completed = [&node]() {
+ if (!node.chainman->m_blockman.IsPruneMode()) {
+ LogPrintf("[snapshot] re-enabling NODE_NETWORK services\n");
+ node.connman->AddLocalServices(NODE_NETWORK);
+ }
+ LogPrintf("[snapshot] restarting indexes\n");
+ // Drain the validation interface queue to ensure that the old indexes
+ // don't have any pending work.
+ Assert(node.validation_signals)->SyncWithValidationInterfaceQueue();
+ for (auto* index : node.indexes) {
+ index->Interrupt();
+ index->Stop();
+ if (!(index->Init() && index->StartBackgroundSync())) {
+ LogPrintf("[snapshot] WARNING failed to restart index %s on snapshot chain\n", index->GetName());
+ }
+ }
+ };
+ node::ChainstateLoadOptions options;
+ options.mempool = Assert(node.mempool.get());
+ options.wipe_block_tree_db = do_reindex;
+ options.wipe_chainstate_db = do_reindex || do_reindex_chainstate;
+ options.prune = chainman.m_blockman.IsPruneMode();
+ options.check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
+ options.check_level = args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
+ options.require_full_verification = args.IsArgSet("-checkblocks") || args.IsArgSet("-checklevel");
+ options.coins_error_cb = [] {
+ uiInterface.ThreadSafeMessageBox(
+ _("Error reading from database, shutting down."),
+ "", CClientUIInterface::MSG_ERROR);
+ };
+ uiInterface.InitMessage(_("Loading block index…").translated);
+ const auto load_block_index_start_time{SteadyClock::now()};
+ auto catch_exceptions = [](auto&& f) {
+ try {
+ return f();
+ } catch (const std::exception& e) {
+ LogError("%s\n", e.what());
+ return std::make_tuple(node::ChainstateLoadStatus::FAILURE, _("Error opening block database"));
+ }
+ };
+ auto [status, error] = catch_exceptions([&] { return LoadChainstate(chainman, cache_sizes, options); });
+ if (status == node::ChainstateLoadStatus::SUCCESS) {
+ uiInterface.InitMessage(_("Verifying blocks…").translated);
+ if (chainman.m_blockman.m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
+ LogWarning("pruned datadir may not have more than %d blocks; only checking available blocks\n",
+ MIN_BLOCKS_TO_KEEP);
+ }
+ std::tie(status, error) = catch_exceptions([&] { return VerifyLoadedChainstate(chainman, options); });
+ if (status == node::ChainstateLoadStatus::SUCCESS) {
+ LogPrintf(" block index %15dms\n", Ticks<std::chrono::milliseconds>(SteadyClock::now() - load_block_index_start_time));
+ }
+ }
+ return {status, error};
+};
+
bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
{
const ArgsManager& args = *Assert(node.args);
@@ -1155,7 +1291,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
return false;
}
- LogPrintf("Using at most %i automatic connections (%i file descriptors available)\n", nMaxConnections, nFD);
+ LogPrintf("Using at most %i automatic connections (%i file descriptors available)\n", nMaxConnections, available_fds);
// Warn about relative -datadir path.
if (args.IsArgSet("-datadir") && !args.GetPathArg("-datadir").is_absolute()) {
@@ -1183,7 +1319,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
constexpr uint64_t min_disk_space = 50 << 20; // 50 MB
if (!CheckDiskSpace(args.GetBlocksDirPath(), min_disk_space)) {
LogError("Shutting down due to lack of disk space!\n");
- if (!(*Assert(node.shutdown))()) {
+ if (!(Assert(node.shutdown_request))()) {
LogError("Failed to send shutdown signal after disk space check\n");
}
}
@@ -1200,6 +1336,17 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
g_wallet_init_interface.Construct(node);
uiInterface.InitWallet();
+ if (interfaces::Ipc* ipc = node.init->ipc()) {
+ for (std::string address : gArgs.GetArgs("-ipcbind")) {
+ try {
+ ipc->listenAddress(address);
+ } catch (const std::exception& e) {
+ return InitError(strprintf(Untranslated("Unable to bind to IPC address '%s'. %s"), address, e.what()));
+ }
+ LogPrintf("Listening for IPC requests on address %s\n", address);
+ }
+ }
+
/* Register RPC commands regardless of -server setting so they will be
* available in the GUI RPC console even if external calls are disabled.
*/
@@ -1211,6 +1358,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
RegisterZMQRPCCommands(tableRPC);
#endif
+ // Check port numbers
+ if (!CheckHostPortOptions(args)) return false;
+
/* Start the RPC server already. It will be started in "warmup" mode
* and not really process calls already (but it will signify connections
* that the server is there and will be ready later). Warmup mode will
@@ -1301,50 +1451,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
validation_signals.RegisterValidationInterface(fee_estimator);
}
- // Check port numbers
- for (const std::string port_option : {
- "-port",
- "-rpcport",
- }) {
- if (args.IsArgSet(port_option)) {
- const std::string port = args.GetArg(port_option, "");
- uint16_t n;
- if (!ParseUInt16(port, &n) || n == 0) {
- return InitError(InvalidPortErrMsg(port_option, port));
- }
- }
- }
-
- for ([[maybe_unused]] const auto& [arg, unix] : std::vector<std::pair<std::string, bool>>{
- // arg name UNIX socket support
- {"-i2psam", false},
- {"-onion", true},
- {"-proxy", true},
- {"-rpcbind", false},
- {"-torcontrol", false},
- {"-whitebind", false},
- {"-zmqpubhashblock", true},
- {"-zmqpubhashtx", true},
- {"-zmqpubrawblock", true},
- {"-zmqpubrawtx", true},
- {"-zmqpubsequence", true},
- }) {
- for (const std::string& socket_addr : args.GetArgs(arg)) {
- std::string host_out;
- uint16_t port_out{0};
- if (!SplitHostPort(socket_addr, port_out, host_out)) {
-#ifdef HAVE_SOCKADDR_UN
- // Allow unix domain sockets for some options e.g. unix:/some/file/path
- if (!unix || socket_addr.find(ADDR_PREFIX_UNIX) != 0) {
- return InitError(InvalidPortErrMsg(arg, socket_addr));
- }
-#else
- return InitError(InvalidPortErrMsg(arg, socket_addr));
-#endif
- }
- }
- }
-
for (const std::string& socket_addr : args.GetArgs("-bind")) {
std::string host_out;
uint16_t port_out{0};
@@ -1494,22 +1600,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
// ********************************************************* Step 7: load block chain
- node.notifications = std::make_unique<KernelNotifications>(*Assert(node.shutdown), node.exit_status, *Assert(node.warnings));
- ReadNotificationArgs(args, *node.notifications);
- ChainstateManager::Options chainman_opts{
- .chainparams = chainparams,
- .datadir = args.GetDataDirNet(),
- .notifications = *node.notifications,
- .signals = &validation_signals,
- };
- Assert(ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
-
- BlockManager::Options blockman_opts{
- .chainparams = chainman_opts.chainparams,
- .blocks_dir = args.GetBlocksDirPath(),
- .notifications = chainman_opts.notifications,
- };
- Assert(ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction
+ node.notifications = std::make_unique<KernelNotifications>(Assert(node.shutdown_request), node.exit_status, *Assert(node.warnings));
+ auto& kernel_notifications{*node.notifications};
+ ReadNotificationArgs(args, kernel_notifications);
// cache size calculations
CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size());
@@ -1528,114 +1621,39 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
assert(!node.mempool);
assert(!node.chainman);
- CTxMemPool::Options mempool_opts{
- .check_ratio = chainparams.DefaultConsistencyChecks() ? 1 : 0,
- .signals = &validation_signals,
- };
- auto result{ApplyArgsManOptions(args, chainparams, mempool_opts)};
- if (!result) {
- return InitError(util::ErrorString(result));
- }
-
bool do_reindex{args.GetBoolArg("-reindex", false)};
const bool do_reindex_chainstate{args.GetBoolArg("-reindex-chainstate", false)};
- for (bool fLoaded = false; !fLoaded && !ShutdownRequested(node);) {
- bilingual_str mempool_error;
- node.mempool = std::make_unique<CTxMemPool>(mempool_opts, mempool_error);
- if (!mempool_error.empty()) {
- return InitError(mempool_error);
- }
- LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), mempool_opts.max_size_bytes * (1.0 / 1024 / 1024));
-
- try {
- node.chainman = std::make_unique<ChainstateManager>(*Assert(node.shutdown), chainman_opts, blockman_opts);
- } catch (std::exception& e) {
- return InitError(strprintf(Untranslated("Failed to initialize ChainstateManager: %s"), e.what()));
- }
- ChainstateManager& chainman = *node.chainman;
-
- // This is defined and set here instead of inline in validation.h to avoid a hard
- // dependency between validation and index/base, since the latter is not in
- // libbitcoinkernel.
- chainman.restart_indexes = [&node]() {
- LogPrintf("[snapshot] restarting indexes\n");
-
- // Drain the validation interface queue to ensure that the old indexes
- // don't have any pending work.
- Assert(node.validation_signals)->SyncWithValidationInterfaceQueue();
-
- for (auto* index : node.indexes) {
- index->Interrupt();
- index->Stop();
- if (!(index->Init() && index->StartBackgroundSync())) {
- LogPrintf("[snapshot] WARNING failed to restart index %s on snapshot chain\n", index->GetName());
- }
- }
- };
-
- node::ChainstateLoadOptions options;
- options.mempool = Assert(node.mempool.get());
- options.wipe_block_tree_db = do_reindex;
- options.wipe_chainstate_db = do_reindex || do_reindex_chainstate;
- options.prune = chainman.m_blockman.IsPruneMode();
- options.check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
- options.check_level = args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
- options.require_full_verification = args.IsArgSet("-checkblocks") || args.IsArgSet("-checklevel");
- options.coins_error_cb = [] {
- uiInterface.ThreadSafeMessageBox(
- _("Error reading from database, shutting down."),
- "", CClientUIInterface::MSG_ERROR);
- };
-
- uiInterface.InitMessage(_("Loading block index…").translated);
- const auto load_block_index_start_time{SteadyClock::now()};
- auto catch_exceptions = [](auto&& f) {
- try {
- return f();
- } catch (const std::exception& e) {
- LogError("%s\n", e.what());
- return std::make_tuple(node::ChainstateLoadStatus::FAILURE, _("Error opening block database"));
- }
- };
- auto [status, error] = catch_exceptions([&]{ return LoadChainstate(chainman, cache_sizes, options); });
- if (status == node::ChainstateLoadStatus::SUCCESS) {
- uiInterface.InitMessage(_("Verifying blocks…").translated);
- if (chainman.m_blockman.m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
- LogWarning("pruned datadir may not have more than %d blocks; only checking available blocks\n",
- MIN_BLOCKS_TO_KEEP);
- }
- std::tie(status, error) = catch_exceptions([&]{ return VerifyLoadedChainstate(chainman, options);});
- if (status == node::ChainstateLoadStatus::SUCCESS) {
- fLoaded = true;
- LogPrintf(" block index %15dms\n", Ticks<std::chrono::milliseconds>(SteadyClock::now() - load_block_index_start_time));
- }
- }
-
- if (status == node::ChainstateLoadStatus::FAILURE_FATAL || status == node::ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB || status == node::ChainstateLoadStatus::FAILURE_INSUFFICIENT_DBCACHE) {
- return InitError(error);
+ // Chainstate initialization and loading may be retried once with reindexing by GUI users
+ auto [status, error] = InitAndLoadChainstate(
+ node,
+ do_reindex,
+ do_reindex_chainstate,
+ cache_sizes,
+ args);
+ if (status == ChainstateLoadStatus::FAILURE && !do_reindex && !ShutdownRequested(node)) {
+ // suggest a reindex
+ bool do_retry = uiInterface.ThreadSafeQuestion(
+ error + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
+ error.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
+ "", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
+ if (!do_retry) {
+ LogError("Aborted block database rebuild. Exiting.\n");
+ return false;
}
-
- if (!fLoaded && !ShutdownRequested(node)) {
- // first suggest a reindex
- if (!do_reindex) {
- bool fRet = uiInterface.ThreadSafeQuestion(
- error + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
- error.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
- "", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
- if (fRet) {
- do_reindex = true;
- if (!Assert(node.shutdown)->reset()) {
- LogError("Internal error: failed to reset shutdown signal.\n");
- }
- } else {
- LogError("Aborted block database rebuild. Exiting.\n");
- return false;
- }
- } else {
- return InitError(error);
- }
+ do_reindex = true;
+ if (!Assert(node.shutdown_signal)->reset()) {
+ LogError("Internal error: failed to reset shutdown signal.\n");
}
+ std::tie(status, error) = InitAndLoadChainstate(
+ node,
+ do_reindex,
+ do_reindex_chainstate,
+ cache_sizes,
+ args);
+ }
+ if (status != ChainstateLoadStatus::SUCCESS && status != ChainstateLoadStatus::INTERRUPTED) {
+ return InitError(error);
}
// As LoadBlockIndex can take several minutes, it's possible the user
@@ -1695,8 +1713,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
}
}
} else {
- LogPrintf("Setting NODE_NETWORK on non-prune mode\n");
- nLocalServices = ServiceFlags(nLocalServices | NODE_NETWORK);
+ // Prior to setting NODE_NETWORK, check if we can provide historical blocks.
+ if (!WITH_LOCK(chainman.GetMutex(), return chainman.BackgroundSyncInProgress())) {
+ LogPrintf("Setting NODE_NETWORK on non-prune mode\n");
+ g_local_services = ServiceFlags(g_local_services | NODE_NETWORK);
+ } else {
+ LogPrintf("Running node in NODE_NETWORK_LIMITED mode until snapshot background sync completes\n");
+ }
}
// ********************************************************* Step 11: import blocks
@@ -1731,15 +1754,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
}
}
- // Either install a handler to notify us when genesis activates, or set fHaveGenesis directly.
- // No locking, as this happens before any background thread is started.
- boost::signals2::connection block_notify_genesis_wait_connection;
- if (WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip() == nullptr)) {
- block_notify_genesis_wait_connection = uiInterface.NotifyBlockTip_connect(std::bind(BlockNotifyGenesisWait, std::placeholders::_2));
- } else {
- fHaveGenesis = true;
- }
-
#if HAVE_SYSTEM
const std::string block_notify = args.GetArg("-blocknotify", "");
if (!block_notify.empty()) {
@@ -1758,13 +1772,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
vImportFiles.push_back(fs::PathFromString(strFile));
}
- chainman.m_thread_load = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
+ node.background_init_thread = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
ScheduleBatchPriority();
// Import blocks
ImportBlocks(chainman, vImportFiles);
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
LogPrintf("Stopping after block import\n");
- if (!(*Assert(node.shutdown))()) {
+ if (!(Assert(node.shutdown_request))()) {
LogError("Failed to send shutdown signal after finishing block import\n");
}
return;
@@ -1784,15 +1798,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
});
// Wait for genesis block to be processed
- {
- WAIT_LOCK(g_genesis_wait_mutex, lock);
- // We previously could hang here if shutdown was requested prior to
- // ImportBlocks getting started, so instead we just wait on a timer to
- // check ShutdownRequested() regularly.
- while (!fHaveGenesis && !ShutdownRequested(node)) {
- g_genesis_wait_cv.wait_for(lock, std::chrono::milliseconds(500));
- }
- block_notify_genesis_wait_connection.disconnect();
+ if (WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip() == nullptr)) {
+ WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
+ kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
+ return !kernel_notifications.m_tip_block.IsNull() || ShutdownRequested(node);
+ });
}
if (ShutdownRequested(node)) {
@@ -1801,17 +1811,17 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
// ********************************************************* Step 12: start node
- //// debug print
int64_t best_block_time{};
{
- LOCK(cs_main);
+ LOCK(chainman.GetMutex());
+ const auto& tip{*Assert(chainman.ActiveTip())};
LogPrintf("block tree size = %u\n", chainman.BlockIndex().size());
- chain_active_height = chainman.ActiveChain().Height();
- best_block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : chainman.GetParams().GenesisBlock().GetBlockTime();
+ chain_active_height = tip.nHeight;
+ best_block_time = tip.GetBlockTime();
if (tip_info) {
tip_info->block_height = chain_active_height;
tip_info->block_time = best_block_time;
- tip_info->verification_progress = GuessVerificationProgress(chainman.GetParams().TxData(), chainman.ActiveChain().Tip());
+ tip_info->verification_progress = GuessVerificationProgress(chainman.GetParams().TxData(), &tip);
}
if (tip_info && chainman.m_best_header) {
tip_info->header_height = chainman.m_best_header->nHeight;
@@ -1821,11 +1831,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
LogPrintf("nBestHeight = %d\n", chain_active_height);
if (node.peerman) node.peerman->SetBestBlock(chain_active_height, std::chrono::seconds{best_block_time});
- // Map ports with UPnP or NAT-PMP.
+ // Map ports with UPnP or NAT-PMP
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), args.GetBoolArg("-natpmp", DEFAULT_NATPMP));
CConnman::Options connOptions;
- connOptions.nLocalServices = nLocalServices;
+ connOptions.m_local_services = g_local_services;
connOptions.m_max_automatic_connections = nMaxConnections;
connOptions.uiInterface = &uiInterface;
connOptions.m_banman = node.banman.get();
@@ -1980,11 +1990,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
// cannot yet be called. Before we make it callable, we need to make sure
// that the RPC's view of the best block is valid and consistent with
// ChainstateManager's active tip.
- //
- // If we do not do this, RPC's view of the best block will be height=0 and
- // hash=0x0. This will lead to erroroneous responses for things like
- // waitforblockheight.
- RPCNotifyBlockChange(WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip()));
SetRPCWarmupFinished();
uiInterface.InitMessage(_("Done loading").translated);