Bitcoin ABC 0.31.2
P2P Digital Currency
Classes | Functions | Variables
blockchain.cpp File Reference
#include <rpc/blockchain.h>
#include <blockfilter.h>
#include <chain.h>
#include <chainparams.h>
#include <clientversion.h>
#include <coins.h>
#include <common/args.h>
#include <config.h>
#include <consensus/amount.h>
#include <consensus/params.h>
#include <consensus/validation.h>
#include <core_io.h>
#include <hash.h>
#include <index/blockfilterindex.h>
#include <index/coinstatsindex.h>
#include <logging/timer.h>
#include <net.h>
#include <net_processing.h>
#include <node/blockstorage.h>
#include <node/coinstats.h>
#include <node/context.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 <serialize.h>
#include <streams.h>
#include <txdb.h>
#include <txmempool.h>
#include <undo.h>
#include <util/check.h>
#include <util/fs.h>
#include <util/strencodings.h>
#include <util/translation.h>
#include <validation.h>
#include <validationinterface.h>
#include <warnings.h>
#include <condition_variable>
#include <cstdint>
#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
 

Functions

static CUpdatedBlock latestblock GUARDED_BY (cs_blockchange)
 
double GetDifficulty (const CBlockIndex *blockindex)
 Calculate the difficulty for a 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, bool txDetails)
 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 ()
 
std::optional< int > GetPruneHeight (const BlockManager &blockman, const CChain &chain)
 
static RPCHelpMan pruneblockchain ()
 
static CoinStatsHashType ParseHashType (const std::string &hash_type_input)
 
static RPCHelpMan gettxoutsetinfo ()
 
RPCHelpMan gettxout ()
 
static RPCHelpMan verifychain ()
 
RPCHelpMan getblockchaininfo ()
 
static RPCHelpMan getchaintips ()
 
static RPCHelpMan preciousblock ()
 
static RPCHelpMan invalidateblock ()
 
RPCHelpMan parkblock ()
 
static RPCHelpMan reconsiderblock ()
 
RPCHelpMan unparkblock ()
 
static RPCHelpMan getchaintxstats ()
 
template<typename T >
static T CalculateTruncatedMedian (std::vector< T > &scores)
 
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 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...
 
static RPCHelpMan loadtxoutset ()
 
static RPCHelpMan getchainstates ()
 
void RegisterBlockchainRPCCommands (CRPCTable &t)
 

Variables

static GlobalMutex cs_blockchange
 
static std::condition_variable cond_blockchange
 
static constexpr size_t PER_UTXO_OVERHEAD
 
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
 
const std::vector< RPCResultRPCHelpForChainstate
 

Function Documentation

◆ blockheaderToJSON()

UniValue blockheaderToJSON ( const CBlockIndex tip,
const CBlockIndex blockindex 
)

Block header to JSON.

Definition at line 133 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,
bool  txDetails 
)

Block description to JSON.

Definition at line 167 of file blockchain.cpp.

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

◆ CalculateTruncatedMedian()

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

Definition at line 1943 of file blockchain.cpp.

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 90 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 2686 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 2627 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 218 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 681 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 1325 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 640 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 200 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 2526 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 461 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 515 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 544 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 1970 of file blockchain.cpp.

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

◆ getchainstates()

static RPCHelpMan getchainstates ( )
static

Definition at line 2920 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

Idea: The set of chain tips is the active chain tip, plus orphan blocks which do not have another orphan building off of them. Algorithm:

  • Make one pass through BlockIndex(), picking out the orphan blocks, and also storing a set of the orphan block's pprev pointers.
  • Iterate through the orphan blocks. If the block isn't pointed to by another orphan, it is a chain tip.
  • Add the active chain tip

Definition at line 1442 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 1830 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 441 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)

Calculate the difficulty for a given block index.

Get the required difficulty of the next block w/r/t the given block index.

Definition at line 72 of file blockchain.cpp.

Here is the caller graph for this function:

◆ GetPruneHeight()

std::optional< int > GetPruneHeight ( const BlockManager blockman,
const CChain chain 
)

Definition at line 820 of file blockchain.cpp.

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

◆ gettxout()

RPCHelpMan gettxout ( )

Definition at line 1181 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 946 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 662 of file blockchain.cpp.

Here is the call graph for this function:
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 1614 of file blockchain.cpp.

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

◆ loadtxoutset()

static RPCHelpMan loadtxoutset ( )
static

Definition at line 2797 of file blockchain.cpp.

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

◆ parkblock()

RPCHelpMan parkblock ( )

Definition at line 1661 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 101 of file blockchain.cpp.

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

◆ ParseHashType()

static CoinStatsHashType ParseHashType ( const std::string &  hash_type_input)
static

Definition at line 932 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 1567 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 858 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 1716 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 2994 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 236 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 2299 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 1957 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 1961 of file blockchain.cpp.

Here is the call graph for this function:

◆ syncwithvalidationinterfacequeue()

static RPCHelpMan syncwithvalidationinterfacequeue ( )
static

Definition at line 424 of file blockchain.cpp.

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

◆ unparkblock()

RPCHelpMan unparkblock ( )

Definition at line 1763 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 1285 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 302 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 364 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 245 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 66 of file blockchain.cpp.

◆ cs_blockchange

GlobalMutex cs_blockchange
static

Definition at line 65 of file blockchain.cpp.

◆ g_scan_in_progress

std::atomic<bool> g_scan_in_progress
static

Definition at line 2274 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 2273 of file blockchain.cpp.

◆ g_should_abort_scan

std::atomic<bool> g_should_abort_scan
static

Definition at line 2275 of file blockchain.cpp.

◆ PER_UTXO_OVERHEAD

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

Definition at line 1967 of file blockchain.cpp.

◆ RPCHelpForChainstate

const std::vector<RPCResult> RPCHelpForChainstate
Initial value:
{
{RPCResult::Type::NUM, "blocks", "number of blocks in this chainstate"},
{RPCResult::Type::STR_HEX, "bestblockhash", "blockhash of the tip"},
{RPCResult::Type::NUM, "difficulty", "difficulty of the tip"},
{RPCResult::Type::NUM, "verificationprogress",
"progress towards the network tip"},
{RPCResult::Type::STR_HEX, "snapshot_blockhash", true,
"the base block of the snapshot this chainstate is based on, if any"},
{RPCResult::Type::NUM, "coins_db_cache_bytes", "size of the coinsdb cache"},
{RPCResult::Type::NUM, "coins_tip_cache_bytes",
"size of the coinstip cache"},
{RPCResult::Type::BOOL, "validated",
"whether the chainstate is fully validated. True if all blocks in the "
"chainstate were validated, false if the chain is based on a snapshot and "
"the snapshot has not yet been validated."},
}
@ STR_HEX
Special string with only hex chars.

Definition at line 2902 of file blockchain.cpp.