11#include <chainparams.h>
49#include <condition_variable>
74 const std::function<
void()> &interruption_point = {})
82 const std::function<
void()> &interruption_point = {});
88 int nShift = (blockindex.
nBits >> 24) & 0xff;
89 double dDiff = double(0x0000ffff) / double(blockindex.
nBits & 0x00ffffff);
107 if (next && next->
pprev == &blockindex) {
111 return &blockindex == &tip ? 1 : -1;
120 const int height{param.
getInt<
int>()};
124 strprintf(
"Target block height %d is negative", height));
126 const int current_tip{active_chain.
Height()};
127 if (height > current_tip) {
130 strprintf(
"Target block height %d after current tip %d", height,
134 return active_chain[height];
157 result.
pushKV(
"confirmations", confirmations);
170 if (blockindex.
pprev) {
171 result.
pushKV(
"previousblockhash",
198 ::cs_main,
return !blockman.IsBlockPruned(blockindex))};
200 blockUndo, blockindex)};
201 for (
size_t i = 0; i < block.
vtx.size(); ++i) {
204 const CTxUndo *txundo = (have_undo && i > 0)
214 result.
pushKV(
"tx", std::move(txs));
222 "Returns the height of the most-work fully-validated chain.\n"
223 "The genesis block has height 0.\n",
240 "Returns the hash of the best (tip) block in the "
241 "most-work fully-validated chain.\n",
259 latestblock.height = pindex->
nHeight;
267 "Waits for a specific new block and returns useful info about it.\n"
268 "\nReturns the current block on timeout or exit.\n",
271 "Time in milliseconds to wait for a response. 0 indicates no "
286 if (!request.params[0].isNull()) {
287 timeout = request.params[0].
getInt<
int>();
296 lock, std::chrono::milliseconds(timeout),
298 return latestblock.height != block.
height ||
299 latestblock.hash != block.
hash ||
306 return latestblock.height != block.
height ||
307 latestblock.hash != block.
hash ||
324 "Waits for a specific new block and returns useful info about it.\n"
325 "\nReturns the current block on timeout or exit.\n",
328 "Block hash to wait for."},
330 "Time in milliseconds to wait for a response. 0 indicates no "
341 "\"0000000000079f8ef3d2c688c244eb7a4570b24c9"
342 "ed7b4a8c619eb02596f8862\" 1000") +
344 "\"0000000000079f8ef3d2c688c244eb7a4570b24c9"
345 "ed7b4a8c619eb02596f8862\", 1000")},
352 if (!request.params[1].isNull()) {
353 timeout = request.params[1].getInt<
int>();
361 lock, std::chrono::milliseconds(timeout),
385 "waitforblockheight",
386 "Waits for (at least) block height and returns the height and "
387 "hash\nof the current tip.\n"
388 "\nReturns the current block on timeout or exit.\n",
391 "Block height to wait for."},
393 "Time in milliseconds to wait for a response. 0 indicates no "
409 int height = request.params[0].
getInt<
int>();
411 if (!request.params[1].isNull()) {
412 timeout = request.params[1].getInt<
int>();
420 lock, std::chrono::milliseconds(timeout),
422 return latestblock.height >= height ||
429 return latestblock.height >= height ||
445 "syncwithvalidationinterfacequeue",
446 "Waits for the validation interface queue to catch up on everything "
447 "that was there when we entered this function.\n",
463 "Returns the proof-of-work difficulty as a multiple of the minimum "
467 "the proof-of-work difficulty as a multiple of the minimum "
483 "Attempt to fetch block from a given peer.\n"
484 "\nWe must have the header for this block, e.g. using submitheader.\n"
485 "The block will not have any undo data which can limit the usage of "
486 "the block data in a context where the undo data is needed.\n"
487 "Subsequent calls for the same block may cause the response from the "
488 "previous peer to be ignored.\n"
489 "\nReturns an empty JSON object if the request was successfully "
493 "The block hash to try to fetch"},
495 "The peer to fetch it from (see getpeerinfo for peer IDs)"},
499 "\"00000000c937983704a73af28acdec37b049d214a"
500 "dbda81d7e2a3dd146f6ed09\" 0") +
502 "\"00000000c937983704a73af28acdec37b049d214a"
503 "dbda81d7e2a3dd146f6ed09\" 0")},
512 const NodeId peer_id{request.params[1].getInt<int64_t>()};
526 if (
const auto err{peerman.
FetchBlock(config, peer_id, *index)}) {
537 "Returns hash of block in best-block-chain at height provided.\n",
551 int nHeight = request.params[0].getInt<
int>();
552 if (nHeight < 0 || nHeight > active_chain.
Height()) {
554 "Block height out of range");
566 "If verbose is false, returns a string that is serialized, hex-encoded "
567 "data for blockheader 'hash'.\n"
568 "If verbose is true, returns an Object with information about "
569 "blockheader <hash>.\n",
574 "true for a json object, false for the hex-encoded data"},
578 "for verbose = true",
584 "the block hash (same as provided)"},
586 "The number of confirmations, or -1 if the block is not "
587 "on the main chain"},
589 "The block height or index"},
592 "The block version formatted in hexadecimal"},
602 "Expected number of hashes required to produce the "
605 "The number of transactions in the block"},
608 "The hash of the previous block (if available)"},
611 "The hash of the next block (if available)"},
614 "A string that is serialized, hex-encoded data for block "
618 "\"00000000c937983704a73af28acdec37b049d214a"
619 "dbda81d7e2a3dd146f6ed09\"") +
621 "\"00000000c937983704a73af28acdec37b049d214a"
622 "dbda81d7e2a3dd146f6ed09\"")},
627 bool fVerbose =
true;
628 if (!request.params[1].isNull()) {
629 fVerbose = request.params[1].get_bool();
650 std::string strHex =
HexStr(ssBlock);
664 if (blockman.IsBlockPruned(blockindex)) {
666 "Block not available (pruned data)");
687 if (blockman.IsBlockPruned(blockindex)) {
689 "Undo data not available (pruned data)");
703 "If verbosity is 0 or false, returns a string that is serialized, "
704 "hex-encoded data for block 'hash'.\n"
705 "If verbosity is 1 or true, returns an Object with information about "
707 "If verbosity is 2, returns an Object with information about block "
708 "<hash> and information about each transaction.\n"
709 "If verbosity is 3, returns an Object with information about block "
710 "<hash> and information about each transaction, including prevout "
711 "information for inputs (only for unpruned blocks in the current best "
717 "0 for hex-encoded data, 1 for a json object, and 2 for json "
718 "object with transaction data",
723 "A string that is serialized, hex-encoded data for block "
732 "the block hash (same as provided)"},
734 "The number of confirmations, or -1 if the block is not "
735 "on the main chain"},
738 "The block height or index"},
741 "The block version formatted in hexadecimal"},
745 "The transaction ids",
755 "Expected number of hashes required to produce the chain "
756 "up to this block (in hex)"},
758 "The number of transactions in the block"},
761 "The hash of the previous block (if available)"},
764 "The hash of the next block (if available)"},
772 "Same output as verbosity = 1"},
782 "The transactions in the format of the "
783 "getrawtransaction RPC. Different from "
784 "verbosity = 1 \"tx\" result"},
786 "The transaction fee in " +
788 ", omitted if block undo data is not "
795 HelpExampleCli(
"getblock",
"\"00000000c937983704a73af28acdec37b049d"
796 "214adbda81d7e2a3dd146f6ed09\"") +
797 HelpExampleRpc(
"getblock",
"\"00000000c937983704a73af28acdec37b049d"
798 "214adbda81d7e2a3dd146f6ed09\"")},
804 if (!request.params[1].isNull()) {
805 if (request.params[1].isNum()) {
806 verbosity = request.params[1].getInt<
int>();
808 verbosity = request.params[1].get_bool() ? 1 : 0;
829 if (verbosity <= 0) {
832 std::string strHex =
HexStr(ssBlock);
837 if (verbosity == 1) {
839 }
else if (verbosity == 2) {
863 if (!first_block || !chain_tip) {
868 if (!(chain_tip->nStatus.hasData() && chain_tip->nStatus.hasUndo())) {
875 blockman.GetFirstBlock(*chain_tip,
877 return status.hasData() && status.hasUndo();
879 if (first_unpruned == first_block) {
895 "The block height to prune up to. May be set to a discrete "
899 " to prune blocks whose block time is at "
900 "least 2 hours older than the provided timestamp."},
911 "Cannot prune blocks because node is not in prune mode.");
918 int heightParam = request.params[0].getInt<
int>();
919 if (heightParam < 0) {
921 "Negative block height.");
927 if (heightParam > 1000000000) {
934 "Could not find block with at least the "
935 "specified timestamp.");
940 unsigned int height = (
unsigned int)heightParam;
941 unsigned int chainHeight = (
unsigned int)active_chain.
Height();
942 if (chainHeight < config.GetChainParams().PruneAfterHeight()) {
944 "Blockchain is too short for pruning.");
945 }
else if (height > chainHeight) {
948 "Blockchain is shorter than the attempted prune height.");
951 "Attempt to prune blocks close to the tip. "
952 "Retaining the minimum number of blocks.\n");
964 if (hash_type_input ==
"hash_serialized") {
965 return CoinStatsHashType::HASH_SERIALIZED;
966 }
else if (hash_type_input ==
"muhash") {
967 return CoinStatsHashType::MUHASH;
968 }
else if (hash_type_input ==
"none") {
973 strprintf(
"%s is not a valid hash_type", hash_type_input));
980 "Returns statistics about the unspent transaction output set.\n"
981 "Note this call may take some time if you are not using "
985 "Which UTXO set hash should be calculated. Options: "
986 "'hash_serialized' (the legacy algorithm), 'muhash', 'none'."},
988 "The block hash or height of the target height (only available "
989 "with coinstatsindex).",
991 .type_str = {
"",
"string or numeric"}}},
993 "Use coinstatsindex, if available."},
1001 "The current block height (index)"},
1003 "The hash of the block at the tip of the chain"},
1005 "The number of unspent transaction outputs"},
1007 "Database-independent, meaningless metric indicating "
1008 "the UTXO set size"},
1011 "The serialized hash (only present if 'hash_serialized' "
1012 "hash_type is chosen)"},
1014 "The serialized hash (only present if 'muhash' "
1015 "hash_type is chosen)"},
1017 "The number of transactions with unspent outputs (not "
1018 "available when coinstatsindex is used)"},
1020 "The estimated size of the chainstate on disk (not "
1021 "available when coinstatsindex is used)"},
1023 "The total amount"},
1025 "The total amount of coins permanently excluded from the UTXO "
1026 "set (only available if coinstatsindex is used)"},
1029 "Info on amounts in the block at this block height (only "
1030 "available if coinstatsindex is used)",
1032 "Total amount of all prevouts spent in this block"},
1034 "Coinbase subsidy amount of this block"},
1036 "Total amount of new outputs created by this block"},
1038 "Total amount of unspendable outputs created in this block"},
1041 "Detailed view of the unspendable categories",
1044 "The unspendable amount of the Genesis block subsidy"},
1046 "Transactions overridden by duplicates (no longer "
1047 "possible with BIP30)"},
1049 "Amounts sent to scripts that are unspendable (for "
1050 "example OP_RETURN outputs)"},
1052 "Fee rewards that miners did not claim in their "
1053 "coinbase transaction"},
1062 R
"("none" '"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"')") +
1068 R
"("none", "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09")")},
1075 request.params[0].isNull()
1076 ? CoinStatsHashType::HASH_SERIALIZED
1078 bool index_requested =
1079 request.params[2].isNull() || request.params[2].get_bool();
1090 coins_view = &active_chainstate.
CoinsDB();
1095 if (!request.params[1].isNull()) {
1098 "Querying specific block heights "
1099 "requires coinstatsindex");
1102 if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
1104 "hash_serialized hash type cannot be "
1105 "queried for a specific block");
1119 if (pindex->nHeight > summary.best_block_height) {
1123 "Unable to get data because coinstatsindex is "
1124 "still syncing. Current height: %d",
1125 summary.best_block_height));
1130 const std::optional<CCoinsStats> maybe_stats =
GetUTXOStats(
1131 coins_view, *blockman, hash_type,
node.rpc_interruption_point,
1132 pindex, index_requested);
1133 if (maybe_stats.has_value()) {
1139 if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
1140 ret.
pushKV(
"hash_serialized",
1143 if (hash_type == CoinStatsHashType::MUHASH) {
1149 ret.
pushKV(
"transactions",
1153 ret.
pushKV(
"total_unspendable_amount",
1157 if (pindex->nHeight > 0) {
1158 const std::optional<CCoinsStats> maybe_prev_stats =
1160 node.rpc_interruption_point,
1161 pindex->pprev, index_requested);
1162 if (!maybe_prev_stats) {
1164 "Unable to read UTXO set");
1166 prev_stats = maybe_prev_stats.value();
1173 prev_stats.total_prevout_spent_amount);
1174 block_info.
pushKV(
"coinbase",
1176 prev_stats.total_coinbase_amount);
1178 "new_outputs_ex_coinbase",
1180 prev_stats.total_new_outputs_ex_coinbase_amount);
1181 block_info.
pushKV(
"unspendable",
1183 prev_stats.total_unspendable_amount);
1189 prev_stats.total_unspendables_genesis_block);
1192 prev_stats.total_unspendables_bip30);
1195 prev_stats.total_unspendables_scripts);
1197 "unclaimed_rewards",
1199 prev_stats.total_unspendables_unclaimed_rewards);
1200 block_info.
pushKV(
"unspendables", std::move(unspendables));
1202 ret.
pushKV(
"block_info", std::move(block_info));
1206 "Unable to read UTXO set");
1216 "Returns details about an unspent transaction output.\n",
1219 "The transaction id"},
1222 "Whether to include the mempool. Note that an unspent output that "
1223 "is spent in the mempool won't appear."},
1235 "The hash of the block at the tip of the chain"},
1237 "The number of confirmations"},
1247 "Number of required signatures"},
1249 "The type, eg pubkeyhash"},
1252 "array of eCash addresses",
1261 "\nAs a JSON-RPC call\n" +
1272 int n = request.params[1].getInt<
int>();
1273 COutPoint out(txid, n);
1274 bool fMempool =
true;
1275 if (!request.params[2].isNull()) {
1276 fMempool = request.params[2].get_bool();
1291 if (!coins_view->
GetCoin(out, coin)) {
1301 ret.
pushKV(
"confirmations", 0);
1303 ret.
pushKV(
"confirmations",
1309 ret.
pushKV(
"scriptPubKey", std::move(o));
1320 "Verifies blockchain database.\n",
1325 strprintf(
"How thorough the block verification is:\n%s",
1329 "The number of blocks to check."},
1332 "Verification finished successfully. If false, check "
1333 "debug.log for reason."},
1338 const int check_level{request.params[0].
isNull()
1340 : request.params[0].getInt<
int>()};
1341 const int check_depth{request.params[1].isNull()
1343 : request.params[1].getInt<
int>()};
1351 active_chainstate.
CoinsTip(), check_level,
1359 "getblockchaininfo",
1360 "Returns an object containing various state info regarding blockchain "
1369 "current network name (main, test, regtest)"},
1371 "the height of the most-work fully-validated "
1372 "non-parked chain. The genesis block has height 0"},
1374 "the current number of headers we have validated"},
1376 "the hash of the avalanche finalized tip if any, otherwise "
1377 "the genesis block hash"},
1379 "the hash of the currently best block"},
1386 "estimate of verification progress [0..1]"},
1388 "(debug information) estimate of whether this node is in "
1389 "Initial Block Download mode"},
1391 "total amount of work in active chain, in hexadecimal"},
1393 "the estimated size of the block and undo files on disk"},
1395 "if the blocks are subject to pruning"},
1397 "lowest-height complete block stored (only present if pruning "
1400 "whether automatic pruning is enabled (only present if "
1401 "pruning is enabled)"},
1403 "the target size used by pruning (only present if automatic "
1404 "pruning is enabled)"},
1406 "any network and blockchain warnings"},
1412 const CChainParams &chainparams = config.GetChainParams();
1420 const int height{tip.
nHeight};
1424 obj.
pushKV(
"blocks", height);
1425 obj.
pushKV(
"headers", chainman.m_best_header
1426 ? chainman.m_best_header->nHeight
1429 obj.
pushKV(
"finalized_blockhash",
1430 avalanche_finalized_tip
1431 ? avalanche_finalized_tip->GetBlockHash().GetHex()
1438 "verificationprogress",
1440 obj.
pushKV(
"initialblockdownload",
1443 obj.
pushKV(
"size_on_disk",
1450 obj.
pushKV(
"pruneheight",
1451 prune_height ? prune_height.value() + 1 : 0);
1453 const bool automatic_pruning{
1455 BlockManager::PRUNE_TARGET_MANUAL};
1456 obj.
pushKV(
"automatic_pruning", automatic_pruning);
1457 if (automatic_pruning) {
1458 obj.
pushKV(
"prune_target_size",
1485 "Return information about all known tips in the block tree, including "
1486 "the main chain as well as orphaned branches.\n",
1499 "zero for main chain, otherwise length of branch connecting "
1500 "the tip to the main chain"},
1502 "status of the chain, \"active\" for the main chain\n"
1503 "Possible values for status:\n"
1504 "1. \"invalid\" This branch contains at "
1505 "least one invalid block\n"
1506 "2. \"parked\" This branch contains at "
1507 "least one parked block\n"
1508 "3. \"headers-only\" Not all blocks for this "
1509 "branch are available, but the headers are valid\n"
1510 "4. \"valid-headers\" All blocks are available for "
1511 "this branch, but they were never fully validated\n"
1512 "5. \"valid-fork\" This branch is not part of "
1513 "the active chain, but is fully validated\n"
1514 "6. \"active\" This is the tip of the "
1515 "active main chain, which is certainly valid"},
1536 std::set<const CBlockIndex *, CompareBlocksByHeight> setTips;
1537 std::set<const CBlockIndex *> setOrphans;
1538 std::set<const CBlockIndex *> setPrevs;
1540 for (
const auto &[
_, block_index] : chainman.
BlockIndex()) {
1541 if (!active_chain.
Contains(&block_index)) {
1542 setOrphans.insert(&block_index);
1543 setPrevs.insert(block_index.pprev);
1547 for (std::set<const CBlockIndex *>::iterator it =
1549 it != setOrphans.end(); ++it) {
1550 if (setPrevs.erase(*it) == 0) {
1551 setTips.insert(*it);
1556 setTips.insert(active_chain.
Tip());
1562 obj.
pushKV(
"height", block->nHeight);
1563 obj.
pushKV(
"hash", block->phashBlock->GetHex());
1565 const int branchLen =
1567 obj.
pushKV(
"branchlen", branchLen);
1570 if (active_chain.
Contains(block)) {
1573 }
else if (block->nStatus.isInvalid()) {
1576 }
else if (block->nStatus.isOnParkedChain()) {
1579 }
else if (!block->HaveNumChainTxs()) {
1582 status =
"headers-only";
1587 status =
"valid-fork";
1592 status =
"valid-headers";
1597 obj.
pushKV(
"status", status);
1610 "Treats a block as if it were received before others with the same "
1612 "\nA later preciousblock call can override the effect of an earlier "
1614 "\nThe effects of preciousblock are not retained across restarts.\n",
1617 "the hash of the block to mark as precious"},
1640 node.avalanche.get());
1681 "Permanently marks a block as invalid, as if it violated a consensus "
1685 "the hash of the block to mark as invalid"},
1708 "Marks a block as parked.\n",
1711 "the hash of the block to park"},
1718 const std::string strHash = request.params[0].
get_str();
1741 active_chainstate.
ParkBlock(state, pblockindex);
1745 node.avalanche.get());
1772 chainman.RecalculateBestHeader();
1787 "Removes invalidity status of a block, its ancestors and its"
1788 "descendants, reconsider them for activation.\n"
1789 "This can be used to undo the effects of invalidateblock.\n",
1792 "the hash of the block to reconsider"},
1816 "Removes parked status of a block and its descendants, reconsider "
1817 "them for activation.\n"
1818 "This can be used to undo the effects of parkblock.\n",
1821 "the hash of the block to unpark"},
1828 const std::string strHash = request.params[0].
get_str();
1844 if (!pblockindex->nStatus.isOnParkedChain()) {
1866 node.avalanche.get());
1883 "Compute statistics about the total number and rate of transactions "
1887 "Size of the window in number of blocks"},
1890 "The hash of the block that ends the window."},
1898 "The timestamp for the final block in the window, "
1902 "The total number of transactions in the chain up to "
1903 "that point, if known. It may be unknown when using "
1906 "The hash of the final block in the window"},
1908 "The height of the final block in the window."},
1910 "Size of the window in number of blocks"},
1912 "The elapsed time in the window in seconds. Only "
1913 "returned if \"window_block_count\" is > 0"},
1915 "The number of transactions in the window. Only "
1916 "returned if \"window_block_count\" is > 0 and if "
1917 "txcount exists for the start and end of the window."},
1919 "The average rate of transactions per second in the "
1920 "window. Only returned if \"window_interval\" is > 0 "
1921 "and if window_tx_count exists."},
1933 config.GetChainParams().GetConsensus().nPowTargetSpacing;
1935 if (request.params[1].isNull()) {
1948 "Block is not in main chain");
1954 if (request.params[0].isNull()) {
1956 std::max(0, std::min(blockcount, pindex->
nHeight - 1));
1958 blockcount = request.params[0].getInt<
int>();
1960 if (blockcount < 0 ||
1961 (blockcount > 0 && blockcount >= pindex->
nHeight)) {
1963 "Invalid block count: "
1964 "should be between 0 and "
1965 "the block's height - 1");
1972 past_block.GetMedianTimePast()};
1979 ret.
pushKV(
"window_final_block_hash",
1982 ret.
pushKV(
"window_block_count", blockcount);
1983 if (blockcount > 0) {
1984 ret.
pushKV(
"window_interval", nTimeDiff);
1985 if (pindex->
nChainTx != 0 && past_block.nChainTx != 0) {
1986 unsigned int window_tx_count =
1987 pindex->
nChainTx - past_block.nChainTx;
1988 ret.
pushKV(
"window_tx_count", window_tx_count);
1989 if (nTimeDiff > 0) {
1991 double(window_tx_count) / nTimeDiff);
2001template <
typename T>
2003 size_t size = scores.size();
2008 std::sort(scores.begin(), scores.end());
2009 if (size % 2 == 0) {
2010 return (scores[size / 2 - 1] + scores[size / 2]) / 2;
2012 return scores[size / 2];
2016template <
typename T>
static inline bool SetHasKeys(
const std::set<T> &set) {
2019template <
typename T,
typename Tk,
typename... Args>
2020static inline bool SetHasKeys(
const std::set<T> &set,
const Tk &key,
2021 const Args &...args) {
2022 return (set.count(key) != 0) ||
SetHasKeys(set, args...);
2027 sizeof(COutPoint) +
sizeof(uint32_t) +
sizeof(bool);
2033 "Compute per block statistics for a given window. All amounts are "
2037 "It won't work for some heights with pruning.\n",
2040 "The block hash or height of the target block",
2042 .type_str = {
"",
"string or numeric"}}},
2046 "Values to plot (see result below)",
2049 "Selected statistic"},
2051 "Selected statistic"},
2062 "Average feerate (in satoshis per virtual byte)"},
2065 "The block hash (to check for potential reorgs)"},
2068 "The number of inputs (excluding coinbase)"},
2071 "Maximum feerate (in satoshis per virtual byte)"},
2074 "Truncated median fee in the block"},
2076 "Truncated median feerate (in " + ticker +
" per byte)"},
2078 "The block median time past"},
2080 "Truncated median transaction size"},
2083 "Minimum feerate (in satoshis per virtual byte)"},
2089 "Total amount in all outputs (excluding coinbase and thus "
2090 "reward [ie subsidy + totalfee])"},
2092 "Total size of all non-coinbase transactions"},
2095 "The number of transactions (including coinbase)"},
2097 "The increase/decrease in the number of unspent outputs"},
2099 "The increase/decrease in size for the utxo index (not "
2100 "discounting op_return and similar)"},
2105 R
"('"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"' '["minfeerate","avgfeerate"]')") +
2107 R
"(1000 '["minfeerate","avgfeerate"]')") +
2110 R
"("00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09", ["minfeerate","avgfeerate"])") +
2112 R
"(1000, ["minfeerate","avgfeerate"])")},
2119 std::set<std::string> stats;
2120 if (!request.params[1].isNull()) {
2122 for (
unsigned int i = 0; i < stats_univalue.
size(); i++) {
2123 const std::string stat = stats_univalue[i].
get_str();
2133 const bool do_all = stats.size() == 0;
2134 const bool do_mediantxsize =
2135 do_all || stats.count(
"mediantxsize") != 0;
2136 const bool do_medianfee = do_all || stats.count(
"medianfee") != 0;
2137 const bool do_medianfeerate =
2138 do_all || stats.count(
"medianfeerate") != 0;
2139 const bool loop_inputs =
2140 do_all || do_medianfee || do_medianfeerate ||
2141 SetHasKeys(stats,
"utxo_size_inc",
"totalfee",
"avgfee",
2142 "avgfeerate",
"minfee",
"maxfee",
"minfeerate",
2144 const bool loop_outputs =
2145 do_all || loop_inputs || stats.count(
"total_out");
2146 const bool do_calculate_size =
2147 do_mediantxsize || loop_inputs ||
2148 SetHasKeys(stats,
"total_size",
"avgtxsize",
"mintxsize",
2151 const int64_t blockMaxSize = config.GetMaxBlockSize();
2159 int64_t maxtxsize = 0;
2160 int64_t mintxsize = blockMaxSize;
2161 int64_t outputs = 0;
2162 int64_t total_size = 0;
2163 int64_t utxo_size_inc = 0;
2164 std::vector<Amount> fee_array;
2165 std::vector<Amount> feerate_array;
2166 std::vector<int64_t> txsize_array;
2168 for (
size_t i = 0; i < block.
vtx.size(); ++i) {
2169 const auto &tx = block.
vtx.at(i);
2170 outputs += tx->vout.size();
2173 for (
const CTxOut &out : tx->vout) {
2174 tx_total_out += out.
nValue;
2181 if (tx->IsCoinBase()) {
2186 inputs += tx->vin.size();
2188 total_out += tx_total_out;
2190 int64_t tx_size = 0;
2191 if (do_calculate_size) {
2192 tx_size = tx->GetTotalSize();
2193 if (do_mediantxsize) {
2194 txsize_array.push_back(tx_size);
2196 maxtxsize = std::max(maxtxsize, tx_size);
2197 mintxsize = std::min(mintxsize, tx_size);
2198 total_size += tx_size;
2203 const auto &txundo = blockUndo.
vtxundo.at(i - 1);
2204 for (
const Coin &coin : txundo.vprevout) {
2207 tx_total_in += prevoutput.
nValue;
2213 Amount txfee = tx_total_in - tx_total_out;
2216 fee_array.push_back(txfee);
2218 maxfee = std::max(maxfee, txfee);
2219 minfee = std::min(minfee, txfee);
2222 Amount feerate = txfee / tx_size;
2223 if (do_medianfeerate) {
2224 feerate_array.push_back(feerate);
2226 maxfeerate = std::max(maxfeerate, feerate);
2227 minfeerate = std::min(minfeerate, feerate);
2233 block.
vtx.size() > 1
2234 ? (totalfee /
int((block.
vtx.size() - 1)))
2236 ret_all.
pushKV(
"avgfeerate", total_size > 0
2237 ? (totalfee / total_size)
2239 ret_all.
pushKV(
"avgtxsize",
2240 (block.
vtx.size() > 1)
2241 ? total_size / (block.
vtx.size() - 1)
2243 ret_all.
pushKV(
"blockhash", pindex.GetBlockHash().GetHex());
2244 ret_all.
pushKV(
"height", (int64_t)pindex.nHeight);
2245 ret_all.
pushKV(
"ins", inputs);
2246 ret_all.
pushKV(
"maxfee", maxfee);
2247 ret_all.
pushKV(
"maxfeerate", maxfeerate);
2248 ret_all.
pushKV(
"maxtxsize", maxtxsize);
2250 ret_all.
pushKV(
"medianfeerate",
2252 ret_all.
pushKV(
"mediantime", pindex.GetMedianTimePast());
2253 ret_all.
pushKV(
"mediantxsize",
2260 ret_all.
pushKV(
"mintxsize",
2261 mintxsize == blockMaxSize ? 0 : mintxsize);
2262 ret_all.
pushKV(
"outs", outputs);
2265 ret_all.
pushKV(
"time", pindex.GetBlockTime());
2266 ret_all.
pushKV(
"total_out", total_out);
2267 ret_all.
pushKV(
"total_size", total_size);
2268 ret_all.
pushKV(
"totalfee", totalfee);
2269 ret_all.
pushKV(
"txs", (int64_t)block.
vtx.size());
2270 ret_all.
pushKV(
"utxo_increase", outputs - inputs);
2271 ret_all.
pushKV(
"utxo_size_inc", utxo_size_inc);
2278 for (
const std::string &stat : stats) {
2279 const UniValue &value = ret_all[stat];
2283 strprintf(
"Invalid selected statistic %s", stat));
2294static bool FindScriptPubKey(std::atomic<int> &scan_progress,
2295 const std::atomic<bool> &should_abort,
2297 const std::set<CScript> &needles,
2298 std::map<COutPoint, Coin> &out_results,
2299 std::function<
void()> &interruption_point) {
2302 while (cursor->
Valid()) {
2308 if (++
count % 8192 == 0) {
2309 interruption_point();
2315 if (
count % 256 == 0) {
2317 const TxId &txid = key.GetTxId();
2318 uint32_t high = 0x100 * *txid.
begin() + *(txid.
begin() + 1);
2319 scan_progress = int(high * 100.0 / 65536.0 + 0.5);
2322 out_results.emplace(key, coin);
2326 scan_progress = 100;
2362 "Scans the unspent transaction output set for entries that match "
2363 "certain output descriptors.\n"
2364 "Examples of output descriptors are:\n"
2365 " addr(<address>) Outputs whose scriptPubKey "
2366 "corresponds to the specified address (does not include P2PK)\n"
2367 " raw(<hex script>) Outputs whose scriptPubKey "
2368 "equals the specified hex scripts\n"
2369 " combo(<pubkey>) P2PK and P2PKH outputs for "
2370 "the given pubkey\n"
2371 " pkh(<pubkey>) P2PKH outputs for the given "
2373 " sh(multi(<n>,<pubkey>,<pubkey>,...)) P2SH-multisig outputs for "
2374 "the given threshold and pubkeys\n"
2375 "\nIn the above, <pubkey> either refers to a fixed public key in "
2376 "hexadecimal notation, or to an xpub/xprv optionally followed by one\n"
2377 "or more path elements separated by \"/\", and optionally ending in "
2378 "\"/*\" (unhardened), or \"/*'\" or \"/*h\" (hardened) to specify all\n"
2379 "unhardened or hardened child keys.\n"
2380 "In the latter case, a range needs to be specified by below if "
2381 "different from 1000.\n"
2382 "For more information on output descriptors, see the documentation in "
2383 "the doc/descriptors.md file.\n",
2386 "The action to execute\n"
2387 " \"start\" for starting a "
2389 " \"abort\" for aborting the "
2390 "current scan (returns true when abort was successful)\n"
2392 "progress report (in %) of the current scan"},
2396 "Array of scan objects. Required for \"start\" action\n"
2397 " Every scan object is either a "
2398 "string descriptor or an object:",
2401 "An output descriptor"},
2406 "An object with output descriptor and metadata",
2409 "An output descriptor"},
2411 "The range of HD chain indexes to explore (either "
2412 "end or [begin,end])"},
2420 RPCResult{
"When action=='status' and no scan is in progress",
2423 "When action=='status' and scan is in progress",
2431 "When action=='start'",
2437 "Whether the scan was completed"},
2439 "The number of unspent transaction outputs scanned"},
2441 "The current block height (index)"},
2443 "The hash of the block at the tip of the chain"},
2453 "The transaction id"},
2458 "A specialized descriptor for the matched "
2461 "The total amount in " + ticker +
2462 " of the unspent output"},
2464 "Whether this is a coinbase output"},
2466 "Height of the unspent transaction output"},
2470 "The total amount of all found unspent outputs in " +
2478 const auto action{self.
Arg<std::string>(
"action")};
2479 if (action ==
"status") {
2487 }
else if (action ==
"abort") {
2496 }
else if (action ==
"start") {
2500 "Scan already in progress, use action "
2501 "\"abort\" or \"status\"");
2504 if (request.params.size() < 2) {
2506 "scanobjects argument is required for "
2507 "the start action");
2510 std::set<CScript> needles;
2511 std::map<CScript, std::string> descriptors;
2520 for (CScript &script : scripts) {
2521 std::string inferred =
2523 needles.emplace(script);
2524 descriptors.emplace(std::move(script),
2525 std::move(inferred));
2531 std::vector<CTxOut> input_txos;
2532 std::map<COutPoint, Coin> coins;
2536 std::unique_ptr<CCoinsViewCursor> pcursor;
2548 bool res = FindScriptPubKey(
2550 needles, coins,
node.rpc_interruption_point);
2551 result.
pushKV(
"success", res);
2556 for (
const auto &it : coins) {
2557 const COutPoint &outpoint = it.first;
2558 const Coin &coin = it.second;
2560 input_txos.push_back(txo);
2564 unspent.
pushKV(
"txid", outpoint.GetTxId().GetHex());
2565 unspent.
pushKV(
"vout", int32_t(outpoint.GetN()));
2574 result.
pushKV(
"unspents", std::move(unspents));
2575 result.
pushKV(
"total_amount", total_in);
2578 strprintf(
"Invalid action '%s'", action));
2588 "Retrieve a BIP 157 content filter for a particular block.\n",
2591 "The hash of the block"},
2593 "The type name of the filter"},
2600 "the hex-encoded filter data"},
2602 "the hex-encoded filter header"},
2606 "\"00000000c937983704a73af28acdec37b049d214a"
2607 "dbda81d7e2a3dd146f6ed09\" \"basic\"") +
2609 "\"00000000c937983704a73af28acdec37b049d214adbda81d7"
2610 "e2a3dd146f6ed09\", \"basic\"")},
2615 std::string filtertype_name =
"basic";
2616 if (!request.params[1].isNull()) {
2617 filtertype_name = request.params[1].get_str();
2623 "Unknown filtertype");
2629 "Index is not enabled for filtertype " +
2634 bool block_was_connected;
2644 block_was_connected =
2648 bool index_ready = index->BlockUntilSyncedToCurrentChain();
2655 std::string errmsg =
"Filter not found.";
2657 if (!block_was_connected) {
2659 errmsg +=
" Block was not connected to active chain.";
2660 }
else if (!index_ready) {
2662 errmsg +=
" Block filters are still in the process of "
2666 errmsg +=
" This error is unexpected and indicates index "
2693 "Network activity could not be suspended.");
2731 "Write the serialized UTXO set to a file. This can be used in "
2732 "loadtxoutset afterwards if this snapshot height is supported in the "
2733 "chainparams as well.\n\n"
2734 "Unless the the \"latest\" type is requested, the node will roll back "
2735 "to the requested height and network activity will be suspended during "
2737 "Because of this it is discouraged to interact with the node in any "
2738 "other way during the execution of this call to avoid inconsistent "
2739 "results and race conditions, particularly RPCs that interact with "
2741 "This call may take several minutes. Make sure to use no RPC timeout "
2742 "(bitcoin-cli -rpcclienttimeout=0)",
2746 "path to the output file. If relative, will be prefixed by "
2749 "The type of snapshot to create. Can be \"latest\" to create a "
2750 "snapshot of the current UTXO set or \"rollback\" to temporarily "
2751 "roll back the state of the node to a historical block before "
2752 "creating the snapshot of a historical UTXO set. This parameter "
2753 "can be omitted if a separate \"rollback\" named parameter is "
2754 "specified indicating the height or hash of a specific historical "
2755 "block. If \"rollback\" is specified and separate \"rollback\" "
2756 "named parameter is not specified, this will roll back to the "
2757 "latest valid snapshot block that can currently be loaded with "
2766 "Height or hash of the block to roll back to before "
2767 "creating the snapshot. Note: The further this number is "
2768 "from the tip, the longer this process will take. "
2769 "Consider setting a higher -rpcclienttimeout value in "
2772 .type_str = {
"",
"string or numeric"}}},
2781 "the number of coins written in the snapshot"},
2783 "the hash of the base of the snapshot"},
2785 "the height of the base of the snapshot"},
2787 "the absolute path that the snapshot was written to"},
2789 "the hash of the UTXO set contents"},
2791 "the number of transactions in the chain up to and "
2792 "including the base block"},
2795 "utxo.dat latest") +
2797 "utxo.dat rollback") +
2799 R
"(utxo.dat rollback=853456)")},
2806 const std::string snapshot_type{self.
Arg<std::string>(
"type")};
2809 : request.params[2]};
2810 if (options.exists(
"rollback")) {
2811 if (!snapshot_type.empty() && snapshot_type !=
"rollback") {
2814 strprintf(
"Invalid snapshot type \"%s\" specified with "
2820 }
else if (snapshot_type ==
"rollback") {
2821 auto snapshot_heights =
2822 node.chainman->GetParams().GetAvailableSnapshotHeights();
2824 auto max_height = std::max_element(snapshot_heights.begin(),
2825 snapshot_heights.end());
2827 }
else if (snapshot_type ==
"latest") {
2832 strprintf(
"Invalid snapshot type \"%s\" specified. Please "
2833 "specify \"rollback\" or \"latest\"",
2839 args.GetDataDirNet(),
fs::u8path(request.params[0].get_str()));
2843 args.GetDataDirNet(),
2844 fs::u8path(request.params[0].get_str() +
".incomplete"));
2849 " already exists. If you are sure this "
2850 "is what you want, "
2851 "move it out of the way first");
2859 std::optional<NetworkDisable> disable_network;
2860 std::optional<TemporaryRollback> temporary_rollback;
2864 if (target_index != tip) {
2867 if (
node.chainman->m_blockman.IsPruneMode()) {
2870 node.chainman->ActiveChain().Tip()};
2872 node.chainman->m_blockman.GetFirstBlock(
2877 if (first_block->nHeight > target_index->nHeight) {
2880 "Could not roll back to requested height since "
2881 "necessary block data is already pruned.");
2895 disable_network.emplace(connman);
2900 return node.chainman->ActiveChain().Next(target_index));
2901 temporary_rollback.emplace(*
node.chainman,
node.avalanche.get(),
2906 std::unique_ptr<CCoinsViewCursor> cursor;
2916 chainstate = &
node.chainman->ActiveChainstate();
2925 if (target_index != chainstate->
m_chain.
Tip()) {
2927 "Failed to roll back to requested height, "
2928 "reverting to tip.\n");
2931 "Could not roll back to requested height.");
2934 *chainstate,
node.rpc_interruption_point);
2940 path, temppath,
node.rpc_interruption_point);
2941 fs::rename(temppath, path);
2950 const std::function<
void()> &interruption_point) {
2951 std::unique_ptr<CCoinsViewCursor> pcursor;
2952 std::optional<CCoinsStats> maybe_stats;
2974 CoinStatsHashType::HASH_SERIALIZED,
2975 interruption_point);
2981 std::unique_ptr<CCoinsViewCursor>(chainstate.
CoinsDB().
Cursor());
2993 const std::function<
void()> &interruption_point) {
2995 strprintf(
"writing UTXO snapshot at height %s (%s) to file %s (via %s)",
3006 unsigned int iter{0};
3007 size_t written_coins_count{0};
3008 std::vector<std::pair<uint32_t, Coin>> coins;
3017 auto write_coins_to_file =
3019 const std::vector<std::pair<uint32_t, Coin>> &coins,
3020 size_t &written_coins_count) {
3023 for (
const auto &[n, coin_] : coins) {
3026 ++written_coins_count;
3031 last_txid = key.GetTxId();
3032 while (pcursor->
Valid()) {
3033 if (iter % 5000 == 0) {
3034 interruption_point();
3038 if (key.GetTxId() != last_txid) {
3039 write_coins_to_file(afile, last_txid, coins,
3040 written_coins_count);
3041 last_txid = key.GetTxId();
3044 coins.emplace_back(key.GetN(), coin);
3049 if (!coins.empty()) {
3050 write_coins_to_file(afile, last_txid, coins, written_coins_count);
3058 result.
pushKV(
"coins_written", written_coins_count);
3074 tmppath,
node.rpc_interruption_point);
3080 "Load the serialized UTXO set from a file.\n"
3081 "Once this snapshot is loaded, its contents will be deserialized into "
3082 "a second chainstate data structure, which is then used to sync to the "
3084 "Meanwhile, the original chainstate will complete the initial block "
3085 "download process in the background, eventually validating up to the "
3086 "block that the snapshot is based upon.\n\n"
3087 "The result is a usable bitcoind instance that is current with the "
3088 "network tip in a matter of minutes rather than hours. UTXO snapshot "
3089 "are typically obtained from third-party sources (HTTP, torrent, etc.) "
3090 "which is reasonable since their contents are always checked by "
3092 "This RPC is incompatible with the -chronik init option, and a node "
3093 "with multiple chainstates may not be restarted with -chronik. After "
3094 "the background validation is finished and the chainstates are merged, "
3095 "the node can be restarted again with Chronik.\n\n"
3096 "You can find more information on this process in the `assumeutxo` "
3097 "design document (https://www.bitcoinabc.org/doc/assumeutxo.html).",
3100 "path to the snapshot file. If relative, will be prefixed by "
3108 "the number of coins loaded from the snapshot"},
3110 "the hash of the base of the snapshot"},
3112 "the height of the base of the snapshot"},
3114 "the absolute path that the snapshot was loaded from"},
3129 "loadtxoutset is not compatible with Chronik.");
3136 "Couldn't open file " + path.
u8string() +
3143 }
catch (
const std::ios_base::failure &e) {
3146 strprintf(
"Unable to parse metadata: %s", e.what()));
3149 auto activation_result{
3151 if (!activation_result) {
3154 strprintf(
"Unable to load UTXO snapshot: %s. (%s)",
3172 result.
pushKV(
"tip_hash", snapshot_index.GetBlockHash().ToString());
3173 result.
pushKV(
"base_height", snapshot_index.nHeight);
3185 "progress towards the network tip"},
3187 "the base block of the snapshot this chainstate is based on, if any"},
3190 "size of the coinstip cache"},
3192 "whether the chainstate is fully validated. True if all blocks in the "
3193 "chainstate were validated, false if the chain is based on a snapshot and "
3194 "the snapshot has not yet been validated."},
3201 "\nReturn information about chainstates.\n",
3208 "the number of headers seen so far"},
3211 "list of the chainstates ordered by work, with the "
3212 "most-work (active) chainstate last",
3226 auto make_chain_data =
3241 "verificationprogress",
3243 data.
pushKV(
"coins_db_cache_bytes",
3245 data.
pushKV(
"coins_tip_cache_bytes",
3249 "snapshot_blockhash",
3252 data.
pushKV(
"validated", validated);
3256 obj.
pushKV(
"headers", chainman.m_best_header
3257 ? chainman.m_best_header->nHeight
3260 const auto &chainstates = chainman.
GetAll();
3263 obj_chainstates.push_back(
3264 make_chain_data(*
cs, !
cs->m_from_snapshot_blockhash ||
3265 chainstates.size() == 1));
3267 obj.
pushKV(
"chainstates", std::move(obj_chainstates));
3310 for (
const auto &c : commands) {
bool MoneyRange(const Amount nValue)
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
fs::path AbsPathForConfigVal(const ArgsManager &args, const fs::path &path, bool net_specific=true)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
static RPCHelpMan getblock()
static RPCHelpMan getdifficulty()
static std::atomic< bool > g_scan_in_progress
static bool SetHasKeys(const std::set< T > &set)
static RPCHelpMan reconsiderblock()
static void InvalidateBlock(ChainstateManager &chainman, avalanche::Processor *const avalanche, const BlockHash &block_hash)
static T CalculateTruncatedMedian(std::vector< T > &scores)
static RPCHelpMan invalidateblock()
static int ComputeNextBlockAndDepth(const CBlockIndex &tip, const CBlockIndex &blockindex, const CBlockIndex *&next)
static RPCHelpMan syncwithvalidationinterfacequeue()
static CBlockUndo GetUndoChecked(BlockManager &blockman, const CBlockIndex &blockindex)
static RPCHelpMan getchaintips()
static RPCHelpMan loadtxoutset()
static RPCHelpMan gettxoutsetinfo()
static RPCHelpMan getchainstates()
std::tuple< std::unique_ptr< CCoinsViewCursor >, CCoinsStats, const CBlockIndex * > PrepareUTXOSnapshot(Chainstate &chainstate, const std::function< void()> &interruption_point)
static RPCHelpMan getblockstats()
static CoinStatsHashType ParseHashType(const std::string &hash_type_input)
static RPCHelpMan preciousblock()
static constexpr size_t PER_UTXO_OVERHEAD
double GetDifficulty(const CBlockIndex &blockindex)
Calculate the difficulty for a given block index.
static RPCHelpMan scantxoutset()
static std::condition_variable cond_blockchange
static std::atomic< int > g_scan_progress
RAII object to prevent concurrency issue when scanning the txout set.
static CBlock GetBlockChecked(BlockManager &blockman, const CBlockIndex &blockindex)
std::optional< int > GetPruneHeight(const BlockManager &blockman, const CChain &chain)
static void ReconsiderBlock(ChainstateManager &chainman, avalanche::Processor *const avalanche, const BlockHash &block_hash)
static RPCHelpMan getblockfilter()
static RPCHelpMan getbestblockhash()
RPCHelpMan getblockchaininfo()
static RPCHelpMan getchaintxstats()
UniValue CreateUTXOSnapshot(node::NodeContext &node, Chainstate &chainstate, AutoFile &afile, const fs::path &path, const fs::path &tmppath)
Test-only helper to create UTXO snapshots given a chainstate and a file handle.
static RPCHelpMan waitforblock()
std::tuple< std::unique_ptr< CCoinsViewCursor >, CCoinsStats, const CBlockIndex * > PrepareUTXOSnapshot(Chainstate &chainstate, const std::function< void()> &interruption_point={}) EXCLUSIVE_LOCKS_REQUIRED(UniValue WriteUTXOSnapshot(Chainstate &chainstate, CCoinsViewCursor *pcursor, CCoinsStats *maybe_stats, const CBlockIndex *tip, AutoFile &afile, const fs::path &path, const fs::path &temppath, const std::function< void()> &interruption_point={})
static RPCHelpMan getblockfrompeer()
static RPCHelpMan getblockhash()
void RegisterBlockchainRPCCommands(CRPCTable &t)
static RPCHelpMan verifychain()
static std::atomic< bool > g_should_abort_scan
const std::vector< RPCResult > RPCHelpForChainstate
UniValue blockheaderToJSON(const CBlockIndex &tip, const CBlockIndex &blockindex)
Block header to JSON.
static RPCHelpMan waitforblockheight()
static const CBlockIndex * ParseHashOrHeight(const UniValue ¶m, ChainstateManager &chainman)
UniValue blockToJSON(BlockManager &blockman, const CBlock &block, const CBlockIndex &tip, const CBlockIndex &blockindex, TxVerbosity verbosity)
Block description to JSON.
static RPCHelpMan pruneblockchain()
static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange)
static RPCHelpMan getblockheader()
static GlobalMutex cs_blockchange
static RPCHelpMan dumptxoutset()
Serialize the UTXO set to a file for loading elsewhere.
static RPCHelpMan getblockcount()
static RPCHelpMan waitfornewblock()
void RPCNotifyBlockChange(const CBlockIndex *pindex)
Callback for when block tip changed.
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
@ SCRIPTS
Scripts & signatures ok.
@ TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
static constexpr int64_t TIMESTAMP_WINDOW
Timestamp window used as a grace period by code that compares external timestamps (such as timestamps...
const CChainParams & Params()
Return the currently selected parameters.
#define CHECK_NONFATAL(condition)
Identity function.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Non-refcounted RAII wrapper for FILE*.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Complete block filter struct as defined in BIP 157.
const std::vector< uint8_t > & GetEncodedFilter() const
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache)
Get a single filter header by block.
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
bool IsValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
CBlockIndex * pprev
pointer to the index of the predecessor of this block
CBlockHeader GetBlockHeader() const
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
int64_t GetBlockTime() const
int64_t GetMedianTimePast() const
unsigned int nTx
Number of transactions in this block.
int32_t nVersion
block header
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
BlockHash GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Undo information for a CBlock.
std::vector< CTxUndo > vtxundo
An in-memory indexed chain of blocks.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
CBlockIndex * FindEarliestAtLeast(int64_t nTime, int height) const
Find the earliest block with timestamp equal or greater than the given time and height equal or great...
int Height() const
Return the maximal height in the chain.
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
std::string GetChainTypeString() const
Return the chain type string.
const CBlock & GenesisBlock() const
const ChainTxData & TxData() const
CCoinsView that adds a memory cache for transactions to another CCoinsView.
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Cursor for iterating over CoinsView state.
virtual bool Valid() const =0
virtual bool GetKey(COutPoint &key) const =0
virtual bool GetValue(Coin &coin) const =0
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Abstract view on the open txout dataset.
virtual BlockHash GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
CCoinsView that brings transactions from a mempool into view.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
GetCoin, returning whether it exists and is not spent.
bool GetNetworkActive() const
void SetNetworkActive(bool active)
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
bool isSpent(const COutPoint &outpoint) const
An output of a transaction.
Restore the UTXO in a Coin at a given COutPoint.
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
VerifyDBResult VerifyDB(Chainstate &chainstate, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
bool IsBlockAvalancheFinalized(const CBlockIndex *pindex) const EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Checks if a block is finalized by avalanche voting.
const std::optional< BlockHash > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr, avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Find the best known block, and make it the tip of the block chain.
CChain m_chain
The current chain of blockheaders we consult and build on.
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
void ForceFlushStateToDisk()
Unconditionally flush all changes to disk.
void UnparkBlockAndChildren(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove parked status from a block and its descendants.
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
bool AvalancheFinalizeBlock(CBlockIndex *pindex, avalanche::Processor &avalanche) EXCLUSIVE_LOCKS_REQUIRED(voi ClearAvalancheFinalizedBlock)() EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Mark a block as finalized by avalanche.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
bool ParkBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Park a block.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
node::BlockMap & BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(
SnapshotCompletionResult MaybeCompleteSnapshotValidation() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
kernel::Notifications & GetNotifications() const
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
const CChainParams & GetParams() const
const Consensus::Params & GetConsensus() const
const CBlockIndex * GetAvalancheFinalizedTip() const
util::Result< CBlockIndex * > ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
uint32_t GetHeight() const
CoinsViewScanReserver()=default
Double ended buffer combining vector and stream-like interfaces.
Different type to mark Mutex at global scope.
RAII class that disables the network in its constructor and enables it in its destructor.
NetworkDisable(CConnman &connman)
virtual std::optional< std::string > FetchBlock(const Config &config, NodeId peer_id, const CBlockIndex &block_index)=0
Attempt to manually fetch block from a given peer.
auto Arg(size_t i) const
Helper to get a required or default-valued request argument.
RAII class that temporarily rolls back the local chain in it's constructor and rolls it forward again...
avalanche::Processor *const m_avalanche
const CBlockIndex & m_invalidate_index
TemporaryRollback(ChainstateManager &chainman, avalanche::Processor *const avalanche, const CBlockIndex &index)
ChainstateManager & m_chainman
void push_back(UniValue val)
const std::string & get_str() const
const std::vector< UniValue > & getValues() const
const UniValue & get_array() const
void pushKV(std::string key, UniValue val)
std::string GetRejectReason() const
std::string ToString() const
std::string ToString() const
std::string GetHex() const
std::string GetHex() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
std::string u8string() const
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
bool IsPruneMode() const
Whether running in -prune mode.
std::unique_ptr< CoinStatsIndex > g_coin_stats_index
The global UTXO set hash object.
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
TxVerbosity
Verbose level for block's transaction.
@ SHOW_DETAILS_AND_PREVOUT
The same as previous option with information about prevouts if available.
@ SHOW_TXID
Only TXID for each block's transaction.
@ SHOW_DETAILS
Include TXID, inputs, outputs, and other common block's transaction information.
void TxToUniv(const CTransaction &tx, const BlockHash &hashBlock, UniValue &entry, bool include_hex=true, const CTxUndo *txundo=nullptr, TxVerbosity verbosity=TxVerbosity::SHOW_DETAILS, std::function< bool(const CTxOut &)> is_change_func={})
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
#define LogPrintLevel(category, level,...)
#define LogPrint(category,...)
static path u8path(const std::string &utf8_str)
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
std::optional< kernel::CCoinsStats > GetUTXOStats(CCoinsView *view, BlockManager &blockman, kernel::CoinStatsHashType hash_type, const std::function< void()> &interruption_point, const CBlockIndex *pindex, bool index_requested)
Calculate statistics about the unspent transaction output set.
bilingual_str ErrorString(const Result< T > &result)
std::shared_ptr< const CTransaction > CTransactionRef
UniValue JSONRPCError(int code, const std::string &message)
@ RPC_MISC_ERROR
General application defined errors std::exception thrown in command handling.
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
@ RPC_DATABASE_ERROR
Database error.
@ RPC_DESERIALIZATION_ERROR
Error parsing or validating structure in raw format.
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
std::vector< CScript > EvalDescriptorStringOrObject(const UniValue &scanobject, FlatSigningProvider &provider)
Evaluate a descriptor given as a string, or as a {"desc":...,"range":...} object, with default range ...
const std::string UNIX_EPOCH_TIME
String used to describe UNIX epoch time in documentation, factored out to a constant for consistency.
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded values (throws error if not hex).
size_t GetSerializeSize(const T &t, int nVersion=0)
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
bool IsRPCRunning()
Query whether RPC is running.
ChainstateManager & EnsureAnyChainman(const std::any &context)
NodeContext & EnsureAnyNodeContext(const std::any &context)
CTxMemPool & EnsureMemPool(const NodeContext &node)
PeerManager & EnsurePeerman(const NodeContext &node)
ChainstateManager & EnsureChainman(const NodeContext &node)
ArgsManager & EnsureArgsman(const NodeContext &node)
CConnman & EnsureConnman(const NodeContext &node)
ArgsManager & EnsureAnyArgsman(const std::any &context)
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
static constexpr Amount zero() noexcept
A BlockHash is a unqiue identifier for a block.
Comparison function for sorting the getchaintips heads.
bool operator()(const CBlockIndex *a, const CBlockIndex *b) const
static const Currency & get()
@ RANGE
Special type that is a NUM or [NUM,NUM].
@ STR_HEX
Special type that is a STR with only hex chars.
@ OBJ_NAMED_PARAMS
Special type that behaves almost exactly like OBJ, defining an options object with a list of pre-defi...
std::string DefaultHint
Hint for default value.
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
UniValue Default
Default constant value.
std::string oneline_description
Should be empty unless it is supposed to override the auto-generated summary line.
@ ELISION
Special type to denote elision (...)
@ NUM_TIME
Special numeric to denote unix epoch time.
@ STR_HEX
Special string with only hex chars.
@ STR_AMOUNT
Special string to represent a floating point amount.
A TxId is the identifier of a transaction.
Amount total_unspendables_scripts
Total cumulative amount of outputs sent to unspendable scripts (OP_RETURN for example) up to and incl...
Amount total_coinbase_amount
Total cumulative amount of coinbase outputs up to and including this block.
Amount total_unspendables_genesis_block
The unspendable coinbase amount from the genesis block.
uint64_t coins_count
The number of coins contained.
uint64_t nTransactionOutputs
bool index_used
Signals if the coinstatsindex was used to retrieve the statistics.
Amount total_unspendables_bip30
The two unspendable coinbase outputs total amount caused by BIP30.
Amount total_prevout_spent_amount
Total cumulative amount of prevouts spent up to and including this block.
Amount total_unspendables_unclaimed_rewards
Total cumulative amount of coins lost due to unclaimed miner rewards up to and including this block.
std::optional< Amount > total_amount
The total amount, or nullopt if an overflow occurred calculating it.
Amount total_new_outputs_ex_coinbase_amount
Total cumulative amount of outputs created up to and including this block.
Amount total_unspendable_amount
Total cumulative amount of unspendable coins up to and including this block.
NodeContext struct containing references to chain state and connection state.
#define WAIT_LOCK(cs, name)
#define AssertLockNotHeld(cs)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define LOG_TIME_SECONDS(end_msg)
bilingual_str _(const char *psz)
Translation function.
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coins to signify they are only in the memory pool(since 0....
uint256 uint256S(const char *str)
uint256 from const char *.
const UniValue NullUniValue
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
double GuessVerificationProgress(const ChainTxData &data, const CBlockIndex *pindex)
Guess how far we are in the verification process at the given block index require cs_main if pindex h...
const std::vector< std::string > CHECKLEVEL_DOC
Documentation for argument 'checklevel'.
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
static constexpr int DEFAULT_CHECKLEVEL
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
static const signed int DEFAULT_CHECKBLOCKS
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
static const int PROTOCOL_VERSION
network protocol versioning
bilingual_str GetWarnings(bool verbose)
Format a string that describes several potential problems detected by the core.