Bitcoin Core  24.99.0
P2P Digital Currency
Classes | Functions | Variables
blockchain.cpp File Reference
#include <rpc/blockchain.h>
#include <blockfilter.h>
#include <chain.h>
#include <chainparams.h>
#include <coins.h>
#include <consensus/amount.h>
#include <consensus/params.h>
#include <consensus/validation.h>
#include <core_io.h>
#include <deploymentinfo.h>
#include <deploymentstatus.h>
#include <fs.h>
#include <hash.h>
#include <index/blockfilterindex.h>
#include <index/coinstatsindex.h>
#include <kernel/coinstats.h>
#include <logging/timer.h>
#include <net.h>
#include <net_processing.h>
#include <node/blockstorage.h>
#include <node/context.h>
#include <node/transaction.h>
#include <node/utxo_snapshot.h>
#include <primitives/transaction.h>
#include <rpc/server.h>
#include <rpc/server_util.h>
#include <rpc/util.h>
#include <script/descriptor.h>
#include <streams.h>
#include <sync.h>
#include <txdb.h>
#include <txmempool.h>
#include <undo.h>
#include <univalue.h>
#include <util/check.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <util/translation.h>
#include <validation.h>
#include <validationinterface.h>
#include <versionbits.h>
#include <warnings.h>
#include <stdint.h>
#include <condition_variable>
#include <memory>
#include <mutex>
Include dependency graph for blockchain.cpp:

Go to the source code of this file.

Classes

struct  CUpdatedBlock
 
struct  CompareBlocksByHeight
 Comparison function for sorting the getchaintips heads. More...
 
class  CoinsViewScanReserver
 
class  BlockFiltersScanReserver
 

Functions

static CUpdatedBlock latestblock GUARDED_BY (cs_blockchange)
 
double GetDifficulty (const CBlockIndex *blockindex)
 Get the difficulty of the net wrt to the given block index. More...
 
static int ComputeNextBlockAndDepth (const CBlockIndex *tip, const CBlockIndex *blockindex, const CBlockIndex *&next)
 
static const CBlockIndexParseHashOrHeight (const UniValue &param, ChainstateManager &chainman)
 
UniValue blockheaderToJSON (const CBlockIndex *tip, const CBlockIndex *blockindex)
 Block header to JSON. More...
 
UniValue blockToJSON (BlockManager &blockman, const CBlock &block, const CBlockIndex *tip, const CBlockIndex *blockindex, TxVerbosity verbosity)
 Block description to JSON. More...
 
static RPCHelpMan getblockcount ()
 
static RPCHelpMan getbestblockhash ()
 
void RPCNotifyBlockChange (const CBlockIndex *pindex)
 Callback for when block tip changed. More...
 
static RPCHelpMan waitfornewblock ()
 
static RPCHelpMan waitforblock ()
 
static RPCHelpMan waitforblockheight ()
 
static RPCHelpMan syncwithvalidationinterfacequeue ()
 
static RPCHelpMan getdifficulty ()
 
static RPCHelpMan getblockfrompeer ()
 
static RPCHelpMan getblockhash ()
 
static RPCHelpMan getblockheader ()
 
static CBlock GetBlockChecked (BlockManager &blockman, const CBlockIndex *pblockindex)
 
static CBlockUndo GetUndoChecked (BlockManager &blockman, const CBlockIndex *pblockindex)
 
static RPCHelpMan getblock ()
 
static RPCHelpMan pruneblockchain ()
 
CoinStatsHashType ParseHashType (const std::string &hash_type_input)
 
static std::optional< kernel::CCoinsStatsGetUTXOStats (CCoinsView *view, node::BlockManager &blockman, kernel::CoinStatsHashType hash_type, const std::function< void()> &interruption_point={}, const CBlockIndex *pindex=nullptr, bool index_requested=true)
 Calculate statistics about the unspent transaction output set. More...
 
static RPCHelpMan gettxoutsetinfo ()
 
static RPCHelpMan gettxout ()
 
static RPCHelpMan verifychain ()
 
static void SoftForkDescPushBack (const CBlockIndex *blockindex, UniValue &softforks, const ChainstateManager &chainman, Consensus::BuriedDeployment dep)
 
static void SoftForkDescPushBack (const CBlockIndex *blockindex, UniValue &softforks, const ChainstateManager &chainman, Consensus::DeploymentPos id)
 
RPCHelpMan getblockchaininfo ()
 
RPCHelpMan getdeploymentinfo ()
 
static RPCHelpMan getchaintips ()
 
static RPCHelpMan preciousblock ()
 
static RPCHelpMan invalidateblock ()
 
static RPCHelpMan reconsiderblock ()
 
static RPCHelpMan getchaintxstats ()
 
template<typename T >
static T CalculateTruncatedMedian (std::vector< T > &scores)
 
void CalculatePercentilesByWeight (CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector< std::pair< CAmount, int64_t >> &scores, int64_t total_weight)
 Used by getblockstats to get feerates at different percentiles by weight
More...
 
template<typename T >
static bool SetHasKeys (const std::set< T > &set)
 
template<typename T , typename Tk , typename... Args>
static bool SetHasKeys (const std::set< T > &set, const Tk &key, const Args &... args)
 
static RPCHelpMan getblockstats ()
 
static RPCHelpMan scantxoutset ()
 
static bool CheckBlockFilterMatches (BlockManager &blockman, const CBlockIndex &blockindex, const GCSFilter::ElementSet &needles)
 
static RPCHelpMan scanblocks ()
 
static RPCHelpMan getblockfilter ()
 
static RPCHelpMan dumptxoutset ()
 Serialize the UTXO set to a file for loading elsewhere. More...
 
UniValue CreateUTXOSnapshot (NodeContext &node, Chainstate &chainstate, AutoFile &afile, const fs::path &path, const fs::path &temppath)
 Helper to create UTXO snapshots given a chainstate and a file handle. More...
 
void RegisterBlockchainRPCCommands (CRPCTable &t)
 

Variables

static GlobalMutex cs_blockchange
 
static std::condition_variable cond_blockchange
 
const RPCResult getblock_vin
 
static constexpr size_t PER_UTXO_OVERHEAD = sizeof(COutPoint) + sizeof(uint32_t) + sizeof(bool)
 
static std::atomic< int > g_scan_progress
 RAII object to prevent concurrency issue when scanning the txout set. More...
 
static std::atomic< bool > g_scan_in_progress
 
static std::atomic< bool > g_should_abort_scan
 
static const auto scan_action_arg_desc
 
static const auto scan_objects_arg_desc
 
static const auto scan_result_abort
 
static const auto scan_result_status_none
 
static const auto scan_result_status_some
 
static std::atomic< int > g_scanfilter_progress
 RAII object to prevent concurrency issue when scanning blockfilters. More...
 
static std::atomic< int > g_scanfilter_progress_height
 
static std::atomic< bool > g_scanfilter_in_progress
 
static std::atomic< bool > g_scanfilter_should_abort_scan
 

Function Documentation

◆ blockheaderToJSON()

UniValue blockheaderToJSON ( const CBlockIndex tip,
const CBlockIndex blockindex 
)

Block header to JSON.

Definition at line 137 of file blockchain.cpp.

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

◆ blockToJSON()

UniValue blockToJSON ( BlockManager blockman,
const CBlock block,
const CBlockIndex tip,
const CBlockIndex blockindex,
TxVerbosity  verbosity 
)

Block description to JSON.

Definition at line 166 of file blockchain.cpp.

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

◆ CalculatePercentilesByWeight()

void CalculatePercentilesByWeight ( CAmount  result[NUM_GETBLOCKSTATS_PERCENTILES],
std::vector< std::pair< CAmount, int64_t >> &  scores,
int64_t  total_weight 
)

Used by getblockstats to get feerates at different percentiles by weight

Definition at line 1700 of file blockchain.cpp.

Here is the caller graph for this function:

◆ CalculateTruncatedMedian()

template<typename T >
static T CalculateTruncatedMedian ( std::vector< T > &  scores)
static

Definition at line 1685 of file blockchain.cpp.

Here is the caller graph for this function:

◆ CheckBlockFilterMatches()

static bool CheckBlockFilterMatches ( BlockManager blockman,
const CBlockIndex blockindex,
const GCSFilter::ElementSet needles 
)
static

Definition at line 2278 of file blockchain.cpp.

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

◆ ComputeNextBlockAndDepth()

static int ComputeNextBlockAndDepth ( const CBlockIndex tip,
const CBlockIndex blockindex,
const CBlockIndex *&  next 
)
static

Definition at line 99 of file blockchain.cpp.

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

◆ CreateUTXOSnapshot()

UniValue CreateUTXOSnapshot ( node::NodeContext node,
Chainstate chainstate,
AutoFile afile,
const fs::path path,
const fs::path tmppath 
)

Helper to create UTXO snapshots given a chainstate and a file handle.

Returns
a UniValue map containing metadata about the snapshot.

Definition at line 2630 of file blockchain.cpp.

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

◆ dumptxoutset()

static RPCHelpMan dumptxoutset ( )
static

Serialize the UTXO set to a file for loading elsewhere.

See also
SnapshotMetadata

Definition at line 2574 of file blockchain.cpp.

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

◆ getbestblockhash()

static RPCHelpMan getbestblockhash ( )
static

Definition at line 225 of file blockchain.cpp.

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

◆ getblock()

static RPCHelpMan getblock ( )
static

Definition at line 645 of file blockchain.cpp.

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

◆ getblockchaininfo()

RPCHelpMan getblockchaininfo ( )

Definition at line 1221 of file blockchain.cpp.

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

◆ GetBlockChecked()

static CBlock GetBlockChecked ( BlockManager blockman,
const CBlockIndex pblockindex 
)
static

Definition at line 580 of file blockchain.cpp.

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

◆ getblockcount()

static RPCHelpMan getblockcount ( )
static

Definition at line 204 of file blockchain.cpp.

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

◆ getblockfilter()

static RPCHelpMan getblockfilter ( )
static

Definition at line 2490 of file blockchain.cpp.

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

◆ getblockfrompeer()

static RPCHelpMan getblockfrompeer ( )
static

Definition at line 427 of file blockchain.cpp.

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

◆ getblockhash()

static RPCHelpMan getblockhash ( )
static

Definition at line 481 of file blockchain.cpp.

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

◆ getblockheader()

static RPCHelpMan getblockheader ( )
static

Definition at line 510 of file blockchain.cpp.

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

◆ getblockstats()

static RPCHelpMan getblockstats ( )
static

Definition at line 1740 of file blockchain.cpp.

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

◆ getchaintips()

static RPCHelpMan getchaintips ( )
static

Definition at line 1385 of file blockchain.cpp.

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

◆ getchaintxstats()

static RPCHelpMan getchaintxstats ( )
static

Definition at line 1604 of file blockchain.cpp.

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

◆ getdeploymentinfo()

RPCHelpMan getdeploymentinfo ( )

Definition at line 1327 of file blockchain.cpp.

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

◆ getdifficulty()

static RPCHelpMan getdifficulty ( )
static

Definition at line 407 of file blockchain.cpp.

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

◆ GetDifficulty()

double GetDifficulty ( const CBlockIndex blockindex)

Get the difficulty of the net wrt to the given block index.

Returns
A floating point number that is a multiple of the main net minimum difficulty (4295032833 hashes).

Definition at line 77 of file blockchain.cpp.

Here is the caller graph for this function:

◆ gettxout()

static RPCHelpMan gettxout ( )
static

Definition at line 1022 of file blockchain.cpp.

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

◆ gettxoutsetinfo()

static RPCHelpMan gettxoutsetinfo ( )
static

Definition at line 867 of file blockchain.cpp.

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

◆ GetUndoChecked()

static CBlockUndo GetUndoChecked ( BlockManager blockman,
const CBlockIndex pblockindex 
)
static

Definition at line 600 of file blockchain.cpp.

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

◆ GetUTXOStats()

static std::optional<kernel::CCoinsStats> GetUTXOStats ( CCoinsView view,
node::BlockManager blockman,
kernel::CoinStatsHashType  hash_type,
const std::function< void()> &  interruption_point = {},
const CBlockIndex pindex = nullptr,
bool  index_requested = true 
)
static

Calculate statistics about the unspent transaction output set.

Parameters
[in]index_requestedSignals if the coinstatsindex should be used (when available).

Definition at line 842 of file blockchain.cpp.

Here is the caller graph for this function:

◆ GUARDED_BY()

static CUpdatedBlock latestblock GUARDED_BY ( cs_blockchange  )
static

◆ invalidateblock()

static RPCHelpMan invalidateblock ( )
static

Definition at line 1523 of file blockchain.cpp.

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

◆ ParseHashOrHeight()

static const CBlockIndex* ParseHashOrHeight ( const UniValue param,
ChainstateManager chainman 
)
static

Definition at line 109 of file blockchain.cpp.

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

◆ ParseHashType()

CoinStatsHashType ParseHashType ( const std::string &  hash_type_input)

Definition at line 824 of file blockchain.cpp.

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

◆ preciousblock()

static RPCHelpMan preciousblock ( )
static

Definition at line 1483 of file blockchain.cpp.

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

◆ pruneblockchain()

static RPCHelpMan pruneblockchain ( )
static

Definition at line 764 of file blockchain.cpp.

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

◆ reconsiderblock()

static RPCHelpMan reconsiderblock ( )
static

Definition at line 1564 of file blockchain.cpp.

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

◆ RegisterBlockchainRPCCommands()

void RegisterBlockchainRPCCommands ( CRPCTable t)

Definition at line 2704 of file blockchain.cpp.

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

◆ RPCNotifyBlockChange()

void RPCNotifyBlockChange ( const CBlockIndex pindex)

Callback for when block tip changed.

Definition at line 245 of file blockchain.cpp.

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

◆ scanblocks()

static RPCHelpMan scanblocks ( )
static

Definition at line 2303 of file blockchain.cpp.

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

◆ scantxoutset()

static RPCHelpMan scantxoutset ( )
static

Definition at line 2095 of file blockchain.cpp.

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

◆ SetHasKeys() [1/2]

template<typename T >
static bool SetHasKeys ( const std::set< T > &  set)
inlinestatic

Definition at line 1730 of file blockchain.cpp.

Here is the caller graph for this function:

◆ SetHasKeys() [2/2]

template<typename T , typename Tk , typename... Args>
static bool SetHasKeys ( const std::set< T > &  set,
const Tk &  key,
const Args &...  args 
)
inlinestatic

Definition at line 1732 of file blockchain.cpp.

Here is the call graph for this function:

◆ SoftForkDescPushBack() [1/2]

static void SoftForkDescPushBack ( const CBlockIndex blockindex,
UniValue softforks,
const ChainstateManager chainman,
Consensus::BuriedDeployment  dep 
)
static

Definition at line 1134 of file blockchain.cpp.

Here is the call graph for this function:

◆ SoftForkDescPushBack() [2/2]

static void SoftForkDescPushBack ( const CBlockIndex blockindex,
UniValue softforks,
const ChainstateManager chainman,
Consensus::DeploymentPos  id 
)
static

Definition at line 1149 of file blockchain.cpp.

Here is the call graph for this function:

◆ syncwithvalidationinterfacequeue()

static RPCHelpMan syncwithvalidationinterfacequeue ( )
static

Definition at line 389 of file blockchain.cpp.

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

◆ verifychain()

static RPCHelpMan verifychain ( )
static

Definition at line 1104 of file blockchain.cpp.

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

◆ waitforblock()

static RPCHelpMan waitforblock ( )
static

Definition at line 297 of file blockchain.cpp.

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

◆ waitforblockheight()

static RPCHelpMan waitforblockheight ( )
static

Definition at line 343 of file blockchain.cpp.

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

◆ waitfornewblock()

static RPCHelpMan waitfornewblock ( )
static

Definition at line 255 of file blockchain.cpp.

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

Variable Documentation

◆ cond_blockchange

std::condition_variable cond_blockchange
static

Definition at line 72 of file blockchain.cpp.

◆ cs_blockchange

GlobalMutex cs_blockchange
static

Definition at line 71 of file blockchain.cpp.

◆ g_scan_in_progress

std::atomic<bool> g_scan_in_progress
static

Definition at line 2034 of file blockchain.cpp.

◆ g_scan_progress

std::atomic<int> g_scan_progress
static

RAII object to prevent concurrency issue when scanning the txout set.

Definition at line 2033 of file blockchain.cpp.

◆ g_scanfilter_in_progress

std::atomic<bool> g_scanfilter_in_progress
static

Definition at line 2253 of file blockchain.cpp.

◆ g_scanfilter_progress

std::atomic<int> g_scanfilter_progress
static

RAII object to prevent concurrency issue when scanning blockfilters.

Definition at line 2251 of file blockchain.cpp.

◆ g_scanfilter_progress_height

std::atomic<int> g_scanfilter_progress_height
static

Definition at line 2252 of file blockchain.cpp.

◆ g_scanfilter_should_abort_scan

std::atomic<bool> g_scanfilter_should_abort_scan
static

Definition at line 2254 of file blockchain.cpp.

◆ g_should_abort_scan

std::atomic<bool> g_should_abort_scan
static

Definition at line 2035 of file blockchain.cpp.

◆ getblock_vin

const RPCResult getblock_vin
Initial value:
{
{
{
{RPCResult::Type::ELISION, "", "The same output as verbosity = 2"},
{RPCResult::Type::OBJ, "prevout", "(Only if undo information is available)",
{
{RPCResult::Type::BOOL, "generated", "Coinbase or not"},
{RPCResult::Type::NUM, "height", "The height of the prevout"},
{RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
{RPCResult::Type::OBJ, "scriptPubKey", "",
{
{RPCResult::Type::STR, "asm", "Disassembly of the public key script"},
{RPCResult::Type::STR, "desc", "Inferred descriptor for the output"},
{RPCResult::Type::STR_HEX, "hex", "The raw public key script bytes, hex-encoded"},
{RPCResult::Type::STR, "address", true, "The Bitcoin address (only if a well-defined address exists)"},
{RPCResult::Type::STR, "type", "The type (one of: " + GetAllOutputTypes() + ")"},
}},
}},
}},
}
}
const std::string CURRENCY_UNIT
Definition: feerate.h:17
std::string GetAllOutputTypes()
Gets all existing output types formatted for RPC help sections.
Definition: util.cpp:24
@ ELISION
Special type to denote elision (...)
@ STR_HEX
Special string with only hex chars.
@ STR_AMOUNT
Special string to represent a floating point amount.

Definition at line 621 of file blockchain.cpp.

◆ PER_UTXO_OVERHEAD

constexpr size_t PER_UTXO_OVERHEAD = sizeof(COutPoint) + sizeof(uint32_t) + sizeof(bool)
staticconstexpr

Definition at line 1738 of file blockchain.cpp.

◆ scan_action_arg_desc

const auto scan_action_arg_desc
static
Initial value:
"action", RPCArg::Type::STR, RPCArg::Optional::NO, "The action to execute\n"
"\"start\" for starting a scan\n"
"\"abort\" for aborting the current scan (returns true when abort was successful)\n"
"\"status\" for progress report (in %) of the current scan"
}
Definition: util.h:135
@ NO
Required arg.

Definition at line 2061 of file blockchain.cpp.

◆ scan_objects_arg_desc

const auto scan_objects_arg_desc
static
Initial value:
"scanobjects", RPCArg::Type::ARR, RPCArg::Optional::OMITTED, "Array of scan objects. Required for \"start\" action\n"
"Every scan object is either a string descriptor or an object:",
{
{"descriptor", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "An output descriptor"},
{"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "An object with output descriptor and metadata",
{
{"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "An output descriptor"},
{"range", RPCArg::Type::RANGE, RPCArg::Default{1000}, "The range of HD chain indexes to explore (either end or [begin,end])"},
}},
},
RPCArgOptions{.oneline_description="[scanobjects,...]"},
}
@ RANGE
Special type that is a NUM or [NUM,NUM].
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
std::string oneline_description
Should be empty unless it is supposed to override the auto-generated summary line.
Definition: util.h:130

Definition at line 2068 of file blockchain.cpp.

◆ scan_result_abort

const auto scan_result_abort
static
Initial value:
"when action=='abort'", RPCResult::Type::BOOL, "success",
"True if scan will be aborted (not necessarily before this RPC returns), or false if there is no scan to abort"
}

Definition at line 2082 of file blockchain.cpp.

◆ scan_result_status_none

const auto scan_result_status_none
static
Initial value:
"when action=='status' and no scan is in progress - possibly already completed", RPCResult::Type::NONE, "", ""
}

Definition at line 2086 of file blockchain.cpp.

◆ scan_result_status_some

const auto scan_result_status_some
static
Initial value:
"when action=='status' and a scan is currently in progress", RPCResult::Type::OBJ, "", "",
{{RPCResult::Type::NUM, "progress", "Approximate percent complete"},}
}

Definition at line 2089 of file blockchain.cpp.