Bitcoin ABC 0.33.6
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 Chainstate &active_chainstate = chainman.ActiveChainstate();
1281 CCoinsViewCache *coins_view = &active_chainstate.CoinsTip();
1282
1283 std::optional<Coin> coin;
1284 if (fMempool) {
1285 const CTxMemPool &mempool = EnsureMemPool(node);
1286 LOCK(mempool.cs);
1287 CCoinsViewMemPool view(coins_view, mempool);
1288 if (!mempool.isSpent(out)) {
1289 coin = view.GetCoin(out);
1290 }
1291 } else {
1292 coin = coins_view->GetCoin(out);
1293 }
1294 if (!coin) {
1295 return UniValue::VNULL;
1296 }
1297
1298 const CBlockIndex *pindex =
1299 active_chainstate.m_blockman.LookupBlockIndex(
1300 coins_view->GetBestBlock());
1301 ret.pushKV("bestblock", pindex->GetBlockHash().GetHex());
1302 if (coin->GetHeight() == MEMPOOL_HEIGHT) {
1303 ret.pushKV("confirmations", 0);
1304 } else {
1305 ret.pushKV("confirmations",
1306 int64_t(pindex->nHeight - coin->GetHeight() + 1));
1307 }
1308 ret.pushKV("value", coin->GetTxOut().nValue);
1310 ScriptPubKeyToUniv(coin->GetTxOut().scriptPubKey, o, true);
1311 ret.pushKV("scriptPubKey", std::move(o));
1312 ret.pushKV("coinbase", coin->IsCoinBase());
1313
1314 return ret;
1315 },
1316 };
1317}
1318
1320 return RPCHelpMan{
1321 "verifychain",
1322 "Verifies blockchain database.\n",
1323 {
1324 {"checklevel", RPCArg::Type::NUM,
1326 strprintf("%d, range=0-4", DEFAULT_CHECKLEVEL)},
1327 strprintf("How thorough the block verification is:\n%s",
1329 {"nblocks", RPCArg::Type::NUM,
1331 "The number of blocks to check."},
1332 },
1334 "Verification finished successfully. If false, check "
1335 "debug.log for reason."},
1336 RPCExamples{HelpExampleCli("verifychain", "") +
1337 HelpExampleRpc("verifychain", "")},
1338 [&](const RPCHelpMan &self, const Config &config,
1339 const JSONRPCRequest &request) -> UniValue {
1340 const int check_level{request.params[0].isNull()
1342 : request.params[0].getInt<int>()};
1343 const int check_depth{request.params[1].isNull()
1345 : request.params[1].getInt<int>()};
1346
1347 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1348 LOCK(cs_main);
1349
1350 Chainstate &active_chainstate = chainman.ActiveChainstate();
1351 return CVerifyDB(chainman.GetNotifications())
1352 .VerifyDB(active_chainstate,
1353 active_chainstate.CoinsTip(), check_level,
1354 check_depth) == VerifyDBResult::SUCCESS;
1355 },
1356 };
1357}
1358
1360 return RPCHelpMan{
1361 "getblockchaininfo",
1362 "Returns an object containing various state info regarding blockchain "
1363 "processing.\n",
1364 {},
1365 RPCResult{
1367 "",
1368 "",
1369 {
1370 {RPCResult::Type::STR, "chain",
1371 "current network name (main, test, regtest)"},
1372 {RPCResult::Type::NUM, "blocks",
1373 "the height of the most-work fully-validated "
1374 "non-parked chain. The genesis block has height 0"},
1375 {RPCResult::Type::NUM, "headers",
1376 "the current number of headers we have validated"},
1377 {RPCResult::Type::NUM, "finalized_blockhash",
1378 "the hash of the avalanche finalized tip if any, otherwise "
1379 "the genesis block hash"},
1380 {RPCResult::Type::STR, "bestblockhash",
1381 "the hash of the currently best block"},
1382 {RPCResult::Type::NUM, "difficulty", "the current difficulty"},
1384 "The block time expressed in " + UNIX_EPOCH_TIME},
1385 {RPCResult::Type::NUM_TIME, "mediantime",
1386 "The median block time expressed in " + UNIX_EPOCH_TIME},
1387 {RPCResult::Type::NUM, "verificationprogress",
1388 "estimate of verification progress [0..1]"},
1389 {RPCResult::Type::BOOL, "initialblockdownload",
1390 "(debug information) estimate of whether this node is in "
1391 "Initial Block Download mode"},
1392 {RPCResult::Type::STR_HEX, "chainwork",
1393 "total amount of work in active chain, in hexadecimal"},
1394 {RPCResult::Type::NUM, "size_on_disk",
1395 "the estimated size of the block and undo files on disk"},
1396 {RPCResult::Type::BOOL, "pruned",
1397 "if the blocks are subject to pruning"},
1398 {RPCResult::Type::NUM, "pruneheight",
1399 "lowest-height complete block stored (only present if pruning "
1400 "is enabled)"},
1401 {RPCResult::Type::BOOL, "automatic_pruning",
1402 "whether automatic pruning is enabled (only present if "
1403 "pruning is enabled)"},
1404 {RPCResult::Type::NUM, "prune_target_size",
1405 "the target size used by pruning (only present if automatic "
1406 "pruning is enabled)"},
1407 {RPCResult::Type::STR, "warnings",
1408 "any network and blockchain warnings"},
1409 }},
1410 RPCExamples{HelpExampleCli("getblockchaininfo", "") +
1411 HelpExampleRpc("getblockchaininfo", "")},
1412 [&](const RPCHelpMan &self, const Config &config,
1413 const JSONRPCRequest &request) -> UniValue {
1414 const CChainParams &chainparams = config.GetChainParams();
1415
1416 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1417 LOCK(cs_main);
1418 Chainstate &active_chainstate = chainman.ActiveChainstate();
1419
1420 const CBlockIndex &tip{
1421 *CHECK_NONFATAL(active_chainstate.m_chain.Tip())};
1422 const int height{tip.nHeight};
1423
1425 obj.pushKV("chain", chainparams.GetChainTypeString());
1426 obj.pushKV("blocks", height);
1427 obj.pushKV("headers", chainman.m_best_header
1428 ? chainman.m_best_header->nHeight
1429 : -1);
1430 auto avalanche_finalized_tip{chainman.GetAvalancheFinalizedTip()};
1431 obj.pushKV("finalized_blockhash",
1432 avalanche_finalized_tip
1433 ? avalanche_finalized_tip->GetBlockHash().GetHex()
1434 : chainparams.GenesisBlock().GetHash().GetHex());
1435 obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex());
1436 obj.pushKV("difficulty", GetDifficulty(tip));
1437 obj.pushKV("time", tip.GetBlockTime());
1438 obj.pushKV("mediantime", tip.GetMedianTimePast());
1439 obj.pushKV(
1440 "verificationprogress",
1441 GuessVerificationProgress(chainman.GetParams().TxData(), &tip));
1442 obj.pushKV("initialblockdownload",
1443 chainman.IsInitialBlockDownload());
1444 obj.pushKV("chainwork", tip.nChainWork.GetHex());
1445 obj.pushKV("size_on_disk",
1447 obj.pushKV("pruned", chainman.m_blockman.IsPruneMode());
1448
1449 if (chainman.m_blockman.IsPruneMode()) {
1450 const auto prune_height{GetPruneHeight(
1451 chainman.m_blockman, active_chainstate.m_chain)};
1452 obj.pushKV("pruneheight",
1453 prune_height ? prune_height.value() + 1 : 0);
1454
1455 const bool automatic_pruning{
1456 chainman.m_blockman.GetPruneTarget() !=
1457 BlockManager::PRUNE_TARGET_MANUAL};
1458 obj.pushKV("automatic_pruning", automatic_pruning);
1459 if (automatic_pruning) {
1460 obj.pushKV("prune_target_size",
1461 chainman.m_blockman.GetPruneTarget());
1462 }
1463 }
1464
1465 obj.pushKV("warnings", GetWarnings(false).original);
1466 return obj;
1467 },
1468 };
1469}
1470
1473 bool operator()(const CBlockIndex *a, const CBlockIndex *b) const {
1474 // Make sure that unequal blocks with the same height do not compare
1475 // equal. Use the pointers themselves to make a distinction.
1476 if (a->nHeight != b->nHeight) {
1477 return (a->nHeight > b->nHeight);
1478 }
1479
1480 return a < b;
1481 }
1482};
1483
1485 return RPCHelpMan{
1486 "getchaintips",
1487 "Return information about all known tips in the block tree, including "
1488 "the main chain as well as orphaned branches.\n",
1489 {},
1490 RPCResult{
1492 "",
1493 "",
1495 "",
1496 "",
1497 {
1498 {RPCResult::Type::NUM, "height", "height of the chain tip"},
1499 {RPCResult::Type::STR_HEX, "hash", "block hash of the tip"},
1500 {RPCResult::Type::NUM, "branchlen",
1501 "zero for main chain, otherwise length of branch connecting "
1502 "the tip to the main chain"},
1503 {RPCResult::Type::STR, "status",
1504 "status of the chain, \"active\" for the main chain\n"
1505 "Possible values for status:\n"
1506 "1. \"invalid\" This branch contains at "
1507 "least one invalid block\n"
1508 "2. \"parked\" This branch contains at "
1509 "least one parked block\n"
1510 "3. \"headers-only\" Not all blocks for this "
1511 "branch are available, but the headers are valid\n"
1512 "4. \"valid-headers\" All blocks are available for "
1513 "this branch, but they were never fully validated\n"
1514 "5. \"valid-fork\" This branch is not part of "
1515 "the active chain, but is fully validated\n"
1516 "6. \"active\" This is the tip of the "
1517 "active main chain, which is certainly valid"},
1518 }}}},
1519 RPCExamples{HelpExampleCli("getchaintips", "") +
1520 HelpExampleRpc("getchaintips", "")},
1521 [&](const RPCHelpMan &self, const Config &config,
1522 const JSONRPCRequest &request) -> UniValue {
1523 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1524 LOCK(cs_main);
1525 CChain &active_chain = chainman.ActiveChain();
1526
1538 std::set<const CBlockIndex *, CompareBlocksByHeight> setTips;
1539 std::set<const CBlockIndex *> setOrphans;
1540 std::set<const CBlockIndex *> setPrevs;
1541
1542 for (const auto &[_, block_index] : chainman.BlockIndex()) {
1543 if (!active_chain.Contains(&block_index)) {
1544 setOrphans.insert(&block_index);
1545 setPrevs.insert(block_index.pprev);
1546 }
1547 }
1548
1549 for (std::set<const CBlockIndex *>::iterator it =
1550 setOrphans.begin();
1551 it != setOrphans.end(); ++it) {
1552 if (setPrevs.erase(*it) == 0) {
1553 setTips.insert(*it);
1554 }
1555 }
1556
1557 // Always report the currently active tip.
1558 setTips.insert(active_chain.Tip());
1559
1560 /* Construct the output array. */
1562 for (const CBlockIndex *block : setTips) {
1564 obj.pushKV("height", block->nHeight);
1565 obj.pushKV("hash", block->phashBlock->GetHex());
1566
1567 const int branchLen =
1568 block->nHeight - active_chain.FindFork(block)->nHeight;
1569 obj.pushKV("branchlen", branchLen);
1570
1571 std::string status;
1572 if (active_chain.Contains(block)) {
1573 // This block is part of the currently active chain.
1574 status = "active";
1575 } else if (block->nStatus.isInvalid()) {
1576 // This block or one of its ancestors is invalid.
1577 status = "invalid";
1578 } else if (block->nStatus.isOnParkedChain()) {
1579 // This block or one of its ancestors is parked.
1580 status = "parked";
1581 } else if (!block->HaveNumChainTxs()) {
1582 // This block cannot be connected because full block data
1583 // for it or one of its parents is missing.
1584 status = "headers-only";
1585 } else if (block->IsValid(BlockValidity::SCRIPTS)) {
1586 // This block is fully validated, but no longer part of the
1587 // active chain. It was probably the active block once, but
1588 // was reorganized.
1589 status = "valid-fork";
1590 } else if (block->IsValid(BlockValidity::TREE)) {
1591 // The headers for this block are valid, but it has not been
1592 // validated. It was probably never part of the most-work
1593 // chain.
1594 status = "valid-headers";
1595 } else {
1596 // No clue.
1597 status = "unknown";
1598 }
1599 obj.pushKV("status", status);
1600
1601 res.push_back(std::move(obj));
1602 }
1603
1604 return res;
1605 },
1606 };
1607}
1608
1610 return RPCHelpMan{
1611 "preciousblock",
1612 "Treats a block as if it were received before others with the same "
1613 "work.\n"
1614 "\nA later preciousblock call can override the effect of an earlier "
1615 "one.\n"
1616 "\nThe effects of preciousblock are not retained across restarts.\n",
1617 {
1619 "the hash of the block to mark as precious"},
1620 },
1622 RPCExamples{HelpExampleCli("preciousblock", "\"blockhash\"") +
1623 HelpExampleRpc("preciousblock", "\"blockhash\"")},
1624 [&](const RPCHelpMan &self, const Config &config,
1625 const JSONRPCRequest &request) -> UniValue {
1626 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1627 CBlockIndex *pblockindex;
1628
1629 NodeContext &node = EnsureAnyNodeContext(request.context);
1631 {
1632 LOCK(cs_main);
1633 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
1634 if (!pblockindex) {
1636 "Block not found");
1637 }
1638 }
1639
1641 chainman.ActiveChainstate().PreciousBlock(state, pblockindex,
1642 node.avalanche.get());
1643
1644 if (!state.IsValid()) {
1646 }
1647
1648 // Block to make sure wallet/indexers sync before returning
1650
1651 return NullUniValue;
1652 },
1653 };
1654}
1655
1658 const BlockHash &block_hash) {
1660 CBlockIndex *pblockindex;
1661 {
1662 LOCK(chainman.GetMutex());
1663 pblockindex = chainman.m_blockman.LookupBlockIndex(block_hash);
1664 if (!pblockindex) {
1665 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1666 }
1667 }
1668 chainman.ActiveChainstate().InvalidateBlock(state, pblockindex);
1669
1670 if (state.IsValid()) {
1671 chainman.ActiveChainstate().ActivateBestChain(state, /*pblock=*/nullptr,
1672 avalanche);
1673 }
1674
1675 if (!state.IsValid()) {
1677 }
1678}
1679
1681 return RPCHelpMan{
1682 "invalidateblock",
1683 "Permanently marks a block as invalid, as if it violated a consensus "
1684 "rule.\n",
1685 {
1687 "the hash of the block to mark as invalid"},
1688 },
1690 RPCExamples{HelpExampleCli("invalidateblock", "\"blockhash\"") +
1691 HelpExampleRpc("invalidateblock", "\"blockhash\"")},
1692 [&](const RPCHelpMan &self, const Config &config,
1693 const JSONRPCRequest &request) -> UniValue {
1694 NodeContext &node = EnsureAnyNodeContext(request.context);
1696 const BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1697
1698 InvalidateBlock(chainman, node.avalanche.get(), hash);
1699 // Block to make sure wallet/indexers sync before returning
1701
1702 return NullUniValue;
1703 },
1704 };
1705}
1706
1708 return RPCHelpMan{
1709 "parkblock",
1710 "Marks a block as parked.\n",
1711 {
1713 "the hash of the block to park"},
1714 },
1716 RPCExamples{HelpExampleCli("parkblock", "\"blockhash\"") +
1717 HelpExampleRpc("parkblock", "\"blockhash\"")},
1718 [&](const RPCHelpMan &self, const Config &config,
1719 const JSONRPCRequest &request) -> UniValue {
1720 const std::string strHash = request.params[0].get_str();
1721 const BlockHash hash(uint256S(strHash));
1723
1724 NodeContext &node = EnsureAnyNodeContext(request.context);
1726 Chainstate &active_chainstate = chainman.ActiveChainstate();
1727 CBlockIndex *pblockindex = nullptr;
1728 {
1729 LOCK(cs_main);
1730 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
1731 if (!pblockindex) {
1733 "Block not found");
1734 }
1735
1736 if (active_chainstate.IsBlockAvalancheFinalized(pblockindex)) {
1737 // Reset avalanche finalization if we park a finalized
1738 // block.
1739 active_chainstate.ClearAvalancheFinalizedBlock();
1740 }
1741 }
1742
1743 active_chainstate.ParkBlock(state, pblockindex);
1744
1745 if (state.IsValid()) {
1746 active_chainstate.ActivateBestChain(state, /*pblock=*/nullptr,
1747 node.avalanche.get());
1748 }
1749
1750 if (!state.IsValid()) {
1752 }
1753
1754 // Block to make sure wallet/indexers sync before returning
1756
1757 return NullUniValue;
1758 },
1759 };
1760}
1761
1764 const BlockHash &block_hash) {
1765 {
1766 LOCK(chainman.GetMutex());
1767 CBlockIndex *pblockindex =
1768 chainman.m_blockman.LookupBlockIndex(block_hash);
1769 if (!pblockindex) {
1770 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1771 }
1772
1773 chainman.ActiveChainstate().ResetBlockFailureFlags(pblockindex);
1774 chainman.RecalculateBestHeader();
1775 }
1776
1778 chainman.ActiveChainstate().ActivateBestChain(state, /*pblock=*/nullptr,
1779 avalanche);
1780
1781 if (!state.IsValid()) {
1783 }
1784}
1785
1787 return RPCHelpMan{
1788 "reconsiderblock",
1789 "Removes invalidity status of a block, its ancestors and its"
1790 "descendants, reconsider them for activation.\n"
1791 "This can be used to undo the effects of invalidateblock.\n",
1792 {
1794 "the hash of the block to reconsider"},
1795 },
1797 RPCExamples{HelpExampleCli("reconsiderblock", "\"blockhash\"") +
1798 HelpExampleRpc("reconsiderblock", "\"blockhash\"")},
1799 [&](const RPCHelpMan &self, const Config &config,
1800 const JSONRPCRequest &request) -> UniValue {
1801 NodeContext &node = EnsureAnyNodeContext(request.context);
1803 const BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1804
1805 ReconsiderBlock(chainman, node.avalanche.get(), hash);
1806
1807 // Block to make sure wallet/indexers sync before returning
1809
1810 return NullUniValue;
1811 },
1812 };
1813}
1814
1816 return RPCHelpMan{
1817 "unparkblock",
1818 "Removes parked status of a block and its descendants, reconsider "
1819 "them for activation.\n"
1820 "This can be used to undo the effects of parkblock.\n",
1821 {
1823 "the hash of the block to unpark"},
1824 },
1826 RPCExamples{HelpExampleCli("unparkblock", "\"blockhash\"") +
1827 HelpExampleRpc("unparkblock", "\"blockhash\"")},
1828 [&](const RPCHelpMan &self, const Config &config,
1829 const JSONRPCRequest &request) -> UniValue {
1830 const std::string strHash = request.params[0].get_str();
1831 NodeContext &node = EnsureAnyNodeContext(request.context);
1833 const BlockHash hash(uint256S(strHash));
1834 Chainstate &active_chainstate = chainman.ActiveChainstate();
1835
1836 {
1837 LOCK(cs_main);
1838
1839 CBlockIndex *pblockindex =
1840 chainman.m_blockman.LookupBlockIndex(hash);
1841 if (!pblockindex) {
1843 "Block not found");
1844 }
1845
1846 if (!pblockindex->nStatus.isOnParkedChain()) {
1847 // Block to unpark is not parked so there is nothing to do.
1848 return NullUniValue;
1849 }
1850
1851 const CBlockIndex *tip = active_chainstate.m_chain.Tip();
1852 if (tip) {
1853 const CBlockIndex *ancestor =
1854 LastCommonAncestor(tip, pblockindex);
1855 if (active_chainstate.IsBlockAvalancheFinalized(ancestor)) {
1856 // Only reset avalanche finalization if we unpark a
1857 // block that might conflict with avalanche finalized
1858 // blocks.
1859 active_chainstate.ClearAvalancheFinalizedBlock();
1860 }
1861 }
1862
1863 active_chainstate.UnparkBlockAndChildren(pblockindex);
1864 }
1865
1867 active_chainstate.ActivateBestChain(state, /*pblock=*/nullptr,
1868 node.avalanche.get());
1869
1870 if (!state.IsValid()) {
1872 }
1873
1874 // Block to make sure wallet/indexers sync before returning
1876
1877 return NullUniValue;
1878 },
1879 };
1880}
1881
1883 return RPCHelpMan{
1884 "getchaintxstats",
1885 "Compute statistics about the total number and rate of transactions "
1886 "in the chain.\n",
1887 {
1888 {"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint{"one month"},
1889 "Size of the window in number of blocks"},
1890 {"blockhash", RPCArg::Type::STR_HEX,
1891 RPCArg::DefaultHint{"chain tip"},
1892 "The hash of the block that ends the window."},
1893 },
1894 RPCResult{
1896 "",
1897 "",
1898 {
1900 "The timestamp for the final block in the window, "
1901 "expressed in " +
1903 {RPCResult::Type::NUM, "txcount", /*optional=*/true,
1904 "The total number of transactions in the chain up to "
1905 "that point, if known. It may be unknown when using "
1906 "assumeutxo."},
1907 {RPCResult::Type::STR_HEX, "window_final_block_hash",
1908 "The hash of the final block in the window"},
1909 {RPCResult::Type::NUM, "window_final_block_height",
1910 "The height of the final block in the window."},
1911 {RPCResult::Type::NUM, "window_block_count",
1912 "Size of the window in number of blocks"},
1913 {RPCResult::Type::NUM, "window_interval",
1914 "The elapsed time in the window in seconds. Only "
1915 "returned if \"window_block_count\" is > 0"},
1916 {RPCResult::Type::NUM, "window_tx_count", /*optional=*/true,
1917 "The number of transactions in the window. Only "
1918 "returned if \"window_block_count\" is > 0 and if "
1919 "txcount exists for the start and end of the window."},
1920 {RPCResult::Type::NUM, "txrate", /*optional=*/true,
1921 "The average rate of transactions per second in the "
1922 "window. Only returned if \"window_interval\" is > 0 "
1923 "and if window_tx_count exists."},
1924 }},
1925 RPCExamples{HelpExampleCli("getchaintxstats", "") +
1926 HelpExampleRpc("getchaintxstats", "2016")},
1927 [&](const RPCHelpMan &self, const Config &config,
1928 const JSONRPCRequest &request) -> UniValue {
1929 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1930 const CBlockIndex *pindex;
1931
1932 // By default: 1 month
1933 int blockcount =
1934 30 * 24 * 60 * 60 /
1935 config.GetChainParams().GetConsensus().nPowTargetSpacing;
1936
1937 if (request.params[1].isNull()) {
1938 LOCK(cs_main);
1939 pindex = chainman.ActiveTip();
1940 } else {
1941 BlockHash hash(ParseHashV(request.params[1], "blockhash"));
1942 LOCK(cs_main);
1943 pindex = chainman.m_blockman.LookupBlockIndex(hash);
1944 if (!pindex) {
1946 "Block not found");
1947 }
1948 if (!chainman.ActiveChain().Contains(pindex)) {
1950 "Block is not in main chain");
1951 }
1952 }
1953
1954 CHECK_NONFATAL(pindex != nullptr);
1955
1956 if (request.params[0].isNull()) {
1957 blockcount =
1958 std::max(0, std::min(blockcount, pindex->nHeight - 1));
1959 } else {
1960 blockcount = request.params[0].getInt<int>();
1961
1962 if (blockcount < 0 ||
1963 (blockcount > 0 && blockcount >= pindex->nHeight)) {
1965 "Invalid block count: "
1966 "should be between 0 and "
1967 "the block's height - 1");
1968 }
1969 }
1970
1971 const CBlockIndex &past_block{*CHECK_NONFATAL(
1972 pindex->GetAncestor(pindex->nHeight - blockcount))};
1973 const int64_t nTimeDiff{pindex->GetMedianTimePast() -
1974 past_block.GetMedianTimePast()};
1975
1977 ret.pushKV("time", pindex->GetBlockTime());
1978 if (pindex->nChainTx) {
1979 ret.pushKV("txcount", pindex->nChainTx);
1980 }
1981 ret.pushKV("window_final_block_hash",
1982 pindex->GetBlockHash().GetHex());
1983 ret.pushKV("window_final_block_height", pindex->nHeight);
1984 ret.pushKV("window_block_count", blockcount);
1985 if (blockcount > 0) {
1986 ret.pushKV("window_interval", nTimeDiff);
1987 if (pindex->nChainTx != 0 && past_block.nChainTx != 0) {
1988 unsigned int window_tx_count =
1989 pindex->nChainTx - past_block.nChainTx;
1990 ret.pushKV("window_tx_count", window_tx_count);
1991 if (nTimeDiff > 0) {
1992 ret.pushKV("txrate",
1993 double(window_tx_count) / nTimeDiff);
1994 }
1995 }
1996 }
1997
1998 return ret;
1999 },
2000 };
2001}
2002
2003template <typename T>
2004static T CalculateTruncatedMedian(std::vector<T> &scores) {
2005 size_t size = scores.size();
2006 if (size == 0) {
2007 return T();
2008 }
2009
2010 std::sort(scores.begin(), scores.end());
2011 if (size % 2 == 0) {
2012 return (scores[size / 2 - 1] + scores[size / 2]) / 2;
2013 } else {
2014 return scores[size / 2];
2015 }
2016}
2017
2018template <typename T> static inline bool SetHasKeys(const std::set<T> &set) {
2019 return false;
2020}
2021template <typename T, typename Tk, typename... Args>
2022static inline bool SetHasKeys(const std::set<T> &set, const Tk &key,
2023 const Args &...args) {
2024 return (set.count(key) != 0) || SetHasKeys(set, args...);
2025}
2026
2027// outpoint (needed for the utxo index) + nHeight + fCoinBase
2028static constexpr size_t PER_UTXO_OVERHEAD =
2029 sizeof(COutPoint) + sizeof(uint32_t) + sizeof(bool);
2030
2032 const auto &ticker = Currency::get().ticker;
2033 return RPCHelpMan{
2034 "getblockstats",
2035 "Compute per block statistics for a given window. All amounts are "
2036 "in " +
2037 ticker +
2038 ".\n"
2039 "It won't work for some heights with pruning.\n",
2040 {
2041 {"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::NO,
2042 "The block hash or height of the target block",
2044 .type_str = {"", "string or numeric"}}},
2045 {"stats",
2047 RPCArg::DefaultHint{"all values"},
2048 "Values to plot (see result below)",
2049 {
2051 "Selected statistic"},
2053 "Selected statistic"},
2054 },
2056 },
2057 RPCResult{
2059 "",
2060 "",
2061 {
2062 {RPCResult::Type::NUM, "avgfee", "Average fee in the block"},
2063 {RPCResult::Type::NUM, "avgfeerate",
2064 "Average feerate (in satoshis per virtual byte)"},
2065 {RPCResult::Type::NUM, "avgtxsize", "Average transaction size"},
2066 {RPCResult::Type::STR_HEX, "blockhash",
2067 "The block hash (to check for potential reorgs)"},
2068 {RPCResult::Type::NUM, "height", "The height of the block"},
2069 {RPCResult::Type::NUM, "ins",
2070 "The number of inputs (excluding coinbase)"},
2071 {RPCResult::Type::NUM, "maxfee", "Maximum fee in the block"},
2072 {RPCResult::Type::NUM, "maxfeerate",
2073 "Maximum feerate (in satoshis per virtual byte)"},
2074 {RPCResult::Type::NUM, "maxtxsize", "Maximum transaction size"},
2075 {RPCResult::Type::NUM, "medianfee",
2076 "Truncated median fee in the block"},
2077 {RPCResult::Type::NUM, "medianfeerate",
2078 "Truncated median feerate (in " + ticker + " per byte)"},
2079 {RPCResult::Type::NUM, "mediantime",
2080 "The block median time past"},
2081 {RPCResult::Type::NUM, "mediantxsize",
2082 "Truncated median transaction size"},
2083 {RPCResult::Type::NUM, "minfee", "Minimum fee in the block"},
2084 {RPCResult::Type::NUM, "minfeerate",
2085 "Minimum feerate (in satoshis per virtual byte)"},
2086 {RPCResult::Type::NUM, "mintxsize", "Minimum transaction size"},
2087 {RPCResult::Type::NUM, "outs", "The number of outputs"},
2088 {RPCResult::Type::NUM, "subsidy", "The block subsidy"},
2089 {RPCResult::Type::NUM, "time", "The block time"},
2090 {RPCResult::Type::NUM, "total_out",
2091 "Total amount in all outputs (excluding coinbase and thus "
2092 "reward [ie subsidy + totalfee])"},
2093 {RPCResult::Type::NUM, "total_size",
2094 "Total size of all non-coinbase transactions"},
2095 {RPCResult::Type::NUM, "totalfee", "The fee total"},
2096 {RPCResult::Type::NUM, "txs",
2097 "The number of transactions (including coinbase)"},
2098 {RPCResult::Type::NUM, "utxo_increase",
2099 "The increase/decrease in the number of unspent outputs"},
2100 {RPCResult::Type::NUM, "utxo_size_inc",
2101 "The increase/decrease in size for the utxo index (not "
2102 "discounting op_return and similar)"},
2103 }},
2106 "getblockstats",
2107 R"('"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"' '["minfeerate","avgfeerate"]')") +
2108 HelpExampleCli("getblockstats",
2109 R"(1000 '["minfeerate","avgfeerate"]')") +
2111 "getblockstats",
2112 R"("00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09", ["minfeerate","avgfeerate"])") +
2113 HelpExampleRpc("getblockstats",
2114 R"(1000, ["minfeerate","avgfeerate"])")},
2115 [&](const RPCHelpMan &self, const Config &config,
2116 const JSONRPCRequest &request) -> UniValue {
2117 ChainstateManager &chainman = EnsureAnyChainman(request.context);
2118 const CBlockIndex &pindex{*CHECK_NONFATAL(
2119 ParseHashOrHeight(request.params[0], chainman))};
2120
2121 std::set<std::string> stats;
2122 if (!request.params[1].isNull()) {
2123 const UniValue stats_univalue = request.params[1].get_array();
2124 for (unsigned int i = 0; i < stats_univalue.size(); i++) {
2125 const std::string stat = stats_univalue[i].get_str();
2126 stats.insert(stat);
2127 }
2128 }
2129
2130 const CBlock &block = GetBlockChecked(chainman.m_blockman, pindex);
2131 const CBlockUndo &blockUndo =
2132 GetUndoChecked(chainman.m_blockman, pindex);
2133
2134 // Calculate everything if nothing selected (default)
2135 const bool do_all = stats.size() == 0;
2136 const bool do_mediantxsize =
2137 do_all || stats.count("mediantxsize") != 0;
2138 const bool do_medianfee = do_all || stats.count("medianfee") != 0;
2139 const bool do_medianfeerate =
2140 do_all || stats.count("medianfeerate") != 0;
2141 const bool loop_inputs =
2142 do_all || do_medianfee || do_medianfeerate ||
2143 SetHasKeys(stats, "utxo_size_inc", "totalfee", "avgfee",
2144 "avgfeerate", "minfee", "maxfee", "minfeerate",
2145 "maxfeerate");
2146 const bool loop_outputs =
2147 do_all || loop_inputs || stats.count("total_out");
2148 const bool do_calculate_size =
2149 do_mediantxsize || loop_inputs ||
2150 SetHasKeys(stats, "total_size", "avgtxsize", "mintxsize",
2151 "maxtxsize");
2152
2153 const int64_t blockMaxSize = config.GetMaxBlockSize();
2154 Amount maxfee = Amount::zero();
2155 Amount maxfeerate = Amount::zero();
2156 Amount minfee = MAX_MONEY;
2157 Amount minfeerate = MAX_MONEY;
2158 Amount total_out = Amount::zero();
2159 Amount totalfee = Amount::zero();
2160 int64_t inputs = 0;
2161 int64_t maxtxsize = 0;
2162 int64_t mintxsize = blockMaxSize;
2163 int64_t outputs = 0;
2164 int64_t total_size = 0;
2165 int64_t utxo_size_inc = 0;
2166 std::vector<Amount> fee_array;
2167 std::vector<Amount> feerate_array;
2168 std::vector<int64_t> txsize_array;
2169
2170 for (size_t i = 0; i < block.vtx.size(); ++i) {
2171 const auto &tx = block.vtx.at(i);
2172 outputs += tx->vout.size();
2173 Amount tx_total_out = Amount::zero();
2174 if (loop_outputs) {
2175 for (const CTxOut &out : tx->vout) {
2176 tx_total_out += out.nValue;
2177 utxo_size_inc +=
2179 }
2180 }
2181
2182 if (tx->IsCoinBase()) {
2183 continue;
2184 }
2185
2186 // Don't count coinbase's fake input
2187 inputs += tx->vin.size();
2188 // Don't count coinbase reward
2189 total_out += tx_total_out;
2190
2191 int64_t tx_size = 0;
2192 if (do_calculate_size) {
2193 tx_size = tx->GetTotalSize();
2194 if (do_mediantxsize) {
2195 txsize_array.push_back(tx_size);
2196 }
2197 maxtxsize = std::max(maxtxsize, tx_size);
2198 mintxsize = std::min(mintxsize, tx_size);
2199 total_size += tx_size;
2200 }
2201
2202 if (loop_inputs) {
2203 Amount tx_total_in = Amount::zero();
2204 const auto &txundo = blockUndo.vtxundo.at(i - 1);
2205 for (const Coin &coin : txundo.vprevout) {
2206 const CTxOut &prevoutput = coin.GetTxOut();
2207
2208 tx_total_in += prevoutput.nValue;
2209 utxo_size_inc -=
2210 GetSerializeSize(prevoutput) + PER_UTXO_OVERHEAD;
2211 }
2212
2213 Amount txfee = tx_total_in - tx_total_out;
2214 CHECK_NONFATAL(MoneyRange(txfee));
2215 if (do_medianfee) {
2216 fee_array.push_back(txfee);
2217 }
2218 maxfee = std::max(maxfee, txfee);
2219 minfee = std::min(minfee, txfee);
2220 totalfee += txfee;
2221
2222 Amount feerate = txfee / tx_size;
2223 if (do_medianfeerate) {
2224 feerate_array.push_back(feerate);
2225 }
2226 maxfeerate = std::max(maxfeerate, feerate);
2227 minfeerate = std::min(minfeerate, feerate);
2228 }
2229 }
2230
2231 UniValue ret_all(UniValue::VOBJ);
2232 ret_all.pushKV("avgfee",
2233 block.vtx.size() > 1
2234 ? (totalfee / int((block.vtx.size() - 1)))
2235 : Amount::zero());
2236 ret_all.pushKV("avgfeerate", total_size > 0
2237 ? (totalfee / total_size)
2238 : Amount::zero());
2239 ret_all.pushKV("avgtxsize",
2240 (block.vtx.size() > 1)
2241 ? total_size / (block.vtx.size() - 1)
2242 : 0);
2243 ret_all.pushKV("blockhash", pindex.GetBlockHash().GetHex());
2244 ret_all.pushKV("height", (int64_t)pindex.nHeight);
2245 ret_all.pushKV("ins", inputs);
2246 ret_all.pushKV("maxfee", maxfee);
2247 ret_all.pushKV("maxfeerate", maxfeerate);
2248 ret_all.pushKV("maxtxsize", maxtxsize);
2249 ret_all.pushKV("medianfee", CalculateTruncatedMedian(fee_array));
2250 ret_all.pushKV("medianfeerate",
2251 CalculateTruncatedMedian(feerate_array));
2252 ret_all.pushKV("mediantime", pindex.GetMedianTimePast());
2253 ret_all.pushKV("mediantxsize",
2254 CalculateTruncatedMedian(txsize_array));
2255 ret_all.pushKV("minfee",
2256 minfee == MAX_MONEY ? Amount::zero() : minfee);
2257 ret_all.pushKV("minfeerate", minfeerate == MAX_MONEY
2258 ? Amount::zero()
2259 : minfeerate);
2260 ret_all.pushKV("mintxsize",
2261 mintxsize == blockMaxSize ? 0 : mintxsize);
2262 ret_all.pushKV("outs", outputs);
2263 ret_all.pushKV("subsidy", GetBlockSubsidy(pindex.nHeight,
2264 chainman.GetConsensus()));
2265 ret_all.pushKV("time", pindex.GetBlockTime());
2266 ret_all.pushKV("total_out", total_out);
2267 ret_all.pushKV("total_size", total_size);
2268 ret_all.pushKV("totalfee", totalfee);
2269 ret_all.pushKV("txs", (int64_t)block.vtx.size());
2270 ret_all.pushKV("utxo_increase", outputs - inputs);
2271 ret_all.pushKV("utxo_size_inc", utxo_size_inc);
2272
2273 if (do_all) {
2274 return ret_all;
2275 }
2276
2278 for (const std::string &stat : stats) {
2279 const UniValue &value = ret_all[stat];
2280 if (value.isNull()) {
2281 throw JSONRPCError(
2283 strprintf("Invalid selected statistic %s", stat));
2284 }
2285 ret.pushKV(stat, value);
2286 }
2287 return ret;
2288 },
2289 };
2290}
2291
2292namespace {
2294static bool FindScriptPubKey(std::atomic<int> &scan_progress,
2295 const std::atomic<bool> &should_abort,
2296 int64_t &count, CCoinsViewCursor *cursor,
2297 const std::set<CScript> &needles,
2298 std::map<COutPoint, Coin> &out_results,
2299 std::function<void()> &interruption_point) {
2300 scan_progress = 0;
2301 count = 0;
2302 while (cursor->Valid()) {
2303 COutPoint key;
2304 Coin coin;
2305 if (!cursor->GetKey(key) || !cursor->GetValue(coin)) {
2306 return false;
2307 }
2308 if (++count % 8192 == 0) {
2309 interruption_point();
2310 if (should_abort) {
2311 // allow to abort the scan via the abort reference
2312 return false;
2313 }
2314 }
2315 if (count % 256 == 0) {
2316 // update progress reference every 256 item
2317 const TxId &txid = key.GetTxId();
2318 uint32_t high = 0x100 * *txid.begin() + *(txid.begin() + 1);
2319 scan_progress = int(high * 100.0 / 65536.0 + 0.5);
2320 }
2321 if (needles.count(coin.GetTxOut().scriptPubKey)) {
2322 out_results.emplace(key, coin);
2323 }
2324 cursor->Next();
2325 }
2326 scan_progress = 100;
2327 return true;
2328}
2329} // namespace
2330
2332static std::atomic<int> g_scan_progress;
2333static std::atomic<bool> g_scan_in_progress;
2334static std::atomic<bool> g_should_abort_scan;
2336private:
2337 bool m_could_reserve{false};
2338
2339public:
2340 explicit CoinsViewScanReserver() = default;
2341
2342 bool reserve() {
2344 if (g_scan_in_progress.exchange(true)) {
2345 return false;
2346 }
2347 m_could_reserve = true;
2348 return true;
2349 }
2350
2352 if (m_could_reserve) {
2353 g_scan_in_progress = false;
2354 }
2355 }
2356};
2357
2359 const auto &ticker = Currency::get().ticker;
2360 return RPCHelpMan{
2361 "scantxoutset",
2362 "Scans the unspent transaction output set for entries that match "
2363 "certain output descriptors.\n"
2364 "Examples of output descriptors are:\n"
2365 " addr(<address>) Outputs whose scriptPubKey "
2366 "corresponds to the specified address (does not include P2PK)\n"
2367 " raw(<hex script>) Outputs whose scriptPubKey "
2368 "equals the specified hex scripts\n"
2369 " combo(<pubkey>) P2PK and P2PKH outputs for "
2370 "the given pubkey\n"
2371 " pkh(<pubkey>) P2PKH outputs for the given "
2372 "pubkey\n"
2373 " sh(multi(<n>,<pubkey>,<pubkey>,...)) P2SH-multisig outputs for "
2374 "the given threshold and pubkeys\n"
2375 "\nIn the above, <pubkey> either refers to a fixed public key in "
2376 "hexadecimal notation, or to an xpub/xprv optionally followed by one\n"
2377 "or more path elements separated by \"/\", and optionally ending in "
2378 "\"/*\" (unhardened), or \"/*'\" or \"/*h\" (hardened) to specify all\n"
2379 "unhardened or hardened child keys.\n"
2380 "In the latter case, a range needs to be specified by below if "
2381 "different from 1000.\n"
2382 "For more information on output descriptors, see the documentation in "
2383 "the doc/descriptors.md file.\n",
2384 {
2386 "The action to execute\n"
2387 " \"start\" for starting a "
2388 "scan\n"
2389 " \"abort\" for aborting the "
2390 "current scan (returns true when abort was successful)\n"
2391 " \"status\" for "
2392 "progress report (in %) of the current scan"},
2393 {"scanobjects",
2396 "Array of scan objects. Required for \"start\" action\n"
2397 " Every scan object is either a "
2398 "string descriptor or an object:",
2399 {
2401 "An output descriptor"},
2402 {
2403 "",
2406 "An object with output descriptor and metadata",
2407 {
2409 "An output descriptor"},
2410 {"range", RPCArg::Type::RANGE, RPCArg::Default{1000},
2411 "The range of HD chain indexes to explore (either "
2412 "end or [begin,end])"},
2413 },
2414 },
2415 },
2416 RPCArgOptions{.oneline_description = "[scanobjects,...]"}},
2417 },
2418 {
2419 RPCResult{"When action=='abort'", RPCResult::Type::BOOL, "", ""},
2420 RPCResult{"When action=='status' and no scan is in progress",
2421 RPCResult::Type::NONE, "", ""},
2422 RPCResult{
2423 "When action=='status' and scan is in progress",
2425 "",
2426 "",
2427 {
2428 {RPCResult::Type::NUM, "progress", "The scan progress"},
2429 }},
2430 RPCResult{
2431 "When action=='start'",
2433 "",
2434 "",
2435 {
2436 {RPCResult::Type::BOOL, "success",
2437 "Whether the scan was completed"},
2438 {RPCResult::Type::NUM, "txouts",
2439 "The number of unspent transaction outputs scanned"},
2440 {RPCResult::Type::NUM, "height",
2441 "The current block height (index)"},
2442 {RPCResult::Type::STR_HEX, "bestblock",
2443 "The hash of the block at the tip of the chain"},
2445 "unspents",
2446 "",
2447 {
2449 "",
2450 "",
2451 {
2452 {RPCResult::Type::STR_HEX, "txid",
2453 "The transaction id"},
2454 {RPCResult::Type::NUM, "vout", "The vout value"},
2455 {RPCResult::Type::STR_HEX, "scriptPubKey",
2456 "The script key"},
2457 {RPCResult::Type::STR, "desc",
2458 "A specialized descriptor for the matched "
2459 "scriptPubKey"},
2460 {RPCResult::Type::STR_AMOUNT, "amount",
2461 "The total amount in " + ticker +
2462 " of the unspent output"},
2463 {RPCResult::Type::BOOL, "coinbase",
2464 "Whether this is a coinbase output"},
2465 {RPCResult::Type::NUM, "height",
2466 "Height of the unspent transaction output"},
2467 }},
2468 }},
2469 {RPCResult::Type::STR_AMOUNT, "total_amount",
2470 "The total amount of all found unspent outputs in " +
2471 ticker},
2472 }},
2473 },
2474 RPCExamples{""},
2475 [&](const RPCHelpMan &self, const Config &config,
2476 const JSONRPCRequest &request) -> UniValue {
2477 UniValue result(UniValue::VOBJ);
2478 const auto action{self.Arg<std::string>("action")};
2479 if (action == "status") {
2480 CoinsViewScanReserver reserver;
2481 if (reserver.reserve()) {
2482 // no scan in progress
2483 return NullUniValue;
2484 }
2485 result.pushKV("progress", g_scan_progress.load());
2486 return result;
2487 } else if (action == "abort") {
2488 CoinsViewScanReserver reserver;
2489 if (reserver.reserve()) {
2490 // reserve was possible which means no scan was running
2491 return false;
2492 }
2493 // set the abort flag
2494 g_should_abort_scan = true;
2495 return true;
2496 } else if (action == "start") {
2497 CoinsViewScanReserver reserver;
2498 if (!reserver.reserve()) {
2500 "Scan already in progress, use action "
2501 "\"abort\" or \"status\"");
2502 }
2503
2504 if (request.params.size() < 2) {
2506 "scanobjects argument is required for "
2507 "the start action");
2508 }
2509
2510 std::set<CScript> needles;
2511 std::map<CScript, std::string> descriptors;
2512 Amount total_in = Amount::zero();
2513
2514 // loop through the scan objects
2515 for (const UniValue &scanobject :
2516 request.params[1].get_array().getValues()) {
2517 FlatSigningProvider provider;
2518 auto scripts =
2519 EvalDescriptorStringOrObject(scanobject, provider);
2520 for (CScript &script : scripts) {
2521 std::string inferred =
2522 InferDescriptor(script, provider)->ToString();
2523 needles.emplace(script);
2524 descriptors.emplace(std::move(script),
2525 std::move(inferred));
2526 }
2527 }
2528
2529 // Scan the unspent transaction output set for inputs
2530 UniValue unspents(UniValue::VARR);
2531 std::vector<CTxOut> input_txos;
2532 std::map<COutPoint, Coin> coins;
2533 g_should_abort_scan = false;
2534 g_scan_progress = 0;
2535 int64_t count = 0;
2536 std::unique_ptr<CCoinsViewCursor> pcursor;
2537 const CBlockIndex *tip;
2538 NodeContext &node = EnsureAnyNodeContext(request.context);
2539 {
2541 LOCK(cs_main);
2542 Chainstate &active_chainstate = chainman.ActiveChainstate();
2543 active_chainstate.ForceFlushStateToDisk();
2544 pcursor = CHECK_NONFATAL(std::unique_ptr<CCoinsViewCursor>(
2545 active_chainstate.CoinsDB().Cursor()));
2546 tip = CHECK_NONFATAL(active_chainstate.m_chain.Tip());
2547 }
2548 bool res = FindScriptPubKey(
2549 g_scan_progress, g_should_abort_scan, count, pcursor.get(),
2550 needles, coins, node.rpc_interruption_point);
2551 result.pushKV("success", res);
2552 result.pushKV("txouts", count);
2553 result.pushKV("height", tip->nHeight);
2554 result.pushKV("bestblock", tip->GetBlockHash().GetHex());
2555
2556 for (const auto &it : coins) {
2557 const COutPoint &outpoint = it.first;
2558 const Coin &coin = it.second;
2559 const CTxOut &txo = coin.GetTxOut();
2560 input_txos.push_back(txo);
2561 total_in += txo.nValue;
2562
2563 UniValue unspent(UniValue::VOBJ);
2564 unspent.pushKV("txid", outpoint.GetTxId().GetHex());
2565 unspent.pushKV("vout", int32_t(outpoint.GetN()));
2566 unspent.pushKV("scriptPubKey", HexStr(txo.scriptPubKey));
2567 unspent.pushKV("desc", descriptors[txo.scriptPubKey]);
2568 unspent.pushKV("amount", txo.nValue);
2569 unspent.pushKV("coinbase", coin.IsCoinBase());
2570 unspent.pushKV("height", int32_t(coin.GetHeight()));
2571
2572 unspents.push_back(std::move(unspent));
2573 }
2574 result.pushKV("unspents", std::move(unspents));
2575 result.pushKV("total_amount", total_in);
2576 } else {
2578 strprintf("Invalid action '%s'", action));
2579 }
2580 return result;
2581 },
2582 };
2583}
2584
2586 return RPCHelpMan{
2587 "getblockfilter",
2588 "Retrieve a BIP 157 content filter for a particular block.\n",
2589 {
2591 "The hash of the block"},
2592 {"filtertype", RPCArg::Type::STR, RPCArg::Default{"basic"},
2593 "The type name of the filter"},
2594 },
2596 "",
2597 "",
2598 {
2599 {RPCResult::Type::STR_HEX, "filter",
2600 "the hex-encoded filter data"},
2601 {RPCResult::Type::STR_HEX, "header",
2602 "the hex-encoded filter header"},
2603 }},
2605 HelpExampleCli("getblockfilter",
2606 "\"00000000c937983704a73af28acdec37b049d214a"
2607 "dbda81d7e2a3dd146f6ed09\" \"basic\"") +
2608 HelpExampleRpc("getblockfilter",
2609 "\"00000000c937983704a73af28acdec37b049d214adbda81d7"
2610 "e2a3dd146f6ed09\", \"basic\"")},
2611 [&](const RPCHelpMan &self, const Config &config,
2612 const JSONRPCRequest &request) -> UniValue {
2613 const BlockHash block_hash(
2614 ParseHashV(request.params[0], "blockhash"));
2615 std::string filtertype_name = "basic";
2616 if (!request.params[1].isNull()) {
2617 filtertype_name = request.params[1].get_str();
2618 }
2619
2620 BlockFilterType filtertype;
2621 if (!BlockFilterTypeByName(filtertype_name, filtertype)) {
2623 "Unknown filtertype");
2624 }
2625
2626 BlockFilterIndex *index = GetBlockFilterIndex(filtertype);
2627 if (!index) {
2629 "Index is not enabled for filtertype " +
2630 filtertype_name);
2631 }
2632
2633 const CBlockIndex *block_index;
2634 bool block_was_connected;
2635 {
2636 ChainstateManager &chainman =
2637 EnsureAnyChainman(request.context);
2638 LOCK(cs_main);
2639 block_index = chainman.m_blockman.LookupBlockIndex(block_hash);
2640 if (!block_index) {
2642 "Block not found");
2643 }
2644 block_was_connected =
2645 block_index->IsValid(BlockValidity::SCRIPTS);
2646 }
2647
2648 bool index_ready = index->BlockUntilSyncedToCurrentChain();
2649
2650 BlockFilter filter;
2651 uint256 filter_header;
2652 if (!index->LookupFilter(block_index, filter) ||
2653 !index->LookupFilterHeader(block_index, filter_header)) {
2654 int err_code;
2655 std::string errmsg = "Filter not found.";
2656
2657 if (!block_was_connected) {
2658 err_code = RPC_INVALID_ADDRESS_OR_KEY;
2659 errmsg += " Block was not connected to active chain.";
2660 } else if (!index_ready) {
2661 err_code = RPC_MISC_ERROR;
2662 errmsg += " Block filters are still in the process of "
2663 "being indexed.";
2664 } else {
2665 err_code = RPC_INTERNAL_ERROR;
2666 errmsg += " This error is unexpected and indicates index "
2667 "corruption.";
2668 }
2669
2670 throw JSONRPCError(err_code, errmsg);
2671 }
2672
2674 ret.pushKV("filter", HexStr(filter.GetEncodedFilter()));
2675 ret.pushKV("header", filter_header.GetHex());
2676 return ret;
2677 },
2678 };
2679}
2680
2687
2688public:
2689 NetworkDisable(CConnman &connman) : m_connman(connman) {
2693 "Network activity could not be suspended.");
2694 }
2695 };
2697};
2698
2707
2708public:
2711 const CBlockIndex &index)
2712 : m_chainman(chainman), m_avalanche(avalanche),
2713 m_invalidate_index(index) {
2716 };
2720 };
2721};
2722
2729 return RPCHelpMan{
2730 "dumptxoutset",
2731 "Write the serialized UTXO set to a file. This can be used in "
2732 "loadtxoutset afterwards if this snapshot height is supported in the "
2733 "chainparams as well.\n\n"
2734 "Unless the the \"latest\" type is requested, the node will roll back "
2735 "to the requested height and network activity will be suspended during "
2736 "this process. "
2737 "Because of this it is discouraged to interact with the node in any "
2738 "other way during the execution of this call to avoid inconsistent "
2739 "results and race conditions, particularly RPCs that interact with "
2740 "blockstorage.\n\n"
2741 "This call may take several minutes. Make sure to use no RPC timeout "
2742 "(bitcoin-cli -rpcclienttimeout=0)",
2743
2744 {
2746 "path to the output file. If relative, will be prefixed by "
2747 "datadir."},
2748 {"type", RPCArg::Type::STR, RPCArg::Default(""),
2749 "The type of snapshot to create. Can be \"latest\" to create a "
2750 "snapshot of the current UTXO set or \"rollback\" to temporarily "
2751 "roll back the state of the node to a historical block before "
2752 "creating the snapshot of a historical UTXO set. This parameter "
2753 "can be omitted if a separate \"rollback\" named parameter is "
2754 "specified indicating the height or hash of a specific historical "
2755 "block. If \"rollback\" is specified and separate \"rollback\" "
2756 "named parameter is not specified, this will roll back to the "
2757 "latest valid snapshot block that can currently be loaded with "
2758 "loadtxoutset."},
2759 {
2760 "options",
2763 "",
2764 {
2766 "Height or hash of the block to roll back to before "
2767 "creating the snapshot. Note: The further this number is "
2768 "from the tip, the longer this process will take. "
2769 "Consider setting a higher -rpcclienttimeout value in "
2770 "this case.",
2772 .type_str = {"", "string or numeric"}}},
2773 },
2774 },
2775 },
2777 "",
2778 "",
2779 {
2780 {RPCResult::Type::NUM, "coins_written",
2781 "the number of coins written in the snapshot"},
2782 {RPCResult::Type::STR_HEX, "base_hash",
2783 "the hash of the base of the snapshot"},
2784 {RPCResult::Type::NUM, "base_height",
2785 "the height of the base of the snapshot"},
2786 {RPCResult::Type::STR, "path",
2787 "the absolute path that the snapshot was written to"},
2788 {RPCResult::Type::STR_HEX, "txoutset_hash",
2789 "the hash of the UTXO set contents"},
2790 {RPCResult::Type::NUM, "nchaintx",
2791 "the number of transactions in the chain up to and "
2792 "including the base block"},
2793 }},
2794 RPCExamples{HelpExampleCli("-rpcclienttimeout=0 dumptxoutset",
2795 "utxo.dat latest") +
2796 HelpExampleCli("-rpcclienttimeout=0 dumptxoutset",
2797 "utxo.dat rollback") +
2798 HelpExampleCli("-rpcclienttimeout=0 -named dumptxoutset",
2799 R"(utxo.dat rollback=853456)")},
2800 [&](const RPCHelpMan &self, const Config &config,
2801 const JSONRPCRequest &request) -> UniValue {
2802 NodeContext &node = EnsureAnyNodeContext(request.context);
2803 const CBlockIndex *tip{WITH_LOCK(
2804 ::cs_main, return node.chainman->ActiveChain().Tip())};
2805 const CBlockIndex *target_index{nullptr};
2806 const std::string snapshot_type{self.Arg<std::string>("type")};
2807 const UniValue options{request.params[2].isNull()
2809 : request.params[2]};
2810 if (options.exists("rollback")) {
2811 if (!snapshot_type.empty() && snapshot_type != "rollback") {
2812 throw JSONRPCError(
2814 strprintf("Invalid snapshot type \"%s\" specified with "
2815 "rollback option",
2816 snapshot_type));
2817 }
2818 target_index =
2819 ParseHashOrHeight(options["rollback"], *node.chainman);
2820 } else if (snapshot_type == "rollback") {
2821 auto snapshot_heights =
2822 node.chainman->GetParams().GetAvailableSnapshotHeights();
2823 CHECK_NONFATAL(snapshot_heights.size() > 0);
2824 auto max_height = std::max_element(snapshot_heights.begin(),
2825 snapshot_heights.end());
2826 target_index = ParseHashOrHeight(*max_height, *node.chainman);
2827 } else if (snapshot_type == "latest") {
2828 target_index = tip;
2829 } else {
2830 throw JSONRPCError(
2832 strprintf("Invalid snapshot type \"%s\" specified. Please "
2833 "specify \"rollback\" or \"latest\"",
2834 snapshot_type));
2835 }
2836
2837 const ArgsManager &args{EnsureAnyArgsman(request.context)};
2838 const fs::path path = fsbridge::AbsPathJoin(
2839 args.GetDataDirNet(), fs::u8path(request.params[0].get_str()));
2840 // Write to a temporary path and then move into `path` on completion
2841 // to avoid confusion due to an interruption.
2842 const fs::path temppath = fsbridge::AbsPathJoin(
2843 args.GetDataDirNet(),
2844 fs::u8path(request.params[0].get_str() + ".incomplete"));
2845
2846 if (fs::exists(path)) {
2848 path.u8string() +
2849 " already exists. If you are sure this "
2850 "is what you want, "
2851 "move it out of the way first");
2852 }
2853
2854 FILE *file{fsbridge::fopen(temppath, "wb")};
2855 AutoFile afile{file};
2856
2857 CConnman &connman = EnsureConnman(node);
2858 const CBlockIndex *invalidate_index{nullptr};
2859 std::optional<NetworkDisable> disable_network;
2860 std::optional<TemporaryRollback> temporary_rollback;
2861
2862 // If the user wants to dump the txoutset of the current tip, we
2863 // don't have to roll back at all
2864 if (target_index != tip) {
2865 // If the node is running in pruned mode we ensure all necessary
2866 // block data is available before starting to roll back.
2867 if (node.chainman->m_blockman.IsPruneMode()) {
2868 LOCK(node.chainman->GetMutex());
2869 const CBlockIndex *current_tip{
2870 node.chainman->ActiveChain().Tip()};
2871 const CBlockIndex *first_block{
2872 node.chainman->m_blockman.GetFirstBlock(
2873 *current_tip,
2874 /*status_test=*/[](const BlockStatus &status) {
2875 return status.hasData() && status.hasUndo();
2876 })};
2877 if (first_block->nHeight > target_index->nHeight) {
2878 throw JSONRPCError(
2880 "Could not roll back to requested height since "
2881 "necessary block data is already pruned.");
2882 }
2883 }
2884
2885 // Suspend network activity for the duration of the process when
2886 // we are rolling back the chain to get a utxo set from a past
2887 // height. We do this so we don't punish peers that send us that
2888 // send us data that seems wrong in this temporary state. For
2889 // example a normal new block would be classified as a block
2890 // connecting an invalid block.
2891 // Skip if the network is already disabled because this
2892 // automatically re-enables the network activity at the end of
2893 // the process which may not be what the user wants.
2894 if (connman.GetNetworkActive()) {
2895 disable_network.emplace(connman);
2896 }
2897
2898 invalidate_index = WITH_LOCK(
2899 ::cs_main,
2900 return node.chainman->ActiveChain().Next(target_index));
2901 temporary_rollback.emplace(*node.chainman, node.avalanche.get(),
2902 *invalidate_index);
2903 }
2904
2905 Chainstate *chainstate;
2906 std::unique_ptr<CCoinsViewCursor> cursor;
2907 CCoinsStats stats;
2908 {
2909 // Lock the chainstate before calling PrepareUtxoSnapshot, to
2910 // be able to get a UTXO database cursor while the chain is
2911 // pointing at the target block. After that, release the lock
2912 // while calling WriteUTXOSnapshot. The cursor will remain
2913 // valid and be used by WriteUTXOSnapshot to write a consistent
2914 // snapshot even if the chainstate changes.
2915 LOCK(node.chainman->GetMutex());
2916 chainstate = &node.chainman->ActiveChainstate();
2917
2918 // In case there is any issue with a block being read from disk
2919 // we need to stop here, otherwise the dump could still be
2920 // created for the wrong height. The new tip could also not be
2921 // the target block if we have a stale sister block of
2922 // invalidate_index. This block (or a descendant) would be
2923 // activated as the new tip and we would not get to
2924 // new_tip_index.
2925 if (target_index != chainstate->m_chain.Tip()) {
2927 "Failed to roll back to requested height, "
2928 "reverting to tip.\n");
2929 throw JSONRPCError(
2931 "Could not roll back to requested height.");
2932 } else {
2933 std::tie(cursor, stats, tip) = PrepareUTXOSnapshot(
2934 *chainstate, node.rpc_interruption_point);
2935 }
2936 }
2937
2938 UniValue result =
2939 WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile,
2940 path, temppath, node.rpc_interruption_point);
2941 fs::rename(temppath, path);
2942
2943 return result;
2944 },
2945 };
2946}
2947
2948std::tuple<std::unique_ptr<CCoinsViewCursor>, CCoinsStats, const CBlockIndex *>
2950 const std::function<void()> &interruption_point) {
2951 std::unique_ptr<CCoinsViewCursor> pcursor;
2952 std::optional<CCoinsStats> maybe_stats;
2953 const CBlockIndex *tip;
2954
2955 {
2956 // We need to lock cs_main to ensure that the coinsdb isn't
2957 // written to between (i) flushing coins cache to disk
2958 // (coinsdb), (ii) getting stats based upon the coinsdb, and
2959 // (iii) constructing a cursor to the coinsdb for use in
2960 // WriteUTXOSnapshot.
2961 //
2962 // Cursors returned by leveldb iterate over snapshots, so the
2963 // contents of the pcursor will not be affected by simultaneous
2964 // writes during use below this block.
2965 //
2966 // See discussion here:
2967 // https://github.com/bitcoin/bitcoin/pull/15606#discussion_r274479369
2968 //
2970
2971 chainstate.ForceFlushStateToDisk();
2972
2973 maybe_stats = GetUTXOStats(&chainstate.CoinsDB(), chainstate.m_blockman,
2974 CoinStatsHashType::HASH_SERIALIZED,
2975 interruption_point);
2976 if (!maybe_stats) {
2977 throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to read UTXO set");
2978 }
2979
2980 pcursor =
2981 std::unique_ptr<CCoinsViewCursor>(chainstate.CoinsDB().Cursor());
2982 tip = CHECK_NONFATAL(
2983 chainstate.m_blockman.LookupBlockIndex(maybe_stats->hashBlock));
2984 }
2985
2986 return {std::move(pcursor), *CHECK_NONFATAL(maybe_stats), tip};
2987}
2988
2990 CCoinsStats *maybe_stats, const CBlockIndex *tip,
2991 AutoFile &afile, const fs::path &path,
2992 const fs::path &temppath,
2993 const std::function<void()> &interruption_point) {
2995 strprintf("writing UTXO snapshot at height %s (%s) to file %s (via %s)",
2996 tip->nHeight, tip->GetBlockHash().ToString(),
2997 fs::PathToString(path), fs::PathToString(temppath)));
2998
2999 SnapshotMetadata metadata{tip->GetBlockHash(), maybe_stats->coins_count};
3000
3001 afile << metadata;
3002
3003 COutPoint key;
3004 TxId last_txid;
3005 Coin coin;
3006 unsigned int iter{0};
3007 size_t written_coins_count{0};
3008 std::vector<std::pair<uint32_t, Coin>> coins;
3009
3010 // To reduce space the serialization format of the snapshot avoids
3011 // duplication of tx hashes. The code takes advantage of the guarantee by
3012 // leveldb that keys are lexicographically sorted.
3013 // In the coins vector we collect all coins that belong to a certain tx hash
3014 // (key.hash) and when we have them all (key.hash != last_hash) we write
3015 // them to file using the below lambda function.
3016 // See also https://github.com/bitcoin/bitcoin/issues/25675
3017 auto write_coins_to_file =
3018 [&](AutoFile &afile, const TxId &last_txid,
3019 const std::vector<std::pair<uint32_t, Coin>> &coins,
3020 size_t &written_coins_count) {
3021 afile << last_txid;
3022 WriteCompactSize(afile, coins.size());
3023 for (const auto &[n, coin_] : coins) {
3024 WriteCompactSize(afile, n);
3025 afile << coin_;
3026 ++written_coins_count;
3027 }
3028 };
3029
3030 pcursor->GetKey(key);
3031 last_txid = key.GetTxId();
3032 while (pcursor->Valid()) {
3033 if (iter % 5000 == 0) {
3034 interruption_point();
3035 }
3036 ++iter;
3037 if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
3038 if (key.GetTxId() != last_txid) {
3039 write_coins_to_file(afile, last_txid, coins,
3040 written_coins_count);
3041 last_txid = key.GetTxId();
3042 coins.clear();
3043 }
3044 coins.emplace_back(key.GetN(), coin);
3045 }
3046 pcursor->Next();
3047 }
3048
3049 if (!coins.empty()) {
3050 write_coins_to_file(afile, last_txid, coins, written_coins_count);
3051 }
3052
3053 CHECK_NONFATAL(written_coins_count == maybe_stats->coins_count);
3054
3055 afile.fclose();
3056
3057 UniValue result(UniValue::VOBJ);
3058 result.pushKV("coins_written", written_coins_count);
3059 result.pushKV("base_hash", tip->GetBlockHash().ToString());
3060 result.pushKV("base_height", tip->nHeight);
3061 result.pushKV("path", path.u8string());
3062 result.pushKV("txoutset_hash", maybe_stats->hashSerialized.ToString());
3063 result.pushKV("nchaintx", tip->nChainTx);
3064 return result;
3065}
3066
3068 AutoFile &afile, const fs::path &path,
3069 const fs::path &tmppath) {
3070 auto [cursor, stats, tip]{WITH_LOCK(
3071 ::cs_main,
3072 return PrepareUTXOSnapshot(chainstate, node.rpc_interruption_point))};
3073 return WriteUTXOSnapshot(chainstate, cursor.get(), &stats, tip, afile, path,
3074 tmppath, node.rpc_interruption_point);
3075}
3076
3078 return RPCHelpMan{
3079 "loadtxoutset",
3080 "Load the serialized UTXO set from a file.\n"
3081 "Once this snapshot is loaded, its contents will be deserialized into "
3082 "a second chainstate data structure, which is then used to sync to the "
3083 "network's tip. "
3084 "Meanwhile, the original chainstate will complete the initial block "
3085 "download process in the background, eventually validating up to the "
3086 "block that the snapshot is based upon.\n\n"
3087 "The result is a usable bitcoind instance that is current with the "
3088 "network tip in a matter of minutes rather than hours. UTXO snapshot "
3089 "are typically obtained from third-party sources (HTTP, torrent, etc.) "
3090 "which is reasonable since their contents are always checked by "
3091 "hash.\n\n"
3092 "This RPC is incompatible with the -chronik init option, and a node "
3093 "with multiple chainstates may not be restarted with -chronik. After "
3094 "the background validation is finished and the chainstates are merged, "
3095 "the node can be restarted again with Chronik.\n\n"
3096 "You can find more information on this process in the `assumeutxo` "
3097 "design document (https://www.bitcoinabc.org/doc/assumeutxo.html).",
3098 {
3100 "path to the snapshot file. If relative, will be prefixed by "
3101 "datadir."},
3102 },
3104 "",
3105 "",
3106 {
3107 {RPCResult::Type::NUM, "coins_loaded",
3108 "the number of coins loaded from the snapshot"},
3109 {RPCResult::Type::STR_HEX, "tip_hash",
3110 "the hash of the base of the snapshot"},
3111 {RPCResult::Type::NUM, "base_height",
3112 "the height of the base of the snapshot"},
3113 {RPCResult::Type::STR, "path",
3114 "the absolute path that the snapshot was loaded from"},
3115 }},
3117 HelpExampleCli("loadtxoutset -rpcclienttimeout=0", "utxo.dat")},
3118 [&](const RPCHelpMan &self, const Config &config,
3119 const JSONRPCRequest &request) -> UniValue {
3120 NodeContext &node = EnsureAnyNodeContext(request.context);
3123 const fs::path path{AbsPathForConfigVal(
3124 args, fs::u8path(self.Arg<std::string>("path")))};
3125
3126 if (args.GetBoolArg("-chronik", false)) {
3127 throw JSONRPCError(
3129 "loadtxoutset is not compatible with Chronik.");
3130 }
3131
3132 FILE *file{fsbridge::fopen(path, "rb")};
3133 AutoFile afile{file};
3134 if (afile.IsNull()) {
3136 "Couldn't open file " + path.u8string() +
3137 " for reading.");
3138 }
3139
3140 SnapshotMetadata metadata;
3141 try {
3142 afile >> metadata;
3143 } catch (const std::ios_base::failure &e) {
3144 throw JSONRPCError(
3146 strprintf("Unable to parse metadata: %s", e.what()));
3147 }
3148
3149 auto activation_result{
3150 chainman.ActivateSnapshot(afile, metadata, false)};
3151 if (!activation_result) {
3152 throw JSONRPCError(
3154 strprintf("Unable to load UTXO snapshot: %s. (%s)",
3155 util::ErrorString(activation_result).original,
3156 path.u8string()));
3157 }
3158
3159 CBlockIndex &snapshot_index{*CHECK_NONFATAL(*activation_result)};
3160
3161 // Because we can't provide historical blocks during tip or
3162 // background sync. Update local services to reflect we are a
3163 // limited peer until we are fully sync.
3164 node.connman->RemoveLocalServices(NODE_NETWORK);
3165 // Setting the limited state is usually redundant because the node
3166 // can always provide the last 288 blocks, but it doesn't hurt to
3167 // set it.
3168 node.connman->AddLocalServices(NODE_NETWORK_LIMITED);
3169
3170 UniValue result(UniValue::VOBJ);
3171 result.pushKV("coins_loaded", metadata.m_coins_count);
3172 result.pushKV("tip_hash", snapshot_index.GetBlockHash().ToString());
3173 result.pushKV("base_height", snapshot_index.nHeight);
3174 result.pushKV("path", fs::PathToString(path));
3175 return result;
3176 },
3177 };
3178}
3179
3180const std::vector<RPCResult> RPCHelpForChainstate{
3181 {RPCResult::Type::NUM, "blocks", "number of blocks in this chainstate"},
3182 {RPCResult::Type::STR_HEX, "bestblockhash", "blockhash of the tip"},
3183 {RPCResult::Type::NUM, "difficulty", "difficulty of the tip"},
3184 {RPCResult::Type::NUM, "verificationprogress",
3185 "progress towards the network tip"},
3186 {RPCResult::Type::STR_HEX, "snapshot_blockhash", /*optional=*/true,
3187 "the base block of the snapshot this chainstate is based on, if any"},
3188 {RPCResult::Type::NUM, "coins_db_cache_bytes", "size of the coinsdb cache"},
3189 {RPCResult::Type::NUM, "coins_tip_cache_bytes",
3190 "size of the coinstip cache"},
3191 {RPCResult::Type::BOOL, "validated",
3192 "whether the chainstate is fully validated. True if all blocks in the "
3193 "chainstate were validated, false if the chain is based on a snapshot and "
3194 "the snapshot has not yet been validated."},
3195
3196};
3197
3199 return RPCHelpMan{
3200 "getchainstates",
3201 "\nReturn information about chainstates.\n",
3202 {},
3204 "",
3205 "",
3206 {
3207 {RPCResult::Type::NUM, "headers",
3208 "the number of headers seen so far"},
3210 "chainstates",
3211 "list of the chainstates ordered by work, with the "
3212 "most-work (active) chainstate last",
3213 {
3215 }},
3216 }},
3217 RPCExamples{HelpExampleCli("getchainstates", "") +
3218 HelpExampleRpc("getchainstates", "")},
3219 [&](const RPCHelpMan &self, const Config &config,
3220 const JSONRPCRequest &request) -> UniValue {
3221 LOCK(cs_main);
3223
3224 ChainstateManager &chainman = EnsureAnyChainman(request.context);
3225
3226 auto make_chain_data =
3227 [&](const Chainstate &chainstate,
3228 bool validated) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
3231 if (!chainstate.m_chain.Tip()) {
3232 return data;
3233 }
3234 const CChain &chain = chainstate.m_chain;
3235 const CBlockIndex *tip = chain.Tip();
3236
3237 data.pushKV("blocks", chain.Height());
3238 data.pushKV("bestblockhash", tip->GetBlockHash().GetHex());
3239 data.pushKV("difficulty", GetDifficulty(*tip));
3240 data.pushKV(
3241 "verificationprogress",
3242 GuessVerificationProgress(Params().TxData(), tip));
3243 data.pushKV("coins_db_cache_bytes",
3244 chainstate.m_coinsdb_cache_size_bytes);
3245 data.pushKV("coins_tip_cache_bytes",
3246 chainstate.m_coinstip_cache_size_bytes);
3247 if (chainstate.m_from_snapshot_blockhash) {
3248 data.pushKV(
3249 "snapshot_blockhash",
3250 chainstate.m_from_snapshot_blockhash->ToString());
3251 }
3252 data.pushKV("validated", validated);
3253 return data;
3254 };
3255
3256 obj.pushKV("headers", chainman.m_best_header
3257 ? chainman.m_best_header->nHeight
3258 : -1);
3259
3260 const auto &chainstates = chainman.GetAll();
3261 UniValue obj_chainstates{UniValue::VARR};
3262 for (Chainstate *cs : chainstates) {
3263 obj_chainstates.push_back(
3264 make_chain_data(*cs, !cs->m_from_snapshot_blockhash ||
3265 chainstates.size() == 1));
3266 }
3267 obj.pushKV("chainstates", std::move(obj_chainstates));
3268 return obj;
3269 }};
3270}
3271
3273 // clang-format off
3274 static const CRPCCommand commands[] = {
3275 // category actor (function)
3276 // ------------------ ----------------------
3277 { "blockchain", getbestblockhash, },
3278 { "blockchain", getblock, },
3279 { "blockchain", getblockfrompeer, },
3280 { "blockchain", getblockchaininfo, },
3281 { "blockchain", getblockcount, },
3282 { "blockchain", getblockhash, },
3283 { "blockchain", getblockheader, },
3284 { "blockchain", getblockstats, },
3285 { "blockchain", getchaintips, },
3286 { "blockchain", getchaintxstats, },
3287 { "blockchain", getdifficulty, },
3288 { "blockchain", gettxout, },
3289 { "blockchain", gettxoutsetinfo, },
3290 { "blockchain", pruneblockchain, },
3291 { "blockchain", verifychain, },
3292 { "blockchain", preciousblock, },
3293 { "blockchain", scantxoutset, },
3294 { "blockchain", getblockfilter, },
3295 { "blockchain", dumptxoutset, },
3296 { "blockchain", loadtxoutset, },
3297 { "blockchain", getchainstates, },
3298
3299 /* Not shown in help */
3300 { "hidden", invalidateblock, },
3301 { "hidden", parkblock, },
3302 { "hidden", reconsiderblock, },
3303 { "hidden", syncwithvalidationinterfacequeue, },
3304 { "hidden", unparkblock, },
3305 { "hidden", waitfornewblock, },
3306 { "hidden", waitforblock, },
3307 { "hidden", waitforblockheight, },
3308 };
3309 // clang-format on
3310 for (const auto &c : commands) {
3311 t.appendCommand(c.name, &c);
3312 }
3313}
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:358
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:218
std::optional< Coin > GetCoin(const COutPoint &outpoint) 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:217
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:228
Abstract view on the open txout dataset.
Definition: coins.h:304
virtual BlockHash GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:16
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:652
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
GetCoin, returning whether it exists and is not spent.
Definition: txmempool.cpp:779
Definition: net.h:841
bool GetNetworkActive() const
Definition: net.h:933
void SetNetworkActive(bool active)
Definition: net.cpp:2473
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:655
VerifyDBResult VerifyDB(Chainstate &chainstate, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:739
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:846
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:838
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:898
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:865
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
Definition: validation.h:895
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:872
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:1001
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition: validation.h:796
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:1191
node::BlockMap & BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:1468
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:1442
kernel::Notifications & GetNotifications() const
Definition: validation.h:1299
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:1323
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1449
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1446
const CChainParams & GetParams() const
Definition: validation.h:1284
const Consensus::Params & GetConsensus() const
Definition: validation.h:1287
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:1443
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1408
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1332
A UTXO entry.
Definition: coins.h:31
uint32_t GetHeight() const
Definition: coins.h:48
bool IsCoinBase() const
Definition: coins.h:49
CTxOut & GetTxOut()
Definition: coins.h:52
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
@ VNULL
Definition: univalue.h:30
@ 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:365
@ NODE_NETWORK
Definition: protocol.h:342
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:99
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