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 <validation.h>10#include <arith_uint256.h>11#include <chain.h>12#include <checkqueue.h>13#include <clientversion.h>14#include <consensus/amount.h>15#include <consensus/consensus.h>16#include <consensus/merkle.h>17#include <consensus/tx_check.h>18#include <consensus/tx_verify.h>19#include <consensus/validation.h>20#include <cuckoocache.h>21#include <flatfile.h>22#include <hash.h>23#include <kernel/chainparams.h>24#include <kernel/coinstats.h>25#include <kernel/disconnected_transactions.h>26#include <kernel/mempool_entry.h>27#include <kernel/messagestartchars.h>28#include <kernel/notifications_interface.h>29#include <kernel/types.h>30#include <kernel/warning.h>31#include <logging/timer.h>32#include <node/blockstorage.h>33#include <node/utxo_snapshot.h>34#include <policy/ephemeral_policy.h>35#include <policy/policy.h>36#include <policy/rbf.h>37#include <policy/settings.h>38#include <policy/truc_policy.h>39#include <pow.h>40#include <primitives/block.h>41#include <primitives/transaction.h>42#include <random.h>43#include <script/script.h>44#include <script/sigcache.h>45#include <signet.h>46#include <tinyformat.h>47#include <txdb.h>48#include <txmempool.h>49#include <uint256.h>50#include <undo.h>51#include <util/byte_units.h>52#include <util/check.h>53#include <util/fs.h>54#include <util/fs_helpers.h>55#include <util/hasher.h>56#include <util/log.h>57#include <util/moneystr.h>58#include <util/rbf.h>59#include <util/result.h>60#include <util/signalinterrupt.h>61#include <util/strencodings.h>62#include <util/string.h>63#include <util/threadpool.h>64#include <util/time.h>65#include <util/trace.h>66#include <util/translation.h>67#include <validationinterface.h>69#include <algorithm>70#include <cassert>71#include <chrono>72#include <deque>73#include <numeric>74#include <optional>75#include <ranges>76#include <span>77#include <string>78#include <tuple>79#include <utility>81using kernel::CCoinsStats;82using kernel::ChainstateRole;83using kernel::CoinStatsHashType;84using kernel::ComputeUTXOStats;85using kernel::Notifications;87using fsbridge::FopenFn;88using node::BlockManager;89using node::BlockMap;90using node::CBlockIndexHeightOnlyComparator;91using node::CBlockIndexWorkComparator;92using node::SnapshotMetadata;94/** Time window to wait between writing blocks/block index and chainstate to disk.95* Randomize writing time inside the window to prevent a situation where the96* network over time settles into a few cohorts of synchronized writers.97*/98static constexpr auto DATABASE_WRITE_INTERVAL_MIN{50min};99static constexpr auto DATABASE_WRITE_INTERVAL_MAX{70min};100/** Maximum age of our tip for us to be considered current for fee estimation */101static constexpr std::chrono::hours MAX_FEE_ESTIMATION_TIP_AGE{3};102const std::vector<std::string> CHECKLEVEL_DOC {103"level 0 reads the blocks from disk",104"level 1 verifies block validity",105"level 2 verifies undo data",106"level 3 checks disconnection of tip blocks",107"level 4 tries to reconnect the blocks",108"each level includes the checks of the previous levels",109};110/** The number of blocks to keep below the deepest prune lock.111* There is nothing special about this number. It is higher than what we112* expect to see in regular mainnet reorgs, but not so high that it would113* noticeably interfere with the pruning mechanism.114* */115static constexpr int PRUNE_LOCK_BUFFER{10};117// Return whether the completed full flush should compact chainstate118static bool ShouldCompactChainstate(bool in_ibd)119{120static constexpr uint32_t flush_ratio{320}; // Roughly every 2 weeks with hourly flushes121return !in_ibd && FastRandomContext().randrange(flush_ratio) == 0;122}124TRACEPOINT_SEMAPHORE(validation, block_connected);125TRACEPOINT_SEMAPHORE(utxocache, flush);126TRACEPOINT_SEMAPHORE(mempool, replaced);127TRACEPOINT_SEMAPHORE(mempool, rejected);129const CBlockIndex* Chainstate::FindForkInGlobalIndex(const CBlockLocator& locator) const130{131AssertLockHeld(cs_main);133// Find the latest block common to locator and chain - we expect that134// locator.vHave is sorted descending by height.135for (const uint256& hash : locator.vHave) {136const CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};137if (pindex) {138if (m_chain.Contains(*pindex)) {139return pindex;140}141if (pindex->GetAncestor(m_chain.Height()) == m_chain.Tip()) {142return m_chain.Tip();143}144}145}146return m_chain.Genesis();147}149bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,150const CCoinsViewCache& inputs, script_verify_flags flags, bool cacheSigStore,151bool cacheFullScriptStore, PrecomputedTransactionData& txdata,152ValidationCache& validation_cache,153std::vector<CScriptCheck>* pvChecks = nullptr)154EXCLUSIVE_LOCKS_REQUIRED(cs_main);156bool CheckFinalTxAtTip(const CBlockIndex& active_chain_tip, const CTransaction& tx)157{158AssertLockHeld(cs_main);160// CheckFinalTxAtTip() uses active_chain_tip.Height()+1 to evaluate161// nLockTime because when IsFinalTx() is called within162// AcceptBlock(), the height of the block *being*163// evaluated is what is used. Thus if we want to know if a164// transaction can be part of the *next* block, we need to call165// IsFinalTx() with one more than active_chain_tip.Height().166const int nBlockHeight = active_chain_tip.nHeight + 1;168// BIP113 requires that time-locked transactions have nLockTime set to169// less than the median time of the previous block they're contained in.170// When the next block is created its previous block will be the current171// chain tip, so we use that to calculate the median time passed to172// IsFinalTx().173const int64_t nBlockTime{active_chain_tip.GetMedianTimePast()};175return IsFinalTx(tx, nBlockHeight, nBlockTime);176}178namespace {179/**180* A helper which calculates heights of inputs of a given transaction.181*182* @param[in] tip The current chain tip. If an input belongs to a mempool183* transaction, we assume it will be confirmed in the next block.184* @param[in] coins Any CCoinsView that provides access to the relevant coins.185* @param[in] tx The transaction being evaluated.186*187* @returns A vector of input heights or nullopt, in case of an error.188*/189std::optional<std::vector<int>> CalculatePrevHeights(190const CBlockIndex& tip,191const CCoinsView& coins,192const CTransaction& tx)193{194std::vector<int> prev_heights;195prev_heights.resize(tx.vin.size());196for (size_t i = 0; i < tx.vin.size(); ++i) {197if (auto coin{coins.GetCoin(tx.vin[i].prevout)}) {198prev_heights[i] = coin->nHeight == MEMPOOL_HEIGHT199? tip.nHeight + 1 // Assume all mempool transaction confirm in the next block.200: coin->nHeight;201} else {202LogInfo("ERROR: %s: Missing input %d in transaction \'%s\'\n", __func__, i, tx.GetHash().GetHex());203return std::nullopt;204}205}206return prev_heights;207}208} // namespace210std::optional<LockPoints> CalculateLockPointsAtTip(211CBlockIndex* tip,212const CCoinsView& coins_view,213const CTransaction& tx)214{215assert(tip);217auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};218if (!prev_heights.has_value()) return std::nullopt;220CBlockIndex next_tip;221next_tip.pprev = tip;222// When SequenceLocks() is called within ConnectBlock(), the height223// of the block *being* evaluated is what is used.224// Thus if we want to know if a transaction can be part of the225// *next* block, we need to use one more than active_chainstate.m_chain.Height()226next_tip.nHeight = tip->nHeight + 1;227const auto [min_height, min_time] = CalculateSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, prev_heights.value(), next_tip);229// Also store the hash of the block with the highest height of230// all the blocks which have sequence locked prevouts.231// This hash needs to still be on the chain232// for these LockPoint calculations to be valid233// Note: It is impossible to correctly calculate a maxInputBlock234// if any of the sequence locked inputs depend on unconfirmed txs,235// except in the special case where the relative lock time/height236// is 0, which is equivalent to no sequence lock. Since we assume237// input height of tip+1 for mempool txs and test the resulting238// min_height and min_time from CalculateSequenceLocks against tip+1.239int max_input_height{0};240for (const int height : prev_heights.value()) {241// Can ignore mempool inputs since we'll fail if they had non-zero locks242if (height != next_tip.nHeight) {243max_input_height = std::max(max_input_height, height);244}245}247// tip->GetAncestor(max_input_height) should never return a nullptr248// because max_input_height is always less than the tip height.249// It would, however, be a bad bug to continue execution, since a250// LockPoints object with the maxInputBlock member set to nullptr