Bitcoin ABC 0.32.5
P2P Digital Currency
blockchain.cpp
Go to the documentation of this file.
1// Copyright (c) 2010 Satoshi Nakamoto
2// Copyright (c) 2009-2019 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <rpc/blockchain.h>
7
9#include <blockfilter.h>
10#include <chain.h>
11#include <chainparams.h>
12#include <clientversion.h>
13#include <coins.h>
14#include <common/args.h>
15#include <config.h>
16#include <consensus/amount.h>
17#include <consensus/params.h>
19#include <core_io.h>
20#include <hash.h>
23#include <logging/timer.h>
24#include <net.h>
25#include <net_processing.h>
26#include <node/blockstorage.h>
27#include <node/coinstats.h>
28#include <node/context.h>
29#include <node/utxo_snapshot.h>
31#include <rpc/server.h>
32#include <rpc/server_util.h>
33#include <rpc/util.h>
34#include <script/descriptor.h>
35#include <serialize.h>
36#include <streams.h>
37#include <txdb.h>
38#include <txmempool.h>
39#include <undo.h>
40#include <util/check.h>
41#include <util/fs.h>
42#include <util/strencodings.h>
43#include <util/string.h>
44#include <util/translation.h>
45#include <validation.h>
46#include <validationinterface.h>
47#include <warnings.h>
48
49#include <condition_variable>
50#include <cstdint>
51#include <memory>
52#include <mutex>
53#include <optional>
54
57
62
65 int height;
66};
67
69static std::condition_variable cond_blockchange;
71
72std::tuple<std::unique_ptr<CCoinsViewCursor>, CCoinsStats, const CBlockIndex *>
74 const std::function<void()> &interruption_point = {})
76
79 CCoinsStats *maybe_stats, const CBlockIndex *tip,
80 AutoFile &afile, const fs::path &path,
81 const fs::path &temppath,
82 const std::function<void()> &interruption_point = {});
83
87double GetDifficulty(const CBlockIndex &blockindex) {
88 int nShift = (blockindex.nBits >> 24) & 0xff;
89 double dDiff = double(0x0000ffff) / double(blockindex.nBits & 0x00ffffff);
90
91 while (nShift < 29) {
92 dDiff *= 256.0;
93 nShift++;
94 }
95 while (nShift > 29) {
96 dDiff /= 256.0;
97 nShift--;
98 }
99
100 return dDiff;
101}
102
104 const CBlockIndex &blockindex,
105 const CBlockIndex *&next) {
106 next = tip.GetAncestor(blockindex.nHeight + 1);
107 if (next && next->pprev == &blockindex) {
108 return tip.nHeight - blockindex.nHeight + 1;
109 }
110 next = nullptr;
111 return &blockindex == &tip ? 1 : -1;
112}
113
114static const CBlockIndex *ParseHashOrHeight(const UniValue &param,
115 ChainstateManager &chainman) {
117 CChain &active_chain = chainman.ActiveChain();
118
119 if (param.isNum()) {
120 const int height{param.getInt<int>()};
121 if (height < 0) {
122 throw JSONRPCError(
124 strprintf("Target block height %d is negative", height));
125 }
126 const int current_tip{active_chain.Height()};
127 if (height > current_tip) {
128 throw JSONRPCError(
130 strprintf("Target block height %d after current tip %d", height,
131 current_tip));
132 }
133
134 return active_chain[height];
135 } else {
136 const BlockHash hash{ParseHashV(param, "hash_or_height")};
137 const CBlockIndex *pindex = chainman.m_blockman.LookupBlockIndex(hash);
138
139 if (!pindex) {
140 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
141 }
142
143 return pindex;
144 }
145}
147 const CBlockIndex &blockindex) {
148 // Serialize passed information without accessing chain state of the active
149 // chain!
150 // For performance reasons
152
153 UniValue result(UniValue::VOBJ);
154 result.pushKV("hash", blockindex.GetBlockHash().GetHex());
155 const CBlockIndex *pnext;
156 int confirmations = ComputeNextBlockAndDepth(tip, blockindex, pnext);
157 result.pushKV("confirmations", confirmations);
158 result.pushKV("height", blockindex.nHeight);
159 result.pushKV("version", blockindex.nVersion);
160 result.pushKV("versionHex", strprintf("%08x", blockindex.nVersion));
161 result.pushKV("merkleroot", blockindex.hashMerkleRoot.GetHex());
162 result.pushKV("time", blockindex.nTime);
163 result.pushKV("mediantime", blockindex.GetMedianTimePast());
164 result.pushKV("nonce", blockindex.nNonce);
165 result.pushKV("bits", strprintf("%08x", blockindex.nBits));
166 result.pushKV("difficulty", GetDifficulty(blockindex));
167 result.pushKV("chainwork", blockindex.nChainWork.GetHex());
168 result.pushKV("nTx", blockindex.nTx);
169
170 if (blockindex.pprev) {
171 result.pushKV("previousblockhash",
172 blockindex.pprev->GetBlockHash().GetHex());
173 }
174 if (pnext) {
175 result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex());
176 }
177 return result;
178}
179
180UniValue blockToJSON(BlockManager &blockman, const CBlock &block,
181 const CBlockIndex &tip, const CBlockIndex &blockindex,
182 TxVerbosity verbosity) {
183 UniValue result = blockheaderToJSON(tip, blockindex);
184
185 result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION));
187 switch (verbosity) {
189 for (const CTransactionRef &tx : block.vtx) {
190 txs.push_back(tx->GetId().GetHex());
191 }
192 break;
193
196 CBlockUndo blockUndo;
197 const bool is_not_pruned{WITH_LOCK(
198 ::cs_main, return !blockman.IsBlockPruned(blockindex))};
199 const bool have_undo{is_not_pruned && blockman.UndoReadFromDisk(
200 blockUndo, blockindex)};
201 for (size_t i = 0; i < block.vtx.size(); ++i) {
202 const CTransactionRef &tx = block.vtx.at(i);
203 // coinbase transaction (i == 0) doesn't have undo data
204 const CTxUndo *txundo = (have_undo && i > 0)
205 ? &blockUndo.vtxundo.at(i - 1)
206 : nullptr;
208 TxToUniv(*tx, BlockHash(), objTx, true, txundo, verbosity);
209 txs.push_back(objTx);
210 }
211 break;
212 }
213
214 result.pushKV("tx", txs);
215
216 return result;
217}
218
220 return RPCHelpMan{
221 "getblockcount",
222 "Returns the height of the most-work fully-validated chain.\n"
223 "The genesis block has height 0.\n",
224 {},
225 RPCResult{RPCResult::Type::NUM, "", "The current block count"},
226 RPCExamples{HelpExampleCli("getblockcount", "") +
227 HelpExampleRpc("getblockcount", "")},
228 [&](const RPCHelpMan &self, const Config &config,
229 const JSONRPCRequest &request) -> UniValue {
230 ChainstateManager &chainman = EnsureAnyChainman(request.context);
231 LOCK(cs_main);
232 return chainman.ActiveHeight();
233 },
234 };
235}
236
238 return RPCHelpMan{
239 "getbestblockhash",
240 "Returns the hash of the best (tip) block in the "
241 "most-work fully-validated chain.\n",
242 {},
243 RPCResult{RPCResult::Type::STR_HEX, "", "the block hash, hex-encoded"},
244 RPCExamples{HelpExampleCli("getbestblockhash", "") +
245 HelpExampleRpc("getbestblockhash", "")},
246 [&](const RPCHelpMan &self, const Config &config,
247 const JSONRPCRequest &request) -> UniValue {
248 ChainstateManager &chainman = EnsureAnyChainman(request.context);
249 LOCK(cs_main);
250 return chainman.ActiveTip()->GetBlockHash().GetHex();
251 },
252 };
253}
254
256 if (pindex) {
258 latestblock.hash = pindex->GetBlockHash();
259 latestblock.height = pindex->nHeight;
260 }
261 cond_blockchange.notify_all();
262}
263
265 return RPCHelpMan{
266 "waitfornewblock",
267 "Waits for a specific new block and returns useful info about it.\n"
268 "\nReturns the current block on timeout or exit.\n",
269 {
270 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0},
271 "Time in milliseconds to wait for a response. 0 indicates no "
272 "timeout."},
273 },
275 "",
276 "",
277 {
278 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
279 {RPCResult::Type::NUM, "height", "Block height"},
280 }},
281 RPCExamples{HelpExampleCli("waitfornewblock", "1000") +
282 HelpExampleRpc("waitfornewblock", "1000")},
283 [&](const RPCHelpMan &self, const Config &config,
284 const JSONRPCRequest &request) -> UniValue {
285 int timeout = 0;
286 if (!request.params[0].isNull()) {
287 timeout = request.params[0].getInt<int>();
288 }
289
290 CUpdatedBlock block;
291 {
293 block = latestblock;
294 if (timeout) {
295 cond_blockchange.wait_for(
296 lock, std::chrono::milliseconds(timeout),
298 return latestblock.height != block.height ||
299 latestblock.hash != block.hash ||
300 !IsRPCRunning();
301 });
302 } else {
303 cond_blockchange.wait(
304 lock,
306 return latestblock.height != block.height ||
307 latestblock.hash != block.hash ||
308 !IsRPCRunning();
309 });
310 }
311 block = latestblock;
312 }
314 ret.pushKV("hash", block.hash.GetHex());
315 ret.pushKV("height", block.height);
316 return ret;
317 },
318 };
319}
320
322 return RPCHelpMan{
323 "waitforblock",
324 "Waits for a specific new block and returns useful info about it.\n"
325 "\nReturns the current block on timeout or exit.\n",
326 {
328 "Block hash to wait for."},
329 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0},
330 "Time in milliseconds to wait for a response. 0 indicates no "
331 "timeout."},
332 },
334 "",
335 "",
336 {
337 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
338 {RPCResult::Type::NUM, "height", "Block height"},
339 }},
340 RPCExamples{HelpExampleCli("waitforblock",
341 "\"0000000000079f8ef3d2c688c244eb7a4570b24c9"
342 "ed7b4a8c619eb02596f8862\" 1000") +
343 HelpExampleRpc("waitforblock",
344 "\"0000000000079f8ef3d2c688c244eb7a4570b24c9"
345 "ed7b4a8c619eb02596f8862\", 1000")},
346 [&](const RPCHelpMan &self, const Config &config,
347 const JSONRPCRequest &request) -> UniValue {
348 int timeout = 0;
349
350 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
351
352 if (!request.params[1].isNull()) {
353 timeout = request.params[1].getInt<int>();
354 }
355
356 CUpdatedBlock block;
357 {
359 if (timeout) {
360 cond_blockchange.wait_for(
361 lock, std::chrono::milliseconds(timeout),
363 return latestblock.hash == hash || !IsRPCRunning();
364 });
365 } else {
366 cond_blockchange.wait(
367 lock,
369 return latestblock.hash == hash || !IsRPCRunning();
370 });
371 }
372 block = latestblock;
373 }
374
376 ret.pushKV("hash", block.hash.GetHex());
377 ret.pushKV("height", block.height);
378 return ret;
379 },
380 };
381}
382
384 return RPCHelpMan{
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",
389 {
391 "Block height to wait for."},
392 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0},
393 "Time in milliseconds to wait for a response. 0 indicates no "
394 "timeout."},
395 },
397 "",
398 "",
399 {
400 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
401 {RPCResult::Type::NUM, "height", "Block height"},
402 }},
403 RPCExamples{HelpExampleCli("waitforblockheight", "100 1000") +
404 HelpExampleRpc("waitforblockheight", "100, 1000")},
405 [&](const RPCHelpMan &self, const Config &config,
406 const JSONRPCRequest &request) -> UniValue {
407 int timeout = 0;
408
409 int height = request.params[0].getInt<int>();
410
411 if (!request.params[1].isNull()) {
412 timeout = request.params[1].getInt<int>();
413 }
414
415 CUpdatedBlock block;
416 {
418 if (timeout) {
419 cond_blockchange.wait_for(
420 lock, std::chrono::milliseconds(timeout),
422 return latestblock.height >= height ||
423 !IsRPCRunning();
424 });
425 } else {
426 cond_blockchange.wait(
427 lock,
429 return latestblock.height >= height ||
430 !IsRPCRunning();
431 });
432 }
433 block = latestblock;
434 }
436 ret.pushKV("hash", block.hash.GetHex());
437 ret.pushKV("height", block.height);
438 return ret;
439 },
440 };
441}
442
444 return RPCHelpMan{
445 "syncwithvalidationinterfacequeue",
446 "Waits for the validation interface queue to catch up on everything "
447 "that was there when we entered this function.\n",
448 {},
450 RPCExamples{HelpExampleCli("syncwithvalidationinterfacequeue", "") +
451 HelpExampleRpc("syncwithvalidationinterfacequeue", "")},
452 [&](const RPCHelpMan &self, const Config &config,
453 const JSONRPCRequest &request) -> UniValue {
455 return NullUniValue;
456 },
457 };
458}
459
461 return RPCHelpMan{
462 "getdifficulty",
463 "Returns the proof-of-work difficulty as a multiple of the minimum "
464 "difficulty.\n",
465 {},
467 "the proof-of-work difficulty as a multiple of the minimum "
468 "difficulty."},
469 RPCExamples{HelpExampleCli("getdifficulty", "") +
470 HelpExampleRpc("getdifficulty", "")},
471 [&](const RPCHelpMan &self, const Config &config,
472 const JSONRPCRequest &request) -> UniValue {
473 ChainstateManager &chainman = EnsureAnyChainman(request.context);
474 LOCK(cs_main);
475 return GetDifficulty(*CHECK_NONFATAL(chainman.ActiveTip()));
476 },
477 };
478}
479
481 return RPCHelpMan{
482 "getblockfrompeer",
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 "
490 "scheduled.",
491 {
493 "The block hash to try to fetch"},
495 "The peer to fetch it from (see getpeerinfo for peer IDs)"},
496 },
497 RPCResult{RPCResult::Type::OBJ, "", /*optional=*/false, "", {}},
498 RPCExamples{HelpExampleCli("getblockfrompeer",
499 "\"00000000c937983704a73af28acdec37b049d214a"
500 "dbda81d7e2a3dd146f6ed09\" 0") +
501 HelpExampleRpc("getblockfrompeer",
502 "\"00000000c937983704a73af28acdec37b049d214a"
503 "dbda81d7e2a3dd146f6ed09\" 0")},
504 [&](const RPCHelpMan &self, const Config &config,
505 const JSONRPCRequest &request) -> UniValue {
506 const NodeContext &node = EnsureAnyNodeContext(request.context);
508 PeerManager &peerman = EnsurePeerman(node);
509
510 const BlockHash block_hash{
511 ParseHashV(request.params[0], "blockhash")};
512 const NodeId peer_id{request.params[1].getInt<int64_t>()};
513
514 const CBlockIndex *const index = WITH_LOCK(
515 cs_main,
516 return chainman.m_blockman.LookupBlockIndex(block_hash););
517
518 if (!index) {
519 throw JSONRPCError(RPC_MISC_ERROR, "Block header missing");
520 }
521
522 if (WITH_LOCK(::cs_main, return index->nStatus.hasData())) {
523 throw JSONRPCError(RPC_MISC_ERROR, "Block already downloaded");
524 }
525
526 if (const auto err{peerman.FetchBlock(config, peer_id, *index)}) {
527 throw JSONRPCError(RPC_MISC_ERROR, err.value());
528 }
529 return UniValue::VOBJ;
530 },
531 };
532}
533
535 return RPCHelpMan{
536 "getblockhash",
537 "Returns hash of block in best-block-chain at height provided.\n",
538 {
540 "The height index"},
541 },
542 RPCResult{RPCResult::Type::STR_HEX, "", "The block hash"},
543 RPCExamples{HelpExampleCli("getblockhash", "1000") +
544 HelpExampleRpc("getblockhash", "1000")},
545 [&](const RPCHelpMan &self, const Config &config,
546 const JSONRPCRequest &request) -> UniValue {
547 ChainstateManager &chainman = EnsureAnyChainman(request.context);
548 LOCK(cs_main);
549 const CChain &active_chain = chainman.ActiveChain();
550
551 int nHeight = request.params[0].getInt<int>();
552 if (nHeight < 0 || nHeight > active_chain.Height()) {
554 "Block height out of range");
555 }
556
557 const CBlockIndex *pblockindex = active_chain[nHeight];
558 return pblockindex->GetBlockHash().GetHex();
559 },
560 };
561}
562
564 return RPCHelpMan{
565 "getblockheader",
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",
570 {
572 "The block hash"},
573 {"verbose", RPCArg::Type::BOOL, RPCArg::Default{true},
574 "true for a json object, false for the hex-encoded data"},
575 },
576 {
577 RPCResult{
578 "for verbose = true",
580 "",
581 "",
582 {
584 "the block hash (same as provided)"},
585 {RPCResult::Type::NUM, "confirmations",
586 "The number of confirmations, or -1 if the block is not "
587 "on the main chain"},
588 {RPCResult::Type::NUM, "height",
589 "The block height or index"},
590 {RPCResult::Type::NUM, "version", "The block version"},
591 {RPCResult::Type::STR_HEX, "versionHex",
592 "The block version formatted in hexadecimal"},
593 {RPCResult::Type::STR_HEX, "merkleroot", "The merkle root"},
595 "The block time expressed in " + UNIX_EPOCH_TIME},
596 {RPCResult::Type::NUM_TIME, "mediantime",
597 "The median block time expressed in " + UNIX_EPOCH_TIME},
598 {RPCResult::Type::NUM, "nonce", "The nonce"},
599 {RPCResult::Type::STR_HEX, "bits", "The bits"},
600 {RPCResult::Type::NUM, "difficulty", "The difficulty"},
601 {RPCResult::Type::STR_HEX, "chainwork",
602 "Expected number of hashes required to produce the "
603 "current chain"},
604 {RPCResult::Type::NUM, "nTx",
605 "The number of transactions in the block"},
606 {RPCResult::Type::STR_HEX, "previousblockhash",
607 /* optional */ true,
608 "The hash of the previous block (if available)"},
609 {RPCResult::Type::STR_HEX, "nextblockhash",
610 /* optional */ true,
611 "The hash of the next block (if available)"},
612 }},
613 RPCResult{"for verbose=false", RPCResult::Type::STR_HEX, "",
614 "A string that is serialized, hex-encoded data for block "
615 "'hash'"},
616 },
617 RPCExamples{HelpExampleCli("getblockheader",
618 "\"00000000c937983704a73af28acdec37b049d214a"
619 "dbda81d7e2a3dd146f6ed09\"") +
620 HelpExampleRpc("getblockheader",
621 "\"00000000c937983704a73af28acdec37b049d214a"
622 "dbda81d7e2a3dd146f6ed09\"")},
623 [&](const RPCHelpMan &self, const Config &config,
624 const JSONRPCRequest &request) -> UniValue {
625 BlockHash hash(ParseHashV(request.params[0], "hash"));
626
627 bool fVerbose = true;
628 if (!request.params[1].isNull()) {
629 fVerbose = request.params[1].get_bool();
630 }
631
632 const CBlockIndex *pblockindex;
633 const CBlockIndex *tip;
634 {
635 ChainstateManager &chainman =
636 EnsureAnyChainman(request.context);
637 LOCK(cs_main);
638 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
639 tip = chainman.ActiveTip();
640 }
641
642 if (!pblockindex) {
644 "Block not found");
645 }
646
647 if (!fVerbose) {
648 DataStream ssBlock{};
649 ssBlock << pblockindex->GetBlockHeader();
650 std::string strHex = HexStr(ssBlock);
651 return strHex;
652 }
653
654 return blockheaderToJSON(*tip, *pblockindex);
655 },
656 };
657}
658
660 const CBlockIndex &blockindex) {
661 CBlock block;
662 {
663 LOCK(cs_main);
664 if (blockman.IsBlockPruned(blockindex)) {
666 "Block not available (pruned data)");
667 }
668 }
669
670 if (!blockman.ReadBlockFromDisk(block, blockindex)) {
671 // Block not found on disk. This could be because we have the block
672 // header in our index but not yet have the block or did not accept the
673 // block. Or if the block was pruned right after we released the lock
674 // above.
675 throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk");
676 }
677
678 return block;
679}
680
682 const CBlockIndex &blockindex) {
683 CBlockUndo blockUndo;
684
685 {
686 LOCK(cs_main);
687 if (blockman.IsBlockPruned(blockindex)) {
689 "Undo data not available (pruned data)");
690 }
691 }
692
693 if (!blockman.UndoReadFromDisk(blockUndo, blockindex)) {
694 throw JSONRPCError(RPC_MISC_ERROR, "Can't read undo data from disk");
695 }
696
697 return blockUndo;
698}
699
701 return RPCHelpMan{
702 "getblock",
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 "
706 "block <hash>.\n"
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 "
712 "chain).\n",
713 {
715 "The block hash"},
716 {"verbosity|verbose", RPCArg::Type::NUM, RPCArg::Default{1},
717 "0 for hex-encoded data, 1 for a json object, and 2 for json "
718 "object with transaction data",
720 },
721 {
722 RPCResult{"for verbosity = 0", RPCResult::Type::STR_HEX, "",
723 "A string that is serialized, hex-encoded data for block "
724 "'hash'"},
725 RPCResult{
726 "for verbosity = 1",
728 "",
729 "",
730 {
732 "the block hash (same as provided)"},
733 {RPCResult::Type::NUM, "confirmations",
734 "The number of confirmations, or -1 if the block is not "
735 "on the main chain"},
736 {RPCResult::Type::NUM, "size", "The block size"},
737 {RPCResult::Type::NUM, "height",
738 "The block height or index"},
739 {RPCResult::Type::NUM, "version", "The block version"},
740 {RPCResult::Type::STR_HEX, "versionHex",
741 "The block version formatted in hexadecimal"},
742 {RPCResult::Type::STR_HEX, "merkleroot", "The merkle root"},
744 "tx",
745 "The transaction ids",
746 {{RPCResult::Type::STR_HEX, "", "The transaction id"}}},
748 "The block time expressed in " + UNIX_EPOCH_TIME},
749 {RPCResult::Type::NUM_TIME, "mediantime",
750 "The median block time expressed in " + UNIX_EPOCH_TIME},
751 {RPCResult::Type::NUM, "nonce", "The nonce"},
752 {RPCResult::Type::STR_HEX, "bits", "The bits"},
753 {RPCResult::Type::NUM, "difficulty", "The difficulty"},
754 {RPCResult::Type::STR_HEX, "chainwork",
755 "Expected number of hashes required to produce the chain "
756 "up to this block (in hex)"},
757 {RPCResult::Type::NUM, "nTx",
758 "The number of transactions in the block"},
759 {RPCResult::Type::STR_HEX, "previousblockhash",
760 /* optional */ true,
761 "The hash of the previous block (if available)"},
762 {RPCResult::Type::STR_HEX, "nextblockhash",
763 /* optional */ true,
764 "The hash of the next block (if available)"},
765 }},
766 RPCResult{"for verbosity = 2",
768 "",
769 "",
770 {
772 "Same output as verbosity = 1"},
774 "tx",
775 "",
776 {
778 "",
779 "",
780 {
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 "
789 "available"},
790 }},
791 }},
792 }},
793 },
795 HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d"
796 "214adbda81d7e2a3dd146f6ed09\"") +
797 HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d"
798 "214adbda81d7e2a3dd146f6ed09\"")},
799 [&](const RPCHelpMan &self, const Config &config,
800 const JSONRPCRequest &request) -> UniValue {
801 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
802
803 int verbosity = 1;
804 if (!request.params[1].isNull()) {
805 if (request.params[1].isNum()) {
806 verbosity = request.params[1].getInt<int>();
807 } else {
808 verbosity = request.params[1].get_bool() ? 1 : 0;
809 }
810 }
811
812 const CBlockIndex *pblockindex;
813 const CBlockIndex *tip;
814 ChainstateManager &chainman = EnsureAnyChainman(request.context);
815 {
816 LOCK(cs_main);
817 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
818 tip = chainman.ActiveTip();
819
820 if (!pblockindex) {
822 "Block not found");
823 }
824 }
825
826 const CBlock block =
827 GetBlockChecked(chainman.m_blockman, *pblockindex);
828
829 if (verbosity <= 0) {
831 ssBlock << block;
832 std::string strHex = HexStr(ssBlock);
833 return strHex;
834 }
835
836 TxVerbosity tx_verbosity;
837 if (verbosity == 1) {
838 tx_verbosity = TxVerbosity::SHOW_TXID;
839 } else if (verbosity == 2) {
840 tx_verbosity = TxVerbosity::SHOW_DETAILS;
841 } else {
843 }
844
845 return blockToJSON(chainman.m_blockman, block, *tip, *pblockindex,
846 tx_verbosity);
847 },
848 };
849}
850
851std::optional<int> GetPruneHeight(const BlockManager &blockman,
852 const CChain &chain) {
854
855 // Search for the last block missing block data or undo data. Don't let the
856 // search consider the genesis block, because the genesis block does not
857 // have undo data, but should not be considered pruned.
858 const CBlockIndex *first_block{chain[1]};
859 const CBlockIndex *chain_tip{chain.Tip()};
860
861 // If there are no blocks after the genesis block, or no blocks at all,
862 // nothing is pruned.
863 if (!first_block || !chain_tip) {
864 return std::nullopt;
865 }
866
867 // If the chain tip is pruned, everything is pruned.
868 if (!(chain_tip->nStatus.hasData() && chain_tip->nStatus.hasUndo())) {
869 return chain_tip->nHeight;
870 }
871
872 // Get first block with data, after the last block without data.
873 // This is the start of the unpruned range of blocks.
874 const CBlockIndex *first_unpruned{CHECK_NONFATAL(
875 blockman.GetFirstBlock(*chain_tip,
876 /*status_test=*/[](const BlockStatus &status) {
877 return status.hasData() && status.hasUndo();
878 }))};
879 if (first_unpruned == first_block) {
880 // All blocks between first_block and chain_tip have data, so nothing is
881 // pruned.
882 return std::nullopt;
883 }
884
885 // Block before the first unpruned block is the last pruned block.
886 return CHECK_NONFATAL(first_unpruned->pprev)->nHeight;
887}
888
890 return RPCHelpMan{
891 "pruneblockchain",
892 "",
893 {
895 "The block height to prune up to. May be set to a discrete "
896 "height, or to a " +
898 "\n"
899 " to prune blocks whose block time is at "
900 "least 2 hours older than the provided timestamp."},
901 },
902 RPCResult{RPCResult::Type::NUM, "", "Height of the last block pruned"},
903 RPCExamples{HelpExampleCli("pruneblockchain", "1000") +
904 HelpExampleRpc("pruneblockchain", "1000")},
905 [&](const RPCHelpMan &self, const Config &config,
906 const JSONRPCRequest &request) -> UniValue {
907 ChainstateManager &chainman = EnsureAnyChainman(request.context);
908 if (!chainman.m_blockman.IsPruneMode()) {
909 throw JSONRPCError(
911 "Cannot prune blocks because node is not in prune mode.");
912 }
913
914 LOCK(cs_main);
915 Chainstate &active_chainstate = chainman.ActiveChainstate();
916 CChain &active_chain = active_chainstate.m_chain;
917
918 int heightParam = request.params[0].getInt<int>();
919 if (heightParam < 0) {
921 "Negative block height.");
922 }
923
924 // Height value more than a billion is too high to be a block
925 // height, and too low to be a block time (corresponds to timestamp
926 // from Sep 2001).
927 if (heightParam > 1000000000) {
928 // Add a 2 hour buffer to include blocks which might have had
929 // old timestamps
930 const CBlockIndex *pindex = active_chain.FindEarliestAtLeast(
931 heightParam - TIMESTAMP_WINDOW, 0);
932 if (!pindex) {
934 "Could not find block with at least the "
935 "specified timestamp.");
936 }
937 heightParam = pindex->nHeight;
938 }
939
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) {
946 throw JSONRPCError(
948 "Blockchain is shorter than the attempted prune height.");
949 } else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) {
951 "Attempt to prune blocks close to the tip. "
952 "Retaining the minimum number of blocks.\n");
953 height = chainHeight - MIN_BLOCKS_TO_KEEP;
954 }
955
956 PruneBlockFilesManual(active_chainstate, height);
957 return GetPruneHeight(chainman.m_blockman, active_chain)
958 .value_or(-1);
959 },
960 };
961}
962
963static CoinStatsHashType ParseHashType(const std::string &hash_type_input) {
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") {
970 } else {
971 throw JSONRPCError(
973 strprintf("%s is not a valid hash_type", hash_type_input));
974 }
975}
976
978 return RPCHelpMan{
979 "gettxoutsetinfo",
980 "Returns statistics about the unspent transaction output set.\n"
981 "Note this call may take some time if you are not using "
982 "coinstatsindex.\n",
983 {
984 {"hash_type", RPCArg::Type::STR, RPCArg::Default{"hash_serialized"},
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"}}},
992 {"use_index", RPCArg::Type::BOOL, RPCArg::Default{true},
993 "Use coinstatsindex, if available."},
994 },
995 RPCResult{
997 "",
998 "",
999 {
1000 {RPCResult::Type::NUM, "height",
1001 "The current block height (index)"},
1002 {RPCResult::Type::STR_HEX, "bestblock",
1003 "The hash of the block at the tip of the chain"},
1004 {RPCResult::Type::NUM, "txouts",
1005 "The number of unspent transaction outputs"},
1006 {RPCResult::Type::NUM, "bogosize",
1007 "Database-independent, meaningless metric indicating "
1008 "the UTXO set size"},
1009 {RPCResult::Type::STR_HEX, "hash_serialized",
1010 /* optional */ true,
1011 "The serialized hash (only present if 'hash_serialized' "
1012 "hash_type is chosen)"},
1013 {RPCResult::Type::STR_HEX, "muhash", /* optional */ true,
1014 "The serialized hash (only present if 'muhash' "
1015 "hash_type is chosen)"},
1016 {RPCResult::Type::NUM, "transactions",
1017 "The number of transactions with unspent outputs (not "
1018 "available when coinstatsindex is used)"},
1019 {RPCResult::Type::NUM, "disk_size",
1020 "The estimated size of the chainstate on disk (not "
1021 "available when coinstatsindex is used)"},
1022 {RPCResult::Type::STR_AMOUNT, "total_amount",
1023 "The total amount"},
1024 {RPCResult::Type::STR_AMOUNT, "total_unspendable_amount",
1025 "The total amount of coins permanently excluded from the UTXO "
1026 "set (only available if coinstatsindex is used)"},
1028 "block_info",
1029 "Info on amounts in the block at this block height (only "
1030 "available if coinstatsindex is used)",
1031 {{RPCResult::Type::STR_AMOUNT, "prevout_spent",
1032 "Total amount of all prevouts spent in this block"},
1033 {RPCResult::Type::STR_AMOUNT, "coinbase",
1034 "Coinbase subsidy amount of this block"},
1035 {RPCResult::Type::STR_AMOUNT, "new_outputs_ex_coinbase",
1036 "Total amount of new outputs created by this block"},
1037 {RPCResult::Type::STR_AMOUNT, "unspendable",
1038 "Total amount of unspendable outputs created in this block"},
1040 "unspendables",
1041 "Detailed view of the unspendable categories",
1042 {
1043 {RPCResult::Type::STR_AMOUNT, "genesis_block",
1044 "The unspendable amount of the Genesis block subsidy"},
1046 "Transactions overridden by duplicates (no longer "
1047 "possible with BIP30)"},
1048 {RPCResult::Type::STR_AMOUNT, "scripts",
1049 "Amounts sent to scripts that are unspendable (for "
1050 "example OP_RETURN outputs)"},
1051 {RPCResult::Type::STR_AMOUNT, "unclaimed_rewards",
1052 "Fee rewards that miners did not claim in their "
1053 "coinbase transaction"},
1054 }}}},
1055 }},
1057 HelpExampleCli("gettxoutsetinfo", "") +
1058 HelpExampleCli("gettxoutsetinfo", R"("none")") +
1059 HelpExampleCli("gettxoutsetinfo", R"("none" 1000)") +
1061 "gettxoutsetinfo",
1062 R"("none" '"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"')") +
1063 HelpExampleRpc("gettxoutsetinfo", "") +
1064 HelpExampleRpc("gettxoutsetinfo", R"("none")") +
1065 HelpExampleRpc("gettxoutsetinfo", R"("none", 1000)") +
1067 "gettxoutsetinfo",
1068 R"("none", "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09")")},
1069 [&](const RPCHelpMan &self, const Config &config,
1070 const JSONRPCRequest &request) -> UniValue {
1072
1073 const CBlockIndex *pindex{nullptr};
1074 const CoinStatsHashType hash_type{
1075 request.params[0].isNull()
1076 ? CoinStatsHashType::HASH_SERIALIZED
1077 : ParseHashType(request.params[0].get_str())};
1078 bool index_requested =
1079 request.params[2].isNull() || request.params[2].get_bool();
1080
1081 NodeContext &node = EnsureAnyNodeContext(request.context);
1083 Chainstate &active_chainstate = chainman.ActiveChainstate();
1084 active_chainstate.ForceFlushStateToDisk();
1085
1086 CCoinsView *coins_view;
1087 BlockManager *blockman;
1088 {
1089 LOCK(::cs_main);
1090 coins_view = &active_chainstate.CoinsDB();
1091 blockman = &active_chainstate.m_blockman;
1092 pindex = blockman->LookupBlockIndex(coins_view->GetBestBlock());
1093 }
1094
1095 if (!request.params[1].isNull()) {
1096 if (!g_coin_stats_index) {
1098 "Querying specific block heights "
1099 "requires coinstatsindex");
1100 }
1101
1102 if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
1104 "hash_serialized hash type cannot be "
1105 "queried for a specific block");
1106 }
1107
1108 pindex = ParseHashOrHeight(request.params[1], chainman);
1109 }
1110
1111 if (index_requested && g_coin_stats_index) {
1112 if (!g_coin_stats_index->BlockUntilSyncedToCurrentChain()) {
1113 const IndexSummary summary{
1114 g_coin_stats_index->GetSummary()};
1115
1116 // If a specific block was requested and the index has
1117 // already synced past that height, we can return the data
1118 // already even though the index is not fully synced yet.
1119 if (pindex->nHeight > summary.best_block_height) {
1120 throw JSONRPCError(
1122 strprintf(
1123 "Unable to get data because coinstatsindex is "
1124 "still syncing. Current height: %d",
1125 summary.best_block_height));
1126 }
1127 }
1128 }
1129
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()) {
1134 const CCoinsStats &stats = maybe_stats.value();
1135 ret.pushKV("height", int64_t(stats.nHeight));
1136 ret.pushKV("bestblock", stats.hashBlock.GetHex());
1137 ret.pushKV("txouts", int64_t(stats.nTransactionOutputs));
1138 ret.pushKV("bogosize", int64_t(stats.nBogoSize));
1139 if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
1140 ret.pushKV("hash_serialized",
1141 stats.hashSerialized.GetHex());
1142 }
1143 if (hash_type == CoinStatsHashType::MUHASH) {
1144 ret.pushKV("muhash", stats.hashSerialized.GetHex());
1145 }
1146 ret.pushKV("total_amount", stats.nTotalAmount);
1147 if (!stats.index_used) {
1148 ret.pushKV("transactions",
1149 static_cast<int64_t>(stats.nTransactions));
1150 ret.pushKV("disk_size", stats.nDiskSize);
1151 } else {
1152 ret.pushKV("total_unspendable_amount",
1154
1155 CCoinsStats prev_stats{};
1156 if (pindex->nHeight > 0) {
1157 const std::optional<CCoinsStats> maybe_prev_stats =
1158 GetUTXOStats(coins_view, *blockman, hash_type,
1159 node.rpc_interruption_point,
1160 pindex->pprev, index_requested);
1161 if (!maybe_prev_stats) {
1163 "Unable to read UTXO set");
1164 }
1165 prev_stats = maybe_prev_stats.value();
1166 }
1167
1168 UniValue block_info(UniValue::VOBJ);
1169 block_info.pushKV(
1170 "prevout_spent",
1172 prev_stats.total_prevout_spent_amount);
1173 block_info.pushKV("coinbase",
1174 stats.total_coinbase_amount -
1175 prev_stats.total_coinbase_amount);
1176 block_info.pushKV(
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);
1183
1184 UniValue unspendables(UniValue::VOBJ);
1185 unspendables.pushKV(
1186 "genesis_block",
1188 prev_stats.total_unspendables_genesis_block);
1189 unspendables.pushKV(
1190 "bip30", stats.total_unspendables_bip30 -
1191 prev_stats.total_unspendables_bip30);
1192 unspendables.pushKV(
1193 "scripts", stats.total_unspendables_scripts -
1194 prev_stats.total_unspendables_scripts);
1195 unspendables.pushKV(
1196 "unclaimed_rewards",
1198 prev_stats.total_unspendables_unclaimed_rewards);
1199 block_info.pushKV("unspendables", unspendables);
1200
1201 ret.pushKV("block_info", block_info);
1202 }
1203 } else {
1205 "Unable to read UTXO set");
1206 }
1207 return ret;
1208 },
1209 };
1210}
1211
1213 return RPCHelpMan{
1214 "gettxout",
1215 "Returns details about an unspent transaction output.\n",
1216 {
1218 "The transaction id"},
1219 {"n", RPCArg::Type::NUM, RPCArg::Optional::NO, "vout number"},
1220 {"include_mempool", RPCArg::Type::BOOL, RPCArg::Default{true},
1221 "Whether to include the mempool. Note that an unspent output that "
1222 "is spent in the mempool won't appear."},
1223 },
1224 {
1225 RPCResult{"If the UTXO was not found", RPCResult::Type::NONE, "",
1226 ""},
1227 RPCResult{
1228 "Otherwise",
1230 "",
1231 "",
1232 {
1233 {RPCResult::Type::STR_HEX, "bestblock",
1234 "The hash of the block at the tip of the chain"},
1235 {RPCResult::Type::NUM, "confirmations",
1236 "The number of confirmations"},
1238 "The transaction value in " + Currency::get().ticker},
1240 "scriptPubKey",
1241 "",
1242 {
1243 {RPCResult::Type::STR_HEX, "asm", ""},
1244 {RPCResult::Type::STR_HEX, "hex", ""},
1245 {RPCResult::Type::NUM, "reqSigs",
1246 "Number of required signatures"},
1247 {RPCResult::Type::STR_HEX, "type",
1248 "The type, eg pubkeyhash"},
1250 "addresses",
1251 "array of eCash addresses",
1252 {{RPCResult::Type::STR, "address", "eCash address"}}},
1253 }},
1254 {RPCResult::Type::BOOL, "coinbase", "Coinbase or not"},
1255 }},
1256 },
1257 RPCExamples{"\nGet unspent transactions\n" +
1258 HelpExampleCli("listunspent", "") + "\nView the details\n" +
1259 HelpExampleCli("gettxout", "\"txid\" 1") +
1260 "\nAs a JSON-RPC call\n" +
1261 HelpExampleRpc("gettxout", "\"txid\", 1")},
1262 [&](const RPCHelpMan &self, const Config &config,
1263 const JSONRPCRequest &request) -> UniValue {
1264 NodeContext &node = EnsureAnyNodeContext(request.context);
1266 LOCK(cs_main);
1267
1269
1270 TxId txid(ParseHashV(request.params[0], "txid"));
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();
1276 }
1277
1278 Coin coin;
1279 Chainstate &active_chainstate = chainman.ActiveChainstate();
1280 CCoinsViewCache *coins_view = &active_chainstate.CoinsTip();
1281
1282 if (fMempool) {
1283 const CTxMemPool &mempool = EnsureMemPool(node);
1284 LOCK(mempool.cs);
1285 CCoinsViewMemPool view(coins_view, mempool);
1286 if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
1287 return NullUniValue;
1288 }
1289 } else {
1290 if (!coins_view->GetCoin(out, coin)) {
1291 return NullUniValue;
1292 }
1293 }
1294
1295 const CBlockIndex *pindex =
1296 active_chainstate.m_blockman.LookupBlockIndex(
1297 coins_view->GetBestBlock());
1298 ret.pushKV("bestblock", pindex->GetBlockHash().GetHex());
1299 if (coin.GetHeight() == MEMPOOL_HEIGHT) {
1300 ret.pushKV("confirmations", 0);
1301 } else {
1302 ret.pushKV("confirmations",
1303 int64_t(pindex->nHeight - coin.GetHeight() + 1));
1304 }
1305 ret.pushKV("value", coin.GetTxOut().nValue);
1307 ScriptPubKeyToUniv(coin.GetTxOut().scriptPubKey, o, true);
1308 ret.pushKV("scriptPubKey", o);
1309 ret.pushKV("coinbase", coin.IsCoinBase());
1310
1311 return ret;
1312 },
1313 };
1314}
1315
1317 return RPCHelpMan{
1318 "verifychain",
1319 "Verifies blockchain database.\n",
1320 {
1321 {"checklevel", RPCArg::Type::NUM,
1323 strprintf("%d, range=0-4", DEFAULT_CHECKLEVEL)},
1324 strprintf("How thorough the block verification is:\n%s",
1326 {"nblocks", RPCArg::Type::NUM,
1328 "The number of blocks to check."},
1329 },
1331 "Verification finished successfully. If false, check "
1332 "debug.log for reason."},
1333 RPCExamples{HelpExampleCli("verifychain", "") +
1334 HelpExampleRpc("verifychain", "")},
1335 [&](const RPCHelpMan &self, const Config &config,
1336 const JSONRPCRequest &request) -> UniValue {
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>()};
1343
1344 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1345 LOCK(cs_main);
1346
1347 Chainstate &active_chainstate = chainman.ActiveChainstate();
1348 return CVerifyDB(chainman.GetNotifications())
1349 .VerifyDB(active_chainstate,
1350 active_chainstate.CoinsTip(), check_level,
1351 check_depth) == VerifyDBResult::SUCCESS;
1352 },
1353 };
1354}
1355
1357 return RPCHelpMan{
1358 "getblockchaininfo",
1359 "Returns an object containing various state info regarding blockchain "
1360 "processing.\n",
1361 {},
1362 RPCResult{
1364 "",
1365 "",
1366 {
1367 {RPCResult::Type::STR, "chain",
1368 "current network name (main, test, regtest)"},
1369 {RPCResult::Type::NUM, "blocks",
1370 "the height of the most-work fully-validated "
1371 "non-parked chain. The genesis block has height 0"},
1372 {RPCResult::Type::NUM, "headers",
1373 "the current number of headers we have validated"},
1374 {RPCResult::Type::NUM, "finalized_blockhash",
1375 "the hash of the avalanche finalized tip if any, otherwise "
1376 "the genesis block hash"},
1377 {RPCResult::Type::STR, "bestblockhash",
1378 "the hash of the currently best block"},
1379 {RPCResult::Type::NUM, "difficulty", "the current difficulty"},
1381 "The block time expressed in " + UNIX_EPOCH_TIME},
1382 {RPCResult::Type::NUM_TIME, "mediantime",
1383 "The median block time expressed in " + UNIX_EPOCH_TIME},
1384 {RPCResult::Type::NUM, "verificationprogress",
1385 "estimate of verification progress [0..1]"},
1386 {RPCResult::Type::BOOL, "initialblockdownload",
1387 "(debug information) estimate of whether this node is in "
1388 "Initial Block Download mode"},
1389 {RPCResult::Type::STR_HEX, "chainwork",
1390 "total amount of work in active chain, in hexadecimal"},
1391 {RPCResult::Type::NUM, "size_on_disk",
1392 "the estimated size of the block and undo files on disk"},
1393 {RPCResult::Type::BOOL, "pruned",
1394 "if the blocks are subject to pruning"},
1395 {RPCResult::Type::NUM, "pruneheight",
1396 "lowest-height complete block stored (only present if pruning "
1397 "is enabled)"},
1398 {RPCResult::Type::BOOL, "automatic_pruning",
1399 "whether automatic pruning is enabled (only present if "
1400 "pruning is enabled)"},
1401 {RPCResult::Type::NUM, "prune_target_size",
1402 "the target size used by pruning (only present if automatic "
1403 "pruning is enabled)"},
1404 {RPCResult::Type::STR, "warnings",
1405 "any network and blockchain warnings"},
1406 }},
1407 RPCExamples{HelpExampleCli("getblockchaininfo", "") +
1408 HelpExampleRpc("getblockchaininfo", "")},
1409 [&](const RPCHelpMan &self, const Config &config,
1410 const JSONRPCRequest &request) -> UniValue {
1411 const CChainParams &chainparams = config.GetChainParams();
1412
1413 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1414 LOCK(cs_main);
1415 Chainstate &active_chainstate = chainman.ActiveChainstate();
1416
1417 const CBlockIndex &tip{
1418 *CHECK_NONFATAL(active_chainstate.m_chain.Tip())};
1419 const int height{tip.nHeight};
1420
1422 obj.pushKV("chain", chainparams.GetChainTypeString());
1423 obj.pushKV("blocks", height);
1424 obj.pushKV("headers", chainman.m_best_header
1425 ? chainman.m_best_header->nHeight
1426 : -1);
1427 auto avalanche_finalized_tip{chainman.GetAvalancheFinalizedTip()};
1428 obj.pushKV("finalized_blockhash",
1429 avalanche_finalized_tip
1430 ? avalanche_finalized_tip->GetBlockHash().GetHex()
1431 : chainparams.GenesisBlock().GetHash().GetHex());
1432 obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex());
1433 obj.pushKV("difficulty", GetDifficulty(tip));
1434 obj.pushKV("time", tip.GetBlockTime());
1435 obj.pushKV("mediantime", tip.GetMedianTimePast());
1436 obj.pushKV(
1437 "verificationprogress",
1438 GuessVerificationProgress(chainman.GetParams().TxData(), &tip));
1439 obj.pushKV("initialblockdownload",
1440 chainman.IsInitialBlockDownload());
1441 obj.pushKV("chainwork", tip.nChainWork.GetHex());
1442 obj.pushKV("size_on_disk",
1444 obj.pushKV("pruned", chainman.m_blockman.IsPruneMode());
1445
1446 if (chainman.m_blockman.IsPruneMode()) {
1447 const auto prune_height{GetPruneHeight(
1448 chainman.m_blockman, active_chainstate.m_chain)};
1449 obj.pushKV("pruneheight",
1450 prune_height ? prune_height.value() + 1 : 0);
1451
1452 const bool automatic_pruning{
1453 chainman.m_blockman.GetPruneTarget() !=
1454 BlockManager::PRUNE_TARGET_MANUAL};
1455 obj.pushKV("automatic_pruning", automatic_pruning);
1456 if (automatic_pruning) {
1457 obj.pushKV("prune_target_size",
1458 chainman.m_blockman.GetPruneTarget());
1459 }
1460 }
1461
1462 obj.pushKV("warnings", GetWarnings(false).original);
1463 return obj;
1464 },
1465 };
1466}
1467
1470 bool operator()(const CBlockIndex *a, const CBlockIndex *b) const {
1471 // Make sure that unequal blocks with the same height do not compare
1472 // equal. Use the pointers themselves to make a distinction.
1473 if (a->nHeight != b->nHeight) {
1474 return (a->nHeight > b->nHeight);
1475 }
1476
1477 return a < b;
1478 }
1479};
1480
1482 return RPCHelpMan{
1483 "getchaintips",
1484 "Return information about all known tips in the block tree, including "
1485 "the main chain as well as orphaned branches.\n",
1486 {},
1487 RPCResult{
1489 "",
1490 "",
1492 "",
1493 "",
1494 {
1495 {RPCResult::Type::NUM, "height", "height of the chain tip"},
1496 {RPCResult::Type::STR_HEX, "hash", "block hash of the tip"},
1497 {RPCResult::Type::NUM, "branchlen",
1498 "zero for main chain, otherwise length of branch connecting "
1499 "the tip to the main chain"},
1500 {RPCResult::Type::STR, "status",
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"},
1515 }}}},
1516 RPCExamples{HelpExampleCli("getchaintips", "") +
1517 HelpExampleRpc("getchaintips", "")},
1518 [&](const RPCHelpMan &self, const Config &config,
1519 const JSONRPCRequest &request) -> UniValue {
1520 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1521 LOCK(cs_main);
1522 CChain &active_chain = chainman.ActiveChain();
1523
1535 std::set<const CBlockIndex *, CompareBlocksByHeight> setTips;
1536 std::set<const CBlockIndex *> setOrphans;
1537 std::set<const CBlockIndex *> setPrevs;
1538
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);
1543 }
1544 }
1545
1546 for (std::set<const CBlockIndex *>::iterator it =
1547 setOrphans.begin();
1548 it != setOrphans.end(); ++it) {
1549 if (setPrevs.erase(*it) == 0) {
1550 setTips.insert(*it);
1551 }
1552 }
1553
1554 // Always report the currently active tip.
1555 setTips.insert(active_chain.Tip());
1556
1557 /* Construct the output array. */
1559 for (const CBlockIndex *block : setTips) {
1561 obj.pushKV("height", block->nHeight);
1562 obj.pushKV("hash", block->phashBlock->GetHex());
1563
1564 const int branchLen =
1565 block->nHeight - active_chain.FindFork(block)->nHeight;
1566 obj.pushKV("branchlen", branchLen);
1567
1568 std::string status;
1569 if (active_chain.Contains(block)) {
1570 // This block is part of the currently active chain.
1571 status = "active";
1572 } else if (block->nStatus.isInvalid()) {
1573 // This block or one of its ancestors is invalid.
1574 status = "invalid";
1575 } else if (block->nStatus.isOnParkedChain()) {
1576 // This block or one of its ancestors is parked.
1577 status = "parked";
1578 } else if (!block->HaveNumChainTxs()) {
1579 // This block cannot be connected because full block data
1580 // for it or one of its parents is missing.
1581 status = "headers-only";
1582 } else if (block->IsValid(BlockValidity::SCRIPTS)) {
1583 // This block is fully validated, but no longer part of the
1584 // active chain. It was probably the active block once, but
1585 // was reorganized.
1586 status = "valid-fork";
1587 } else if (block->IsValid(BlockValidity::TREE)) {
1588 // The headers for this block are valid, but it has not been
1589 // validated. It was probably never part of the most-work
1590 // chain.
1591 status = "valid-headers";
1592 } else {
1593 // No clue.
1594 status = "unknown";
1595 }
1596 obj.pushKV("status", status);
1597
1598 res.push_back(obj);
1599 }
1600
1601 return res;
1602 },
1603 };
1604}
1605
1607 return RPCHelpMan{
1608 "preciousblock",
1609 "Treats a block as if it were received before others with the same "
1610 "work.\n"
1611 "\nA later preciousblock call can override the effect of an earlier "
1612 "one.\n"
1613 "\nThe effects of preciousblock are not retained across restarts.\n",
1614 {
1616 "the hash of the block to mark as precious"},
1617 },
1619 RPCExamples{HelpExampleCli("preciousblock", "\"blockhash\"") +
1620 HelpExampleRpc("preciousblock", "\"blockhash\"")},
1621 [&](const RPCHelpMan &self, const Config &config,
1622 const JSONRPCRequest &request) -> UniValue {
1623 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1624 CBlockIndex *pblockindex;
1625
1626 NodeContext &node = EnsureAnyNodeContext(request.context);
1628 {
1629 LOCK(cs_main);
1630 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
1631 if (!pblockindex) {
1633 "Block not found");
1634 }
1635 }
1636
1638 chainman.ActiveChainstate().PreciousBlock(state, pblockindex,
1639 node.avalanche.get());
1640
1641 if (!state.IsValid()) {
1643 }
1644
1645 // Block to make sure wallet/indexers sync before returning
1647
1648 return NullUniValue;
1649 },
1650 };
1651}
1652
1655 const BlockHash &block_hash) {
1657 CBlockIndex *pblockindex;
1658 {
1659 LOCK(chainman.GetMutex());
1660 pblockindex = chainman.m_blockman.LookupBlockIndex(block_hash);
1661 if (!pblockindex) {
1662 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1663 }
1664 }
1665 chainman.ActiveChainstate().InvalidateBlock(state, pblockindex);
1666
1667 if (state.IsValid()) {
1668 chainman.ActiveChainstate().ActivateBestChain(state, /*pblock=*/nullptr,
1669 avalanche);
1670 }
1671
1672 if (!state.IsValid()) {
1674 }
1675}
1676
1678 return RPCHelpMan{
1679 "invalidateblock",
1680 "Permanently marks a block as invalid, as if it violated a consensus "
1681 "rule.\n",
1682 {
1684 "the hash of the block to mark as invalid"},
1685 },
1687 RPCExamples{HelpExampleCli("invalidateblock", "\"blockhash\"") +
1688 HelpExampleRpc("invalidateblock", "\"blockhash\"")},
1689 [&](const RPCHelpMan &self, const Config &config,
1690 const JSONRPCRequest &request) -> UniValue {
1691 NodeContext &node = EnsureAnyNodeContext(request.context);
1693 const BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1694
1695 InvalidateBlock(chainman, node.avalanche.get(), hash);
1696 // Block to make sure wallet/indexers sync before returning
1698
1699 return NullUniValue;
1700 },
1701 };
1702}
1703
1705 return RPCHelpMan{
1706 "parkblock",
1707 "Marks a block as parked.\n",
1708 {
1710 "the hash of the block to park"},
1711 },
1713 RPCExamples{HelpExampleCli("parkblock", "\"blockhash\"") +
1714 HelpExampleRpc("parkblock", "\"blockhash\"")},
1715 [&](const RPCHelpMan &self, const Config &config,
1716 const JSONRPCRequest &request) -> UniValue {
1717 const std::string strHash = request.params[0].get_str();
1718 const BlockHash hash(uint256S(strHash));
1720
1721 NodeContext &node = EnsureAnyNodeContext(request.context);
1723 Chainstate &active_chainstate = chainman.ActiveChainstate();
1724 CBlockIndex *pblockindex = nullptr;
1725 {
1726 LOCK(cs_main);
1727 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
1728 if (!pblockindex) {
1730 "Block not found");
1731 }
1732
1733 if (active_chainstate.IsBlockAvalancheFinalized(pblockindex)) {
1734 // Reset avalanche finalization if we park a finalized
1735 // block.
1736 active_chainstate.ClearAvalancheFinalizedBlock();
1737 }
1738 }
1739
1740 active_chainstate.ParkBlock(state, pblockindex);
1741
1742 if (state.IsValid()) {
1743 active_chainstate.ActivateBestChain(state, /*pblock=*/nullptr,
1744 node.avalanche.get());
1745 }
1746
1747 if (!state.IsValid()) {
1749 }
1750
1751 // Block to make sure wallet/indexers sync before returning
1753
1754 return NullUniValue;
1755 },
1756 };
1757}
1758
1761 const BlockHash &block_hash) {
1762 {
1763 LOCK(chainman.GetMutex());
1764 CBlockIndex *pblockindex =
1765 chainman.m_blockman.LookupBlockIndex(block_hash);
1766 if (!pblockindex) {
1767 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1768 }
1769
1770 chainman.ActiveChainstate().ResetBlockFailureFlags(pblockindex);
1771 chainman.RecalculateBestHeader();
1772 }
1773
1775 chainman.ActiveChainstate().ActivateBestChain(state, /*pblock=*/nullptr,
1776 avalanche);
1777
1778 if (!state.IsValid()) {
1780 }
1781}
1782
1784 return RPCHelpMan{
1785 "reconsiderblock",
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",
1789 {
1791 "the hash of the block to reconsider"},
1792 },
1794 RPCExamples{HelpExampleCli("reconsiderblock", "\"blockhash\"") +
1795 HelpExampleRpc("reconsiderblock", "\"blockhash\"")},
1796 [&](const RPCHelpMan &self, const Config &config,
1797 const JSONRPCRequest &request) -> UniValue {
1798 NodeContext &node = EnsureAnyNodeContext(request.context);
1800 const BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1801
1802 ReconsiderBlock(chainman, node.avalanche.get(), hash);
1803
1804 // Block to make sure wallet/indexers sync before returning
1806
1807 return NullUniValue;
1808 },
1809 };
1810}
1811
1813 return RPCHelpMan{
1814 "unparkblock",
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",
1818 {
1820 "the hash of the block to unpark"},
1821 },
1823 RPCExamples{HelpExampleCli("unparkblock", "\"blockhash\"") +
1824 HelpExampleRpc("unparkblock", "\"blockhash\"")},
1825 [&](const RPCHelpMan &self, const Config &config,
1826 const JSONRPCRequest &request) -> UniValue {
1827 const std::string strHash = request.params[0].get_str();
1828 NodeContext &node = EnsureAnyNodeContext(request.context);
1830 const BlockHash hash(uint256S(strHash));
1831 Chainstate &active_chainstate = chainman.ActiveChainstate();
1832
1833 {
1834 LOCK(cs_main);
1835
1836 CBlockIndex *pblockindex =
1837 chainman.m_blockman.LookupBlockIndex(hash);
1838 if (!pblockindex) {
1840 "Block not found");
1841 }
1842
1843 if (!pblockindex->nStatus.isOnParkedChain()) {
1844 // Block to unpark is not parked so there is nothing to do.
1845 return NullUniValue;
1846 }
1847
1848 const CBlockIndex *tip = active_chainstate.m_chain.Tip();
1849 if (tip) {
1850 const CBlockIndex *ancestor =
1851 LastCommonAncestor(tip, pblockindex);
1852 if (active_chainstate.IsBlockAvalancheFinalized(ancestor)) {
1853 // Only reset avalanche finalization if we unpark a
1854 // block that might conflict with avalanche finalized
1855 // blocks.
1856 active_chainstate.ClearAvalancheFinalizedBlock();
1857 }
1858 }
1859
1860 active_chainstate.UnparkBlockAndChildren(pblockindex);
1861 }
1862
1864 active_chainstate.ActivateBestChain(state, /*pblock=*/nullptr,
1865 node.avalanche.get());
1866
1867 if (!state.IsValid()) {
1869 }
1870
1871 // Block to make sure wallet/indexers sync before returning
1873
1874 return NullUniValue;
1875 },
1876 };
1877}
1878
1880 return RPCHelpMan{
1881 "getchaintxstats",
1882 "Compute statistics about the total number and rate of transactions "
1883 "in the chain.\n",
1884 {
1885 {"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint{"one month"},
1886 "Size of the window in number of blocks"},
1887 {"blockhash", RPCArg::Type::STR_HEX,
1888 RPCArg::DefaultHint{"chain tip"},
1889 "The hash of the block that ends the window."},
1890 },
1891 RPCResult{
1893 "",
1894 "",
1895 {
1897 "The timestamp for the final block in the window, "
1898 "expressed in " +
1900 {RPCResult::Type::NUM, "txcount", /*optional=*/true,
1901 "The total number of transactions in the chain up to "
1902 "that point, if known. It may be unknown when using "
1903 "assumeutxo."},
1904 {RPCResult::Type::STR_HEX, "window_final_block_hash",
1905 "The hash of the final block in the window"},
1906 {RPCResult::Type::NUM, "window_final_block_height",
1907 "The height of the final block in the window."},
1908 {RPCResult::Type::NUM, "window_block_count",
1909 "Size of the window in number of blocks"},
1910 {RPCResult::Type::NUM, "window_interval",
1911 "The elapsed time in the window in seconds. Only "
1912 "returned if \"window_block_count\" is > 0"},
1913 {RPCResult::Type::NUM, "window_tx_count", /*optional=*/true,
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."},
1917 {RPCResult::Type::NUM, "txrate", /*optional=*/true,
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."},
1921 }},
1922 RPCExamples{HelpExampleCli("getchaintxstats", "") +
1923 HelpExampleRpc("getchaintxstats", "2016")},
1924 [&](const RPCHelpMan &self, const Config &config,
1925 const JSONRPCRequest &request) -> UniValue {
1926 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1927 const CBlockIndex *pindex;
1928
1929 // By default: 1 month
1930 int blockcount =
1931 30 * 24 * 60 * 60 /
1932 config.GetChainParams().GetConsensus().nPowTargetSpacing;
1933
1934 if (request.params[1].isNull()) {
1935 LOCK(cs_main);
1936 pindex = chainman.ActiveTip();
1937 } else {
1938 BlockHash hash(ParseHashV(request.params[1], "blockhash"));
1939 LOCK(cs_main);
1940 pindex = chainman.m_blockman.LookupBlockIndex(hash);
1941 if (!pindex) {
1943 "Block not found");
1944 }
1945 if (!chainman.ActiveChain().Contains(pindex)) {
1947 "Block is not in main chain");
1948 }
1949 }
1950
1951 CHECK_NONFATAL(pindex != nullptr);
1952
1953 if (request.params[0].isNull()) {
1954 blockcount =
1955 std::max(0, std::min(blockcount, pindex->nHeight - 1));
1956 } else {
1957 blockcount = request.params[0].getInt<int>();
1958
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");
1965 }
1966 }
1967
1968 const CBlockIndex &past_block{*CHECK_NONFATAL(
1969 pindex->GetAncestor(pindex->nHeight - blockcount))};
1970 const int64_t nTimeDiff{pindex->GetMedianTimePast() -
1971 past_block.GetMedianTimePast()};
1972
1974 ret.pushKV("time", pindex->GetBlockTime());
1975 if (pindex->nChainTx) {
1976 ret.pushKV("txcount", pindex->nChainTx);
1977 }
1978 ret.pushKV("window_final_block_hash",
1979 pindex->GetBlockHash().GetHex());
1980 ret.pushKV("window_final_block_height", pindex->nHeight);
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) {
1989 ret.pushKV("txrate",
1990 double(window_tx_count) / nTimeDiff);
1991 }
1992 }
1993 }
1994
1995 return ret;
1996 },
1997 };
1998}
1999
2000template <typename T>
2001static T CalculateTruncatedMedian(std::vector<T> &scores) {
2002 size_t size = scores.size();
2003 if (size == 0) {
2004 return T();
2005 }
2006
2007 std::sort(scores.begin(), scores.end());
2008 if (size % 2 == 0) {
2009 return (scores[size / 2 - 1] + scores[size / 2]) / 2;
2010 } else {
2011 return scores[size / 2];
2012 }
2013}
2014
2015template <typename T> static inline bool SetHasKeys(const std::set<T> &set) {
2016 return false;
2017}
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...);
2022}
2023
2024// outpoint (needed for the utxo index) + nHeight + fCoinBase
2025static constexpr size_t PER_UTXO_OVERHEAD =
2026 sizeof(COutPoint) + sizeof(uint32_t) + sizeof(bool);
2027
2029 const auto &ticker = Currency::get().ticker;
2030 return RPCHelpMan{
2031 "getblockstats",
2032 "Compute per block statistics for a given window. All amounts are "
2033 "in " +
2034 ticker +
2035 ".\n"
2036 "It won't work for some heights with pruning.\n",
2037 {
2038 {"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::NO,
2039 "The block hash or height of the target block",
2041 .type_str = {"", "string or numeric"}}},
2042 {"stats",
2044 RPCArg::DefaultHint{"all values"},
2045 "Values to plot (see result below)",
2046 {
2048 "Selected statistic"},
2050 "Selected statistic"},
2051 },
2053 },
2054 RPCResult{
2056 "",
2057 "",
2058 {
2059 {RPCResult::Type::NUM, "avgfee", "Average fee in the block"},
2060 {RPCResult::Type::NUM, "avgfeerate",
2061 "Average feerate (in satoshis per virtual byte)"},
2062 {RPCResult::Type::NUM, "avgtxsize", "Average transaction size"},
2063 {RPCResult::Type::STR_HEX, "blockhash",
2064 "The block hash (to check for potential reorgs)"},
2065 {RPCResult::Type::NUM, "height", "The height of the block"},
2066 {RPCResult::Type::NUM, "ins",
2067 "The number of inputs (excluding coinbase)"},
2068 {RPCResult::Type::NUM, "maxfee", "Maximum fee in the block"},
2069 {RPCResult::Type::NUM, "maxfeerate",
2070 "Maximum feerate (in satoshis per virtual byte)"},
2071 {RPCResult::Type::NUM, "maxtxsize", "Maximum transaction size"},
2072 {RPCResult::Type::NUM, "medianfee",
2073 "Truncated median fee in the block"},
2074 {RPCResult::Type::NUM, "medianfeerate",
2075 "Truncated median feerate (in " + ticker + " per byte)"},
2076 {RPCResult::Type::NUM, "mediantime",
2077 "The block median time past"},
2078 {RPCResult::Type::NUM, "mediantxsize",
2079 "Truncated median transaction size"},
2080 {RPCResult::Type::NUM, "minfee", "Minimum fee in the block"},
2081 {RPCResult::Type::NUM, "minfeerate",
2082 "Minimum feerate (in satoshis per virtual byte)"},
2083 {RPCResult::Type::NUM, "mintxsize", "Minimum transaction size"},
2084 {RPCResult::Type::NUM, "outs", "The number of outputs"},
2085 {RPCResult::Type::NUM, "subsidy", "The block subsidy"},
2086 {RPCResult::Type::NUM, "time", "The block time"},
2087 {RPCResult::Type::NUM, "total_out",
2088 "Total amount in all outputs (excluding coinbase and thus "
2089 "reward [ie subsidy + totalfee])"},
2090 {RPCResult::Type::NUM, "total_size",
2091 "Total size of all non-coinbase transactions"},
2092 {RPCResult::Type::NUM, "totalfee", "The fee total"},
2093 {RPCResult::Type::NUM, "txs",
2094 "The number of transactions (including coinbase)"},
2095 {RPCResult::Type::NUM, "utxo_increase",
2096 "The increase/decrease in the number of unspent outputs"},
2097 {RPCResult::Type::NUM, "utxo_size_inc",
2098 "The increase/decrease in size for the utxo index (not "
2099 "discounting op_return and similar)"},
2100 }},
2103 "getblockstats",
2104 R"('"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"' '["minfeerate","avgfeerate"]')") +
2105 HelpExampleCli("getblockstats",
2106 R"(1000 '["minfeerate","avgfeerate"]')") +
2108 "getblockstats",
2109 R"("00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09", ["minfeerate","avgfeerate"])") +
2110 HelpExampleRpc("getblockstats",
2111 R"(1000, ["minfeerate","avgfeerate"])")},
2112 [&](const RPCHelpMan &self, const Config &config,
2113 const JSONRPCRequest &request) -> UniValue {
2114 ChainstateManager &chainman = EnsureAnyChainman(request.context);
2115 const CBlockIndex &pindex{*CHECK_NONFATAL(
2116 ParseHashOrHeight(request.params[0], chainman))};
2117
2118 std::set<std::string> stats;
2119 if (!request.params[1].isNull()) {
2120 const UniValue stats_univalue = request.params[1].get_array();
2121 for (unsigned int i = 0; i < stats_univalue.size(); i++) {
2122 const std::string stat = stats_univalue[i].get_str();
2123 stats.insert(stat);
2124 }
2125 }
2126
2127 const CBlock &block = GetBlockChecked(chainman.m_blockman, pindex);
2128 const CBlockUndo &blockUndo =
2129 GetUndoChecked(chainman.m_blockman, pindex);
2130
2131 // Calculate everything if nothing selected (default)
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",
2142 "maxfeerate");
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",
2148 "maxtxsize");
2149
2150 const int64_t blockMaxSize = config.GetMaxBlockSize();
2151 Amount maxfee = Amount::zero();
2152 Amount maxfeerate = Amount::zero();
2153 Amount minfee = MAX_MONEY;
2154 Amount minfeerate = MAX_MONEY;
2155 Amount total_out = Amount::zero();
2156 Amount totalfee = Amount::zero();
2157 int64_t inputs = 0;
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;
2166
2167 for (size_t i = 0; i < block.vtx.size(); ++i) {
2168 const auto &tx = block.vtx.at(i);
2169 outputs += tx->vout.size();
2170 Amount tx_total_out = Amount::zero();
2171 if (loop_outputs) {
2172 for (const CTxOut &out : tx->vout) {
2173 tx_total_out += out.nValue;
2174 utxo_size_inc +=
2177 }
2178 }
2179
2180 if (tx->IsCoinBase()) {
2181 continue;
2182 }
2183
2184 // Don't count coinbase's fake input
2185 inputs += tx->vin.size();
2186 // Don't count coinbase reward
2187 total_out += tx_total_out;
2188
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);
2194 }
2195 maxtxsize = std::max(maxtxsize, tx_size);
2196 mintxsize = std::min(mintxsize, tx_size);
2197 total_size += tx_size;
2198 }
2199
2200 if (loop_inputs) {
2201 Amount tx_total_in = Amount::zero();
2202 const auto &txundo = blockUndo.vtxundo.at(i - 1);
2203 for (const Coin &coin : txundo.vprevout) {
2204 const CTxOut &prevoutput = coin.GetTxOut();
2205
2206 tx_total_in += prevoutput.nValue;
2207 utxo_size_inc -=
2208 GetSerializeSize(prevoutput, PROTOCOL_VERSION) +
2210 }
2211
2212 Amount txfee = tx_total_in - tx_total_out;
2213 CHECK_NONFATAL(MoneyRange(txfee));
2214 if (do_medianfee) {
2215 fee_array.push_back(txfee);
2216 }
2217 maxfee = std::max(maxfee, txfee);
2218 minfee = std::min(minfee, txfee);
2219 totalfee += txfee;
2220
2221 Amount feerate = txfee / tx_size;
2222 if (do_medianfeerate) {
2223 feerate_array.push_back(feerate);
2224 }
2225 maxfeerate = std::max(maxfeerate, feerate);
2226 minfeerate = std::min(minfeerate, feerate);
2227 }
2228 }
2229
2230 UniValue ret_all(UniValue::VOBJ);
2231 ret_all.pushKV("avgfee",
2232 block.vtx.size() > 1
2233 ? (totalfee / int((block.vtx.size() - 1)))
2234 : Amount::zero());
2235 ret_all.pushKV("avgfeerate", total_size > 0
2236 ? (totalfee / total_size)
2237 : Amount::zero());
2238 ret_all.pushKV("avgtxsize",
2239 (block.vtx.size() > 1)
2240 ? total_size / (block.vtx.size() - 1)
2241 : 0);
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);
2248 ret_all.pushKV("medianfee", CalculateTruncatedMedian(fee_array));
2249 ret_all.pushKV("medianfeerate",
2250 CalculateTruncatedMedian(feerate_array));
2251 ret_all.pushKV("mediantime", pindex.GetMedianTimePast());
2252 ret_all.pushKV("mediantxsize",
2253 CalculateTruncatedMedian(txsize_array));
2254 ret_all.pushKV("minfee",
2255 minfee == MAX_MONEY ? Amount::zero() : minfee);
2256 ret_all.pushKV("minfeerate", minfeerate == MAX_MONEY
2257 ? Amount::zero()
2258 : minfeerate);
2259 ret_all.pushKV("mintxsize",
2260 mintxsize == blockMaxSize ? 0 : mintxsize);
2261 ret_all.pushKV("outs", outputs);
2262 ret_all.pushKV("subsidy", GetBlockSubsidy(pindex.nHeight,
2263 chainman.GetConsensus()));
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);
2271
2272 if (do_all) {
2273 return ret_all;
2274 }
2275
2277 for (const std::string &stat : stats) {
2278 const UniValue &value = ret_all[stat];
2279 if (value.isNull()) {
2280 throw JSONRPCError(
2282 strprintf("Invalid selected statistic %s", stat));
2283 }
2284 ret.pushKV(stat, value);
2285 }
2286 return ret;
2287 },
2288 };
2289}
2290
2291namespace {
2293static bool FindScriptPubKey(std::atomic<int> &scan_progress,
2294 const std::atomic<bool> &should_abort,
2295 int64_t &count, CCoinsViewCursor *cursor,
2296 const std::set<CScript> &needles,
2297 std::map<COutPoint, Coin> &out_results,
2298 std::function<void()> &interruption_point) {
2299 scan_progress = 0;
2300 count = 0;
2301 while (cursor->Valid()) {
2302 COutPoint key;
2303 Coin coin;
2304 if (!cursor->GetKey(key) || !cursor->GetValue(coin)) {
2305 return false;
2306 }
2307 if (++count % 8192 == 0) {
2308 interruption_point();
2309 if (should_abort) {
2310 // allow to abort the scan via the abort reference
2311 return false;
2312 }
2313 }
2314 if (count % 256 == 0) {
2315 // update progress reference every 256 item
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);
2319 }
2320 if (needles.count(coin.GetTxOut().scriptPubKey)) {
2321 out_results.emplace(key, coin);
2322 }
2323 cursor->Next();
2324 }
2325 scan_progress = 100;
2326 return true;
2327}
2328} // namespace
2329
2331static std::atomic<int> g_scan_progress;
2332static std::atomic<bool> g_scan_in_progress;
2333static std::atomic<bool> g_should_abort_scan;
2335private:
2336 bool m_could_reserve{false};
2337
2338public:
2339 explicit CoinsViewScanReserver() = default;
2340
2341 bool reserve() {
2343 if (g_scan_in_progress.exchange(true)) {
2344 return false;
2345 }
2346 m_could_reserve = true;
2347 return true;
2348 }
2349
2351 if (m_could_reserve) {
2352 g_scan_in_progress = false;
2353 }
2354 }
2355};
2356
2358 const auto &ticker = Currency::get().ticker;
2359 return RPCHelpMan{
2360 "scantxoutset",
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 "
2371 "pubkey\n"
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",
2383 {
2385 "The action to execute\n"
2386 " \"start\" for starting a "
2387 "scan\n"
2388 " \"abort\" for aborting the "
2389 "current scan (returns true when abort was successful)\n"
2390 " \"status\" for "
2391 "progress report (in %) of the current scan"},
2392 {"scanobjects",
2395 "Array of scan objects. Required for \"start\" action\n"
2396 " Every scan object is either a "
2397 "string descriptor or an object:",
2398 {
2400 "An output descriptor"},
2401 {
2402 "",
2405 "An object with output descriptor and metadata",
2406 {
2408 "An output descriptor"},
2409 {"range", RPCArg::Type::RANGE, RPCArg::Default{1000},
2410 "The range of HD chain indexes to explore (either "
2411 "end or [begin,end])"},
2412 },
2413 },
2414 },
2415 RPCArgOptions{.oneline_description = "[scanobjects,...]"}},
2416 },
2417 {
2418 RPCResult{"When action=='abort'", RPCResult::Type::BOOL, "", ""},
2419 RPCResult{"When action=='status' and no scan is in progress",
2420 RPCResult::Type::NONE, "", ""},
2421 RPCResult{
2422 "When action=='status' and scan is in progress",
2424 "",
2425 "",
2426 {
2427 {RPCResult::Type::NUM, "progress", "The scan progress"},
2428 }},
2429 RPCResult{
2430 "When action=='start'",
2432 "",
2433 "",
2434 {
2435 {RPCResult::Type::BOOL, "success",
2436 "Whether the scan was completed"},
2437 {RPCResult::Type::NUM, "txouts",
2438 "The number of unspent transaction outputs scanned"},
2439 {RPCResult::Type::NUM, "height",
2440 "The current block height (index)"},
2441 {RPCResult::Type::STR_HEX, "bestblock",
2442 "The hash of the block at the tip of the chain"},
2444 "unspents",
2445 "",
2446 {
2448 "",
2449 "",
2450 {
2451 {RPCResult::Type::STR_HEX, "txid",
2452 "The transaction id"},
2453 {RPCResult::Type::NUM, "vout", "The vout value"},
2454 {RPCResult::Type::STR_HEX, "scriptPubKey",
2455 "The script key"},
2456 {RPCResult::Type::STR, "desc",
2457 "A specialized descriptor for the matched "
2458 "scriptPubKey"},
2459 {RPCResult::Type::STR_AMOUNT, "amount",
2460 "The total amount in " + ticker +
2461 " of the unspent output"},
2462 {RPCResult::Type::BOOL, "coinbase",
2463 "Whether this is a coinbase output"},
2464 {RPCResult::Type::NUM, "height",
2465 "Height of the unspent transaction output"},
2466 }},
2467 }},
2468 {RPCResult::Type::STR_AMOUNT, "total_amount",
2469 "The total amount of all found unspent outputs in " +
2470 ticker},
2471 }},
2472 },
2473 RPCExamples{""},
2474 [&](const RPCHelpMan &self, const Config &config,
2475 const JSONRPCRequest &request) -> UniValue {
2476 UniValue result(UniValue::VOBJ);
2477 const auto action{self.Arg<std::string>("action")};
2478 if (action == "status") {
2479 CoinsViewScanReserver reserver;
2480 if (reserver.reserve()) {
2481 // no scan in progress
2482 return NullUniValue;
2483 }
2484 result.pushKV("progress", g_scan_progress.load());
2485 return result;
2486 } else if (action == "abort") {
2487 CoinsViewScanReserver reserver;
2488 if (reserver.reserve()) {
2489 // reserve was possible which means no scan was running
2490 return false;
2491 }
2492 // set the abort flag
2493 g_should_abort_scan = true;
2494 return true;
2495 } else if (action == "start") {
2496 CoinsViewScanReserver reserver;
2497 if (!reserver.reserve()) {
2499 "Scan already in progress, use action "
2500 "\"abort\" or \"status\"");
2501 }
2502
2503 if (request.params.size() < 2) {
2505 "scanobjects argument is required for "
2506 "the start action");
2507 }
2508
2509 std::set<CScript> needles;
2510 std::map<CScript, std::string> descriptors;
2511 Amount total_in = Amount::zero();
2512
2513 // loop through the scan objects
2514 for (const UniValue &scanobject :
2515 request.params[1].get_array().getValues()) {
2516 FlatSigningProvider provider;
2517 auto scripts =
2518 EvalDescriptorStringOrObject(scanobject, provider);
2519 for (CScript &script : scripts) {
2520 std::string inferred =
2521 InferDescriptor(script, provider)->ToString();
2522 needles.emplace(script);
2523 descriptors.emplace(std::move(script),
2524 std::move(inferred));
2525 }
2526 }
2527
2528 // Scan the unspent transaction output set for inputs
2529 UniValue unspents(UniValue::VARR);
2530 std::vector<CTxOut> input_txos;
2531 std::map<COutPoint, Coin> coins;
2532 g_should_abort_scan = false;
2533 g_scan_progress = 0;
2534 int64_t count = 0;
2535 std::unique_ptr<CCoinsViewCursor> pcursor;
2536 const CBlockIndex *tip;
2537 NodeContext &node = EnsureAnyNodeContext(request.context);
2538 {
2540 LOCK(cs_main);
2541 Chainstate &active_chainstate = chainman.ActiveChainstate();
2542 active_chainstate.ForceFlushStateToDisk();
2543 pcursor = CHECK_NONFATAL(std::unique_ptr<CCoinsViewCursor>(
2544 active_chainstate.CoinsDB().Cursor()));
2545 tip = CHECK_NONFATAL(active_chainstate.m_chain.Tip());
2546 }
2547 bool res = FindScriptPubKey(
2548 g_scan_progress, g_should_abort_scan, count, pcursor.get(),
2549 needles, coins, node.rpc_interruption_point);
2550 result.pushKV("success", res);
2551 result.pushKV("txouts", count);
2552 result.pushKV("height", tip->nHeight);
2553 result.pushKV("bestblock", tip->GetBlockHash().GetHex());
2554
2555 for (const auto &it : coins) {
2556 const COutPoint &outpoint = it.first;
2557 const Coin &coin = it.second;
2558 const CTxOut &txo = coin.GetTxOut();
2559 input_txos.push_back(txo);
2560 total_in += txo.nValue;
2561
2562 UniValue unspent(UniValue::VOBJ);
2563 unspent.pushKV("txid", outpoint.GetTxId().GetHex());
2564 unspent.pushKV("vout", int32_t(outpoint.GetN()));
2565 unspent.pushKV("scriptPubKey", HexStr(txo.scriptPubKey));
2566 unspent.pushKV("desc", descriptors[txo.scriptPubKey]);
2567 unspent.pushKV("amount", txo.nValue);
2568 unspent.pushKV("coinbase", coin.IsCoinBase());
2569 unspent.pushKV("height", int32_t(coin.GetHeight()));
2570
2571 unspents.push_back(unspent);
2572 }
2573 result.pushKV("unspents", unspents);
2574 result.pushKV("total_amount", total_in);
2575 } else {
2577 strprintf("Invalid action '%s'", action));
2578 }
2579 return result;
2580 },
2581 };
2582}
2583
2585 return RPCHelpMan{
2586 "getblockfilter",
2587 "Retrieve a BIP 157 content filter for a particular block.\n",
2588 {
2590 "The hash of the block"},
2591 {"filtertype", RPCArg::Type::STR, RPCArg::Default{"basic"},
2592 "The type name of the filter"},
2593 },
2595 "",
2596 "",
2597 {
2598 {RPCResult::Type::STR_HEX, "filter",
2599 "the hex-encoded filter data"},
2600 {RPCResult::Type::STR_HEX, "header",
2601 "the hex-encoded filter header"},
2602 }},
2604 HelpExampleCli("getblockfilter",
2605 "\"00000000c937983704a73af28acdec37b049d214a"
2606 "dbda81d7e2a3dd146f6ed09\" \"basic\"") +
2607 HelpExampleRpc("getblockfilter",
2608 "\"00000000c937983704a73af28acdec37b049d214adbda81d7"
2609 "e2a3dd146f6ed09\", \"basic\"")},
2610 [&](const RPCHelpMan &self, const Config &config,
2611 const JSONRPCRequest &request) -> UniValue {
2612 const BlockHash block_hash(
2613 ParseHashV(request.params[0], "blockhash"));
2614 std::string filtertype_name = "basic";
2615 if (!request.params[1].isNull()) {
2616 filtertype_name = request.params[1].get_str();
2617 }
2618
2619 BlockFilterType filtertype;
2620 if (!BlockFilterTypeByName(filtertype_name, filtertype)) {
2622 "Unknown filtertype");
2623 }
2624
2625 BlockFilterIndex *index = GetBlockFilterIndex(filtertype);
2626 if (!index) {
2628 "Index is not enabled for filtertype " +
2629 filtertype_name);
2630 }
2631
2632 const CBlockIndex *block_index;
2633 bool block_was_connected;
2634 {
2635 ChainstateManager &chainman =
2636 EnsureAnyChainman(request.context);
2637 LOCK(cs_main);
2638 block_index = chainman.m_blockman.LookupBlockIndex(block_hash);
2639 if (!block_index) {
2641 "Block not found");
2642 }
2643 block_was_connected =
2644 block_index->IsValid(BlockValidity::SCRIPTS);
2645 }
2646
2647 bool index_ready = index->BlockUntilSyncedToCurrentChain();
2648
2649 BlockFilter filter;
2650 uint256 filter_header;
2651 if (!index->LookupFilter(block_index, filter) ||
2652 !index->LookupFilterHeader(block_index, filter_header)) {
2653 int err_code;
2654 std::string errmsg = "Filter not found.";
2655
2656 if (!block_was_connected) {
2657 err_code = RPC_INVALID_ADDRESS_OR_KEY;
2658 errmsg += " Block was not connected to active chain.";
2659 } else if (!index_ready) {
2660 err_code = RPC_MISC_ERROR;
2661 errmsg += " Block filters are still in the process of "
2662 "being indexed.";
2663 } else {
2664 err_code = RPC_INTERNAL_ERROR;
2665 errmsg += " This error is unexpected and indicates index "
2666 "corruption.";
2667 }
2668
2669 throw JSONRPCError(err_code, errmsg);
2670 }
2671
2673 ret.pushKV("filter", HexStr(filter.GetEncodedFilter()));
2674 ret.pushKV("header", filter_header.GetHex());
2675 return ret;
2676 },
2677 };
2678}
2679
2686
2687public:
2688 NetworkDisable(CConnman &connman) : m_connman(connman) {
2692 "Network activity could not be suspended.");
2693 }
2694 };
2696};
2697
2706
2707public:
2710 const CBlockIndex &index)
2711 : m_chainman(chainman), m_avalanche(avalanche),
2712 m_invalidate_index(index) {
2715 };
2719 };
2720};
2721
2728 return RPCHelpMan{
2729 "dumptxoutset",
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 "
2735 "this process. "
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 "
2739 "blockstorage.\n\n"
2740 "This call may take several minutes. Make sure to use no RPC timeout "
2741 "(bitcoin-cli -rpcclienttimeout=0)",
2742
2743 {
2745 "path to the output file. If relative, will be prefixed by "
2746 "datadir."},
2747 {"type", RPCArg::Type::STR, RPCArg::Default(""),
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 "
2757 "loadtxoutset."},
2758 {
2759 "options",
2762 "",
2763 {
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 "
2769 "this case.",
2771 .type_str = {"", "string or numeric"}}},
2772 },
2773 },
2774 },
2776 "",
2777 "",
2778 {
2779 {RPCResult::Type::NUM, "coins_written",
2780 "the number of coins written in the snapshot"},
2781 {RPCResult::Type::STR_HEX, "base_hash",
2782 "the hash of the base of the snapshot"},
2783 {RPCResult::Type::NUM, "base_height",
2784 "the height of the base of the snapshot"},
2785 {RPCResult::Type::STR, "path",
2786 "the absolute path that the snapshot was written to"},
2787 {RPCResult::Type::STR_HEX, "txoutset_hash",
2788 "the hash of the UTXO set contents"},
2789 {RPCResult::Type::NUM, "nchaintx",
2790 "the number of transactions in the chain up to and "
2791 "including the base block"},
2792 }},
2793 RPCExamples{HelpExampleCli("-rpcclienttimeout=0 dumptxoutset",
2794 "utxo.dat latest") +
2795 HelpExampleCli("-rpcclienttimeout=0 dumptxoutset",
2796 "utxo.dat rollback") +
2797 HelpExampleCli("-rpcclienttimeout=0 -named dumptxoutset",
2798 R"(utxo.dat rollback=853456)")},
2799 [&](const RPCHelpMan &self, const Config &config,
2800 const JSONRPCRequest &request) -> UniValue {
2801 NodeContext &node = EnsureAnyNodeContext(request.context);
2802 const CBlockIndex *tip{WITH_LOCK(
2803 ::cs_main, return node.chainman->ActiveChain().Tip())};
2804 const CBlockIndex *target_index{nullptr};
2805 const std::string snapshot_type{self.Arg<std::string>("type")};
2806 const UniValue options{request.params[2].isNull()
2808 : request.params[2]};
2809 if (options.exists("rollback")) {
2810 if (!snapshot_type.empty() && snapshot_type != "rollback") {
2811 throw JSONRPCError(
2813 strprintf("Invalid snapshot type \"%s\" specified with "
2814 "rollback option",
2815 snapshot_type));
2816 }
2817 target_index =
2818 ParseHashOrHeight(options["rollback"], *node.chainman);
2819 } else if (snapshot_type == "rollback") {
2820 auto snapshot_heights =
2821 node.chainman->GetParams().GetAvailableSnapshotHeights();
2822 CHECK_NONFATAL(snapshot_heights.size() > 0);
2823 auto max_height = std::max_element(snapshot_heights.begin(),
2824 snapshot_heights.end());
2825 target_index = ParseHashOrHeight(*max_height, *node.chainman);
2826 } else if (snapshot_type == "latest") {
2827 target_index = tip;
2828 } else {
2829 throw JSONRPCError(
2831 strprintf("Invalid snapshot type \"%s\" specified. Please "
2832 "specify \"rollback\" or \"latest\"",
2833 snapshot_type));
2834 }
2835
2836 const ArgsManager &args{EnsureAnyArgsman(request.context)};
2837 const fs::path path = fsbridge::AbsPathJoin(
2838 args.GetDataDirNet(), fs::u8path(request.params[0].get_str()));
2839 // Write to a temporary path and then move into `path` on completion
2840 // to avoid confusion due to an interruption.
2841 const fs::path temppath = fsbridge::AbsPathJoin(
2842 args.GetDataDirNet(),
2843 fs::u8path(request.params[0].get_str() + ".incomplete"));
2844
2845 if (fs::exists(path)) {
2847 path.u8string() +
2848 " already exists. If you are sure this "
2849 "is what you want, "
2850 "move it out of the way first");
2851 }
2852
2853 FILE *file{fsbridge::fopen(temppath, "wb")};
2854 AutoFile afile{file};
2855
2856 CConnman &connman = EnsureConnman(node);
2857 const CBlockIndex *invalidate_index{nullptr};
2858 std::optional<NetworkDisable> disable_network;
2859 std::optional<TemporaryRollback> temporary_rollback;
2860
2861 // If the user wants to dump the txoutset of the current tip, we
2862 // don't have to roll back at all
2863 if (target_index != tip) {
2864 // If the node is running in pruned mode we ensure all necessary
2865 // block data is available before starting to roll back.
2866 if (node.chainman->m_blockman.IsPruneMode()) {
2867 LOCK(node.chainman->GetMutex());
2868 const CBlockIndex *current_tip{
2869 node.chainman->ActiveChain().Tip()};
2870 const CBlockIndex *first_block{
2871 node.chainman->m_blockman.GetFirstBlock(
2872 *current_tip,
2873 /*status_test=*/[](const BlockStatus &status) {
2874 return status.hasData() && status.hasUndo();
2875 })};
2876 if (first_block->nHeight > target_index->nHeight) {
2877 throw JSONRPCError(
2879 "Could not roll back to requested height since "
2880 "necessary block data is already pruned.");
2881 }
2882 }
2883
2884 // Suspend network activity for the duration of the process when
2885 // we are rolling back the chain to get a utxo set from a past
2886 // height. We do this so we don't punish peers that send us that
2887 // send us data that seems wrong in this temporary state. For
2888 // example a normal new block would be classified as a block
2889 // connecting an invalid block.
2890 // Skip if the network is already disabled because this
2891 // automatically re-enables the network activity at the end of
2892 // the process which may not be what the user wants.
2893 if (connman.GetNetworkActive()) {
2894 disable_network.emplace(connman);
2895 }
2896
2897 invalidate_index = WITH_LOCK(
2898 ::cs_main,
2899 return node.chainman->ActiveChain().Next(target_index));
2900 temporary_rollback.emplace(*node.chainman, node.avalanche.get(),
2901 *invalidate_index);
2902 }
2903
2904 Chainstate *chainstate;
2905 std::unique_ptr<CCoinsViewCursor> cursor;
2906 CCoinsStats stats;
2907 {
2908 // Lock the chainstate before calling PrepareUtxoSnapshot, to
2909 // be able to get a UTXO database cursor while the chain is
2910 // pointing at the target block. After that, release the lock
2911 // while calling WriteUTXOSnapshot. The cursor will remain
2912 // valid and be used by WriteUTXOSnapshot to write a consistent
2913 // snapshot even if the chainstate changes.
2914 LOCK(node.chainman->GetMutex());
2915 chainstate = &node.chainman->ActiveChainstate();
2916
2917 // In case there is any issue with a block being read from disk
2918 // we need to stop here, otherwise the dump could still be
2919 // created for the wrong height. The new tip could also not be
2920 // the target block if we have a stale sister block of
2921 // invalidate_index. This block (or a descendant) would be
2922 // activated as the new tip and we would not get to
2923 // new_tip_index.
2924 if (target_index != chainstate->m_chain.Tip()) {
2926 "Failed to roll back to requested height, "
2927 "reverting to tip.\n");
2928 throw JSONRPCError(
2930 "Could not roll back to requested height.");
2931 } else {
2932 std::tie(cursor, stats, tip) = PrepareUTXOSnapshot(
2933 *chainstate, node.rpc_interruption_point);
2934 }
2935 }
2936
2937 UniValue result =
2938 WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile,
2939 path, temppath, node.rpc_interruption_point);
2940 fs::rename(temppath, path);
2941
2942 return result;
2943 },
2944 };
2945}
2946
2947std::tuple<std::unique_ptr<CCoinsViewCursor>, CCoinsStats, const CBlockIndex *>
2949 const std::function<void()> &interruption_point) {
2950 std::unique_ptr<CCoinsViewCursor> pcursor;
2951 std::optional<CCoinsStats> maybe_stats;
2952 const CBlockIndex *tip;
2953
2954 {
2955 // We need to lock cs_main to ensure that the coinsdb isn't
2956 // written to between (i) flushing coins cache to disk
2957 // (coinsdb), (ii) getting stats based upon the coinsdb, and
2958 // (iii) constructing a cursor to the coinsdb for use in
2959 // WriteUTXOSnapshot.
2960 //
2961 // Cursors returned by leveldb iterate over snapshots, so the
2962 // contents of the pcursor will not be affected by simultaneous
2963 // writes during use below this block.
2964 //
2965 // See discussion here:
2966 // https://github.com/bitcoin/bitcoin/pull/15606#discussion_r274479369
2967 //
2969
2970 chainstate.ForceFlushStateToDisk();
2971
2972 maybe_stats = GetUTXOStats(&chainstate.CoinsDB(), chainstate.m_blockman,
2973 CoinStatsHashType::HASH_SERIALIZED,
2974 interruption_point);
2975 if (!maybe_stats) {
2976 throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to read UTXO set");
2977 }
2978
2979 pcursor =
2980 std::unique_ptr<CCoinsViewCursor>(chainstate.CoinsDB().Cursor());
2981 tip = CHECK_NONFATAL(
2982 chainstate.m_blockman.LookupBlockIndex(maybe_stats->hashBlock));
2983 }
2984
2985 return {std::move(pcursor), *CHECK_NONFATAL(maybe_stats), tip};
2986}
2987
2989 CCoinsStats *maybe_stats, const CBlockIndex *tip,
2990 AutoFile &afile, const fs::path &path,
2991 const fs::path &temppath,
2992 const std::function<void()> &interruption_point) {
2994 strprintf("writing UTXO snapshot at height %s (%s) to file %s (via %s)",
2995 tip->nHeight, tip->GetBlockHash().ToString(),
2996 fs::PathToString(path), fs::PathToString(temppath)));
2997
2998 SnapshotMetadata metadata{tip->GetBlockHash(), maybe_stats->coins_count};
2999
3000 afile << metadata;
3001
3002 COutPoint key;
3003 TxId last_txid;
3004 Coin coin;
3005 unsigned int iter{0};
3006 size_t written_coins_count{0};
3007 std::vector<std::pair<uint32_t, Coin>> coins;
3008
3009 // To reduce space the serialization format of the snapshot avoids
3010 // duplication of tx hashes. The code takes advantage of the guarantee by
3011 // leveldb that keys are lexicographically sorted.
3012 // In the coins vector we collect all coins that belong to a certain tx hash
3013 // (key.hash) and when we have them all (key.hash != last_hash) we write
3014 // them to file using the below lambda function.
3015 // See also https://github.com/bitcoin/bitcoin/issues/25675
3016 auto write_coins_to_file =
3017 [&](AutoFile &afile, const TxId &last_txid,
3018 const std::vector<std::pair<uint32_t, Coin>> &coins,
3019 size_t &written_coins_count) {
3020 afile << last_txid;
3021 WriteCompactSize(afile, coins.size());
3022 for (const auto &[n, coin_] : coins) {
3023 WriteCompactSize(afile, n);
3024 afile << coin_;
3025 ++written_coins_count;
3026 }
3027 };
3028
3029 pcursor->GetKey(key);
3030 last_txid = key.GetTxId();
3031 while (pcursor->Valid()) {
3032 if (iter % 5000 == 0) {
3033 interruption_point();
3034 }
3035 ++iter;
3036 if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
3037 if (key.GetTxId() != last_txid) {
3038 write_coins_to_file(afile, last_txid, coins,
3039 written_coins_count);
3040 last_txid = key.GetTxId();
3041 coins.clear();
3042 }
3043 coins.emplace_back(key.GetN(), coin);
3044 }
3045 pcursor->Next();
3046 }
3047
3048 if (!coins.empty()) {
3049 write_coins_to_file(afile, last_txid, coins, written_coins_count);
3050 }
3051
3052 CHECK_NONFATAL(written_coins_count == maybe_stats->coins_count);
3053
3054 afile.fclose();
3055
3056 UniValue result(UniValue::VOBJ);
3057 result.pushKV("coins_written", written_coins_count);
3058 result.pushKV("base_hash", tip->GetBlockHash().ToString());
3059 result.pushKV("base_height", tip->nHeight);
3060 result.pushKV("path", path.u8string());
3061 result.pushKV("txoutset_hash", maybe_stats->hashSerialized.ToString());
3062 result.pushKV("nchaintx", tip->nChainTx);
3063 return result;
3064}
3065
3067 AutoFile &afile, const fs::path &path,
3068 const fs::path &tmppath) {
3069 auto [cursor, stats, tip]{WITH_LOCK(
3070 ::cs_main,
3071 return PrepareUTXOSnapshot(chainstate, node.rpc_interruption_point))};
3072 return WriteUTXOSnapshot(chainstate, cursor.get(), &stats, tip, afile, path,
3073 tmppath, node.rpc_interruption_point);
3074}
3075
3077 return RPCHelpMan{
3078 "loadtxoutset",
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 "
3082 "network's tip. "
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 "
3090 "hash.\n\n"
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).",
3097 {
3099 "path to the snapshot file. If relative, will be prefixed by "
3100 "datadir."},
3101 },
3103 "",
3104 "",
3105 {
3106 {RPCResult::Type::NUM, "coins_loaded",
3107 "the number of coins loaded from the snapshot"},
3108 {RPCResult::Type::STR_HEX, "tip_hash",
3109 "the hash of the base of the snapshot"},
3110 {RPCResult::Type::NUM, "base_height",
3111 "the height of the base of the snapshot"},
3112 {RPCResult::Type::STR, "path",
3113 "the absolute path that the snapshot was loaded from"},
3114 }},
3116 HelpExampleCli("loadtxoutset -rpcclienttimeout=0", "utxo.dat")},
3117 [&](const RPCHelpMan &self, const Config &config,
3118 const JSONRPCRequest &request) -> UniValue {
3119 NodeContext &node = EnsureAnyNodeContext(request.context);
3122 const fs::path path{AbsPathForConfigVal(
3123 args, fs::u8path(self.Arg<std::string>("path")))};
3124
3125 if (args.GetBoolArg("-chronik", false)) {
3126 throw JSONRPCError(
3128 "loadtxoutset is not compatible with Chronik.");
3129 }
3130
3131 FILE *file{fsbridge::fopen(path, "rb")};
3132 AutoFile afile{file};
3133 if (afile.IsNull()) {
3135 "Couldn't open file " + path.u8string() +
3136 " for reading.");
3137 }
3138
3139 SnapshotMetadata metadata;
3140 try {
3141 afile >> metadata;
3142 } catch (const std::ios_base::failure &e) {
3143 throw JSONRPCError(
3145 strprintf("Unable to parse metadata: %s", e.what()));
3146 }
3147
3148 auto activation_result{
3149 chainman.ActivateSnapshot(afile, metadata, false)};
3150 if (!activation_result) {
3151 throw JSONRPCError(
3153 strprintf("Unable to load UTXO snapshot: %s. (%s)",
3154 util::ErrorString(activation_result).original,
3155 path.u8string()));
3156 }
3157
3158 CBlockIndex &snapshot_index{*CHECK_NONFATAL(*activation_result)};
3159
3160 // Because we can't provide historical blocks during tip or
3161 // background sync. Update local services to reflect we are a
3162 // limited peer until we are fully sync.
3163 node.connman->RemoveLocalServices(NODE_NETWORK);
3164 // Setting the limited state is usually redundant because the node
3165 // can always provide the last 288 blocks, but it doesn't hurt to
3166 // set it.
3167 node.connman->AddLocalServices(NODE_NETWORK_LIMITED);
3168
3169 UniValue result(UniValue::VOBJ);
3170 result.pushKV("coins_loaded", metadata.m_coins_count);
3171 result.pushKV("tip_hash", snapshot_index.GetBlockHash().ToString());
3172 result.pushKV("base_height", snapshot_index.nHeight);
3173 result.pushKV("path", fs::PathToString(path));
3174 return result;
3175 },
3176 };
3177}
3178
3179const std::vector<RPCResult> RPCHelpForChainstate{
3180 {RPCResult::Type::NUM, "blocks", "number of blocks in this chainstate"},
3181 {RPCResult::Type::STR_HEX, "bestblockhash", "blockhash of the tip"},
3182 {RPCResult::Type::NUM, "difficulty", "difficulty of the tip"},
3183 {RPCResult::Type::NUM, "verificationprogress",
3184 "progress towards the network tip"},
3185 {RPCResult::Type::STR_HEX, "snapshot_blockhash", /*optional=*/true,
3186 "the base block of the snapshot this chainstate is based on, if any"},
3187 {RPCResult::Type::NUM, "coins_db_cache_bytes", "size of the coinsdb cache"},
3188 {RPCResult::Type::NUM, "coins_tip_cache_bytes",
3189 "size of the coinstip cache"},
3190 {RPCResult::Type::BOOL, "validated",
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."},
3194
3195};
3196
3198 return RPCHelpMan{
3199 "getchainstates",
3200 "\nReturn information about chainstates.\n",
3201 {},
3203 "",
3204 "",
3205 {
3206 {RPCResult::Type::NUM, "headers",
3207 "the number of headers seen so far"},
3209 "chainstates",
3210 "list of the chainstates ordered by work, with the "
3211 "most-work (active) chainstate last",
3212 {
3214 }},
3215 }},
3216 RPCExamples{HelpExampleCli("getchainstates", "") +
3217 HelpExampleRpc("getchainstates", "")},
3218 [&](const RPCHelpMan &self, const Config &config,
3219 const JSONRPCRequest &request) -> UniValue {
3220 LOCK(cs_main);
3222
3223 ChainstateManager &chainman = EnsureAnyChainman(request.context);
3224
3225 auto make_chain_data =
3226 [&](const Chainstate &chainstate,
3227 bool validated) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
3230 if (!chainstate.m_chain.Tip()) {
3231 return data;
3232 }
3233 const CChain &chain = chainstate.m_chain;
3234 const CBlockIndex *tip = chain.Tip();
3235
3236 data.pushKV("blocks", chain.Height());
3237 data.pushKV("bestblockhash", tip->GetBlockHash().GetHex());
3238 data.pushKV("difficulty", GetDifficulty(*tip));
3239 data.pushKV(
3240 "verificationprogress",
3241 GuessVerificationProgress(Params().TxData(), tip));
3242 data.pushKV("coins_db_cache_bytes",
3243 chainstate.m_coinsdb_cache_size_bytes);
3244 data.pushKV("coins_tip_cache_bytes",
3245 chainstate.m_coinstip_cache_size_bytes);
3246 if (chainstate.m_from_snapshot_blockhash) {
3247 data.pushKV(
3248 "snapshot_blockhash",
3249 chainstate.m_from_snapshot_blockhash->ToString());
3250 }
3251 data.pushKV("validated", validated);
3252 return data;
3253 };
3254
3255 obj.pushKV("headers", chainman.m_best_header
3256 ? chainman.m_best_header->nHeight
3257 : -1);
3258
3259 const auto &chainstates = chainman.GetAll();
3260 UniValue obj_chainstates{UniValue::VARR};
3261 for (Chainstate *cs : chainstates) {
3262 obj_chainstates.push_back(
3263 make_chain_data(*cs, !cs->m_from_snapshot_blockhash ||
3264 chainstates.size() == 1));
3265 }
3266 obj.pushKV("chainstates", std::move(obj_chainstates));
3267 return obj;
3268 }};
3269}
3270
3272 // clang-format off
3273 static const CRPCCommand commands[] = {
3274 // category actor (function)
3275 // ------------------ ----------------------
3276 { "blockchain", getbestblockhash, },
3277 { "blockchain", getblock, },
3278 { "blockchain", getblockfrompeer, },
3279 { "blockchain", getblockchaininfo, },
3280 { "blockchain", getblockcount, },
3281 { "blockchain", getblockhash, },
3282 { "blockchain", getblockheader, },
3283 { "blockchain", getblockstats, },
3284 { "blockchain", getchaintips, },
3285 { "blockchain", getchaintxstats, },
3286 { "blockchain", getdifficulty, },
3287 { "blockchain", gettxout, },
3288 { "blockchain", gettxoutsetinfo, },
3289 { "blockchain", pruneblockchain, },
3290 { "blockchain", verifychain, },
3291 { "blockchain", preciousblock, },
3292 { "blockchain", scantxoutset, },
3293 { "blockchain", getblockfilter, },
3294 { "blockchain", dumptxoutset, },
3295 { "blockchain", loadtxoutset, },
3296 { "blockchain", getchainstates, },
3297
3298 /* Not shown in help */
3299 { "hidden", invalidateblock, },
3300 { "hidden", parkblock, },
3301 { "hidden", reconsiderblock, },
3302 { "hidden", syncwithvalidationinterfacequeue, },
3303 { "hidden", unparkblock, },
3304 { "hidden", waitfornewblock, },
3305 { "hidden", waitforblock, },
3306 { "hidden", waitforblockheight, },
3307 };
3308 // clang-format on
3309 for (const auto &c : commands) {
3310 t.appendCommand(c.name, &c);
3311 }
3312}
bool MoneyRange(const Amount nValue)
Definition: amount.h:166
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:165
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...
Definition: configfile.cpp:236
RPCHelpMan gettxout()
static RPCHelpMan getblock()
Definition: blockchain.cpp:700
static RPCHelpMan getdifficulty()
Definition: blockchain.cpp:460
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)
Definition: blockchain.cpp:103
static RPCHelpMan syncwithvalidationinterfacequeue()
Definition: blockchain.cpp:443
static CBlockUndo GetUndoChecked(BlockManager &blockman, const CBlockIndex &blockindex)
Definition: blockchain.cpp:681
static RPCHelpMan getchaintips()
static RPCHelpMan loadtxoutset()
static RPCHelpMan gettxoutsetinfo()
Definition: blockchain.cpp:977
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)
Definition: blockchain.cpp:963
static RPCHelpMan preciousblock()
static constexpr size_t PER_UTXO_OVERHEAD
double GetDifficulty(const CBlockIndex &blockindex)
Calculate the difficulty for a given block index.
Definition: blockchain.cpp:87
static RPCHelpMan scantxoutset()
static std::condition_variable cond_blockchange
Definition: blockchain.cpp:69
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)
Definition: blockchain.cpp:659
std::optional< int > GetPruneHeight(const BlockManager &blockman, const CChain &chain)
Definition: blockchain.cpp:851
static void ReconsiderBlock(ChainstateManager &chainman, avalanche::Processor *const avalanche, const BlockHash &block_hash)
static RPCHelpMan getblockfilter()
static RPCHelpMan getbestblockhash()
Definition: blockchain.cpp:237
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()
Definition: blockchain.cpp:321
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()
Definition: blockchain.cpp:480
static RPCHelpMan getblockhash()
Definition: blockchain.cpp:534
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.
Definition: blockchain.cpp:146
RPCHelpMan unparkblock()
static RPCHelpMan waitforblockheight()
Definition: blockchain.cpp:383
static const CBlockIndex * ParseHashOrHeight(const UniValue &param, ChainstateManager &chainman)
Definition: blockchain.cpp:114
UniValue blockToJSON(BlockManager &blockman, const CBlock &block, const CBlockIndex &tip, const CBlockIndex &blockindex, TxVerbosity verbosity)
Block description to JSON.
Definition: blockchain.cpp:180
static RPCHelpMan pruneblockchain()
Definition: blockchain.cpp:889
static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange)
static RPCHelpMan getblockheader()
Definition: blockchain.cpp:563
RPCHelpMan parkblock()
static GlobalMutex cs_blockchange
Definition: blockchain.cpp:68
static RPCHelpMan dumptxoutset()
Serialize the UTXO set to a file for loading elsewhere.
static RPCHelpMan getblockcount()
Definition: blockchain.cpp:219
static RPCHelpMan waitfornewblock()
Definition: blockchain.cpp:264
void RPCNotifyBlockChange(const CBlockIndex *pindex)
Callback for when block tip changed.
Definition: blockchain.cpp:255
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
BlockFilterType
Definition: blockfilter.h:88
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.
Definition: chain.cpp:112
static constexpr int64_t TIMESTAMP_WINDOW
Timestamp window used as a grace period by code that compares external timestamps (such as timestamps...
Definition: chain.h:36
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
#define CHECK_NONFATAL(condition)
Identity function.
Definition: check.h:53
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:525
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:526
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:568
int fclose()
Definition: streams.h:541
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:111
const std::vector< uint8_t > & GetEncodedFilter() const
Definition: blockfilter.h:134
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.
BlockHash GetHash() const
Definition: block.cpp:11
Definition: block.h:60
std::vector< CTransactionRef > vtx
Definition: block.h:63
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
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.
Definition: blockindex.h:191
uint256 hashMerkleRoot
Definition: blockindex.h:75
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
CBlockHeader GetBlockHeader() const
Definition: blockindex.h:117
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:51
uint32_t nTime
Definition: blockindex.h:76
uint32_t nNonce
Definition: blockindex.h:78
int64_t GetBlockTime() const
Definition: blockindex.h:160
int64_t GetMedianTimePast() const
Definition: blockindex.h:172
uint32_t nBits
Definition: blockindex.h:77
unsigned int nTx
Number of transactions in this block.
Definition: blockindex.h:55
int32_t nVersion
block header
Definition: blockindex.h:74
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:62
BlockHash GetBlockHash() const
Definition: blockindex.h:130
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Definition: blockindex.h:68
Undo information for a CBlock.
Definition: undo.h:73
std::vector< CTxUndo > vtxundo
Definition: undo.h:76
An in-memory indexed chain of blocks.
Definition: chain.h:134
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:150
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...
Definition: chain.cpp:62
int Height() const
Return the maximal height in the chain.
Definition: chain.h:186
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:49
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:166
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
std::string GetChainTypeString() const
Return the chain type string.
Definition: chainparams.h:134
const CBlock & GenesisBlock() const
Definition: chainparams.h:112
const ChainTxData & TxData() const
Definition: chainparams.h:158
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:363
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:214
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:91
Cursor for iterating over CoinsView state.
Definition: coins.h:222
virtual void Next()=0
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.
Definition: txdb.cpp:214
Abstract view on the open txout dataset.
Definition: coins.h:305
virtual BlockHash GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:16
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:647
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
GetCoin, returning whether it exists and is not spent.
Definition: txmempool.cpp:779
Definition: net.h:824
bool GetNetworkActive() const
Definition: net.h:916
void SetNetworkActive(bool active)
Definition: net.cpp:2352
RPC command dispatcher.
Definition: server.h:194
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:328
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:221
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:317
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:137
An output of a transaction.
Definition: transaction.h:128
CScript scriptPubKey
Definition: transaction.h:131
Amount nValue
Definition: transaction.h:130
Restore the UTXO in a Coin at a given COutPoint.
Definition: undo.h:62
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
Definition: validation.h:655
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.
Definition: validation.h:734
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.
Definition: validation.h:841
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.
Definition: validation.h:833
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:893
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:860
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
Definition: validation.h:890
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(
Definition: validation.h:867
bool AvalancheFinalizeBlock(CBlockIndex *pindex, avalanche::Processor &avalanche) EXCLUSIVE_LOCKS_REQUIRED(voi ClearAvalancheFinalizedBlock)() EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Mark a block as finalized by avalanche.
Definition: validation.h:996
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition: validation.h:791
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...
Definition: validation.h:1186
node::BlockMap & BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:1463
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...
Definition: validation.h:1437
kernel::Notifications & GetNotifications() const
Definition: validation.h:1294
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.
Definition: validation.h:1318
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1444
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1441
const CChainParams & GetParams() const
Definition: validation.h:1279
const Consensus::Params & GetConsensus() const
Definition: validation.h:1282
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())
Definition: validation.h:1438
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1403
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1327
A UTXO entry.
Definition: coins.h:29
uint32_t GetHeight() const
Definition: coins.h:46
bool IsCoinBase() const
Definition: coins.h:47
CTxOut & GetTxOut()
Definition: coins.h:50
CoinsViewScanReserver()=default
Definition: config.h:19
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:173
Different type to mark Mutex at global scope.
Definition: sync.h:144
RAII class that disables the network in its constructor and enables it in its destructor.
NetworkDisable(CConnman &connman)
CConnman & m_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.
Definition: util.h:410
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)
Definition: univalue.cpp:96
const std::string & get_str() const
@ VOBJ
Definition: univalue.h:31
@ VARR
Definition: univalue.h:32
bool isNull() const
Definition: univalue.h:104
size_t size() const
Definition: univalue.h:92
const std::vector< UniValue > & getValues() const
Int getInt() const
Definition: univalue.h:157
const UniValue & get_array() const
bool isNum() const
Definition: univalue.h:109
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:115
bool IsValid() const
Definition: validation.h:119
std::string GetRejectReason() const
Definition: validation.h:123
std::string ToString() const
Definition: validation.h:125
uint8_t * begin()
Definition: uint256.h:85
std::string ToString() const
Definition: uint256.h:80
std::string GetHex() const
Definition: uint256.cpp:16
std::string GetHex() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
std::string u8string() const
Definition: fs.h:72
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:107
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.
Definition: blockstorage.h:362
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
bool IsPruneMode() const
Whether running in -prune mode.
Definition: blockstorage.h:359
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:30
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:41
256-bit opaque blob.
Definition: uint256.h:129
std::unique_ptr< CoinStatsIndex > g_coin_stats_index
The global UTXO set hash object.
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
Definition: core_write.cpp:194
TxVerbosity
Verbose level for block's transaction.
Definition: core_io.h:29
@ 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={})
Definition: core_write.cpp:221
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
int64_t NodeId
Definition: eviction.h:16
#define LogPrintLevel(category, level,...)
Definition: logging.h:437
#define LogPrint(category,...)
Definition: logging.h:452
unsigned int nHeight
static void pool cs
@ RPC
Definition: logging.h:76
@ NONE
Definition: logging.h:68
static path u8path(const std::string &utf8_str)
Definition: fs.h:90
static bool exists(const path &p)
Definition: fs.h:107
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:147
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:39
CoinStatsHashType
Definition: coinstats.h:23
Definition: init.h:31
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.
Definition: coinstats.cpp:16
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:90
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
@ NODE_NETWORK_LIMITED
Definition: protocol.h:366
@ NODE_NETWORK
Definition: protocol.h:343
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:58
@ RPC_MISC_ERROR
General application defined errors std::exception thrown in command handling.
Definition: protocol.h:38
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
Definition: protocol.h:46
@ RPC_INTERNAL_ERROR
Definition: protocol.h:33
@ RPC_DATABASE_ERROR
Database error.
Definition: protocol.h:48
@ RPC_DESERIALIZATION_ERROR
Error parsing or validating structure in raw format.
Definition: protocol.h:50
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition: protocol.h:42
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:153
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:170
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 ...
Definition: util.cpp:1362
const std::string UNIX_EPOCH_TIME
String used to describe UNIX epoch time in documentation, factored out to a constant for consistency.
Definition: util.cpp:25
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded values (throws error if not hex).
Definition: util.cpp:76
@ SER_NETWORK
Definition: serialize.h:154
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1207
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1203
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:379
ChainstateManager & EnsureAnyChainman(const std::any &context)
Definition: server_util.cpp:59
NodeContext & EnsureAnyNodeContext(const std::any &context)
Definition: server_util.cpp:21
CTxMemPool & EnsureMemPool(const NodeContext &node)
Definition: server_util.cpp:29
PeerManager & EnsurePeerman(const NodeContext &node)
Definition: server_util.cpp:72
ChainstateManager & EnsureChainman(const NodeContext &node)
Definition: server_util.cpp:52
ArgsManager & EnsureArgsman(const NodeContext &node)
Definition: server_util.cpp:41
CConnman & EnsureConnman(const NodeContext &node)
Definition: server_util.cpp:63
ArgsManager & EnsureAnyArgsman(const std::any &context)
Definition: server_util.cpp:48
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
Definition: string.h:90
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
bool hasUndo() const
Definition: blockstatus.h:65
bool hasData() const
Definition: blockstatus.h:59
BlockHash hash
Definition: blockchain.cpp:64
Comparison function for sorting the getchaintips heads.
bool operator()(const CBlockIndex *a, const CBlockIndex *b) const
static const Currency & get()
Definition: amount.cpp:18
std::string ticker
Definition: amount.h:150
@ 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.
Definition: util.h:206
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
@ NO
Required arg.
UniValue Default
Default constant value.
Definition: util.h:208
std::string oneline_description
Should be empty unless it is supposed to override the auto-generated summary line.
Definition: util.h:143
bool skip_type_check
Definition: util.h:140
@ 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.
Definition: txid.h:14
uint64_t nDiskSize
Definition: coinstats.h:36
Amount total_unspendables_scripts
Total cumulative amount of outputs sent to unspendable scripts (OP_RETURN for example) up to and incl...
Definition: coinstats.h:67
Amount total_coinbase_amount
Total cumulative amount of coinbase outputs up to and including this block.
Definition: coinstats.h:60
Amount total_unspendables_genesis_block
The unspendable coinbase amount from the genesis block.
Definition: coinstats.h:62
uint64_t coins_count
The number of coins contained.
Definition: coinstats.h:40
uint64_t nTransactions
Definition: coinstats.h:32
uint64_t nTransactionOutputs
Definition: coinstats.h:33
uint64_t nBogoSize
Definition: coinstats.h:34
bool index_used
Signals if the coinstatsindex was used to retrieve the statistics.
Definition: coinstats.h:43
Amount total_unspendables_bip30
The two unspendable coinbase outputs total amount caused by BIP30.
Definition: coinstats.h:64
Amount total_prevout_spent_amount
Total cumulative amount of prevouts spent up to and including this block.
Definition: coinstats.h:54
BlockHash hashBlock
Definition: coinstats.h:31
Amount total_unspendables_unclaimed_rewards
Total cumulative amount of coins lost due to unclaimed miner rewards up to and including this block.
Definition: coinstats.h:70
uint256 hashSerialized
Definition: coinstats.h:35
Amount total_new_outputs_ex_coinbase_amount
Total cumulative amount of outputs created up to and including this block.
Definition: coinstats.h:57
Amount total_unspendable_amount
Total cumulative amount of unspendable coins up to and including this block.
Definition: coinstats.h:52
NodeContext struct containing references to chain state and connection state.
Definition: context.h:48
#define WAIT_LOCK(cs, name)
Definition: sync.h:317
#define AssertLockNotHeld(cs)
Definition: sync.h:163
#define LOCK(cs)
Definition: sync.h:306
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:357
static int count
Definition: tests.c:31
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
#define LOG_TIME_SECONDS(end_msg)
Definition: timer.h:103
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coins to signify they are only in the memory pool(since 0....
Definition: txmempool.h:55
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:143
const UniValue NullUniValue
Definition: univalue.cpp:16
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'.
Definition: validation.cpp:101
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
AssertLockHeld(pool.cs)
static constexpr int DEFAULT_CHECKLEVEL
Definition: validation.h:102
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...
Definition: validation.h:100
static const signed int DEFAULT_CHECKBLOCKS
Definition: validation.h:101
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
Definition: version.h:11
bilingual_str GetWarnings(bool verbose)
Format a string that describes several potential problems detected by the core.
Definition: warnings.cpp:41