Bitcoin ABC 0.32.12
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
63
66 int height;
67};
68
70static std::condition_variable cond_blockchange;
72
73std::tuple<std::unique_ptr<CCoinsViewCursor>, CCoinsStats, const CBlockIndex *>
75 const std::function<void()> &interruption_point = {})
77
80 CCoinsStats *maybe_stats, const CBlockIndex *tip,
81 AutoFile &afile, const fs::path &path,
82 const fs::path &temppath,
83 const std::function<void()> &interruption_point = {});
84
88double GetDifficulty(const CBlockIndex &blockindex) {
89 int nShift = (blockindex.nBits >> 24) & 0xff;
90 double dDiff = double(0x0000ffff) / double(blockindex.nBits & 0x00ffffff);
91
92 while (nShift < 29) {
93 dDiff *= 256.0;
94 nShift++;
95 }
96 while (nShift > 29) {
97 dDiff /= 256.0;
98 nShift--;
99 }
100
101 return dDiff;
102}
103
105 const CBlockIndex &blockindex,
106 const CBlockIndex *&next) {
107 next = tip.GetAncestor(blockindex.nHeight + 1);
108 if (next && next->pprev == &blockindex) {
109 return tip.nHeight - blockindex.nHeight + 1;
110 }
111 next = nullptr;
112 return &blockindex == &tip ? 1 : -1;
113}
114
115static const CBlockIndex *ParseHashOrHeight(const UniValue &param,
116 ChainstateManager &chainman) {
118 CChain &active_chain = chainman.ActiveChain();
119
120 if (param.isNum()) {
121 const int height{param.getInt<int>()};
122 if (height < 0) {
123 throw JSONRPCError(
125 strprintf("Target block height %d is negative", height));
126 }
127 const int current_tip{active_chain.Height()};
128 if (height > current_tip) {
129 throw JSONRPCError(
131 strprintf("Target block height %d after current tip %d", height,
132 current_tip));
133 }
134
135 return active_chain[height];
136 } else {
137 const BlockHash hash{ParseHashV(param, "hash_or_height")};
138 const CBlockIndex *pindex = chainman.m_blockman.LookupBlockIndex(hash);
139
140 if (!pindex) {
141 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
142 }
143
144 return pindex;
145 }
146}
148 const CBlockIndex &blockindex) {
149 // Serialize passed information without accessing chain state of the active
150 // chain!
151 // For performance reasons
153
154 UniValue result(UniValue::VOBJ);
155 result.pushKV("hash", blockindex.GetBlockHash().GetHex());
156 const CBlockIndex *pnext;
157 int confirmations = ComputeNextBlockAndDepth(tip, blockindex, pnext);
158 result.pushKV("confirmations", confirmations);
159 result.pushKV("height", blockindex.nHeight);
160 result.pushKV("version", blockindex.nVersion);
161 result.pushKV("versionHex", strprintf("%08x", blockindex.nVersion));
162 result.pushKV("merkleroot", blockindex.hashMerkleRoot.GetHex());
163 result.pushKV("time", blockindex.nTime);
164 result.pushKV("mediantime", blockindex.GetMedianTimePast());
165 result.pushKV("nonce", blockindex.nNonce);
166 result.pushKV("bits", strprintf("%08x", blockindex.nBits));
167 result.pushKV("difficulty", GetDifficulty(blockindex));
168 result.pushKV("chainwork", blockindex.nChainWork.GetHex());
169 result.pushKV("nTx", blockindex.nTx);
170
171 if (blockindex.pprev) {
172 result.pushKV("previousblockhash",
173 blockindex.pprev->GetBlockHash().GetHex());
174 }
175 if (pnext) {
176 result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex());
177 }
178 return result;
179}
180
181UniValue blockToJSON(BlockManager &blockman, const CBlock &block,
182 const CBlockIndex &tip, const CBlockIndex &blockindex,
183 TxVerbosity verbosity) {
184 UniValue result = blockheaderToJSON(tip, blockindex);
185
186 result.pushKV("size", (int)::GetSerializeSize(block));
188 switch (verbosity) {
190 for (const CTransactionRef &tx : block.vtx) {
191 txs.push_back(tx->GetId().GetHex());
192 }
193 break;
194
197 CBlockUndo blockUndo;
198 const bool is_not_pruned{WITH_LOCK(
199 ::cs_main, return !blockman.IsBlockPruned(blockindex))};
200 const bool have_undo{is_not_pruned &&
201 blockman.ReadBlockUndo(blockUndo, blockindex)};
202 for (size_t i = 0; i < block.vtx.size(); ++i) {
203 const CTransactionRef &tx = block.vtx.at(i);
204 // coinbase transaction (i == 0) doesn't have undo data
205 const CTxUndo *txundo = (have_undo && i > 0)
206 ? &blockUndo.vtxundo.at(i - 1)
207 : nullptr;
209 TxToUniv(*tx, BlockHash(), objTx, true, txundo, verbosity);
210 txs.push_back(std::move(objTx));
211 }
212 break;
213 }
214
215 result.pushKV("tx", std::move(txs));
216
217 return result;
218}
219
221 return RPCHelpMan{
222 "getblockcount",
223 "Returns the height of the most-work fully-validated chain.\n"
224 "The genesis block has height 0.\n",
225 {},
226 RPCResult{RPCResult::Type::NUM, "", "The current block count"},
227 RPCExamples{HelpExampleCli("getblockcount", "") +
228 HelpExampleRpc("getblockcount", "")},
229 [&](const RPCHelpMan &self, const Config &config,
230 const JSONRPCRequest &request) -> UniValue {
231 ChainstateManager &chainman = EnsureAnyChainman(request.context);
232 LOCK(cs_main);
233 return chainman.ActiveHeight();
234 },
235 };
236}
237
239 return RPCHelpMan{
240 "getbestblockhash",
241 "Returns the hash of the best (tip) block in the "
242 "most-work fully-validated chain.\n",
243 {},
244 RPCResult{RPCResult::Type::STR_HEX, "", "the block hash, hex-encoded"},
245 RPCExamples{HelpExampleCli("getbestblockhash", "") +
246 HelpExampleRpc("getbestblockhash", "")},
247 [&](const RPCHelpMan &self, const Config &config,
248 const JSONRPCRequest &request) -> UniValue {
249 ChainstateManager &chainman = EnsureAnyChainman(request.context);
250 LOCK(cs_main);
251 return chainman.ActiveTip()->GetBlockHash().GetHex();
252 },
253 };
254}
255
257 if (pindex) {
259 latestblock.hash = pindex->GetBlockHash();
260 latestblock.height = pindex->nHeight;
261 }
262 cond_blockchange.notify_all();
263}
264
266 return RPCHelpMan{
267 "waitfornewblock",
268 "Waits for a specific new block and returns useful info about it.\n"
269 "\nReturns the current block on timeout or exit.\n",
270 {
271 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0},
272 "Time in milliseconds to wait for a response. 0 indicates no "
273 "timeout."},
274 },
276 "",
277 "",
278 {
279 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
280 {RPCResult::Type::NUM, "height", "Block height"},
281 }},
282 RPCExamples{HelpExampleCli("waitfornewblock", "1000") +
283 HelpExampleRpc("waitfornewblock", "1000")},
284 [&](const RPCHelpMan &self, const Config &config,
285 const JSONRPCRequest &request) -> UniValue {
286 int timeout = 0;
287 if (!request.params[0].isNull()) {
288 timeout = request.params[0].getInt<int>();
289 }
290
291 CUpdatedBlock block;
292 {
294 block = latestblock;
295 if (timeout) {
296 cond_blockchange.wait_for(
297 lock, std::chrono::milliseconds(timeout),
299 return latestblock.height != block.height ||
300 latestblock.hash != block.hash ||
301 !IsRPCRunning();
302 });
303 } else {
304 cond_blockchange.wait(
305 lock,
307 return latestblock.height != block.height ||
308 latestblock.hash != block.hash ||
309 !IsRPCRunning();
310 });
311 }
312 block = latestblock;
313 }
315 ret.pushKV("hash", block.hash.GetHex());
316 ret.pushKV("height", block.height);
317 return ret;
318 },
319 };
320}
321
323 return RPCHelpMan{
324 "waitforblock",
325 "Waits for a specific new block and returns useful info about it.\n"
326 "\nReturns the current block on timeout or exit.\n",
327 {
329 "Block hash to wait for."},
330 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0},
331 "Time in milliseconds to wait for a response. 0 indicates no "
332 "timeout."},
333 },
335 "",
336 "",
337 {
338 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
339 {RPCResult::Type::NUM, "height", "Block height"},
340 }},
341 RPCExamples{HelpExampleCli("waitforblock",
342 "\"0000000000079f8ef3d2c688c244eb7a4570b24c9"
343 "ed7b4a8c619eb02596f8862\" 1000") +
344 HelpExampleRpc("waitforblock",
345 "\"0000000000079f8ef3d2c688c244eb7a4570b24c9"
346 "ed7b4a8c619eb02596f8862\", 1000")},
347 [&](const RPCHelpMan &self, const Config &config,
348 const JSONRPCRequest &request) -> UniValue {
349 int timeout = 0;
350
351 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
352
353 if (!request.params[1].isNull()) {
354 timeout = request.params[1].getInt<int>();
355 }
356
357 CUpdatedBlock block;
358 {
360 if (timeout) {
361 cond_blockchange.wait_for(
362 lock, std::chrono::milliseconds(timeout),
364 return latestblock.hash == hash || !IsRPCRunning();
365 });
366 } else {
367 cond_blockchange.wait(
368 lock,
370 return latestblock.hash == hash || !IsRPCRunning();
371 });
372 }
373 block = latestblock;
374 }
375
377 ret.pushKV("hash", block.hash.GetHex());
378 ret.pushKV("height", block.height);
379 return ret;
380 },
381 };
382}
383
385 return RPCHelpMan{
386 "waitforblockheight",
387 "Waits for (at least) block height and returns the height and "
388 "hash\nof the current tip.\n"
389 "\nReturns the current block on timeout or exit.\n",
390 {
392 "Block height to wait for."},
393 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0},
394 "Time in milliseconds to wait for a response. 0 indicates no "
395 "timeout."},
396 },
398 "",
399 "",
400 {
401 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
402 {RPCResult::Type::NUM, "height", "Block height"},
403 }},
404 RPCExamples{HelpExampleCli("waitforblockheight", "100 1000") +
405 HelpExampleRpc("waitforblockheight", "100, 1000")},
406 [&](const RPCHelpMan &self, const Config &config,
407 const JSONRPCRequest &request) -> UniValue {
408 int timeout = 0;
409
410 int height = request.params[0].getInt<int>();
411
412 if (!request.params[1].isNull()) {
413 timeout = request.params[1].getInt<int>();
414 }
415
416 CUpdatedBlock block;
417 {
419 if (timeout) {
420 cond_blockchange.wait_for(
421 lock, std::chrono::milliseconds(timeout),
423 return latestblock.height >= height ||
424 !IsRPCRunning();
425 });
426 } else {
427 cond_blockchange.wait(
428 lock,
430 return latestblock.height >= height ||
431 !IsRPCRunning();
432 });
433 }
434 block = latestblock;
435 }
437 ret.pushKV("hash", block.hash.GetHex());
438 ret.pushKV("height", block.height);
439 return ret;
440 },
441 };
442}
443
445 return RPCHelpMan{
446 "syncwithvalidationinterfacequeue",
447 "Waits for the validation interface queue to catch up on everything "
448 "that was there when we entered this function.\n",
449 {},
451 RPCExamples{HelpExampleCli("syncwithvalidationinterfacequeue", "") +
452 HelpExampleRpc("syncwithvalidationinterfacequeue", "")},
453 [&](const RPCHelpMan &self, const Config &config,
454 const JSONRPCRequest &request) -> UniValue {
456 return NullUniValue;
457 },
458 };
459}
460
462 return RPCHelpMan{
463 "getdifficulty",
464 "Returns the proof-of-work difficulty as a multiple of the minimum "
465 "difficulty.\n",
466 {},
468 "the proof-of-work difficulty as a multiple of the minimum "
469 "difficulty."},
470 RPCExamples{HelpExampleCli("getdifficulty", "") +
471 HelpExampleRpc("getdifficulty", "")},
472 [&](const RPCHelpMan &self, const Config &config,
473 const JSONRPCRequest &request) -> UniValue {
474 ChainstateManager &chainman = EnsureAnyChainman(request.context);
475 LOCK(cs_main);
476 return GetDifficulty(*CHECK_NONFATAL(chainman.ActiveTip()));
477 },
478 };
479}
480
482 return RPCHelpMan{
483 "getblockfrompeer",
484 "Attempt to fetch block from a given peer.\n"
485 "\nWe must have the header for this block, e.g. using submitheader.\n"
486 "The block will not have any undo data which can limit the usage of "
487 "the block data in a context where the undo data is needed.\n"
488 "Subsequent calls for the same block may cause the response from the "
489 "previous peer to be ignored.\n"
490 "\nReturns an empty JSON object if the request was successfully "
491 "scheduled.",
492 {
494 "The block hash to try to fetch"},
496 "The peer to fetch it from (see getpeerinfo for peer IDs)"},
497 },
498 RPCResult{RPCResult::Type::OBJ, "", /*optional=*/false, "", {}},
499 RPCExamples{HelpExampleCli("getblockfrompeer",
500 "\"00000000c937983704a73af28acdec37b049d214a"
501 "dbda81d7e2a3dd146f6ed09\" 0") +
502 HelpExampleRpc("getblockfrompeer",
503 "\"00000000c937983704a73af28acdec37b049d214a"
504 "dbda81d7e2a3dd146f6ed09\" 0")},
505 [&](const RPCHelpMan &self, const Config &config,
506 const JSONRPCRequest &request) -> UniValue {
507 const NodeContext &node = EnsureAnyNodeContext(request.context);
509 PeerManager &peerman = EnsurePeerman(node);
510
511 const BlockHash block_hash{
512 ParseHashV(request.params[0], "blockhash")};
513 const NodeId peer_id{request.params[1].getInt<int64_t>()};
514
515 const CBlockIndex *const index = WITH_LOCK(
516 cs_main,
517 return chainman.m_blockman.LookupBlockIndex(block_hash););
518
519 if (!index) {
520 throw JSONRPCError(RPC_MISC_ERROR, "Block header missing");
521 }
522
523 if (WITH_LOCK(::cs_main, return index->nStatus.hasData())) {
524 throw JSONRPCError(RPC_MISC_ERROR, "Block already downloaded");
525 }
526
527 if (const auto err{peerman.FetchBlock(config, peer_id, *index)}) {
528 throw JSONRPCError(RPC_MISC_ERROR, err.value());
529 }
530 return UniValue::VOBJ;
531 },
532 };
533}
534
536 return RPCHelpMan{
537 "getblockhash",
538 "Returns hash of block in best-block-chain at height provided.\n",
539 {
541 "The height index"},
542 },
543 RPCResult{RPCResult::Type::STR_HEX, "", "The block hash"},
544 RPCExamples{HelpExampleCli("getblockhash", "1000") +
545 HelpExampleRpc("getblockhash", "1000")},
546 [&](const RPCHelpMan &self, const Config &config,
547 const JSONRPCRequest &request) -> UniValue {
548 ChainstateManager &chainman = EnsureAnyChainman(request.context);
549 LOCK(cs_main);
550 const CChain &active_chain = chainman.ActiveChain();
551
552 int nHeight = request.params[0].getInt<int>();
553 if (nHeight < 0 || nHeight > active_chain.Height()) {
555 "Block height out of range");
556 }
557
558 const CBlockIndex *pblockindex = active_chain[nHeight];
559 return pblockindex->GetBlockHash().GetHex();
560 },
561 };
562}
563
565 return RPCHelpMan{
566 "getblockheader",
567 "If verbose is false, returns a string that is serialized, hex-encoded "
568 "data for blockheader 'hash'.\n"
569 "If verbose is true, returns an Object with information about "
570 "blockheader <hash>.\n",
571 {
573 "The block hash"},
574 {"verbose", RPCArg::Type::BOOL, RPCArg::Default{true},
575 "true for a json object, false for the hex-encoded data"},
576 },
577 {
578 RPCResult{
579 "for verbose = true",
581 "",
582 "",
583 {
585 "the block hash (same as provided)"},
586 {RPCResult::Type::NUM, "confirmations",
587 "The number of confirmations, or -1 if the block is not "
588 "on the main chain"},
589 {RPCResult::Type::NUM, "height",
590 "The block height or index"},
591 {RPCResult::Type::NUM, "version", "The block version"},
592 {RPCResult::Type::STR_HEX, "versionHex",
593 "The block version formatted in hexadecimal"},
594 {RPCResult::Type::STR_HEX, "merkleroot", "The merkle root"},
596 "The block time expressed in " + UNIX_EPOCH_TIME},
597 {RPCResult::Type::NUM_TIME, "mediantime",
598 "The median block time expressed in " + UNIX_EPOCH_TIME},
599 {RPCResult::Type::NUM, "nonce", "The nonce"},
600 {RPCResult::Type::STR_HEX, "bits", "The bits"},
601 {RPCResult::Type::NUM, "difficulty", "The difficulty"},
602 {RPCResult::Type::STR_HEX, "chainwork",
603 "Expected number of hashes required to produce the "
604 "current chain"},
605 {RPCResult::Type::NUM, "nTx",
606 "The number of transactions in the block"},
607 {RPCResult::Type::STR_HEX, "previousblockhash",
608 /* optional */ true,
609 "The hash of the previous block (if available)"},
610 {RPCResult::Type::STR_HEX, "nextblockhash",
611 /* optional */ true,
612 "The hash of the next block (if available)"},
613 }},
614 RPCResult{"for verbose=false", RPCResult::Type::STR_HEX, "",
615 "A string that is serialized, hex-encoded data for block "
616 "'hash'"},
617 },
618 RPCExamples{HelpExampleCli("getblockheader",
619 "\"00000000c937983704a73af28acdec37b049d214a"
620 "dbda81d7e2a3dd146f6ed09\"") +
621 HelpExampleRpc("getblockheader",
622 "\"00000000c937983704a73af28acdec37b049d214a"
623 "dbda81d7e2a3dd146f6ed09\"")},
624 [&](const RPCHelpMan &self, const Config &config,
625 const JSONRPCRequest &request) -> UniValue {
626 BlockHash hash(ParseHashV(request.params[0], "hash"));
627
628 bool fVerbose = true;
629 if (!request.params[1].isNull()) {
630 fVerbose = request.params[1].get_bool();
631 }
632
633 const CBlockIndex *pblockindex;
634 const CBlockIndex *tip;
635 {
636 ChainstateManager &chainman =
637 EnsureAnyChainman(request.context);
638 LOCK(cs_main);
639 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
640 tip = chainman.ActiveTip();
641 }
642
643 if (!pblockindex) {
645 "Block not found");
646 }
647
648 if (!fVerbose) {
649 DataStream ssBlock{};
650 ssBlock << pblockindex->GetBlockHeader();
651 std::string strHex = HexStr(ssBlock);
652 return strHex;
653 }
654
655 return blockheaderToJSON(*tip, *pblockindex);
656 },
657 };
658}
659
661 const CBlockIndex &blockindex) {
662 CBlock block;
663 {
664 LOCK(cs_main);
665 if (blockman.IsBlockPruned(blockindex)) {
667 "Block not available (pruned data)");
668 }
669 }
670
671 if (!blockman.ReadBlock(block, blockindex)) {
672 // Block not found on disk. This could be because we have the block
673 // header in our index but not yet have the block or did not accept the
674 // block. Or if the block was pruned right after we released the lock
675 // above.
676 throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk");
677 }
678
679 return block;
680}
681
683 const CBlockIndex &blockindex) {
684 CBlockUndo blockUndo;
685
686 {
687 LOCK(cs_main);
688 if (blockman.IsBlockPruned(blockindex)) {
690 "Undo data not available (pruned data)");
691 }
692 }
693
694 if (!blockman.ReadBlockUndo(blockUndo, blockindex)) {
695 throw JSONRPCError(RPC_MISC_ERROR, "Can't read undo data from disk");
696 }
697
698 return blockUndo;
699}
700
702 return RPCHelpMan{
703 "getblock",
704 "If verbosity is 0 or false, returns a string that is serialized, "
705 "hex-encoded data for block 'hash'.\n"
706 "If verbosity is 1 or true, returns an Object with information about "
707 "block <hash>.\n"
708 "If verbosity is 2, returns an Object with information about block "
709 "<hash> and information about each transaction.\n"
710 "If verbosity is 3, returns an Object with information about block "
711 "<hash> and information about each transaction, including prevout "
712 "information for inputs (only for unpruned blocks in the current best "
713 "chain).\n",
714 {
716 "The block hash"},
717 {"verbosity|verbose", RPCArg::Type::NUM, RPCArg::Default{1},
718 "0 for hex-encoded data, 1 for a json object, and 2 for json "
719 "object with transaction data",
721 },
722 {
723 RPCResult{"for verbosity = 0", RPCResult::Type::STR_HEX, "",
724 "A string that is serialized, hex-encoded data for block "
725 "'hash'"},
726 RPCResult{
727 "for verbosity = 1",
729 "",
730 "",
731 {
733 "the block hash (same as provided)"},
734 {RPCResult::Type::NUM, "confirmations",
735 "The number of confirmations, or -1 if the block is not "
736 "on the main chain"},
737 {RPCResult::Type::NUM, "size", "The block size"},
738 {RPCResult::Type::NUM, "height",
739 "The block height or index"},
740 {RPCResult::Type::NUM, "version", "The block version"},
741 {RPCResult::Type::STR_HEX, "versionHex",
742 "The block version formatted in hexadecimal"},
743 {RPCResult::Type::STR_HEX, "merkleroot", "The merkle root"},
745 "tx",
746 "The transaction ids",
747 {{RPCResult::Type::STR_HEX, "", "The transaction id"}}},
749 "The block time expressed in " + UNIX_EPOCH_TIME},
750 {RPCResult::Type::NUM_TIME, "mediantime",
751 "The median block time expressed in " + UNIX_EPOCH_TIME},
752 {RPCResult::Type::NUM, "nonce", "The nonce"},
753 {RPCResult::Type::STR_HEX, "bits", "The bits"},
754 {RPCResult::Type::NUM, "difficulty", "The difficulty"},
755 {RPCResult::Type::STR_HEX, "chainwork",
756 "Expected number of hashes required to produce the chain "
757 "up to this block (in hex)"},
758 {RPCResult::Type::NUM, "nTx",
759 "The number of transactions in the block"},
760 {RPCResult::Type::STR_HEX, "previousblockhash",
761 /* optional */ true,
762 "The hash of the previous block (if available)"},
763 {RPCResult::Type::STR_HEX, "nextblockhash",
764 /* optional */ true,
765 "The hash of the next block (if available)"},
766 }},
767 RPCResult{"for verbosity = 2",
769 "",
770 "",
771 {
773 "Same output as verbosity = 1"},
775 "tx",
776 "",
777 {
779 "",
780 "",
781 {
783 "The transactions in the format of the "
784 "getrawtransaction RPC. Different from "
785 "verbosity = 1 \"tx\" result"},
787 "The transaction fee in " +
789 ", omitted if block undo data is not "
790 "available"},
791 }},
792 }},
793 }},
794 },
796 HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d"
797 "214adbda81d7e2a3dd146f6ed09\"") +
798 HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d"
799 "214adbda81d7e2a3dd146f6ed09\"")},
800 [&](const RPCHelpMan &self, const Config &config,
801 const JSONRPCRequest &request) -> UniValue {
802 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
803
804 int verbosity = 1;
805 if (!request.params[1].isNull()) {
806 if (request.params[1].isNum()) {
807 verbosity = request.params[1].getInt<int>();
808 } else {
809 verbosity = request.params[1].get_bool() ? 1 : 0;
810 }
811 }
812
813 const CBlockIndex *pblockindex;
814 const CBlockIndex *tip;
815 ChainstateManager &chainman = EnsureAnyChainman(request.context);
816 {
817 LOCK(cs_main);
818 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
819 tip = chainman.ActiveTip();
820
821 if (!pblockindex) {
823 "Block not found");
824 }
825 }
826
827 const CBlock block =
828 GetBlockChecked(chainman.m_blockman, *pblockindex);
829
830 if (verbosity <= 0) {
831 DataStream ssBlock{};
832 ssBlock << block;
833 std::string strHex = HexStr(ssBlock);
834 return strHex;
835 }
836
837 TxVerbosity tx_verbosity;
838 if (verbosity == 1) {
839 tx_verbosity = TxVerbosity::SHOW_TXID;
840 } else if (verbosity == 2) {
841 tx_verbosity = TxVerbosity::SHOW_DETAILS;
842 } else {
844 }
845
846 return blockToJSON(chainman.m_blockman, block, *tip, *pblockindex,
847 tx_verbosity);
848 },
849 };
850}
851
852std::optional<int> GetPruneHeight(const BlockManager &blockman,
853 const CChain &chain) {
855
856 // Search for the last block missing block data or undo data. Don't let the
857 // search consider the genesis block, because the genesis block does not
858 // have undo data, but should not be considered pruned.
859 const CBlockIndex *first_block{chain[1]};
860 const CBlockIndex *chain_tip{chain.Tip()};
861
862 // If there are no blocks after the genesis block, or no blocks at all,
863 // nothing is pruned.
864 if (!first_block || !chain_tip) {
865 return std::nullopt;
866 }
867
868 // If the chain tip is pruned, everything is pruned.
869 if (!(chain_tip->nStatus.hasData() && chain_tip->nStatus.hasUndo())) {
870 return chain_tip->nHeight;
871 }
872
873 // Get first block with data, after the last block without data.
874 // This is the start of the unpruned range of blocks.
875 const CBlockIndex *first_unpruned{CHECK_NONFATAL(
876 blockman.GetFirstBlock(*chain_tip,
877 /*status_test=*/[](const BlockStatus &status) {
878 return status.hasData() && status.hasUndo();
879 }))};
880 if (first_unpruned == first_block) {
881 // All blocks between first_block and chain_tip have data, so nothing is
882 // pruned.
883 return std::nullopt;
884 }
885
886 // Block before the first unpruned block is the last pruned block.
887 return CHECK_NONFATAL(first_unpruned->pprev)->nHeight;
888}
889
891 return RPCHelpMan{
892 "pruneblockchain",
893 "",
894 {
896 "The block height to prune up to. May be set to a discrete "
897 "height, or to a " +
899 "\n"
900 " to prune blocks whose block time is at "
901 "least 2 hours older than the provided timestamp."},
902 },
903 RPCResult{RPCResult::Type::NUM, "", "Height of the last block pruned"},
904 RPCExamples{HelpExampleCli("pruneblockchain", "1000") +
905 HelpExampleRpc("pruneblockchain", "1000")},
906 [&](const RPCHelpMan &self, const Config &config,
907 const JSONRPCRequest &request) -> UniValue {
908 ChainstateManager &chainman = EnsureAnyChainman(request.context);
909 if (!chainman.m_blockman.IsPruneMode()) {
910 throw JSONRPCError(
912 "Cannot prune blocks because node is not in prune mode.");
913 }
914
915 LOCK(cs_main);
916 Chainstate &active_chainstate = chainman.ActiveChainstate();
917 CChain &active_chain = active_chainstate.m_chain;
918
919 int heightParam = request.params[0].getInt<int>();
920 if (heightParam < 0) {
922 "Negative block height.");
923 }
924
925 // Height value more than a billion is too high to be a block
926 // height, and too low to be a block time (corresponds to timestamp
927 // from Sep 2001).
928 if (heightParam > 1000000000) {
929 // Add a 2 hour buffer to include blocks which might have had
930 // old timestamps
931 const CBlockIndex *pindex = active_chain.FindEarliestAtLeast(
932 heightParam - TIMESTAMP_WINDOW, 0);
933 if (!pindex) {
935 "Could not find block with at least the "
936 "specified timestamp.");
937 }
938 heightParam = pindex->nHeight;
939 }
940
941 unsigned int height = (unsigned int)heightParam;
942 unsigned int chainHeight = (unsigned int)active_chain.Height();
943 if (chainHeight < config.GetChainParams().PruneAfterHeight()) {
945 "Blockchain is too short for pruning.");
946 } else if (height > chainHeight) {
947 throw JSONRPCError(
949 "Blockchain is shorter than the attempted prune height.");
950 } else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) {
952 "Attempt to prune blocks close to the tip. "
953 "Retaining the minimum number of blocks.\n");
954 height = chainHeight - MIN_BLOCKS_TO_KEEP;
955 }
956
957 PruneBlockFilesManual(active_chainstate, height);
958 return GetPruneHeight(chainman.m_blockman, active_chain)
959 .value_or(-1);
960 },
961 };
962}
963
964static CoinStatsHashType ParseHashType(const std::string &hash_type_input) {
965 if (hash_type_input == "hash_serialized") {
966 return CoinStatsHashType::HASH_SERIALIZED;
967 } else if (hash_type_input == "muhash") {
968 return CoinStatsHashType::MUHASH;
969 } else if (hash_type_input == "none") {
971 } else {
972 throw JSONRPCError(
974 strprintf("%s is not a valid hash_type", hash_type_input));
975 }
976}
977
979 return RPCHelpMan{
980 "gettxoutsetinfo",
981 "Returns statistics about the unspent transaction output set.\n"
982 "Note this call may take some time if you are not using "
983 "coinstatsindex.\n",
984 {
985 {"hash_type", RPCArg::Type::STR, RPCArg::Default{"hash_serialized"},
986 "Which UTXO set hash should be calculated. Options: "
987 "'hash_serialized' (the legacy algorithm), 'muhash', 'none'."},
989 "The block hash or height of the target height (only available "
990 "with coinstatsindex).",
992 .type_str = {"", "string or numeric"}}},
993 {"use_index", RPCArg::Type::BOOL, RPCArg::Default{true},
994 "Use coinstatsindex, if available."},
995 },
996 RPCResult{
998 "",
999 "",
1000 {
1001 {RPCResult::Type::NUM, "height",
1002 "The current block height (index)"},
1003 {RPCResult::Type::STR_HEX, "bestblock",
1004 "The hash of the block at the tip of the chain"},
1005 {RPCResult::Type::NUM, "txouts",
1006 "The number of unspent transaction outputs"},
1007 {RPCResult::Type::NUM, "bogosize",
1008 "Database-independent, meaningless metric indicating "
1009 "the UTXO set size"},
1010 {RPCResult::Type::STR_HEX, "hash_serialized",
1011 /* optional */ true,
1012 "The serialized hash (only present if 'hash_serialized' "
1013 "hash_type is chosen)"},
1014 {RPCResult::Type::STR_HEX, "muhash", /* optional */ true,
1015 "The serialized hash (only present if 'muhash' "
1016 "hash_type is chosen)"},
1017 {RPCResult::Type::NUM, "transactions",
1018 "The number of transactions with unspent outputs (not "
1019 "available when coinstatsindex is used)"},
1020 {RPCResult::Type::NUM, "disk_size",
1021 "The estimated size of the chainstate on disk (not "
1022 "available when coinstatsindex is used)"},
1023 {RPCResult::Type::STR_AMOUNT, "total_amount",
1024 "The total amount"},
1025 {RPCResult::Type::STR_AMOUNT, "total_unspendable_amount",
1026 "The total amount of coins permanently excluded from the UTXO "
1027 "set (only available if coinstatsindex is used)"},
1029 "block_info",
1030 "Info on amounts in the block at this block height (only "
1031 "available if coinstatsindex is used)",
1032 {{RPCResult::Type::STR_AMOUNT, "prevout_spent",
1033 "Total amount of all prevouts spent in this block"},
1034 {RPCResult::Type::STR_AMOUNT, "coinbase",
1035 "Coinbase subsidy amount of this block"},
1036 {RPCResult::Type::STR_AMOUNT, "new_outputs_ex_coinbase",
1037 "Total amount of new outputs created by this block"},
1038 {RPCResult::Type::STR_AMOUNT, "unspendable",
1039 "Total amount of unspendable outputs created in this block"},
1041 "unspendables",
1042 "Detailed view of the unspendable categories",
1043 {
1044 {RPCResult::Type::STR_AMOUNT, "genesis_block",
1045 "The unspendable amount of the Genesis block subsidy"},
1047 "Transactions overridden by duplicates (no longer "
1048 "possible with BIP30)"},
1049 {RPCResult::Type::STR_AMOUNT, "scripts",
1050 "Amounts sent to scripts that are unspendable (for "
1051 "example OP_RETURN outputs)"},
1052 {RPCResult::Type::STR_AMOUNT, "unclaimed_rewards",
1053 "Fee rewards that miners did not claim in their "
1054 "coinbase transaction"},
1055 }}}},
1056 }},
1058 HelpExampleCli("gettxoutsetinfo", "") +
1059 HelpExampleCli("gettxoutsetinfo", R"("none")") +
1060 HelpExampleCli("gettxoutsetinfo", R"("none" 1000)") +
1062 "gettxoutsetinfo",
1063 R"("none" '"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"')") +
1064 HelpExampleRpc("gettxoutsetinfo", "") +
1065 HelpExampleRpc("gettxoutsetinfo", R"("none")") +
1066 HelpExampleRpc("gettxoutsetinfo", R"("none", 1000)") +
1068 "gettxoutsetinfo",
1069 R"("none", "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09")")},
1070 [&](const RPCHelpMan &self, const Config &config,
1071 const JSONRPCRequest &request) -> UniValue {
1073
1074 const CBlockIndex *pindex{nullptr};
1075 const CoinStatsHashType hash_type{
1076 request.params[0].isNull()
1077 ? CoinStatsHashType::HASH_SERIALIZED
1078 : ParseHashType(request.params[0].get_str())};
1079 bool index_requested =
1080 request.params[2].isNull() || request.params[2].get_bool();
1081
1082 NodeContext &node = EnsureAnyNodeContext(request.context);
1084 Chainstate &active_chainstate = chainman.ActiveChainstate();
1085 active_chainstate.ForceFlushStateToDisk();
1086
1087 CCoinsView *coins_view;
1088 BlockManager *blockman;
1089 {
1090 LOCK(::cs_main);
1091 coins_view = &active_chainstate.CoinsDB();
1092 blockman = &active_chainstate.m_blockman;
1093 pindex = blockman->LookupBlockIndex(coins_view->GetBestBlock());
1094 }
1095
1096 if (!request.params[1].isNull()) {
1097 if (!g_coin_stats_index) {
1099 "Querying specific block heights "
1100 "requires coinstatsindex");
1101 }
1102
1103 if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
1105 "hash_serialized hash type cannot be "
1106 "queried for a specific block");
1107 }
1108
1109 pindex = ParseHashOrHeight(request.params[1], chainman);
1110 }
1111
1112 if (index_requested && g_coin_stats_index) {
1113 if (!g_coin_stats_index->BlockUntilSyncedToCurrentChain()) {
1114 const IndexSummary summary{
1115 g_coin_stats_index->GetSummary()};
1116
1117 // If a specific block was requested and the index has
1118 // already synced past that height, we can return the data
1119 // already even though the index is not fully synced yet.
1120 if (pindex->nHeight > summary.best_block_height) {
1121 throw JSONRPCError(
1123 strprintf(
1124 "Unable to get data because coinstatsindex is "
1125 "still syncing. Current height: %d",
1126 summary.best_block_height));
1127 }
1128 }
1129 }
1130
1131 const std::optional<CCoinsStats> maybe_stats = GetUTXOStats(
1132 coins_view, *blockman, hash_type, node.rpc_interruption_point,
1133 pindex, index_requested);
1134 if (maybe_stats.has_value()) {
1135 const CCoinsStats &stats = maybe_stats.value();
1136 ret.pushKV("height", int64_t(stats.nHeight));
1137 ret.pushKV("bestblock", stats.hashBlock.GetHex());
1138 ret.pushKV("txouts", int64_t(stats.nTransactionOutputs));
1139 ret.pushKV("bogosize", int64_t(stats.nBogoSize));
1140 if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
1141 ret.pushKV("hash_serialized",
1142 stats.hashSerialized.GetHex());
1143 }
1144 if (hash_type == CoinStatsHashType::MUHASH) {
1145 ret.pushKV("muhash", stats.hashSerialized.GetHex());
1146 }
1147 CHECK_NONFATAL(stats.total_amount.has_value());
1148 ret.pushKV("total_amount", stats.total_amount.value());
1149 if (!stats.index_used) {
1150 ret.pushKV("transactions",
1151 static_cast<int64_t>(stats.nTransactions));
1152 ret.pushKV("disk_size", stats.nDiskSize);
1153 } else {
1154 ret.pushKV("total_unspendable_amount",
1156
1157 CCoinsStats prev_stats{};
1158 if (pindex->nHeight > 0) {
1159 const std::optional<CCoinsStats> maybe_prev_stats =
1160 GetUTXOStats(coins_view, *blockman, hash_type,
1161 node.rpc_interruption_point,
1162 pindex->pprev, index_requested);
1163 if (!maybe_prev_stats) {
1165 "Unable to read UTXO set");
1166 }
1167 prev_stats = maybe_prev_stats.value();
1168 }
1169
1170 UniValue block_info(UniValue::VOBJ);
1171 block_info.pushKV(
1172 "prevout_spent",
1174 prev_stats.total_prevout_spent_amount);
1175 block_info.pushKV("coinbase",
1176 stats.total_coinbase_amount -
1177 prev_stats.total_coinbase_amount);
1178 block_info.pushKV(
1179 "new_outputs_ex_coinbase",
1181 prev_stats.total_new_outputs_ex_coinbase_amount);
1182 block_info.pushKV("unspendable",
1184 prev_stats.total_unspendable_amount);
1185
1186 UniValue unspendables(UniValue::VOBJ);
1187 unspendables.pushKV(
1188 "genesis_block",
1190 prev_stats.total_unspendables_genesis_block);
1191 unspendables.pushKV(
1192 "bip30", stats.total_unspendables_bip30 -
1193 prev_stats.total_unspendables_bip30);
1194 unspendables.pushKV(
1195 "scripts", stats.total_unspendables_scripts -
1196 prev_stats.total_unspendables_scripts);
1197 unspendables.pushKV(
1198 "unclaimed_rewards",
1200 prev_stats.total_unspendables_unclaimed_rewards);
1201 block_info.pushKV("unspendables", std::move(unspendables));
1202
1203 ret.pushKV("block_info", std::move(block_info));
1204 }
1205 } else {
1207 "Unable to read UTXO set");
1208 }
1209 return ret;
1210 },
1211 };
1212}
1213
1215 return RPCHelpMan{
1216 "gettxout",
1217 "Returns details about an unspent transaction output.\n",
1218 {
1220 "The transaction id"},
1221 {"n", RPCArg::Type::NUM, RPCArg::Optional::NO, "vout number"},
1222 {"include_mempool", RPCArg::Type::BOOL, RPCArg::Default{true},
1223 "Whether to include the mempool. Note that an unspent output that "
1224 "is spent in the mempool won't appear."},
1225 },
1226 {
1227 RPCResult{"If the UTXO was not found", RPCResult::Type::NONE, "",
1228 ""},
1229 RPCResult{
1230 "Otherwise",
1232 "",
1233 "",
1234 {
1235 {RPCResult::Type::STR_HEX, "bestblock",
1236 "The hash of the block at the tip of the chain"},
1237 {RPCResult::Type::NUM, "confirmations",
1238 "The number of confirmations"},
1240 "The transaction value in " + Currency::get().ticker},
1242 "scriptPubKey",
1243 "",
1244 {
1245 {RPCResult::Type::STR_HEX, "asm", ""},
1246 {RPCResult::Type::STR_HEX, "hex", ""},
1247 {RPCResult::Type::NUM, "reqSigs",
1248 "Number of required signatures"},
1249 {RPCResult::Type::STR_HEX, "type",
1250 "The type, eg pubkeyhash"},
1252 "addresses",
1253 "array of eCash addresses",
1254 {{RPCResult::Type::STR, "address", "eCash address"}}},
1255 }},
1256 {RPCResult::Type::BOOL, "coinbase", "Coinbase or not"},
1257 }},
1258 },
1259 RPCExamples{"\nGet unspent transactions\n" +
1260 HelpExampleCli("listunspent", "") + "\nView the details\n" +
1261 HelpExampleCli("gettxout", "\"txid\" 1") +
1262 "\nAs a JSON-RPC call\n" +
1263 HelpExampleRpc("gettxout", "\"txid\", 1")},
1264 [&](const RPCHelpMan &self, const Config &config,
1265 const JSONRPCRequest &request) -> UniValue {
1266 NodeContext &node = EnsureAnyNodeContext(request.context);
1268 LOCK(cs_main);
1269
1271
1272 TxId txid(ParseHashV(request.params[0], "txid"));
1273 int n = request.params[1].getInt<int>();
1274 COutPoint out(txid, n);
1275 bool fMempool = true;
1276 if (!request.params[2].isNull()) {
1277 fMempool = request.params[2].get_bool();
1278 }
1279
1280 Coin coin;
1281 Chainstate &active_chainstate = chainman.ActiveChainstate();
1282 CCoinsViewCache *coins_view = &active_chainstate.CoinsTip();
1283
1284 if (fMempool) {
1285 const CTxMemPool &mempool = EnsureMemPool(node);
1286 LOCK(mempool.cs);
1287 CCoinsViewMemPool view(coins_view, mempool);
1288 if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
1289 return NullUniValue;
1290 }
1291 } else {
1292 if (!coins_view->GetCoin(out, coin)) {
1293 return NullUniValue;
1294 }
1295 }
1296
1297 const CBlockIndex *pindex =
1298 active_chainstate.m_blockman.LookupBlockIndex(
1299 coins_view->GetBestBlock());
1300 ret.pushKV("bestblock", pindex->GetBlockHash().GetHex());
1301 if (coin.GetHeight() == MEMPOOL_HEIGHT) {
1302 ret.pushKV("confirmations", 0);
1303 } else {
1304 ret.pushKV("confirmations",
1305 int64_t(pindex->nHeight - coin.GetHeight() + 1));
1306 }
1307 ret.pushKV("value", coin.GetTxOut().nValue);
1309 ScriptPubKeyToUniv(coin.GetTxOut().scriptPubKey, o, true);
1310 ret.pushKV("scriptPubKey", std::move(o));
1311 ret.pushKV("coinbase", coin.IsCoinBase());
1312
1313 return ret;
1314 },
1315 };
1316}
1317
1319 return RPCHelpMan{
1320 "verifychain",
1321 "Verifies blockchain database.\n",
1322 {
1323 {"checklevel", RPCArg::Type::NUM,
1325 strprintf("%d, range=0-4", DEFAULT_CHECKLEVEL)},
1326 strprintf("How thorough the block verification is:\n%s",
1328 {"nblocks", RPCArg::Type::NUM,
1330 "The number of blocks to check."},
1331 },
1333 "Verification finished successfully. If false, check "
1334 "debug.log for reason."},
1335 RPCExamples{HelpExampleCli("verifychain", "") +
1336 HelpExampleRpc("verifychain", "")},
1337 [&](const RPCHelpMan &self, const Config &config,
1338 const JSONRPCRequest &request) -> UniValue {
1339 const int check_level{request.params[0].isNull()
1341 : request.params[0].getInt<int>()};
1342 const int check_depth{request.params[1].isNull()
1344 : request.params[1].getInt<int>()};
1345
1346 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1347 LOCK(cs_main);
1348
1349 Chainstate &active_chainstate = chainman.ActiveChainstate();
1350 return CVerifyDB(chainman.GetNotifications())
1351 .VerifyDB(active_chainstate,
1352 active_chainstate.CoinsTip(), check_level,
1353 check_depth) == VerifyDBResult::SUCCESS;
1354 },
1355 };
1356}
1357
1359 return RPCHelpMan{
1360 "getblockchaininfo",
1361 "Returns an object containing various state info regarding blockchain "
1362 "processing.\n",
1363 {},
1364 RPCResult{
1366 "",
1367 "",
1368 {
1369 {RPCResult::Type::STR, "chain",
1370 "current network name (main, test, regtest)"},
1371 {RPCResult::Type::NUM, "blocks",
1372 "the height of the most-work fully-validated "
1373 "non-parked chain. The genesis block has height 0"},
1374 {RPCResult::Type::NUM, "headers",
1375 "the current number of headers we have validated"},
1376 {RPCResult::Type::NUM, "finalized_blockhash",
1377 "the hash of the avalanche finalized tip if any, otherwise "
1378 "the genesis block hash"},
1379 {RPCResult::Type::STR, "bestblockhash",
1380 "the hash of the currently best block"},
1381 {RPCResult::Type::NUM, "difficulty", "the current difficulty"},
1383 "The block time expressed in " + UNIX_EPOCH_TIME},
1384 {RPCResult::Type::NUM_TIME, "mediantime",
1385 "The median block time expressed in " + UNIX_EPOCH_TIME},
1386 {RPCResult::Type::NUM, "verificationprogress",
1387 "estimate of verification progress [0..1]"},
1388 {RPCResult::Type::BOOL, "initialblockdownload",
1389 "(debug information) estimate of whether this node is in "
1390 "Initial Block Download mode"},
1391 {RPCResult::Type::STR_HEX, "chainwork",
1392 "total amount of work in active chain, in hexadecimal"},
1393 {RPCResult::Type::NUM, "size_on_disk",
1394 "the estimated size of the block and undo files on disk"},
1395 {RPCResult::Type::BOOL, "pruned",
1396 "if the blocks are subject to pruning"},
1397 {RPCResult::Type::NUM, "pruneheight",
1398 "lowest-height complete block stored (only present if pruning "
1399 "is enabled)"},
1400 {RPCResult::Type::BOOL, "automatic_pruning",
1401 "whether automatic pruning is enabled (only present if "
1402 "pruning is enabled)"},
1403 {RPCResult::Type::NUM, "prune_target_size",
1404 "the target size used by pruning (only present if automatic "
1405 "pruning is enabled)"},
1406 {RPCResult::Type::STR, "warnings",
1407 "any network and blockchain warnings"},
1408 }},
1409 RPCExamples{HelpExampleCli("getblockchaininfo", "") +
1410 HelpExampleRpc("getblockchaininfo", "")},
1411 [&](const RPCHelpMan &self, const Config &config,
1412 const JSONRPCRequest &request) -> UniValue {
1413 const CChainParams &chainparams = config.GetChainParams();
1414
1415 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1416 LOCK(cs_main);
1417 Chainstate &active_chainstate = chainman.ActiveChainstate();
1418
1419 const CBlockIndex &tip{
1420 *CHECK_NONFATAL(active_chainstate.m_chain.Tip())};
1421 const int height{tip.nHeight};
1422
1424 obj.pushKV("chain", chainparams.GetChainTypeString());
1425 obj.pushKV("blocks", height);
1426 obj.pushKV("headers", chainman.m_best_header
1427 ? chainman.m_best_header->nHeight
1428 : -1);
1429 auto avalanche_finalized_tip{chainman.GetAvalancheFinalizedTip()};
1430 obj.pushKV("finalized_blockhash",
1431 avalanche_finalized_tip
1432 ? avalanche_finalized_tip->GetBlockHash().GetHex()
1433 : chainparams.GenesisBlock().GetHash().GetHex());
1434 obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex());
1435 obj.pushKV("difficulty", GetDifficulty(tip));
1436 obj.pushKV("time", tip.GetBlockTime());
1437 obj.pushKV("mediantime", tip.GetMedianTimePast());
1438 obj.pushKV(
1439 "verificationprogress",
1440 GuessVerificationProgress(chainman.GetParams().TxData(), &tip));
1441 obj.pushKV("initialblockdownload",
1442 chainman.IsInitialBlockDownload());
1443 obj.pushKV("chainwork", tip.nChainWork.GetHex());
1444 obj.pushKV("size_on_disk",
1446 obj.pushKV("pruned", chainman.m_blockman.IsPruneMode());
1447
1448 if (chainman.m_blockman.IsPruneMode()) {
1449 const auto prune_height{GetPruneHeight(
1450 chainman.m_blockman, active_chainstate.m_chain)};
1451 obj.pushKV("pruneheight",
1452 prune_height ? prune_height.value() + 1 : 0);
1453
1454 const bool automatic_pruning{
1455 chainman.m_blockman.GetPruneTarget() !=
1456 BlockManager::PRUNE_TARGET_MANUAL};
1457 obj.pushKV("automatic_pruning", automatic_pruning);
1458 if (automatic_pruning) {
1459 obj.pushKV("prune_target_size",
1460 chainman.m_blockman.GetPruneTarget());
1461 }
1462 }
1463
1464 obj.pushKV("warnings", GetWarnings(false).original);
1465 return obj;
1466 },
1467 };
1468}
1469
1472 bool operator()(const CBlockIndex *a, const CBlockIndex *b) const {
1473 // Make sure that unequal blocks with the same height do not compare
1474 // equal. Use the pointers themselves to make a distinction.
1475 if (a->nHeight != b->nHeight) {
1476 return (a->nHeight > b->nHeight);
1477 }
1478
1479 return a < b;
1480 }
1481};
1482
1484 return RPCHelpMan{
1485 "getchaintips",
1486 "Return information about all known tips in the block tree, including "
1487 "the main chain as well as orphaned branches.\n",
1488 {},
1489 RPCResult{
1491 "",
1492 "",
1494 "",
1495 "",
1496 {
1497 {RPCResult::Type::NUM, "height", "height of the chain tip"},
1498 {RPCResult::Type::STR_HEX, "hash", "block hash of the tip"},
1499 {RPCResult::Type::NUM, "branchlen",
1500 "zero for main chain, otherwise length of branch connecting "
1501 "the tip to the main chain"},
1502 {RPCResult::Type::STR, "status",
1503 "status of the chain, \"active\" for the main chain\n"
1504 "Possible values for status:\n"
1505 "1. \"invalid\" This branch contains at "
1506 "least one invalid block\n"
1507 "2. \"parked\" This branch contains at "
1508 "least one parked block\n"
1509 "3. \"headers-only\" Not all blocks for this "
1510 "branch are available, but the headers are valid\n"
1511 "4. \"valid-headers\" All blocks are available for "
1512 "this branch, but they were never fully validated\n"
1513 "5. \"valid-fork\" This branch is not part of "
1514 "the active chain, but is fully validated\n"
1515 "6. \"active\" This is the tip of the "
1516 "active main chain, which is certainly valid"},
1517 }}}},
1518 RPCExamples{HelpExampleCli("getchaintips", "") +
1519 HelpExampleRpc("getchaintips", "")},
1520 [&](const RPCHelpMan &self, const Config &config,
1521 const JSONRPCRequest &request) -> UniValue {
1522 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1523 LOCK(cs_main);
1524 CChain &active_chain = chainman.ActiveChain();
1525
1537 std::set<const CBlockIndex *, CompareBlocksByHeight> setTips;
1538 std::set<const CBlockIndex *> setOrphans;
1539 std::set<const CBlockIndex *> setPrevs;
1540
1541 for (const auto &[_, block_index] : chainman.BlockIndex()) {
1542 if (!active_chain.Contains(&block_index)) {
1543 setOrphans.insert(&block_index);
1544 setPrevs.insert(block_index.pprev);
1545 }
1546 }
1547
1548 for (std::set<const CBlockIndex *>::iterator it =
1549 setOrphans.begin();
1550 it != setOrphans.end(); ++it) {
1551 if (setPrevs.erase(*it) == 0) {
1552 setTips.insert(*it);
1553 }
1554 }
1555
1556 // Always report the currently active tip.
1557 setTips.insert(active_chain.Tip());
1558
1559 /* Construct the output array. */
1561 for (const CBlockIndex *block : setTips) {
1563 obj.pushKV("height", block->nHeight);
1564 obj.pushKV("hash", block->phashBlock->GetHex());
1565
1566 const int branchLen =
1567 block->nHeight - active_chain.FindFork(block)->nHeight;
1568 obj.pushKV("branchlen", branchLen);
1569
1570 std::string status;
1571 if (active_chain.Contains(block)) {
1572 // This block is part of the currently active chain.
1573 status = "active";
1574 } else if (block->nStatus.isInvalid()) {
1575 // This block or one of its ancestors is invalid.
1576 status = "invalid";
1577 } else if (block->nStatus.isOnParkedChain()) {
1578 // This block or one of its ancestors is parked.
1579 status = "parked";
1580 } else if (!block->HaveNumChainTxs()) {
1581 // This block cannot be connected because full block data
1582 // for it or one of its parents is missing.
1583 status = "headers-only";
1584 } else if (block->IsValid(BlockValidity::SCRIPTS)) {
1585 // This block is fully validated, but no longer part of the
1586 // active chain. It was probably the active block once, but
1587 // was reorganized.
1588 status = "valid-fork";
1589 } else if (block->IsValid(BlockValidity::TREE)) {
1590 // The headers for this block are valid, but it has not been
1591 // validated. It was probably never part of the most-work
1592 // chain.
1593 status = "valid-headers";
1594 } else {
1595 // No clue.
1596 status = "unknown";
1597 }
1598 obj.pushKV("status", status);
1599
1600 res.push_back(std::move(obj));
1601 }
1602
1603 return res;
1604 },
1605 };
1606}
1607
1609 return RPCHelpMan{
1610 "preciousblock",
1611 "Treats a block as if it were received before others with the same "
1612 "work.\n"
1613 "\nA later preciousblock call can override the effect of an earlier "
1614 "one.\n"
1615 "\nThe effects of preciousblock are not retained across restarts.\n",
1616 {
1618 "the hash of the block to mark as precious"},
1619 },
1621 RPCExamples{HelpExampleCli("preciousblock", "\"blockhash\"") +
1622 HelpExampleRpc("preciousblock", "\"blockhash\"")},
1623 [&](const RPCHelpMan &self, const Config &config,
1624 const JSONRPCRequest &request) -> UniValue {
1625 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1626 CBlockIndex *pblockindex;
1627
1628 NodeContext &node = EnsureAnyNodeContext(request.context);
1630 {
1631 LOCK(cs_main);
1632 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
1633 if (!pblockindex) {
1635 "Block not found");
1636 }
1637 }
1638
1640 chainman.ActiveChainstate().PreciousBlock(state, pblockindex,
1641 node.avalanche.get());
1642
1643 if (!state.IsValid()) {
1645 }
1646
1647 // Block to make sure wallet/indexers sync before returning
1649
1650 return NullUniValue;
1651 },
1652 };
1653}
1654
1657 const BlockHash &block_hash) {
1659 CBlockIndex *pblockindex;
1660 {
1661 LOCK(chainman.GetMutex());
1662 pblockindex = chainman.m_blockman.LookupBlockIndex(block_hash);
1663 if (!pblockindex) {
1664 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1665 }
1666 }
1667 chainman.ActiveChainstate().InvalidateBlock(state, pblockindex);
1668
1669 if (state.IsValid()) {
1670 chainman.ActiveChainstate().ActivateBestChain(state, /*pblock=*/nullptr,
1671 avalanche);
1672 }
1673
1674 if (!state.IsValid()) {
1676 }
1677}
1678
1680 return RPCHelpMan{
1681 "invalidateblock",
1682 "Permanently marks a block as invalid, as if it violated a consensus "
1683 "rule.\n",
1684 {
1686 "the hash of the block to mark as invalid"},
1687 },
1689 RPCExamples{HelpExampleCli("invalidateblock", "\"blockhash\"") +
1690 HelpExampleRpc("invalidateblock", "\"blockhash\"")},
1691 [&](const RPCHelpMan &self, const Config &config,
1692 const JSONRPCRequest &request) -> UniValue {
1693 NodeContext &node = EnsureAnyNodeContext(request.context);
1695 const BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1696
1697 InvalidateBlock(chainman, node.avalanche.get(), hash);
1698 // Block to make sure wallet/indexers sync before returning
1700
1701 return NullUniValue;
1702 },
1703 };
1704}
1705
1707 return RPCHelpMan{
1708 "parkblock",
1709 "Marks a block as parked.\n",
1710 {
1712 "the hash of the block to park"},
1713 },
1715 RPCExamples{HelpExampleCli("parkblock", "\"blockhash\"") +
1716 HelpExampleRpc("parkblock", "\"blockhash\"")},
1717 [&](const RPCHelpMan &self, const Config &config,
1718 const JSONRPCRequest &request) -> UniValue {
1719 const std::string strHash = request.params[0].get_str();
1720 const BlockHash hash(uint256S(strHash));
1722
1723 NodeContext &node = EnsureAnyNodeContext(request.context);
1725 Chainstate &active_chainstate = chainman.ActiveChainstate();
1726 CBlockIndex *pblockindex = nullptr;
1727 {
1728 LOCK(cs_main);
1729 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
1730 if (!pblockindex) {
1732 "Block not found");
1733 }
1734
1735 if (active_chainstate.IsBlockAvalancheFinalized(pblockindex)) {
1736 // Reset avalanche finalization if we park a finalized
1737 // block.
1738 active_chainstate.ClearAvalancheFinalizedBlock();
1739 }
1740 }
1741
1742 active_chainstate.ParkBlock(state, pblockindex);
1743
1744 if (state.IsValid()) {
1745 active_chainstate.ActivateBestChain(state, /*pblock=*/nullptr,
1746 node.avalanche.get());
1747 }
1748
1749 if (!state.IsValid()) {
1751 }
1752
1753 // Block to make sure wallet/indexers sync before returning
1755
1756 return NullUniValue;
1757 },
1758 };
1759}
1760
1763 const BlockHash &block_hash) {
1764 {
1765 LOCK(chainman.GetMutex());
1766 CBlockIndex *pblockindex =
1767 chainman.m_blockman.LookupBlockIndex(block_hash);
1768 if (!pblockindex) {
1769 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1770 }
1771
1772 chainman.ActiveChainstate().ResetBlockFailureFlags(pblockindex);
1773 chainman.RecalculateBestHeader();
1774 }
1775
1777 chainman.ActiveChainstate().ActivateBestChain(state, /*pblock=*/nullptr,
1778 avalanche);
1779
1780 if (!state.IsValid()) {
1782 }
1783}
1784
1786 return RPCHelpMan{
1787 "reconsiderblock",
1788 "Removes invalidity status of a block, its ancestors and its"
1789 "descendants, reconsider them for activation.\n"
1790 "This can be used to undo the effects of invalidateblock.\n",
1791 {
1793 "the hash of the block to reconsider"},
1794 },
1796 RPCExamples{HelpExampleCli("reconsiderblock", "\"blockhash\"") +
1797 HelpExampleRpc("reconsiderblock", "\"blockhash\"")},
1798 [&](const RPCHelpMan &self, const Config &config,
1799 const JSONRPCRequest &request) -> UniValue {
1800 NodeContext &node = EnsureAnyNodeContext(request.context);
1802 const BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1803
1804 ReconsiderBlock(chainman, node.avalanche.get(), hash);
1805
1806 // Block to make sure wallet/indexers sync before returning
1808
1809 return NullUniValue;
1810 },
1811 };
1812}
1813
1815 return RPCHelpMan{
1816 "unparkblock",
1817 "Removes parked status of a block and its descendants, reconsider "
1818 "them for activation.\n"
1819 "This can be used to undo the effects of parkblock.\n",
1820 {
1822 "the hash of the block to unpark"},
1823 },
1825 RPCExamples{HelpExampleCli("unparkblock", "\"blockhash\"") +
1826 HelpExampleRpc("unparkblock", "\"blockhash\"")},
1827 [&](const RPCHelpMan &self, const Config &config,
1828 const JSONRPCRequest &request) -> UniValue {
1829 const std::string strHash = request.params[0].get_str();
1830 NodeContext &node = EnsureAnyNodeContext(request.context);
1832 const BlockHash hash(uint256S(strHash));
1833 Chainstate &active_chainstate = chainman.ActiveChainstate();
1834
1835 {
1836 LOCK(cs_main);
1837
1838 CBlockIndex *pblockindex =
1839 chainman.m_blockman.LookupBlockIndex(hash);
1840 if (!pblockindex) {
1842 "Block not found");
1843 }
1844
1845 if (!pblockindex->nStatus.isOnParkedChain()) {
1846 // Block to unpark is not parked so there is nothing to do.
1847 return NullUniValue;
1848 }
1849
1850 const CBlockIndex *tip = active_chainstate.m_chain.Tip();
1851 if (tip) {
1852 const CBlockIndex *ancestor =
1853 LastCommonAncestor(tip, pblockindex);
1854 if (active_chainstate.IsBlockAvalancheFinalized(ancestor)) {
1855 // Only reset avalanche finalization if we unpark a
1856 // block that might conflict with avalanche finalized
1857 // blocks.
1858 active_chainstate.ClearAvalancheFinalizedBlock();
1859 }
1860 }
1861
1862 active_chainstate.UnparkBlockAndChildren(pblockindex);
1863 }
1864
1866 active_chainstate.ActivateBestChain(state, /*pblock=*/nullptr,
1867 node.avalanche.get());
1868
1869 if (!state.IsValid()) {
1871 }
1872
1873 // Block to make sure wallet/indexers sync before returning
1875
1876 return NullUniValue;
1877 },
1878 };
1879}
1880
1882 return RPCHelpMan{
1883 "getchaintxstats",
1884 "Compute statistics about the total number and rate of transactions "
1885 "in the chain.\n",
1886 {
1887 {"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint{"one month"},
1888 "Size of the window in number of blocks"},
1889 {"blockhash", RPCArg::Type::STR_HEX,
1890 RPCArg::DefaultHint{"chain tip"},
1891 "The hash of the block that ends the window."},
1892 },
1893 RPCResult{
1895 "",
1896 "",
1897 {
1899 "The timestamp for the final block in the window, "
1900 "expressed in " +
1902 {RPCResult::Type::NUM, "txcount", /*optional=*/true,
1903 "The total number of transactions in the chain up to "
1904 "that point, if known. It may be unknown when using "
1905 "assumeutxo."},
1906 {RPCResult::Type::STR_HEX, "window_final_block_hash",
1907 "The hash of the final block in the window"},
1908 {RPCResult::Type::NUM, "window_final_block_height",
1909 "The height of the final block in the window."},
1910 {RPCResult::Type::NUM, "window_block_count",
1911 "Size of the window in number of blocks"},
1912 {RPCResult::Type::NUM, "window_interval",
1913 "The elapsed time in the window in seconds. Only "
1914 "returned if \"window_block_count\" is > 0"},
1915 {RPCResult::Type::NUM, "window_tx_count", /*optional=*/true,
1916 "The number of transactions in the window. Only "
1917 "returned if \"window_block_count\" is > 0 and if "
1918 "txcount exists for the start and end of the window."},
1919 {RPCResult::Type::NUM, "txrate", /*optional=*/true,
1920 "The average rate of transactions per second in the "
1921 "window. Only returned if \"window_interval\" is > 0 "
1922 "and if window_tx_count exists."},
1923 }},
1924 RPCExamples{HelpExampleCli("getchaintxstats", "") +
1925 HelpExampleRpc("getchaintxstats", "2016")},
1926 [&](const RPCHelpMan &self, const Config &config,
1927 const JSONRPCRequest &request) -> UniValue {
1928 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1929 const CBlockIndex *pindex;
1930
1931 // By default: 1 month
1932 int blockcount =
1933 30 * 24 * 60 * 60 /
1934 config.GetChainParams().GetConsensus().nPowTargetSpacing;
1935
1936 if (request.params[1].isNull()) {
1937 LOCK(cs_main);
1938 pindex = chainman.ActiveTip();
1939 } else {
1940 BlockHash hash(ParseHashV(request.params[1], "blockhash"));
1941 LOCK(cs_main);
1942 pindex = chainman.m_blockman.LookupBlockIndex(hash);
1943 if (!pindex) {
1945 "Block not found");
1946 }
1947 if (!chainman.ActiveChain().Contains(pindex)) {
1949 "Block is not in main chain");
1950 }
1951 }
1952
1953 CHECK_NONFATAL(pindex != nullptr);
1954
1955 if (request.params[0].isNull()) {
1956 blockcount =
1957 std::max(0, std::min(blockcount, pindex->nHeight - 1));
1958 } else {
1959 blockcount = request.params[0].getInt<int>();
1960
1961 if (blockcount < 0 ||
1962 (blockcount > 0 && blockcount >= pindex->nHeight)) {
1964 "Invalid block count: "
1965 "should be between 0 and "
1966 "the block's height - 1");
1967 }
1968 }
1969
1970 const CBlockIndex &past_block{*CHECK_NONFATAL(
1971 pindex->GetAncestor(pindex->nHeight - blockcount))};
1972 const int64_t nTimeDiff{pindex->GetMedianTimePast() -
1973 past_block.GetMedianTimePast()};
1974
1976 ret.pushKV("time", pindex->GetBlockTime());
1977 if (pindex->nChainTx) {
1978 ret.pushKV("txcount", pindex->nChainTx);
1979 }
1980 ret.pushKV("window_final_block_hash",
1981 pindex->GetBlockHash().GetHex());
1982 ret.pushKV("window_final_block_height", pindex->nHeight);
1983 ret.pushKV("window_block_count", blockcount);
1984 if (blockcount > 0) {
1985 ret.pushKV("window_interval", nTimeDiff);
1986 if (pindex->nChainTx != 0 && past_block.nChainTx != 0) {
1987 unsigned int window_tx_count =
1988 pindex->nChainTx - past_block.nChainTx;
1989 ret.pushKV("window_tx_count", window_tx_count);
1990 if (nTimeDiff > 0) {
1991 ret.pushKV("txrate",
1992 double(window_tx_count) / nTimeDiff);
1993 }
1994 }
1995 }
1996
1997 return ret;
1998 },
1999 };
2000}
2001
2002template <typename T>
2003static T CalculateTruncatedMedian(std::vector<T> &scores) {
2004 size_t size = scores.size();
2005 if (size == 0) {
2006 return T();
2007 }
2008
2009 std::sort(scores.begin(), scores.end());
2010 if (size % 2 == 0) {
2011 return (scores[size / 2 - 1] + scores[size / 2]) / 2;
2012 } else {
2013 return scores[size / 2];
2014 }
2015}
2016
2017template <typename T> static inline bool SetHasKeys(const std::set<T> &set) {
2018 return false;
2019}
2020template <typename T, typename Tk, typename... Args>
2021static inline bool SetHasKeys(const std::set<T> &set, const Tk &key,
2022 const Args &...args) {
2023 return (set.count(key) != 0) || SetHasKeys(set, args...);
2024}
2025
2026// outpoint (needed for the utxo index) + nHeight + fCoinBase
2027static constexpr size_t PER_UTXO_OVERHEAD =
2028 sizeof(COutPoint) + sizeof(uint32_t) + sizeof(bool);
2029
2031 const auto &ticker = Currency::get().ticker;
2032 return RPCHelpMan{
2033 "getblockstats",
2034 "Compute per block statistics for a given window. All amounts are "
2035 "in " +
2036 ticker +
2037 ".\n"
2038 "It won't work for some heights with pruning.\n",
2039 {
2040 {"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::NO,
2041 "The block hash or height of the target block",
2043 .type_str = {"", "string or numeric"}}},
2044 {"stats",
2046 RPCArg::DefaultHint{"all values"},
2047 "Values to plot (see result below)",
2048 {
2050 "Selected statistic"},
2052 "Selected statistic"},
2053 },
2055 },
2056 RPCResult{
2058 "",
2059 "",
2060 {
2061 {RPCResult::Type::NUM, "avgfee", "Average fee in the block"},
2062 {RPCResult::Type::NUM, "avgfeerate",
2063 "Average feerate (in satoshis per virtual byte)"},
2064 {RPCResult::Type::NUM, "avgtxsize", "Average transaction size"},
2065 {RPCResult::Type::STR_HEX, "blockhash",
2066 "The block hash (to check for potential reorgs)"},
2067 {RPCResult::Type::NUM, "height", "The height of the block"},
2068 {RPCResult::Type::NUM, "ins",
2069 "The number of inputs (excluding coinbase)"},
2070 {RPCResult::Type::NUM, "maxfee", "Maximum fee in the block"},
2071 {RPCResult::Type::NUM, "maxfeerate",
2072 "Maximum feerate (in satoshis per virtual byte)"},
2073 {RPCResult::Type::NUM, "maxtxsize", "Maximum transaction size"},
2074 {RPCResult::Type::NUM, "medianfee",
2075 "Truncated median fee in the block"},
2076 {RPCResult::Type::NUM, "medianfeerate",
2077 "Truncated median feerate (in " + ticker + " per byte)"},
2078 {RPCResult::Type::NUM, "mediantime",
2079 "The block median time past"},
2080 {RPCResult::Type::NUM, "mediantxsize",
2081 "Truncated median transaction size"},
2082 {RPCResult::Type::NUM, "minfee", "Minimum fee in the block"},
2083 {RPCResult::Type::NUM, "minfeerate",
2084 "Minimum feerate (in satoshis per virtual byte)"},
2085 {RPCResult::Type::NUM, "mintxsize", "Minimum transaction size"},
2086 {RPCResult::Type::NUM, "outs", "The number of outputs"},
2087 {RPCResult::Type::NUM, "subsidy", "The block subsidy"},
2088 {RPCResult::Type::NUM, "time", "The block time"},
2089 {RPCResult::Type::NUM, "total_out",
2090 "Total amount in all outputs (excluding coinbase and thus "
2091 "reward [ie subsidy + totalfee])"},
2092 {RPCResult::Type::NUM, "total_size",
2093 "Total size of all non-coinbase transactions"},
2094 {RPCResult::Type::NUM, "totalfee", "The fee total"},
2095 {RPCResult::Type::NUM, "txs",
2096 "The number of transactions (including coinbase)"},
2097 {RPCResult::Type::NUM, "utxo_increase",
2098 "The increase/decrease in the number of unspent outputs"},
2099 {RPCResult::Type::NUM, "utxo_size_inc",
2100 "The increase/decrease in size for the utxo index (not "
2101 "discounting op_return and similar)"},
2102 }},
2105 "getblockstats",
2106 R"('"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"' '["minfeerate","avgfeerate"]')") +
2107 HelpExampleCli("getblockstats",
2108 R"(1000 '["minfeerate","avgfeerate"]')") +
2110 "getblockstats",
2111 R"("00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09", ["minfeerate","avgfeerate"])") +
2112 HelpExampleRpc("getblockstats",
2113 R"(1000, ["minfeerate","avgfeerate"])")},
2114 [&](const RPCHelpMan &self, const Config &config,
2115 const JSONRPCRequest &request) -> UniValue {
2116 ChainstateManager &chainman = EnsureAnyChainman(request.context);
2117 const CBlockIndex &pindex{*CHECK_NONFATAL(
2118 ParseHashOrHeight(request.params[0], chainman))};
2119
2120 std::set<std::string> stats;
2121 if (!request.params[1].isNull()) {
2122 const UniValue stats_univalue = request.params[1].get_array();
2123 for (unsigned int i = 0; i < stats_univalue.size(); i++) {
2124 const std::string stat = stats_univalue[i].get_str();
2125 stats.insert(stat);
2126 }
2127 }
2128
2129 const CBlock &block = GetBlockChecked(chainman.m_blockman, pindex);
2130 const CBlockUndo &blockUndo =
2131 GetUndoChecked(chainman.m_blockman, pindex);
2132
2133 // Calculate everything if nothing selected (default)
2134 const bool do_all = stats.size() == 0;
2135 const bool do_mediantxsize =
2136 do_all || stats.count("mediantxsize") != 0;
2137 const bool do_medianfee = do_all || stats.count("medianfee") != 0;
2138 const bool do_medianfeerate =
2139 do_all || stats.count("medianfeerate") != 0;
2140 const bool loop_inputs =
2141 do_all || do_medianfee || do_medianfeerate ||
2142 SetHasKeys(stats, "utxo_size_inc", "totalfee", "avgfee",
2143 "avgfeerate", "minfee", "maxfee", "minfeerate",
2144 "maxfeerate");
2145 const bool loop_outputs =
2146 do_all || loop_inputs || stats.count("total_out");
2147 const bool do_calculate_size =
2148 do_mediantxsize || loop_inputs ||
2149 SetHasKeys(stats, "total_size", "avgtxsize", "mintxsize",
2150 "maxtxsize");
2151
2152 const int64_t blockMaxSize = config.GetMaxBlockSize();
2153 Amount maxfee = Amount::zero();
2154 Amount maxfeerate = Amount::zero();
2155 Amount minfee = MAX_MONEY;
2156 Amount minfeerate = MAX_MONEY;
2157 Amount total_out = Amount::zero();
2158 Amount totalfee = Amount::zero();
2159 int64_t inputs = 0;
2160 int64_t maxtxsize = 0;
2161 int64_t mintxsize = blockMaxSize;
2162 int64_t outputs = 0;
2163 int64_t total_size = 0;
2164 int64_t utxo_size_inc = 0;
2165 std::vector<Amount> fee_array;
2166 std::vector<Amount> feerate_array;
2167 std::vector<int64_t> txsize_array;
2168
2169 for (size_t i = 0; i < block.vtx.size(); ++i) {
2170 const auto &tx = block.vtx.at(i);
2171 outputs += tx->vout.size();
2172 Amount tx_total_out = Amount::zero();
2173 if (loop_outputs) {
2174 for (const CTxOut &out : tx->vout) {
2175 tx_total_out += out.nValue;
2176 utxo_size_inc +=
2178 }
2179 }
2180
2181 if (tx->IsCoinBase()) {
2182 continue;
2183 }
2184
2185 // Don't count coinbase's fake input
2186 inputs += tx->vin.size();
2187 // Don't count coinbase reward
2188 total_out += tx_total_out;
2189
2190 int64_t tx_size = 0;
2191 if (do_calculate_size) {
2192 tx_size = tx->GetTotalSize();
2193 if (do_mediantxsize) {
2194 txsize_array.push_back(tx_size);
2195 }
2196 maxtxsize = std::max(maxtxsize, tx_size);
2197 mintxsize = std::min(mintxsize, tx_size);
2198 total_size += tx_size;
2199 }
2200
2201 if (loop_inputs) {
2202 Amount tx_total_in = Amount::zero();
2203 const auto &txundo = blockUndo.vtxundo.at(i - 1);
2204 for (const Coin &coin : txundo.vprevout) {
2205 const CTxOut &prevoutput = coin.GetTxOut();
2206
2207 tx_total_in += prevoutput.nValue;
2208 utxo_size_inc -=
2209 GetSerializeSize(prevoutput) + PER_UTXO_OVERHEAD;
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(std::move(unspent));
2572 }
2573 result.pushKV("unspents", std::move(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:171
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:170
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:239
RPCHelpMan gettxout()
static RPCHelpMan getblock()
Definition: blockchain.cpp:701
static RPCHelpMan getdifficulty()
Definition: blockchain.cpp:461
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:104
static RPCHelpMan syncwithvalidationinterfacequeue()
Definition: blockchain.cpp:444
static CBlockUndo GetUndoChecked(BlockManager &blockman, const CBlockIndex &blockindex)
Definition: blockchain.cpp:682
static RPCHelpMan getchaintips()
static RPCHelpMan loadtxoutset()
static RPCHelpMan gettxoutsetinfo()
Definition: blockchain.cpp:978
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:964
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:88
static RPCHelpMan scantxoutset()
static std::condition_variable cond_blockchange
Definition: blockchain.cpp:70
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:660
std::optional< int > GetPruneHeight(const BlockManager &blockman, const CChain &chain)
Definition: blockchain.cpp:852
static void ReconsiderBlock(ChainstateManager &chainman, avalanche::Processor *const avalanche, const BlockHash &block_hash)
static RPCHelpMan getblockfilter()
static RPCHelpMan getbestblockhash()
Definition: blockchain.cpp:238
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:322
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:481
static RPCHelpMan getblockhash()
Definition: blockchain.cpp:535
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:147
RPCHelpMan unparkblock()
static RPCHelpMan waitforblockheight()
Definition: blockchain.cpp:384
static const CBlockIndex * ParseHashOrHeight(const UniValue &param, ChainstateManager &chainman)
Definition: blockchain.cpp:115
UniValue blockToJSON(BlockManager &blockman, const CBlock &block, const CBlockIndex &tip, const CBlockIndex &blockindex, TxVerbosity verbosity)
Block description to JSON.
Definition: blockchain.cpp:181
static RPCHelpMan pruneblockchain()
Definition: blockchain.cpp:890
static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange)
static RPCHelpMan getblockheader()
Definition: blockchain.cpp:564
RPCHelpMan parkblock()
static GlobalMutex cs_blockchange
Definition: blockchain.cpp:69
static RPCHelpMan dumptxoutset()
Serialize the UTXO set to a file for loading elsewhere.
static RPCHelpMan getblockcount()
Definition: blockchain.cpp:220
static RPCHelpMan waitfornewblock()
Definition: blockchain.cpp:265
void RPCNotifyBlockChange(const CBlockIndex *pindex)
Callback for when block tip changed.
Definition: blockchain.cpp:256
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:524
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:430
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:472
int fclose()
Definition: streams.h:445
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:72
std::vector< CTxUndo > vtxundo
Definition: undo.h:75
An in-memory indexed chain of blocks.
Definition: chain.h:138
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:154
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:190
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:170
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:213
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:90
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:15
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:652
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
GetCoin, returning whether it exists and is not spent.
Definition: txmempool.cpp:778
Definition: net.h:830
bool GetNetworkActive() const
Definition: net.h:922
void SetNetworkActive(bool active)
Definition: net.cpp:2404
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:330
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:136
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:61
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
Definition: validation.h:654
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:733
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:840
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:832
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:892
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:859
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
Definition: validation.h:889
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:866
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:995
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition: validation.h:790
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:1185
node::BlockMap & BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:1462
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:1436
kernel::Notifications & GetNotifications() const
Definition: validation.h:1293
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:1317
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1443
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1440
const CChainParams & GetParams() const
Definition: validation.h:1278
const Consensus::Params & GetConsensus() const
Definition: validation.h:1281
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:1437
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1402
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1326
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:118
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:416
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:116
bool ReadBlockUndo(CBlockUndo &blockundo, const CBlockIndex &index) const
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
Definition: blockstorage.h:353
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:350
bool ReadBlock(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
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
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:30
#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:24
Definition: messages.h:12
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:17
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:90
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
Definition: string.h:132
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:163
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:180
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:1382
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:35
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded values (throws error if not hex).
Definition: util.cpp:86
size_t GetSerializeSize(const T &t)
Definition: serialize.h:1262
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Definition: serialize.h:1258
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:381
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
Definition: amount.h:21
static constexpr Amount zero() noexcept
Definition: amount.h:34
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:65
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:155
@ 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:212
@ 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:214
std::string oneline_description
Should be empty unless it is supposed to override the auto-generated summary line.
Definition: util.h:149
bool skip_type_check
Definition: util.h:146
@ 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:37
Amount total_unspendables_scripts
Total cumulative amount of outputs sent to unspendable scripts (OP_RETURN for example) up to and incl...
Definition: coinstats.h:69
Amount total_coinbase_amount
Total cumulative amount of coinbase outputs up to and including this block.
Definition: coinstats.h:62
Amount total_unspendables_genesis_block
The unspendable coinbase amount from the genesis block.
Definition: coinstats.h:64
uint64_t coins_count
The number of coins contained.
Definition: coinstats.h:42
uint64_t nTransactions
Definition: coinstats.h:33
uint64_t nTransactionOutputs
Definition: coinstats.h:34
uint64_t nBogoSize
Definition: coinstats.h:35
bool index_used
Signals if the coinstatsindex was used to retrieve the statistics.
Definition: coinstats.h:45
Amount total_unspendables_bip30
The two unspendable coinbase outputs total amount caused by BIP30.
Definition: coinstats.h:66
Amount total_prevout_spent_amount
Total cumulative amount of prevouts spent up to and including this block.
Definition: coinstats.h:56
BlockHash hashBlock
Definition: coinstats.h:32
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:72
uint256 hashSerialized
Definition: coinstats.h:36
std::optional< Amount > total_amount
The total amount, or nullopt if an overflow occurred calculating it.
Definition: coinstats.h:39
Amount total_new_outputs_ex_coinbase_amount
Total cumulative amount of outputs created up to and including this block.
Definition: coinstats.h:59
Amount total_unspendable_amount
Total cumulative amount of unspendable coins up to and including this block.
Definition: coinstats.h:54
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
#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
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:101
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:99
static const signed int DEFAULT_CHECKBLOCKS
Definition: validation.h:100
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
bilingual_str GetWarnings(bool verbose)
Format a string that describes several potential problems detected by the core.
Definition: warnings.cpp:43