Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <bitcoin-build-config.h> // IWYU pragma: keep8#include <init.h>10#include <addrdb.h>11#include <addrman.h>12#include <banman.h>13#include <blockfilter.h>14#include <chain.h>15#include <chainparams.h>16#include <chainparamsbase.h>17#include <clientversion.h>18#include <common/args.h>19#include <common/messages.h>20#include <common/system.h>21#include <compat/compat.h>22#include <consensus/params.h>23#include <crypto/hex_base.h>24#include <dbwrapper.h>25#include <httprpc.h>26#include <httpserver.h>27#include <index/base.h>28#include <index/blockfilterindex.h>29#include <index/coinstatsindex.h>30#include <index/txindex.h>31#include <index/txospenderindex.h>32#include <init/common.h>33#include <interfaces/chain.h>34#include <interfaces/init.h>35#include <interfaces/ipc.h>36#include <interfaces/mining.h>37#include <interfaces/node.h>38#include <ipc/exception.h>39#include <kernel/blockmanager_opts.h>40#include <kernel/caches.h>41#include <kernel/chainstatemanager_opts.h>42#include <kernel/checks.h>43#include <kernel/context.h>44#include <kernel/notifications_interface.h>45#include <key.h>46#include <logging.h>47#include <mapport.h>48#include <net.h>49#include <net_permissions.h>50#include <net_processing.h>51#include <netaddress.h>52#include <netbase.h>53#include <netgroup.h>54#include <node/blockmanager_args.h>55#include <node/blockstorage.h>56#include <node/caches.h>57#include <node/chainstate.h>58#include <node/chainstatemanager_args.h>59#include <node/context.h>60#include <node/interface_ui.h>61#include <node/kernel_notifications.h>62#include <node/mempool_args.h>63#include <node/mempool_persist.h>64#include <node/mempool_persist_args.h>65#include <node/mining_args.h>66#include <node/mining_types.h>67#include <node/peerman_args.h>68#include <policy/feerate.h>69#include <policy/fees/block_policy_estimator.h>70#include <policy/fees/estimator_args.h>71#include <policy/fees/estimator_man.h>72#include <policy/policy.h>73#include <policy/settings.h>74#include <protocol.h>75#include <random.h>76#include <rpc/register.h>77#include <rpc/server.h>78#include <rpc/util.h>79#include <scheduler.h>80#include <script/sigcache.h>81#include <sync.h>82#include <tinyformat.h>83#include <torcontrol.h>84#include <txgraph.h>85#include <txmempool.h>86#include <uint256.h>87#include <util/asmap.h>88#include <util/batchpriority.h>89#include <util/btcsignals.h>90#include <util/byte_units.h>91#include <util/chaintype.h>92#include <util/check.h>93#include <util/fs.h>94#include <util/fs_helpers.h>95#include <util/moneystr.h>96#include <util/result.h>97#include <util/signalinterrupt.h>98#include <util/strencodings.h>99#include <util/string.h>100#include <util/syserror.h>101#include <util/thread.h>102#include <util/threadnames.h>103#include <util/time.h>104#include <util/translation.h>105#include <validation.h>106#include <validationinterface.h>107#include <walletinitinterface.h>109#include <algorithm>110#include <any>111#include <cerrno>112#include <condition_variable>113#include <cstddef>114#include <cstdint>115#include <exception>116#include <fstream>117#include <functional>118#include <limits>119#include <list>120#include <memory>121#include <new>122#include <optional>123#include <set>124#include <span>125#include <string>126#include <system_error>127#include <thread>128#include <tuple>129#include <utility>130#include <vector>132#ifndef WIN32133#include <csignal>134#endif136#ifdef ENABLE_ZMQ137#include <zmq/zmqabstractnotifier.h>138#include <zmq/zmqnotificationinterface.h>139#include <zmq/zmqrpc.h>140#endif142#ifdef ENABLE_EMBEDDED_ASMAP143#include <node/data/ip_asn.dat.h>144#endif146using common::InvalidPortErrMsg;147using common::ResolveErrMsg;149using node::ApplyArgsManOptions;150using node::BlockManager;151using node::CalculateCacheSizes;152using node::ChainstateLoadResult;153using node::ChainstateLoadStatus;154using node::DEFAULT_PERSIST_MEMPOOL;155using node::DEFAULT_PRINT_MODIFIED_FEE;156using node::DEFAULT_STOPATHEIGHT;157using node::DumpMempool;158using node::ImportBlocks;159using node::KernelNotifications;160using node::LoadChainstate;161using node::LoadMempool;162using node::MempoolPath;163using node::NodeContext;164using node::ShouldPersistMempool;165using node::VerifyLoadedChainstate;166using util::Join;167using util::ReplaceAll;168using util::ToString;169using util::TrimStringView;171static constexpr bool DEFAULT_PROXYRANDOMIZE{true};172static constexpr bool DEFAULT_REST_ENABLE{false};173static constexpr bool DEFAULT_I2P_ACCEPT_INCOMING{true};174static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};176#ifdef WIN32177// Win32 LevelDB doesn't use filedescriptors, and the ones used for178// accessing block files don't count towards the fd_set size limit179// anyway.180#define MIN_LEVELDB_FDS 0181#else182#define MIN_LEVELDB_FDS 150183#endif185static constexpr int MIN_CORE_FDS = MIN_LEVELDB_FDS + NUM_FDS_MESSAGE_CAPTURE;187/**188* The PID file facilities.189*/190static const char* BITCOIN_PID_FILENAME = "bitcoind.pid";191/**192* True if this process has created a PID file.193* Used to determine whether we should remove the PID file on shutdown.194*/195static bool g_generated_pid{false};197static fs::path GetPidFile(const ArgsManager& args)198{199return AbsPathForConfigVal(args, args.GetPathArg("-pid", BITCOIN_PID_FILENAME));200}202[[nodiscard]] static bool CreatePidFile(const ArgsManager& args)203{204if (args.IsArgNegated("-pid")) return true;206std::ofstream file{GetPidFile(args).std_path()};207if (file) {208#ifdef WIN32209tfm::format(file, "%d\n", GetCurrentProcessId());210#else211tfm::format(file, "%d\n", getpid());212#endif213g_generated_pid = true;214return true;215} else {216return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno)));217}218}220static void RemovePidFile(const ArgsManager& args)221{222if (!g_generated_pid) return;223const auto pid_path{GetPidFile(args)};224if (std::error_code error; !fs::remove(pid_path, error)) {225std::string msg{error ? error.message() : "File does not exist"};226LogWarning("Unable to remove PID file (%s): %s", fs::PathToString(pid_path), msg);227}228}230static std::optional<util::SignalInterrupt> g_shutdown;232void InitContext(NodeContext& node)233{234assert(!g_shutdown);235g_shutdown.emplace();237node.args = &gArgs;238node.shutdown_signal = &*g_shutdown;239node.shutdown_request = [&node] {240assert(node.shutdown_signal);241if (!(*node.shutdown_signal)()) return false;242return true;243};244}246//////////////////////////////////////////////////////////////////////////////247//248// Shutdown249//