Bitcoin Core  0.19.99
P2P Digital Currency
Classes | Functions | Variables
blockchain.cpp File Reference
#include <rpc/blockchain.h>
#include <amount.h>
#include <blockfilter.h>
#include <chain.h>
#include <chainparams.h>
#include <coins.h>
#include <consensus/validation.h>
#include <core_io.h>
#include <hash.h>
#include <index/blockfilterindex.h>
#include <node/coinstats.h>
#include <node/context.h>
#include <node/utxo_snapshot.h>
#include <policy/feerate.h>
#include <policy/policy.h>
#include <policy/rbf.h>
#include <primitives/transaction.h>
#include <rpc/server.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 <util/strencodings.h>
#include <util/system.h>
#include <validation.h>
#include <validationinterface.h>
#include <warnings.h>
#include <stdint.h>
#include <univalue.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
 

Functions

CTxMemPoolEnsureMemPool ()
 
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)
 
UniValue blockheaderToJSON (const CBlockIndex *tip, const CBlockIndex *blockindex)
 Block header to JSON. More...
 
UniValue blockToJSON (const CBlock &block, const CBlockIndex *tip, const CBlockIndex *blockindex, bool txDetails)
 Block description to JSON. More...
 
static UniValue getblockcount (const JSONRPCRequest &request)
 
static UniValue getbestblockhash (const JSONRPCRequest &request)
 
void RPCNotifyBlockChange (bool ibd, const CBlockIndex *pindex)
 Callback for when block tip changed. More...
 
static UniValue waitfornewblock (const JSONRPCRequest &request)
 
static UniValue waitforblock (const JSONRPCRequest &request)
 
static UniValue waitforblockheight (const JSONRPCRequest &request)
 
static UniValue syncwithvalidationinterfacequeue (const JSONRPCRequest &request)
 
static UniValue getdifficulty (const JSONRPCRequest &request)
 
static std::vector< RPCResultMempoolEntryDescription ()
 
static void entryToJSON (const CTxMemPool &pool, UniValue &info, const CTxMemPoolEntry &e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
 
UniValue MempoolToJSON (const CTxMemPool &pool, bool verbose)
 Mempool to JSON. More...
 
static UniValue getrawmempool (const JSONRPCRequest &request)
 
static UniValue getmempoolancestors (const JSONRPCRequest &request)
 
static UniValue getmempooldescendants (const JSONRPCRequest &request)
 
static UniValue getmempoolentry (const JSONRPCRequest &request)
 
static UniValue getblockhash (const JSONRPCRequest &request)
 
static UniValue getblockheader (const JSONRPCRequest &request)
 
static CBlock GetBlockChecked (const CBlockIndex *pblockindex)
 
static CBlockUndo GetUndoChecked (const CBlockIndex *pblockindex)
 
static UniValue getblock (const JSONRPCRequest &request)
 
static UniValue pruneblockchain (const JSONRPCRequest &request)
 
static UniValue gettxoutsetinfo (const JSONRPCRequest &request)
 
UniValue gettxout (const JSONRPCRequest &request)
 
static UniValue verifychain (const JSONRPCRequest &request)
 
static void BuriedForkDescPushBack (UniValue &softforks, const std::string &name, int height) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
static void BIP9SoftForkDescPushBack (UniValue &softforks, const std::string &name, const Consensus::Params &consensusParams, Consensus::DeploymentPos id) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
UniValue getblockchaininfo (const JSONRPCRequest &request)
 
static UniValue getchaintips (const JSONRPCRequest &request)
 
UniValue MempoolInfoToJSON (const CTxMemPool &pool)
 Mempool information to JSON. More...
 
static UniValue getmempoolinfo (const JSONRPCRequest &request)
 
static UniValue preciousblock (const JSONRPCRequest &request)
 
static UniValue invalidateblock (const JSONRPCRequest &request)
 
static UniValue reconsiderblock (const JSONRPCRequest &request)
 
static UniValue getchaintxstats (const JSONRPCRequest &request)
 
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 UniValue getblockstats (const JSONRPCRequest &request)
 
static UniValue savemempool (const JSONRPCRequest &request)
 
bool FindScriptPubKey (std::atomic< int > &scan_progress, const std::atomic< bool > &should_abort, int64_t &count, CCoinsViewCursor *cursor, const std::set< CScript > &needles, std::map< COutPoint, Coin > &out_results)
 Search for a given set of pubkey scripts. More...
 
UniValue scantxoutset (const JSONRPCRequest &request)
 
static UniValue getblockfilter (const JSONRPCRequest &request)
 
UniValue dumptxoutset (const JSONRPCRequest &request)
 Serialize the UTXO set to a file for loading elsewhere. More...
 
void RegisterBlockchainRPCCommands (CRPCTable &t)
 Register block chain RPC commands. More...
 

Variables

static Mutex cs_blockchange
 
static std::condition_variable cond_blockchange
 
static CUpdatedBlock latestblock
 
static constexpr size_t PER_UTXO_OVERHEAD = sizeof(COutPoint) + sizeof(uint32_t) + sizeof(bool)
 
static std::mutex g_utxosetscan
 RAII object to prevent concurrency issue when scanning the txout set. More...
 
static std::atomic< int > g_scan_progress
 
static std::atomic< bool > g_scan_in_progress
 
static std::atomic< bool > g_should_abort_scan
 
NodeContextg_rpc_node = nullptr
 Pointer to node state that needs to be declared as a global to be accessible RPC methods. More...
 

Function Documentation

◆ BIP9SoftForkDescPushBack()

static void BIP9SoftForkDescPushBack ( UniValue softforks,
const std::string &  name,
const Consensus::Params consensusParams,
Consensus::DeploymentPos  id 
)
static

Definition at line 1126 of file blockchain.cpp.

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

◆ blockheaderToJSON()

UniValue blockheaderToJSON ( const CBlockIndex tip,
const CBlockIndex blockindex 
)

Block header to JSON.

Definition at line 99 of file blockchain.cpp.

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

◆ blockToJSON()

UniValue blockToJSON ( const CBlock block,
const CBlockIndex tip,
const CBlockIndex blockindex,
bool  txDetails 
)

Block description to JSON.

Definition at line 128 of file blockchain.cpp.

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

◆ BuriedForkDescPushBack()

static void BuriedForkDescPushBack ( UniValue softforks,
const std::string &  name,
int  height 
)
static

Definition at line 1108 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 1634 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 1619 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 89 of file blockchain.cpp.

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

◆ dumptxoutset()

UniValue dumptxoutset ( const JSONRPCRequest request)

Serialize the UTXO set to a file for loading elsewhere.

See also
SnapshotMetadata

Definition at line 2247 of file blockchain.cpp.

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

◆ EnsureMemPool()

CTxMemPool& EnsureMemPool ( )

Definition at line 56 of file blockchain.cpp.

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

◆ entryToJSON()

static void entryToJSON ( const CTxMemPool pool,
UniValue info,
const CTxMemPoolEntry e 
)
static

Definition at line 406 of file blockchain.cpp.

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

◆ FindScriptPubKey()

bool FindScriptPubKey ( std::atomic< int > &  scan_progress,
const std::atomic< bool > &  should_abort,
int64_t &  count,
CCoinsViewCursor cursor,
const std::set< CScript > &  needles,
std::map< COutPoint, Coin > &  out_results 
)

Search for a given set of pubkey scripts.

Definition at line 1957 of file blockchain.cpp.

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

◆ getbestblockhash()

static UniValue getbestblockhash ( const JSONRPCRequest request)
static

Definition at line 191 of file blockchain.cpp.

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

◆ getblock()

static UniValue getblock ( const JSONRPCRequest request)
static

Definition at line 807 of file blockchain.cpp.

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

◆ getblockchaininfo()

UniValue getblockchaininfo ( const JSONRPCRequest request)

Definition at line 1174 of file blockchain.cpp.

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

◆ GetBlockChecked()

static CBlock GetBlockChecked ( const CBlockIndex pblockindex)
static

Definition at line 774 of file blockchain.cpp.

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

◆ getblockcount()

static UniValue getblockcount ( const JSONRPCRequest request)
static

Definition at line 173 of file blockchain.cpp.

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

◆ getblockfilter()

static UniValue getblockfilter ( const JSONRPCRequest request)
static

Definition at line 2166 of file blockchain.cpp.

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

◆ getblockhash()

static UniValue getblockhash ( const JSONRPCRequest request)
static

Definition at line 682 of file blockchain.cpp.

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

◆ getblockheader()

static UniValue getblockheader ( const JSONRPCRequest request)
static

Definition at line 707 of file blockchain.cpp.

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

◆ getblockstats()

static UniValue getblockstats ( const JSONRPCRequest request)
static

Definition at line 1674 of file blockchain.cpp.

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

◆ getchaintips()

static UniValue getchaintips ( const JSONRPCRequest request)
static

Definition at line 1290 of file blockchain.cpp.

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

◆ getchaintxstats()

static UniValue getchaintxstats ( const JSONRPCRequest request)
static

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

Here is the caller graph for this function:

◆ getdifficulty()

static UniValue getdifficulty ( const JSONRPCRequest request)
static

Definition at line 359 of file blockchain.cpp.

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

◆ getmempoolancestors()

static UniValue getmempoolancestors ( const JSONRPCRequest request)
static

Definition at line 528 of file blockchain.cpp.

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

◆ getmempooldescendants()

static UniValue getmempooldescendants ( const JSONRPCRequest request)
static

Definition at line 588 of file blockchain.cpp.

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

◆ getmempoolentry()

static UniValue getmempoolentry ( const JSONRPCRequest request)
static

Definition at line 651 of file blockchain.cpp.

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

◆ getmempoolinfo()

static UniValue getmempoolinfo ( const JSONRPCRequest request)
static

Definition at line 1404 of file blockchain.cpp.

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

◆ getrawmempool()

static UniValue getrawmempool ( const JSONRPCRequest request)
static

Definition at line 495 of file blockchain.cpp.

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

◆ gettxout()

UniValue gettxout ( const JSONRPCRequest request)

Definition at line 1000 of file blockchain.cpp.

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

◆ gettxoutsetinfo()

static UniValue gettxoutsetinfo ( const JSONRPCRequest request)
static

Definition at line 955 of file blockchain.cpp.

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

◆ GetUndoChecked()

static CBlockUndo GetUndoChecked ( const CBlockIndex pblockindex)
static

Definition at line 793 of file blockchain.cpp.

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

◆ invalidateblock()

static UniValue invalidateblock ( const JSONRPCRequest request)
static

Definition at line 1466 of file blockchain.cpp.

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

◆ MempoolEntryDescription()

static std::vector<RPCResult> MempoolEntryDescription ( )
static

Definition at line 376 of file blockchain.cpp.

Here is the caller graph for this function:

◆ MempoolInfoToJSON()

UniValue MempoolInfoToJSON ( const CTxMemPool pool)

Mempool information to JSON.

Definition at line 1387 of file blockchain.cpp.

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

◆ MempoolToJSON()

UniValue MempoolToJSON ( const CTxMemPool pool,
bool  verbose 
)

Mempool to JSON.

Definition at line 468 of file blockchain.cpp.

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

◆ preciousblock()

static UniValue preciousblock ( const JSONRPCRequest request)
static

Definition at line 1429 of file blockchain.cpp.

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

◆ pruneblockchain()

static UniValue pruneblockchain ( const JSONRPCRequest request)
static

Definition at line 900 of file blockchain.cpp.

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

◆ reconsiderblock()

static UniValue reconsiderblock ( const JSONRPCRequest request)
static

Definition at line 1504 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)

Register block chain RPC commands.

Definition at line 2350 of file blockchain.cpp.

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

◆ RPCNotifyBlockChange()

void RPCNotifyBlockChange ( bool  ibd,
const CBlockIndex  
)

Callback for when block tip changed.

Definition at line 208 of file blockchain.cpp.

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

◆ savemempool()

static UniValue savemempool ( const JSONRPCRequest request)
static

Definition at line 1931 of file blockchain.cpp.

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

◆ scantxoutset()

UniValue scantxoutset ( const JSONRPCRequest request)

Definition at line 2015 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 1664 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 1666 of file blockchain.cpp.

Here is the call graph for this function:

◆ syncwithvalidationinterfacequeue()

static UniValue syncwithvalidationinterfacequeue ( const JSONRPCRequest request)
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:

◆ verifychain()

static UniValue verifychain ( const JSONRPCRequest request)
static

Definition at line 1079 of file blockchain.cpp.

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

◆ waitforblock()

static UniValue waitforblock ( const JSONRPCRequest request)
static

Definition at line 257 of file blockchain.cpp.

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

◆ waitforblockheight()

static UniValue waitforblockheight ( const JSONRPCRequest request)
static

Definition at line 300 of file blockchain.cpp.

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

◆ waitfornewblock()

static UniValue waitfornewblock ( const JSONRPCRequest request)
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:

Variable Documentation

◆ cond_blockchange

std::condition_variable cond_blockchange
static

Definition at line 53 of file blockchain.cpp.

◆ cs_blockchange

Mutex cs_blockchange
static

Definition at line 52 of file blockchain.cpp.

◆ g_rpc_node

NodeContext* g_rpc_node = nullptr

Pointer to node state that needs to be declared as a global to be accessible RPC methods.

Due to limitations of the RPC framework, there's currently no direct way to pass in state to RPC methods without globals.

Definition at line 2396 of file blockchain.cpp.

◆ g_scan_in_progress

std::atomic<bool> g_scan_in_progress
static

Definition at line 1987 of file blockchain.cpp.

◆ g_scan_progress

std::atomic<int> g_scan_progress
static

Definition at line 1986 of file blockchain.cpp.

◆ g_should_abort_scan

std::atomic<bool> g_should_abort_scan
static

Definition at line 1988 of file blockchain.cpp.

◆ g_utxosetscan

std::mutex g_utxosetscan
static

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

Definition at line 1985 of file blockchain.cpp.

◆ latestblock

CUpdatedBlock latestblock
static

Definition at line 54 of file blockchain.cpp.

◆ PER_UTXO_OVERHEAD

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

Definition at line 1672 of file blockchain.cpp.