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) 2018-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <bitcoin-build-config.h> // IWYU pragma: keep7#include <banman.h>8#include <blockfilter.h>9#include <chain.h>10#include <chainparams.h>11#include <coins.h>12#include <common/args.h>13#include <common/settings.h>14#include <consensus/amount.h>15#include <consensus/merkle.h>16#include <consensus/validation.h>17#include <external_signer.h>18#include <httprpc.h>19#include <index/blockfilterindex.h>20#include <init.h>21#include <interfaces/chain.h>22#include <interfaces/handler.h>23#include <interfaces/mining.h>24#include <interfaces/node.h>25#include <interfaces/rpc.h>26#include <interfaces/types.h>27#include <kernel/context.h>28#include <key.h>29#include <logging.h>30#include <mapport.h>31#include <net.h>32#include <net_processing.h>33#include <net_types.h>34#include <netaddress.h>35#include <netbase.h>36#include <node/blockstorage.h>37#include <node/coin.h>38#include <node/context.h>39#include <node/interface_ui.h>40#include <node/kernel_notifications.h>41#include <node/miner.h>42#include <node/mini_miner.h>43#include <node/mining_args.h>44#include <node/mining_types.h>45#include <node/transaction.h>46#include <node/types.h>47#include <node/warnings.h>48#include <policy/feerate.h>49#include <policy/fees/estimator_man.h>50#include <policy/policy.h>51#include <policy/rbf.h>52#include <primitives/block.h>53#include <primitives/transaction.h>54#include <rpc/blockchain.h>55#include <rpc/protocol.h>56#include <rpc/request.h>57#include <rpc/server.h>58#include <sync.h>59#include <txmempool.h>60#include <uint256.h>61#include <univalue.h>62#include <util/btcsignals.h>63#include <util/check.h>64#include <util/expected.h>65#include <util/fees.h>66#include <util/result.h>67#include <util/signalinterrupt.h>68#include <util/string.h>69#include <util/time.h>70#include <util/translation.h>71#include <validation.h>72#include <validationinterface.h>74#include <any>75#include <atomic>76#include <condition_variable>77#include <cstdint>78#include <cstdlib>79#include <functional>80#include <map>81#include <memory>82#include <optional>83#include <string>84#include <tuple>85#include <utility>86#include <vector>88using interfaces::BlockRef;89using interfaces::BlockTemplate;90using interfaces::BlockTip;91using interfaces::Chain;92using interfaces::FoundBlock;93using interfaces::Handler;94using interfaces::MakeSignalHandler;95using interfaces::Mining;96using interfaces::Node;97using interfaces::Rpc;98using interfaces::WalletLoader;99using kernel::ChainstateRole;100using node::BlockAssembler;101using node::BlockCreateOptions;102using node::BlockWaitOptions;103using node::CoinbaseTx;104using util::Join;106namespace node {107// All members of the classes in this namespace are intentionally public, as the108// classes themselves are private.109namespace {110#ifdef ENABLE_EXTERNAL_SIGNER111class ExternalSignerImpl : public interfaces::ExternalSigner112{113public:114ExternalSignerImpl(::ExternalSigner signer) : m_signer(std::move(signer)) {}115std::string getName() override { return m_signer.m_name; }116::ExternalSigner m_signer;117};118#endif120class NodeImpl : public Node121{122public:123explicit NodeImpl(NodeContext& context) { setContext(&context); }124void initLogging() override { InitLogging(args()); }125void initParameterInteraction() override { InitParameterInteraction(args()); }126bilingual_str getWarnings() override { return Join(Assert(m_context->warnings)->GetMessages(), Untranslated("<hr />")); }127int getExitStatus() override { return Assert(m_context)->exit_status.load(); }128BCLog::CategoryMask getLogCategories() override { return LogInstance().GetCategoryMask(); }129bool baseInitialize() override130{131if (!AppInitBasicSetup(args(), Assert(context())->exit_status)) return false;132if (!AppInitParameterInteraction(args())) return false;134m_context->warnings = std::make_unique<node::Warnings>();135m_context->kernel = std::make_unique<kernel::Context>();136m_context->ecc_context = std::make_unique<ECC_Context>();137if (!AppInitSanityChecks(*m_context->kernel)) return false;139if (!AppInitLockDirectories()) return false;140if (!AppInitInterfaces(*m_context)) return false;142return true;143}144bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override145{146if (AppInitMain(*m_context, tip_info)) return true;147// Error during initialization, set exit status before continue148m_context->exit_status.store(EXIT_FAILURE);149return false;150}151void appShutdown() override152{153Shutdown(*m_context);154}155void startShutdown() override156{157NodeContext& ctx{*Assert(m_context)};158if (!(Assert(ctx.shutdown_request))()) {159LogError("Failed to send shutdown signal\n");160}161Interrupt(*m_context);162}163bool shutdownRequested() override { return ShutdownRequested(*Assert(m_context)); };164bool isSettingIgnored(const std::string& name) override165{166bool ignored = false;167args().LockSettings([&](common::Settings& settings) {168if (auto* options = common::FindKey(settings.command_line_options, name)) {169ignored = !options->empty();170}171});172return ignored;173}174common::SettingsValue getPersistentSetting(const std::string& name) override { return args().GetPersistentSetting(name); }175void updateRwSetting(const std::string& name, const common::SettingsValue& value) override176{177args().LockSettings([&](common::Settings& settings) {178if (value.isNull()) {179settings.rw_settings.erase(name);180} else {181settings.rw_settings[name] = value;182}183});184args().WriteSettingsFile();185}186void forceSetting(const std::string& name, const common::SettingsValue& value) override187{188args().LockSettings([&](common::Settings& settings) {189if (value.isNull()) {190settings.forced_settings.erase(name);191} else {192settings.forced_settings[name] = value;193}194});195}196void resetSettings() override197{198args().WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true);199args().LockSettings([&](common::Settings& settings) {200settings.rw_settings.clear();201});202args().WriteSettingsFile();203}204void mapPort(bool enable) override { StartMapPort(enable); }205std::optional<Proxy> getProxy(Network net) override { return GetProxy(net); }206size_t getNodeCount(ConnectionDirection flags) override207{208return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;209}210bool getNodesStats(NodesStats& stats) override211{212stats.clear();214if (m_context->connman) {215std::vector<CNodeStats> stats_temp;216m_context->connman->GetNodeStats(stats_temp);218stats.reserve(stats_temp.size());219for (auto& node_stats_temp : stats_temp) {220stats.emplace_back(std::move(node_stats_temp), false, CNodeStateStats());221}223// Try to retrieve the CNodeStateStats for each node.224if (m_context->peerman) {225TRY_LOCK(::cs_main, lockMain);226if (lockMain) {227for (auto& node_stats : stats) {228std::get<1>(node_stats) =229m_context->peerman->GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));230}231}232}233return true;234}235return false;236}237bool getBanned(banmap_t& banmap) override238{239if (m_context->banman) {240m_context->banman->GetBanned(banmap);241return true;242}243return false;244}245bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) override246{247if (m_context->banman) {248m_context->banman->Ban(net_addr, ban_time_offset);249return true;250}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.