Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
init.cpp File Reference
#include <bitcoin-build-config.h>
#include <init.h>
#include <kernel/checks.h>
#include <addrman.h>
#include <banman.h>
#include <blockfilter.h>
#include <chain.h>
#include <chainparams.h>
#include <chainparamsbase.h>
#include <clientversion.h>
#include <common/args.h>
#include <common/system.h>
#include <consensus/amount.h>
#include <consensus/consensus.h>
#include <deploymentstatus.h>
#include <hash.h>
#include <httprpc.h>
#include <httpserver.h>
#include <index/blockfilterindex.h>
#include <index/coinstatsindex.h>
#include <index/txindex.h>
#include <index/txospenderindex.h>
#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 <ipc/exception.h>
#include <kernel/caches.h>
#include <kernel/context.h>
#include <key.h>
#include <logging.h>
#include <mapport.h>
#include <net.h>
#include <net_permissions.h>
#include <net_processing.h>
#include <netbase.h>
#include <netgroup.h>
#include <node/blockmanager_args.h>
#include <node/blockstorage.h>
#include <node/caches.h>
#include <node/chainstate.h>
#include <node/chainstatemanager_args.h>
#include <node/context.h>
#include <node/interface_ui.h>
#include <node/kernel_notifications.h>
#include <node/mempool_args.h>
#include <node/mempool_persist.h>
#include <node/mempool_persist_args.h>
#include <node/miner.h>
#include <node/peerman_args.h>
#include <policy/feerate.h>
#include <policy/fees/block_policy_estimator.h>
#include <policy/fees/block_policy_estimator_args.h>
#include <policy/policy.h>
#include <policy/settings.h>
#include <protocol.h>
#include <rpc/blockchain.h>
#include <rpc/register.h>
#include <rpc/server.h>
#include <rpc/util.h>
#include <scheduler.h>
#include <script/sigcache.h>
#include <sync.h>
#include <torcontrol.h>
#include <txdb.h>
#include <txmempool.h>
#include <util/asmap.h>
#include <util/batchpriority.h>
#include <util/chaintype.h>
#include <util/check.h>
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/moneystr.h>
#include <util/result.h>
#include <util/signalinterrupt.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/syserror.h>
#include <util/thread.h>
#include <util/threadnames.h>
#include <util/time.h>
#include <util/translation.h>
#include <validation.h>
#include <validationinterface.h>
#include <walletinitinterface.h>
#include <algorithm>
#include <cerrno>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <functional>
#include <set>
#include <string>
#include <thread>
#include <vector>
#include <csignal>
#include <sys/stat.h>
#include <boost/signals2/signal.hpp>

Go to the source code of this file.

Classes

class  BlockManager
 Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-work tip is. More...
class  KernelNotifications

Macros

#define MIN_LEVELDB_FDS   150

Typedefs

using ChainstateLoadResult
 Chainstate load status code and optional error string.

Enumerations

enum class  ChainstateLoadStatus

Functions

static fs::path GetPidFile (const ArgsManager &args)
static bool CreatePidFile (const ArgsManager &args)
static void RemovePidFile (const ArgsManager &args)
void InitContext (NodeContext &node)
 Initialize node context shutdown and args variables.
bool ShutdownRequested (node::NodeContext &node)
 Return whether node shutdown was requested.
void Interrupt (NodeContext &node)
 Interrupt threads.
void Shutdown (NodeContext &node)
static void HandleSIGTERM (int)
 Signal handlers are very limited in what they are allowed to do.
static void HandleSIGHUP (int)
static void registerSignalHandler (int signal, void(*handler)(int))
void SetupServerArgs (ArgsManager &argsman, bool can_listen_ipc)
 Register all arguments with the ArgsManager.
static bool AppInitServers (NodeContext &node)
void InitParameterInteraction (ArgsManager &args)
 Parameter interaction: change current parameters depending on various rules.
void InitLogging (const ArgsManager &args)
 Initialize global loggers.
static void new_handler_terminate ()
bool AppInitBasicSetup (const ArgsManager &args, std::atomic< int > &exit_status)
 Initialize bitcoin core: Basic context setup.
bool AppInitParameterInteraction (const ArgsManager &args)
 Initialization: parameter interaction.
static bool LockDirectory (const fs::path &dir, bool probeOnly)
static bool LockDirectories (bool probeOnly)
bool AppInitSanityChecks (const kernel::Context &kernel)
 Initialization sanity checks.
bool AppInitLockDirectories ()
 Lock bitcoin core critical directories.
bool AppInitInterfaces (NodeContext &node)
 Initialize node and wallet interface pointers.
bool CheckHostPortOptions (const ArgsManager &args)
static std::optional< CServiceCheckBindingConflicts (const CConnman::Options &conn_options)
 Checks for duplicate bindings across all binding configurations.
static ChainstateLoadResult InitAndLoadChainstate (NodeContext &node, bool do_reindex, const bool do_reindex_chainstate, const kernel::CacheSizes &cache_sizes, const ArgsManager &args)
bool AppInitMain (NodeContext &node, interfaces::BlockAndHeaderTipInfo *tip_info)
 Bitcoin core main initialization.
bool StartIndexBackgroundSync (NodeContext &node)
 Validates requirements to run the indexes and spawns each index initial sync thread.
bilingual_str AmountErrMsg (const std::string &optname, const std::string &strValue)
bilingual_str InvalidPortErrMsg (const std::string &optname, const std::string &invalid_value)
bilingual_str ResolveErrMsg (const std::string &optname, const std::string &strBind)
util::Result< void > ApplyArgsManOptions (const ArgsManager &args, BlockManager::Options &opts)
CacheSizes CalculateCacheSizes (const ArgsManager &args, size_t n_indexes)
bool DumpMempool (const CTxMemPool &pool, const fs::path &dump_path, fsbridge::FopenFn mockable_fopen_function=fsbridge::fopen, bool skip_file_commit=false)
 Dump the mempool to a file.
void ImportBlocks (ChainstateManager &chainman, std::span< const fs::path > import_paths)
ChainstateLoadResult LoadChainstate (ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options)
bool LoadMempool (CTxMemPool &pool, const fs::path &load_path, Chainstate &active_chainstate, ImportMempoolOptions &&opts)
 Import the file and attempt to add its contents to the mempool.
fs::path MempoolPath (const ArgsManager &argsman)
bool ShouldPersistMempool (const ArgsManager &argsman)
ChainstateLoadResult VerifyLoadedChainstate (ChainstateManager &chainman, const ChainstateLoadOptions &options)
template<typename C, typename S, typename UnaryOp>
auto Join (const C &container, const S &separator, UnaryOp unary_op)
 Join all container items.
void ReplaceAll (std::string &in_out, const std::string &search, const std::string &substitute)
template<typename T>
std::string ToString (const T &t)
 Locale-independent version of std::to_string.

Variables

static constexpr bool DEFAULT_PROXYRANDOMIZE {true}
static constexpr bool DEFAULT_REST_ENABLE {false}
static constexpr bool DEFAULT_I2P_ACCEPT_INCOMING {true}
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT {false}
static constexpr int MIN_CORE_FDS = MIN_LEVELDB_FDS + NUM_FDS_MESSAGE_CAPTURE
static const char * BITCOIN_PID_FILENAME = "bitcoind.pid"
 The PID file facilities.
static bool g_generated_pid {false}
 True if this process has created a PID file.
static std::optional< util::SignalInterruptg_shutdown
static constexpr bool DEFAULT_PERSIST_MEMPOOL
 Default for -persistmempool, indicating whether the node should attempt to automatically load the mempool on start and save to disk on shutdown.
static const bool DEFAULT_PRINT_MODIFIED_FEE
static constexpr int DEFAULT_STOPATHEIGHT

Macro Definition Documentation

◆ MIN_LEVELDB_FDS

#define MIN_LEVELDB_FDS   150

Definition at line 163 of file init.cpp.

Typedef Documentation

◆ ChainstateLoadResult

Chainstate load status code and optional error string.

Definition at line 54 of file chainstate.h.

Enumeration Type Documentation

◆ ChainstateLoadStatus

enum class node::ChainstateLoadStatus
strong

Chainstate load status. Simple applications can just check for the success case, and treat other cases as errors. More complex applications may want to try reindexing in the generic failure case, and pass an interrupt callback and exit cleanly in the interrupted case.

Definition at line 44 of file chainstate.h.

Function Documentation

◆ AmountErrMsg()

bilingual_str common::AmountErrMsg ( const std::string & optname,
const std::string & strValue )

Definition at line 166 of file messages.cpp.

Here is the caller graph for this function:

◆ AppInitBasicSetup()

bool AppInitBasicSetup ( const ArgsManager & args,
std::atomic< int > & exit_status )

Initialize bitcoin core: Basic context setup.

Note
This can be done before daemonization. Do not call Shutdown() if this function fails.
Precondition
Parameters should be parsed and config file should be read.

Definition at line 882 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppInitInterfaces()

bool AppInitInterfaces ( node::NodeContext & node)

Initialize node and wallet interface pointers.

Has no prerequisites or side effects besides allocating memory.

Definition at line 1207 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppInitLockDirectories()

bool AppInitLockDirectories ( )

Lock bitcoin core critical directories.

Note
This should only be done after daemonization. Do not call Shutdown() if this function fails.
Precondition
Parameters should be parsed and config file should be read, AppInitSanityChecks should have been called.

Definition at line 1195 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppInitMain()

bool AppInitMain ( node::NodeContext & node,
interfaces::BlockAndHeaderTipInfo * tip_info = nullptr )

Bitcoin core main initialization.

Note
This should only be done after daemonization. Call Shutdown() if this function fails.
Precondition
Parameters should be parsed and config file should be read, AppInitLockDirectories should have been called.

Definition at line 1421 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppInitParameterInteraction()

bool AppInitParameterInteraction ( const ArgsManager & args)

Initialization: parameter interaction.

Note
This can be done before daemonization. Do not call Shutdown() if this function fails.
Precondition
Parameters should be parsed and config file should be read, AppInitBasicSetup should have been called.

Definition at line 919 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppInitSanityChecks()

bool AppInitSanityChecks ( const kernel::Context & kernel)

Initialization sanity checks.

Note
This can be done before daemonization. Do not call Shutdown() if this function fails.
Precondition
Parameters should be parsed and config file should be read, AppInitParameterInteraction should have been called.

Definition at line 1176 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppInitServers()

bool AppInitServers ( NodeContext & node)
static

Definition at line 748 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ApplyArgsManOptions()

util::Result< void > node::ApplyArgsManOptions ( const ArgsManager & args,
BlockManager::Options & opts )
nodiscard

Definition at line 18 of file blockmanager_args.cpp.

Here is the caller graph for this function:

◆ CalculateCacheSizes()

CacheSizes node::CalculateCacheSizes ( const ArgsManager & args,
size_t n_indexes = 0 )

Definition at line 57 of file caches.cpp.

◆ CheckBindingConflicts()

std::optional< CService > CheckBindingConflicts ( const CConnman::Options & conn_options)
static

Checks for duplicate bindings across all binding configurations.

Parameters
[in]conn_optionsConnection options containing the binding vectors to check
Returns
std::optional<CService> containing the first duplicate found, or std::nullopt if no duplicates

Definition at line 1271 of file init.cpp.

◆ CheckHostPortOptions()

bool CheckHostPortOptions ( const ArgsManager & args)

Definition at line 1216 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreatePidFile()

bool CreatePidFile ( const ArgsManager & args)
staticnodiscard

Definition at line 183 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DumpMempool()

bool node::DumpMempool ( const CTxMemPool & pool,
const fs::path & dump_path,
fsbridge::FopenFn mockable_fopen_function = fsbridge::fopen,
bool skip_file_commit = false )

Dump the mempool to a file.

Definition at line 153 of file mempool_persist.cpp.

Here is the caller graph for this function:

◆ GetPidFile()

fs::path GetPidFile ( const ArgsManager & args)
static

Definition at line 178 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleSIGHUP()

void HandleSIGHUP ( int )
static

Definition at line 432 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleSIGTERM()

void HandleSIGTERM ( int )
static

Signal handlers are very limited in what they are allowed to do.

The execution context the handler is invoked in is not guaranteed, so we restrict handler operations to just touching variables:

Definition at line 425 of file init.cpp.

Here is the caller graph for this function:

◆ ImportBlocks()

void node::ImportBlocks ( ChainstateManager & chainman,
std::span< const fs::path > import_paths )

Definition at line 1261 of file blockstorage.cpp.

◆ InitAndLoadChainstate()

ChainstateLoadResult InitAndLoadChainstate ( NodeContext & node,
bool do_reindex,
const bool do_reindex_chainstate,
const kernel::CacheSizes & cache_sizes,
const ArgsManager & args )
static

Definition at line 1301 of file init.cpp.

Here is the call graph for this function:

◆ InitContext()

void InitContext ( NodeContext & node)

Initialize node context shutdown and args variables.

Definition at line 213 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitLogging()

void InitLogging ( const ArgsManager & args)

Initialize global loggers.

Initialize the logging infrastructure.

Note that this is called very early in the process lifetime, so you should be careful about what global state you rely on here.

Definition at line 853 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitParameterInteraction()

void InitParameterInteraction ( ArgsManager & args)

Parameter interaction: change current parameters depending on various rules.

Definition at line 764 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Interrupt()

void Interrupt ( NodeContext & node)

Interrupt threads.

Definition at line 268 of file init.cpp.

Here is the call graph for this function:

◆ InvalidPortErrMsg()

bilingual_str common::InvalidPortErrMsg ( const std::string & optname,
const std::string & invalid_value )

Definition at line 156 of file messages.cpp.

Here is the caller graph for this function:

◆ Join()

template<typename C, typename S, typename UnaryOp>
auto util::Join ( const C & container,
const S & separator,
UnaryOp unary_op )

Join all container items.

Typically used to concatenate strings but accepts containers with elements of any type.

Parameters
containerThe items to join
separatorThe separator
unary_opApply this operator to each item

Definition at line 205 of file string.h.

Here is the caller graph for this function:

◆ LoadChainstate()

ChainstateLoadResult node::LoadChainstate ( ChainstateManager & chainman,
const CacheSizes & cache_sizes,
const ChainstateLoadOptions & options )

Definition at line 151 of file chainstate.cpp.

Here is the caller graph for this function:

◆ LoadMempool()

bool node::LoadMempool ( CTxMemPool & pool,
const fs::path & load_path,
Chainstate & active_chainstate,
ImportMempoolOptions && opts )

Import the file and attempt to add its contents to the mempool.

Definition at line 43 of file mempool_persist.cpp.

◆ LockDirectories()

bool LockDirectories ( bool probeOnly)
static

Definition at line 1170 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LockDirectory()

bool LockDirectory ( const fs::path & dir,
bool probeOnly )
static

Definition at line 1158 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MempoolPath()

Definition at line 18 of file mempool_persist_args.cpp.

Here is the caller graph for this function:

◆ new_handler_terminate()

void new_handler_terminate ( )
static

Definition at line 869 of file init.cpp.

Here is the caller graph for this function:

◆ registerSignalHandler()

void registerSignalHandler ( int signal,
void(* handler )(int) )
static

Definition at line 449 of file init.cpp.

Here is the caller graph for this function:

◆ RemovePidFile()

void RemovePidFile ( const ArgsManager & args)
static

Definition at line 201 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReplaceAll()

void util::ReplaceAll ( std::string & in_out,
const std::string & search,
const std::string & substitute )

Definition at line 11 of file string.cpp.

◆ ResolveErrMsg()

bilingual_str common::ResolveErrMsg ( const std::string & optname,
const std::string & strBind )

Definition at line 151 of file messages.cpp.

Here is the caller graph for this function:

◆ SetupServerArgs()

void SetupServerArgs ( ArgsManager & argsman,
bool can_listen_ipc )

Register all arguments with the ArgsManager.

Definition at line 459 of file init.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ShouldPersistMempool()

bool node::ShouldPersistMempool ( const ArgsManager & argsman)

Definition at line 13 of file mempool_persist_args.cpp.

Here is the caller graph for this function:

◆ Shutdown()

void Shutdown ( NodeContext & node)

Note: Shutdown() must be able to handle cases in which initialization failed part of the way, for example if the data directory was found to be locked. Be sure that anything that writes files or flushes caches only does this if the respective module was initialized.

Definition at line 288 of file init.cpp.

Here is the call graph for this function:

◆ ShutdownRequested()

bool ShutdownRequested ( node::NodeContext & node)

Return whether node shutdown was requested.

Definition at line 250 of file init.cpp.

◆ StartIndexBackgroundSync()

bool StartIndexBackgroundSync ( NodeContext & node)

Validates requirements to run the indexes and spawns each index initial sync thread.

Definition at line 2315 of file init.cpp.

Here is the call graph for this function:

◆ ToString()

template<typename T>
std::string util::ToString ( const T & t)

Locale-independent version of std::to_string.

Definition at line 246 of file string.h.

◆ VerifyLoadedChainstate()

ChainstateLoadResult node::VerifyLoadedChainstate ( ChainstateManager & chainman,
const ChainstateLoadOptions & options )

Definition at line 240 of file chainstate.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ BITCOIN_PID_FILENAME

const char* BITCOIN_PID_FILENAME = "bitcoind.pid"
static

The PID file facilities.

Definition at line 171 of file init.cpp.

◆ DEFAULT_I2P_ACCEPT_INCOMING

bool DEFAULT_I2P_ACCEPT_INCOMING {true}
staticconstexpr

Definition at line 154 of file init.cpp.

◆ DEFAULT_PERSIST_MEMPOOL

bool node::DEFAULT_PERSIST_MEMPOOL
staticconstexpr

Default for -persistmempool, indicating whether the node should attempt to automatically load the mempool on start and save to disk on shutdown.

Definition at line 18 of file mempool_persist_args.h.

◆ DEFAULT_PRINT_MODIFIED_FEE

Definition at line 40 of file miner.h.

◆ DEFAULT_PROXYRANDOMIZE

bool DEFAULT_PROXYRANDOMIZE {true}
staticconstexpr

Definition at line 152 of file init.cpp.

◆ DEFAULT_REST_ENABLE

bool DEFAULT_REST_ENABLE {false}
staticconstexpr

Definition at line 153 of file init.cpp.

◆ DEFAULT_STOPAFTERBLOCKIMPORT

bool DEFAULT_STOPAFTERBLOCKIMPORT {false}
staticconstexpr

Definition at line 155 of file init.cpp.

◆ DEFAULT_STOPATHEIGHT

int node::DEFAULT_STOPATHEIGHT
staticconstexpr

Definition at line 30 of file kernel_notifications.h.

◆ g_generated_pid

bool g_generated_pid {false}
static

True if this process has created a PID file.

Used to determine whether we should remove the PID file on shutdown.

Definition at line 176 of file init.cpp.

◆ g_shutdown

std::optional<util::SignalInterrupt> g_shutdown
static

Definition at line 211 of file init.cpp.

◆ MIN_CORE_FDS

int MIN_CORE_FDS = MIN_LEVELDB_FDS + NUM_FDS_MESSAGE_CAPTURE
staticconstexpr

Definition at line 166 of file init.cpp.