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)
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) {
1148 ret.
pushKV(
"transactions",
1152 ret.
pushKV(
"total_unspendable_amount",
1156 if (pindex->nHeight > 0) {
1157 const std::optional<CCoinsStats> maybe_prev_stats =
1159 node.rpc_interruption_point,
1160 pindex->pprev, index_requested);
1161 if (!maybe_prev_stats) {
1163 "Unable to read UTXO set");
1165 prev_stats = maybe_prev_stats.value();
1172 prev_stats.total_prevout_spent_amount);
1173 block_info.
pushKV(
"coinbase",
1175 prev_stats.total_coinbase_amount);
1177 "new_outputs_ex_coinbase",
1179 prev_stats.total_new_outputs_ex_coinbase_amount);
1180 block_info.
pushKV(
"unspendable",
1182 prev_stats.total_unspendable_amount);
1188 prev_stats.total_unspendables_genesis_block);
1191 prev_stats.total_unspendables_bip30);
1194 prev_stats.total_unspendables_scripts);
1196 "unclaimed_rewards",
1198 prev_stats.total_unspendables_unclaimed_rewards);
1199 block_info.
pushKV(
"unspendables", unspendables);
1201 ret.
pushKV(
"block_info", block_info);
1205 "Unable to read UTXO set");
1215 "Returns details about an unspent transaction output.\n",
1218 "The transaction id"},
1221 "Whether to include the mempool. Note that an unspent output that "
1222 "is spent in the mempool won't appear."},
1234 "The hash of the block at the tip of the chain"},
1236 "The number of confirmations"},
1246 "Number of required signatures"},
1248 "The type, eg pubkeyhash"},
1251 "array of eCash addresses",
1260 "\nAs a JSON-RPC call\n" +
1271 int n = request.params[1].getInt<
int>();
1272 COutPoint out(txid, n);
1273 bool fMempool =
true;
1274 if (!request.params[2].isNull()) {
1275 fMempool = request.params[2].get_bool();
1290 if (!coins_view->
GetCoin(out, coin)) {
1300 ret.
pushKV(
"confirmations", 0);
1302 ret.
pushKV(
"confirmations",
1308 ret.
pushKV(
"scriptPubKey", o);
1319 "Verifies blockchain database.\n",
1324 strprintf(
"How thorough the block verification is:\n%s",
1328 "The number of blocks to check."},
1331 "Verification finished successfully. If false, check "
1332 "debug.log for reason."},
1337 const int check_level{request.params[0].
isNull()
1339 : request.params[0].getInt<
int>()};
1340 const int check_depth{request.params[1].isNull()
1342 : request.params[1].getInt<
int>()};
1350 active_chainstate.
CoinsTip(), check_level,
1358 "getblockchaininfo",
1359 "Returns an object containing various state info regarding blockchain "
1368 "current network name (main, test, regtest)"},
1370 "the height of the most-work fully-validated "
1371 "non-parked chain. The genesis block has height 0"},
1373 "the current number of headers we have validated"},
1375 "the hash of the avalanche finalized tip if any, otherwise "
1376 "the genesis block hash"},
1378 "the hash of the currently best block"},
1385 "estimate of verification progress [0..1]"},
1387 "(debug information) estimate of whether this node is in "
1388 "Initial Block Download mode"},
1390 "total amount of work in active chain, in hexadecimal"},
1392 "the estimated size of the block and undo files on disk"},
1394 "if the blocks are subject to pruning"},
1396 "lowest-height complete block stored (only present if pruning "
1399 "whether automatic pruning is enabled (only present if "
1400 "pruning is enabled)"},
1402 "the target size used by pruning (only present if automatic "
1403 "pruning is enabled)"},
1405 "any network and blockchain warnings"},
1411 const CChainParams &chainparams = config.GetChainParams();
1419 const int height{tip.
nHeight};
1423 obj.
pushKV(
"blocks", height);
1424 obj.
pushKV(
"headers", chainman.m_best_header
1425 ? chainman.m_best_header->nHeight
1428 obj.
pushKV(
"finalized_blockhash",
1429 avalanche_finalized_tip
1430 ? avalanche_finalized_tip->GetBlockHash().GetHex()
1437 "verificationprogress",
1439 obj.
pushKV(
"initialblockdownload",
1442 obj.
pushKV(
"size_on_disk",
1449 obj.
pushKV(
"pruneheight",
1450 prune_height ? prune_height.value() + 1 : 0);
1452 const bool automatic_pruning{
1454 BlockManager::PRUNE_TARGET_MANUAL};
1455 obj.
pushKV(
"automatic_pruning", automatic_pruning);
1456 if (automatic_pruning) {
1457 obj.
pushKV(
"prune_target_size",
1484 "Return information about all known tips in the block tree, including "
1485 "the main chain as well as orphaned branches.\n",
1498 "zero for main chain, otherwise length of branch connecting "
1499 "the tip to the main chain"},
1501 "status of the chain, \"active\" for the main chain\n"
1502 "Possible values for status:\n"
1503 "1. \"invalid\" This branch contains at "
1504 "least one invalid block\n"
1505 "2. \"parked\" This branch contains at "
1506 "least one parked block\n"
1507 "3. \"headers-only\" Not all blocks for this "
1508 "branch are available, but the headers are valid\n"
1509 "4. \"valid-headers\" All blocks are available for "
1510 "this branch, but they were never fully validated\n"
1511 "5. \"valid-fork\" This branch is not part of "
1512 "the active chain, but is fully validated\n"
1513 "6. \"active\" This is the tip of the "
1514 "active main chain, which is certainly valid"},
1535 std::set<const CBlockIndex *, CompareBlocksByHeight> setTips;
1536 std::set<const CBlockIndex *> setOrphans;
1537 std::set<const CBlockIndex *> setPrevs;
1539 for (
const auto &[
_, block_index] : chainman.
BlockIndex()) {
1540 if (!active_chain.
Contains(&block_index)) {
1541 setOrphans.insert(&block_index);
1542 setPrevs.insert(block_index.pprev);
1546 for (std::set<const CBlockIndex *>::iterator it =
1548 it != setOrphans.end(); ++it) {
1549 if (setPrevs.erase(*it) == 0) {
1550 setTips.insert(*it);
1555 setTips.insert(active_chain.
Tip());
1561 obj.
pushKV(
"height", block->nHeight);
1562 obj.
pushKV(
"hash", block->phashBlock->GetHex());
1564 const int branchLen =
1566 obj.
pushKV(
"branchlen", branchLen);
1569 if (active_chain.
Contains(block)) {
1572 }
else if (block->nStatus.isInvalid()) {
1575 }
else if (block->nStatus.isOnParkedChain()) {
1578 }
else if (!block->HaveNumChainTxs()) {
1581 status =
"headers-only";
1586 status =
"valid-fork";
1591 status =
"valid-headers";
1596 obj.
pushKV(
"status", status);
1609 "Treats a block as if it were received before others with the same "
1611 "\nA later preciousblock call can override the effect of an earlier "
1613 "\nThe effects of preciousblock are not retained across restarts.\n",
1616 "the hash of the block to mark as precious"},
1639 node.avalanche.get());
1680 "Permanently marks a block as invalid, as if it violated a consensus "
1684 "the hash of the block to mark as invalid"},
1707 "Marks a block as parked.\n",
1710 "the hash of the block to park"},
1717 const std::string strHash = request.params[0].
get_str();
1740 active_chainstate.
ParkBlock(state, pblockindex);
1744 node.avalanche.get());
1771 chainman.RecalculateBestHeader();
1786 "Removes invalidity status of a block, its ancestors and its"
1787 "descendants, reconsider them for activation.\n"
1788 "This can be used to undo the effects of invalidateblock.\n",
1791 "the hash of the block to reconsider"},
1815 "Removes parked status of a block and its descendants, reconsider "
1816 "them for activation.\n"
1817 "This can be used to undo the effects of parkblock.\n",
1820 "the hash of the block to unpark"},
1827 const std::string strHash = request.params[0].
get_str();
1843 if (!pblockindex->nStatus.isOnParkedChain()) {
1865 node.avalanche.get());
1882 "Compute statistics about the total number and rate of transactions "
1886 "Size of the window in number of blocks"},
1889 "The hash of the block that ends the window."},
1897 "The timestamp for the final block in the window, "
1901 "The total number of transactions in the chain up to "
1902 "that point, if known. It may be unknown when using "
1905 "The hash of the final block in the window"},
1907 "The height of the final block in the window."},
1909 "Size of the window in number of blocks"},
1911 "The elapsed time in the window in seconds. Only "
1912 "returned if \"window_block_count\" is > 0"},
1914 "The number of transactions in the window. Only "
1915 "returned if \"window_block_count\" is > 0 and if "
1916 "txcount exists for the start and end of the window."},
1918 "The average rate of transactions per second in the "
1919 "window. Only returned if \"window_interval\" is > 0 "
1920 "and if window_tx_count exists."},
1932 config.GetChainParams().GetConsensus().nPowTargetSpacing;
1934 if (request.params[1].isNull()) {
1947 "Block is not in main chain");
1953 if (request.params[0].isNull()) {
1955 std::max(0, std::min(blockcount, pindex->
nHeight - 1));
1957 blockcount = request.params[0].getInt<
int>();
1959 if (blockcount < 0 ||
1960 (blockcount > 0 && blockcount >= pindex->
nHeight)) {
1962 "Invalid block count: "
1963 "should be between 0 and "
1964 "the block's height - 1");
1971 past_block.GetMedianTimePast()};
1978 ret.
pushKV(
"window_final_block_hash",
1981 ret.
pushKV(
"window_block_count", blockcount);
1982 if (blockcount > 0) {
1983 ret.
pushKV(
"window_interval", nTimeDiff);
1984 if (pindex->
nChainTx != 0 && past_block.nChainTx != 0) {
1985 unsigned int window_tx_count =
1986 pindex->
nChainTx - past_block.nChainTx;
1987 ret.
pushKV(
"window_tx_count", window_tx_count);
1988 if (nTimeDiff > 0) {
1990 double(window_tx_count) / nTimeDiff);
2000template <
typename T>
2002 size_t size = scores.size();
2007 std::sort(scores.begin(), scores.end());
2008 if (size % 2 == 0) {
2009 return (scores[size / 2 - 1] + scores[size / 2]) / 2;
2011 return scores[size / 2];
2015template <
typename T>
static inline bool SetHasKeys(
const std::set<T> &set) {
2018template <
typename T,
typename Tk,
typename... Args>
2019static inline bool SetHasKeys(
const std::set<T> &set,
const Tk &key,
2020 const Args &...args) {
2021 return (set.count(key) != 0) ||
SetHasKeys(set, args...);
2026 sizeof(COutPoint) +
sizeof(uint32_t) +
sizeof(bool);
2032 "Compute per block statistics for a given window. All amounts are "
2036 "It won't work for some heights with pruning.\n",
2039 "The block hash or height of the target block",
2041 .type_str = {
"",
"string or numeric"}}},
2045 "Values to plot (see result below)",
2048 "Selected statistic"},
2050 "Selected statistic"},
2061 "Average feerate (in satoshis per virtual byte)"},
2064 "The block hash (to check for potential reorgs)"},
2067 "The number of inputs (excluding coinbase)"},
2070 "Maximum feerate (in satoshis per virtual byte)"},
2073 "Truncated median fee in the block"},
2075 "Truncated median feerate (in " + ticker +
" per byte)"},
2077 "The block median time past"},
2079 "Truncated median transaction size"},
2082 "Minimum feerate (in satoshis per virtual byte)"},
2088 "Total amount in all outputs (excluding coinbase and thus "
2089 "reward [ie subsidy + totalfee])"},
2091 "Total size of all non-coinbase transactions"},
2094 "The number of transactions (including coinbase)"},
2096 "The increase/decrease in the number of unspent outputs"},
2098 "The increase/decrease in size for the utxo index (not "
2099 "discounting op_return and similar)"},
2104 R
"('"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"' '["minfeerate","avgfeerate"]')") +
2106 R
"(1000 '["minfeerate","avgfeerate"]')") +
2109 R
"("00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09", ["minfeerate","avgfeerate"])") +
2111 R
"(1000, ["minfeerate","avgfeerate"])")},
2118 std::set<std::string> stats;
2119 if (!request.params[1].isNull()) {
2121 for (
unsigned int i = 0; i < stats_univalue.
size(); i++) {
2122 const std::string stat = stats_univalue[i].
get_str();
2132 const bool do_all = stats.size() == 0;
2133 const bool do_mediantxsize =
2134 do_all || stats.count(
"mediantxsize") != 0;
2135 const bool do_medianfee = do_all || stats.count(
"medianfee") != 0;
2136 const bool do_medianfeerate =
2137 do_all || stats.count(
"medianfeerate") != 0;
2138 const bool loop_inputs =
2139 do_all || do_medianfee || do_medianfeerate ||
2140 SetHasKeys(stats,
"utxo_size_inc",
"totalfee",
"avgfee",
2141 "avgfeerate",
"minfee",
"maxfee",
"minfeerate",
2143 const bool loop_outputs =
2144 do_all || loop_inputs || stats.count(
"total_out");
2145 const bool do_calculate_size =
2146 do_mediantxsize || loop_inputs ||
2147 SetHasKeys(stats,
"total_size",
"avgtxsize",
"mintxsize",
2150 const int64_t blockMaxSize = config.GetMaxBlockSize();
2158 int64_t maxtxsize = 0;
2159 int64_t mintxsize = blockMaxSize;
2160 int64_t outputs = 0;
2161 int64_t total_size = 0;
2162 int64_t utxo_size_inc = 0;
2163 std::vector<Amount> fee_array;
2164 std::vector<Amount> feerate_array;
2165 std::vector<int64_t> txsize_array;
2167 for (
size_t i = 0; i < block.
vtx.size(); ++i) {
2168 const auto &tx = block.
vtx.at(i);
2169 outputs += tx->vout.size();
2172 for (
const CTxOut &out : tx->vout) {
2173 tx_total_out += out.
nValue;
2180 if (tx->IsCoinBase()) {
2185 inputs += tx->vin.size();
2187 total_out += tx_total_out;
2189 int64_t tx_size = 0;
2190 if (do_calculate_size) {
2191 tx_size = tx->GetTotalSize();
2192 if (do_mediantxsize) {
2193 txsize_array.push_back(tx_size);
2195 maxtxsize = std::max(maxtxsize, tx_size);
2196 mintxsize = std::min(mintxsize, tx_size);
2197 total_size += tx_size;
2202 const auto &txundo = blockUndo.
vtxundo.at(i - 1);
2203 for (
const Coin &coin : txundo.vprevout) {
2206 tx_total_in += prevoutput.
nValue;
2212 Amount txfee = tx_total_in - tx_total_out;
2215 fee_array.push_back(txfee);
2217 maxfee = std::max(maxfee, txfee);
2218 minfee = std::min(minfee, txfee);
2221 Amount feerate = txfee / tx_size;
2222 if (do_medianfeerate) {
2223 feerate_array.push_back(feerate);
2225 maxfeerate = std::max(maxfeerate, feerate);
2226 minfeerate = std::min(minfeerate, feerate);
2232 block.
vtx.size() > 1
2233 ? (totalfee /
int((block.
vtx.size() - 1)))
2235 ret_all.
pushKV(
"avgfeerate", total_size > 0
2236 ? (totalfee / total_size)
2238 ret_all.
pushKV(
"avgtxsize",
2239 (block.
vtx.size() > 1)
2240 ? total_size / (block.
vtx.size() - 1)
2242 ret_all.
pushKV(
"blockhash", pindex.GetBlockHash().GetHex());
2243 ret_all.
pushKV(
"height", (int64_t)pindex.nHeight);
2244 ret_all.
pushKV(
"ins", inputs);
2245 ret_all.
pushKV(
"maxfee", maxfee);
2246 ret_all.
pushKV(
"maxfeerate", maxfeerate);
2247 ret_all.
pushKV(
"maxtxsize", maxtxsize);
2249 ret_all.
pushKV(
"medianfeerate",
2251 ret_all.
pushKV(
"mediantime", pindex.GetMedianTimePast());
2252 ret_all.
pushKV(
"mediantxsize",
2259 ret_all.
pushKV(
"mintxsize",
2260 mintxsize == blockMaxSize ? 0 : mintxsize);
2261 ret_all.
pushKV(
"outs", outputs);
2264 ret_all.
pushKV(
"time", pindex.GetBlockTime());
2265 ret_all.
pushKV(
"total_out", total_out);
2266 ret_all.
pushKV(
"total_size", total_size);
2267 ret_all.
pushKV(
"totalfee", totalfee);
2268 ret_all.
pushKV(
"txs", (int64_t)block.
vtx.size());
2269 ret_all.
pushKV(
"utxo_increase", outputs - inputs);
2270 ret_all.
pushKV(
"utxo_size_inc", utxo_size_inc);
2277 for (
const std::string &stat : stats) {
2278 const UniValue &value = ret_all[stat];
2282 strprintf(
"Invalid selected statistic %s", stat));
2293static bool FindScriptPubKey(std::atomic<int> &scan_progress,
2294 const std::atomic<bool> &should_abort,
2296 const std::set<CScript> &needles,
2297 std::map<COutPoint, Coin> &out_results,
2298 std::function<
void()> &interruption_point) {
2301 while (cursor->
Valid()) {
2307 if (++
count % 8192 == 0) {
2308 interruption_point();
2314 if (
count % 256 == 0) {
2316 const TxId &txid = key.GetTxId();
2317 uint32_t high = 0x100 * *txid.
begin() + *(txid.
begin() + 1);
2318 scan_progress = int(high * 100.0 / 65536.0 + 0.5);
2321 out_results.emplace(key, coin);
2325 scan_progress = 100;
2361 "Scans the unspent transaction output set for entries that match "
2362 "certain output descriptors.\n"
2363 "Examples of output descriptors are:\n"
2364 " addr(<address>) Outputs whose scriptPubKey "
2365 "corresponds to the specified address (does not include P2PK)\n"
2366 " raw(<hex script>) Outputs whose scriptPubKey "
2367 "equals the specified hex scripts\n"
2368 " combo(<pubkey>) P2PK and P2PKH outputs for "
2369 "the given pubkey\n"
2370 " pkh(<pubkey>) P2PKH outputs for the given "
2372 " sh(multi(<n>,<pubkey>,<pubkey>,...)) P2SH-multisig outputs for "
2373 "the given threshold and pubkeys\n"
2374 "\nIn the above, <pubkey> either refers to a fixed public key in "
2375 "hexadecimal notation, or to an xpub/xprv optionally followed by one\n"
2376 "or more path elements separated by \"/\", and optionally ending in "
2377 "\"/*\" (unhardened), or \"/*'\" or \"/*h\" (hardened) to specify all\n"
2378 "unhardened or hardened child keys.\n"
2379 "In the latter case, a range needs to be specified by below if "
2380 "different from 1000.\n"
2381 "For more information on output descriptors, see the documentation in "
2382 "the doc/descriptors.md file.\n",
2385 "The action to execute\n"
2386 " \"start\" for starting a "
2388 " \"abort\" for aborting the "
2389 "current scan (returns true when abort was successful)\n"
2391 "progress report (in %) of the current scan"},
2395 "Array of scan objects. Required for \"start\" action\n"
2396 " Every scan object is either a "
2397 "string descriptor or an object:",
2400 "An output descriptor"},
2405 "An object with output descriptor and metadata",
2408 "An output descriptor"},
2410 "The range of HD chain indexes to explore (either "
2411 "end or [begin,end])"},
2419 RPCResult{
"When action=='status' and no scan is in progress",
2422 "When action=='status' and scan is in progress",
2430 "When action=='start'",
2436 "Whether the scan was completed"},
2438 "The number of unspent transaction outputs scanned"},
2440 "The current block height (index)"},
2442 "The hash of the block at the tip of the chain"},
2452 "The transaction id"},
2457 "A specialized descriptor for the matched "
2460 "The total amount in " + ticker +
2461 " of the unspent output"},
2463 "Whether this is a coinbase output"},
2465 "Height of the unspent transaction output"},
2469 "The total amount of all found unspent outputs in " +
2477 const auto action{self.
Arg<std::string>(
"action")};
2478 if (action ==
"status") {
2486 }
else if (action ==
"abort") {
2495 }
else if (action ==
"start") {
2499 "Scan already in progress, use action "
2500 "\"abort\" or \"status\"");
2503 if (request.params.size() < 2) {
2505 "scanobjects argument is required for "
2506 "the start action");
2509 std::set<CScript> needles;
2510 std::map<CScript, std::string> descriptors;
2519 for (CScript &script : scripts) {
2520 std::string inferred =
2522 needles.emplace(script);
2523 descriptors.emplace(std::move(script),
2524 std::move(inferred));
2530 std::vector<CTxOut> input_txos;
2531 std::map<COutPoint, Coin> coins;
2535 std::unique_ptr<CCoinsViewCursor> pcursor;
2547 bool res = FindScriptPubKey(
2549 needles, coins,
node.rpc_interruption_point);
2550 result.
pushKV(
"success", res);
2555 for (
const auto &it : coins) {
2556 const COutPoint &outpoint = it.first;
2557 const Coin &coin = it.second;
2559 input_txos.push_back(txo);
2563 unspent.
pushKV(
"txid", outpoint.GetTxId().GetHex());
2564 unspent.
pushKV(
"vout", int32_t(outpoint.GetN()));
2573 result.
pushKV(
"unspents", unspents);
2574 result.
pushKV(
"total_amount", total_in);
2577 strprintf(
"Invalid action '%s'", action));
2587 "Retrieve a BIP 157 content filter for a particular block.\n",
2590 "The hash of the block"},
2592 "The type name of the filter"},
2599 "the hex-encoded filter data"},
2601 "the hex-encoded filter header"},
2605 "\"00000000c937983704a73af28acdec37b049d214a"
2606 "dbda81d7e2a3dd146f6ed09\" \"basic\"") +
2608 "\"00000000c937983704a73af28acdec37b049d214adbda81d7"
2609 "e2a3dd146f6ed09\", \"basic\"")},
2614 std::string filtertype_name =
"basic";
2615 if (!request.params[1].isNull()) {
2616 filtertype_name = request.params[1].get_str();
2622 "Unknown filtertype");
2628 "Index is not enabled for filtertype " +
2633 bool block_was_connected;
2643 block_was_connected =
2647 bool index_ready = index->BlockUntilSyncedToCurrentChain();
2654 std::string errmsg =
"Filter not found.";
2656 if (!block_was_connected) {
2658 errmsg +=
" Block was not connected to active chain.";
2659 }
else if (!index_ready) {
2661 errmsg +=
" Block filters are still in the process of "
2665 errmsg +=
" This error is unexpected and indicates index "
2692 "Network activity could not be suspended.");
2730 "Write the serialized UTXO set to a file. This can be used in "
2731 "loadtxoutset afterwards if this snapshot height is supported in the "
2732 "chainparams as well.\n\n"
2733 "Unless the the \"latest\" type is requested, the node will roll back "
2734 "to the requested height and network activity will be suspended during "
2736 "Because of this it is discouraged to interact with the node in any "
2737 "other way during the execution of this call to avoid inconsistent "
2738 "results and race conditions, particularly RPCs that interact with "
2740 "This call may take several minutes. Make sure to use no RPC timeout "
2741 "(bitcoin-cli -rpcclienttimeout=0)",
2745 "path to the output file. If relative, will be prefixed by "
2748 "The type of snapshot to create. Can be \"latest\" to create a "
2749 "snapshot of the current UTXO set or \"rollback\" to temporarily "
2750 "roll back the state of the node to a historical block before "
2751 "creating the snapshot of a historical UTXO set. This parameter "
2752 "can be omitted if a separate \"rollback\" named parameter is "
2753 "specified indicating the height or hash of a specific historical "
2754 "block. If \"rollback\" is specified and separate \"rollback\" "
2755 "named parameter is not specified, this will roll back to the "
2756 "latest valid snapshot block that can currently be loaded with "
2765 "Height or hash of the block to roll back to before "
2766 "creating the snapshot. Note: The further this number is "
2767 "from the tip, the longer this process will take. "
2768 "Consider setting a higher -rpcclienttimeout value in "
2771 .type_str = {
"",
"string or numeric"}}},
2780 "the number of coins written in the snapshot"},
2782 "the hash of the base of the snapshot"},
2784 "the height of the base of the snapshot"},
2786 "the absolute path that the snapshot was written to"},
2788 "the hash of the UTXO set contents"},
2790 "the number of transactions in the chain up to and "
2791 "including the base block"},
2794 "utxo.dat latest") +
2796 "utxo.dat rollback") +
2798 R
"(utxo.dat rollback=853456)")},
2805 const std::string snapshot_type{self.
Arg<std::string>(
"type")};
2808 : request.params[2]};
2809 if (options.exists(
"rollback")) {
2810 if (!snapshot_type.empty() && snapshot_type !=
"rollback") {
2813 strprintf(
"Invalid snapshot type \"%s\" specified with "
2819 }
else if (snapshot_type ==
"rollback") {
2820 auto snapshot_heights =
2821 node.chainman->GetParams().GetAvailableSnapshotHeights();
2823 auto max_height = std::max_element(snapshot_heights.begin(),
2824 snapshot_heights.end());
2826 }
else if (snapshot_type ==
"latest") {
2831 strprintf(
"Invalid snapshot type \"%s\" specified. Please "
2832 "specify \"rollback\" or \"latest\"",
2838 args.GetDataDirNet(),
fs::u8path(request.params[0].get_str()));
2842 args.GetDataDirNet(),
2843 fs::u8path(request.params[0].get_str() +
".incomplete"));
2848 " already exists. If you are sure this "
2849 "is what you want, "
2850 "move it out of the way first");
2858 std::optional<NetworkDisable> disable_network;
2859 std::optional<TemporaryRollback> temporary_rollback;
2863 if (target_index != tip) {
2866 if (
node.chainman->m_blockman.IsPruneMode()) {
2869 node.chainman->ActiveChain().Tip()};
2871 node.chainman->m_blockman.GetFirstBlock(
2876 if (first_block->nHeight > target_index->nHeight) {
2879 "Could not roll back to requested height since "
2880 "necessary block data is already pruned.");
2894 disable_network.emplace(connman);
2899 return node.chainman->ActiveChain().Next(target_index));
2900 temporary_rollback.emplace(*
node.chainman,
node.avalanche.get(),
2905 std::unique_ptr<CCoinsViewCursor> cursor;
2915 chainstate = &
node.chainman->ActiveChainstate();
2924 if (target_index != chainstate->
m_chain.
Tip()) {
2926 "Failed to roll back to requested height, "
2927 "reverting to tip.\n");
2930 "Could not roll back to requested height.");
2933 *chainstate,
node.rpc_interruption_point);
2939 path, temppath,
node.rpc_interruption_point);
2940 fs::rename(temppath, path);
2949 const std::function<
void()> &interruption_point) {
2950 std::unique_ptr<CCoinsViewCursor> pcursor;
2951 std::optional<CCoinsStats> maybe_stats;
2973 CoinStatsHashType::HASH_SERIALIZED,
2974 interruption_point);
2980 std::unique_ptr<CCoinsViewCursor>(chainstate.
CoinsDB().
Cursor());
2992 const std::function<
void()> &interruption_point) {
2994 strprintf(
"writing UTXO snapshot at height %s (%s) to file %s (via %s)",
3005 unsigned int iter{0};
3006 size_t written_coins_count{0};
3007 std::vector<std::pair<uint32_t, Coin>> coins;
3016 auto write_coins_to_file =
3018 const std::vector<std::pair<uint32_t, Coin>> &coins,
3019 size_t &written_coins_count) {
3022 for (
const auto &[n, coin_] : coins) {
3025 ++written_coins_count;
3030 last_txid = key.GetTxId();
3031 while (pcursor->
Valid()) {
3032 if (iter % 5000 == 0) {
3033 interruption_point();
3037 if (key.GetTxId() != last_txid) {
3038 write_coins_to_file(afile, last_txid, coins,
3039 written_coins_count);
3040 last_txid = key.GetTxId();
3043 coins.emplace_back(key.GetN(), coin);
3048 if (!coins.empty()) {
3049 write_coins_to_file(afile, last_txid, coins, written_coins_count);
3057 result.
pushKV(
"coins_written", written_coins_count);
3073 tmppath,
node.rpc_interruption_point);
3079 "Load the serialized UTXO set from a file.\n"
3080 "Once this snapshot is loaded, its contents will be deserialized into "
3081 "a second chainstate data structure, which is then used to sync to the "
3083 "Meanwhile, the original chainstate will complete the initial block "
3084 "download process in the background, eventually validating up to the "
3085 "block that the snapshot is based upon.\n\n"
3086 "The result is a usable bitcoind instance that is current with the "
3087 "network tip in a matter of minutes rather than hours. UTXO snapshot "
3088 "are typically obtained from third-party sources (HTTP, torrent, etc.) "
3089 "which is reasonable since their contents are always checked by "
3091 "This RPC is incompatible with the -chronik init option, and a node "
3092 "with multiple chainstates may not be restarted with -chronik. After "
3093 "the background validation is finished and the chainstates are merged, "
3094 "the node can be restarted again with Chronik.\n\n"
3095 "You can find more information on this process in the `assumeutxo` "
3096 "design document (https://www.bitcoinabc.org/doc/assumeutxo.html).",
3099 "path to the snapshot file. If relative, will be prefixed by "
3107 "the number of coins loaded from the snapshot"},
3109 "the hash of the base of the snapshot"},
3111 "the height of the base of the snapshot"},
3113 "the absolute path that the snapshot was loaded from"},
3128 "loadtxoutset is not compatible with Chronik.");
3135 "Couldn't open file " + path.
u8string() +
3142 }
catch (
const std::ios_base::failure &e) {
3145 strprintf(
"Unable to parse metadata: %s", e.what()));
3148 auto activation_result{
3150 if (!activation_result) {
3153 strprintf(
"Unable to load UTXO snapshot: %s. (%s)",
3171 result.
pushKV(
"tip_hash", snapshot_index.GetBlockHash().ToString());
3172 result.
pushKV(
"base_height", snapshot_index.nHeight);
3184 "progress towards the network tip"},
3186 "the base block of the snapshot this chainstate is based on, if any"},
3189 "size of the coinstip cache"},
3191 "whether the chainstate is fully validated. True if all blocks in the "
3192 "chainstate were validated, false if the chain is based on a snapshot and "
3193 "the snapshot has not yet been validated."},
3200 "\nReturn information about chainstates.\n",
3207 "the number of headers seen so far"},
3210 "list of the chainstates ordered by work, with the "
3211 "most-work (active) chainstate last",
3225 auto make_chain_data =
3240 "verificationprogress",
3242 data.
pushKV(
"coins_db_cache_bytes",
3244 data.
pushKV(
"coins_tip_cache_bytes",
3248 "snapshot_blockhash",
3251 data.
pushKV(
"validated", validated);
3255 obj.
pushKV(
"headers", chainman.m_best_header
3256 ? chainman.m_best_header->nHeight
3259 const auto &chainstates = chainman.
GetAll();
3262 obj_chainstates.push_back(
3263 make_chain_data(*
cs, !
cs->m_from_snapshot_blockhash ||
3264 chainstates.size() == 1));
3266 obj.
pushKV(
"chainstates", std::move(obj_chainstates));
3309 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.
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.