Bitcoin ABC 0.32.5
P2P Digital Currency
blockchain.cpp
Go to the documentation of this file.
1// Copyright (c) 2010 Satoshi Nakamoto
2// Copyright (c) 2009-2019 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <rpc/blockchain.h>
7
9#include <blockfilter.h>
10#include <chain.h>
11#include <chainparams.h>
12#include <clientversion.h>
13#include <coins.h>
14#include <common/args.h>
15#include <config.h>
16#include <consensus/amount.h>
17#include <consensus/params.h>
19#include <core_io.h>
20#include <hash.h>
23#include <logging/timer.h>
24#include <net.h>
25#include <net_processing.h>
26#include <node/blockstorage.h>
27#include <node/coinstats.h>
28#include <node/context.h>
29#include <node/utxo_snapshot.h>
31#include <rpc/server.h>
32#include <rpc/server_util.h>
33#include <rpc/util.h>
34#include <script/descriptor.h>
35#include <serialize.h>
36#include <streams.h>
37#include <txdb.h>
38#include <txmempool.h>
39#include <undo.h>
40#include <util/check.h>
41#include <util/fs.h>
42#include <util/strencodings.h>
43#include <util/string.h>
44#include <util/translation.h>
45#include <validation.h>
46#include <validationinterface.h>
47#include <warnings.h>
48
49#include <condition_variable>
50#include <cstdint>
51#include <memory>
52#include <mutex>
53#include <optional>
54
57
62
65 int height;
66};
67
69static std::condition_variable cond_blockchange;
71
72std::tuple<std::unique_ptr<CCoinsViewCursor>, CCoinsStats, const CBlockIndex *>
74 const std::function<void()> &interruption_point = {})
76
79 CCoinsStats *maybe_stats, const CBlockIndex *tip,
80 AutoFile &afile, const fs::path &path,
81 const fs::path &temppath,
82 const std::function<void()> &interruption_point = {});
83
87double GetDifficulty(const CBlockIndex &blockindex) {
88 int nShift = (blockindex.nBits >> 24) & 0xff;
89 double dDiff = double(0x0000ffff) / double(blockindex.nBits & 0x00ffffff);
90
91 while (nShift < 29) {
92 dDiff *= 256.0;
93 nShift++;
94 }
95 while (nShift > 29) {
96 dDiff /= 256.0;
97 nShift--;
98 }
99
100 return dDiff;
101}
102
104 const CBlockIndex &blockindex,
105 const CBlockIndex *&next) {
106 next = tip.GetAncestor(blockindex.nHeight + 1);
107 if (next && next->pprev == &blockindex) {
108 return tip.nHeight - blockindex.nHeight + 1;
109 }
110 next = nullptr;
111 return &blockindex == &tip ? 1 : -1;
112}
113
114static const CBlockIndex *ParseHashOrHeight(const UniValue &param,
115 ChainstateManager &chainman) {
117 CChain &active_chain = chainman.ActiveChain();
118
119 if (param.isNum()) {
120 const int height{param.getInt<int>()};
121 if (height < 0) {
122 throw JSONRPCError(
124 strprintf("Target block height %d is negative", height));
125 }
126 const int current_tip{active_chain.Height()};
127 if (height > current_tip) {
128 throw JSONRPCError(
130 strprintf("Target block height %d after current tip %d", height,
131 current_tip));
132 }
133
134 return active_chain[height];
135 } else {
136 const BlockHash hash{ParseHashV(param, "hash_or_height")};
137 const CBlockIndex *pindex = chainman.m_blockman.LookupBlockIndex(hash);
138
139 if (!pindex) {
140 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
141 }
142
143 return pindex;
144 }
145}
147 const CBlockIndex &blockindex) {
148 // Serialize passed information without accessing chain state of the active
149 // chain!
150 // For performance reasons
152
153 UniValue result(UniValue::VOBJ);
154 result.pushKV("hash", blockindex.GetBlockHash().GetHex());
155 const CBlockIndex *pnext;
156 int confirmations = ComputeNextBlockAndDepth(tip, blockindex, pnext);
157 result.pushKV("confirmations", confirmations);
158 result.pushKV("height", blockindex.nHeight);
159 result.pushKV("version", blockindex.nVersion);
160 result.pushKV("versionHex", strprintf("%08x", blockindex.nVersion));
161 result.pushKV("merkleroot", blockindex.hashMerkleRoot.GetHex());
162 result.pushKV("time", blockindex.nTime);
163 result.pushKV("mediantime", blockindex.GetMedianTimePast());
164 result.pushKV("nonce", blockindex.nNonce);
165 result.pushKV("bits", strprintf("%08x", blockindex.nBits));
166 result.pushKV("difficulty", GetDifficulty(blockindex));
167 result.pushKV("chainwork", blockindex.nChainWork.GetHex());
168 result.pushKV("nTx", blockindex.nTx);
169
170 if (blockindex.pprev) {
171 result.pushKV("previousblockhash",
172 blockindex.pprev->GetBlockHash().GetHex());
173 }
174 if (pnext) {
175 result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex());
176 }
177 return result;
178}
179
180UniValue blockToJSON(BlockManager &blockman, const CBlock &block,
181 const CBlockIndex &tip, const CBlockIndex &blockindex,
182 TxVerbosity verbosity) {
183 UniValue result = blockheaderToJSON(tip, blockindex);
184
185 result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION));
187 switch (verbosity) {
189 for (const CTransactionRef &tx : block.vtx) {
190 txs.push_back(tx->GetId().GetHex());
191 }
192 break;
193
196 CBlockUndo blockUndo;
197 const bool is_not_pruned{WITH_LOCK(
198 ::cs_main, return !blockman.IsBlockPruned(blockindex))};
199 const bool have_undo{is_not_pruned && blockman.UndoReadFromDisk(
200 blockUndo, blockindex)};
201 for (size_t i = 0; i < block.vtx.size(); ++i) {
202 const CTransactionRef &tx = block.vtx.at(i);
203 // coinbase transaction (i == 0) doesn't have undo data
204 const CTxUndo *txundo = (have_undo && i > 0)
205 ? &blockUndo.vtxundo.at(i - 1)
206 : nullptr;
208 TxToUniv(*tx, BlockHash(), objTx, true, txundo, verbosity);
209 txs.push_back(std::move(objTx));
210 }
211 break;
212 }
213
214 result.pushKV("tx", std::move(txs));
215
216 return result;
217}
218
220 return RPCHelpMan{
221 "getblockcount",
222 "Returns the height of the most-work fully-validated chain.\n"
223 "The genesis block has height 0.\n",
224 {},
225 RPCResult{RPCResult::Type::NUM, "", "The current block count"},
226 RPCExamples{HelpExampleCli("getblockcount", "") +
227 HelpExampleRpc("getblockcount", "")},
228 [&](const RPCHelpMan &self, const Config &config,
229 const JSONRPCRequest &request) -> UniValue {
230 ChainstateManager &chainman = EnsureAnyChainman(request.context);
231 LOCK(cs_main);
232 return chainman.ActiveHeight();
233 },
234 };
235}
236
238 return RPCHelpMan{
239 "getbestblockhash",
240 "Returns the hash of the best (tip) block in the "
241 "most-work fully-validated chain.\n",
242 {},
243 RPCResult{RPCResult::Type::STR_HEX, "", "the block hash, hex-encoded"},
244 RPCExamples{HelpExampleCli("getbestblockhash", "") +
245 HelpExampleRpc("getbestblockhash", "")},
246 [&](const RPCHelpMan &self, const Config &config,
247 const JSONRPCRequest &request) -> UniValue {
248 ChainstateManager &chainman = EnsureAnyChainman(request.context);
249 LOCK(cs_main);
250 return chainman.ActiveTip()->GetBlockHash().GetHex();
251 },
252 };
253}
254
256 if (pindex) {
258 latestblock.hash = pindex->GetBlockHash();
259 latestblock.height = pindex->nHeight;
260 }
261 cond_blockchange.notify_all();
262}
263
265 return RPCHelpMan{
266 "waitfornewblock",
267 "Waits for a specific new block and returns useful info about it.\n"
268 "\nReturns the current block on timeout or exit.\n",
269 {
270 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0},
271 "Time in milliseconds to wait for a response. 0 indicates no "
272 "timeout."},
273 },
275 "",
276 "",
277 {
278 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
279 {RPCResult::Type::NUM, "height", "Block height"},
280 }},
281 RPCExamples{HelpExampleCli("waitfornewblock", "1000") +
282 HelpExampleRpc("waitfornewblock", "1000")},
283 [&](const RPCHelpMan &self, const Config &config,
284 const JSONRPCRequest &request) -> UniValue {
285 int timeout = 0;
286 if (!request.params[0].isNull()) {
287 timeout = request.params[0].getInt<int>();
288 }
289
290 CUpdatedBlock block;
291 {
293 block = latestblock;
294 if (timeout) {
295 cond_blockchange.wait_for(
296 lock, std::chrono::milliseconds(timeout),
298 return latestblock.height != block.height ||
299 latestblock.hash != block.hash ||
300 !IsRPCRunning();
301 });
302 } else {
303 cond_blockchange.wait(
304 lock,
306 return latestblock.height != block.height ||
307 latestblock.hash != block.hash ||
308 !IsRPCRunning();
309 });
310 }
311 block = latestblock;
312 }
314 ret.pushKV("hash", block.hash.GetHex());
315 ret.pushKV("height", block.height);
316 return ret;
317 },
318 };
319}
320
322 return RPCHelpMan{
323 "waitforblock",
324 "Waits for a specific new block and returns useful info about it.\n"
325 "\nReturns the current block on timeout or exit.\n",
326 {
328 "Block hash to wait for."},
329 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0},
330 "Time in milliseconds to wait for a response. 0 indicates no "
331 "timeout."},
332 },
334 "",
335 "",
336 {
337 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
338 {RPCResult::Type::NUM, "height", "Block height"},
339 }},
340 RPCExamples{HelpExampleCli("waitforblock",
341 "\"0000000000079f8ef3d2c688c244eb7a4570b24c9"
342 "ed7b4a8c619eb02596f8862\" 1000") +
343 HelpExampleRpc("waitforblock",
344 "\"0000000000079f8ef3d2c688c244eb7a4570b24c9"
345 "ed7b4a8c619eb02596f8862\", 1000")},
346 [&](const RPCHelpMan &self, const Config &config,
347 const JSONRPCRequest &request) -> UniValue {
348 int timeout = 0;
349
350 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
351
352 if (!request.params[1].isNull()) {
353 timeout = request.params[1].getInt<int>();
354 }
355
356 CUpdatedBlock block;
357 {
359 if (timeout) {
360 cond_blockchange.wait_for(
361 lock, std::chrono::milliseconds(timeout),
363 return latestblock.hash == hash || !IsRPCRunning();
364 });
365 } else {
366 cond_blockchange.wait(
367 lock,
369 return latestblock.hash == hash || !IsRPCRunning();
370 });
371 }
372 block = latestblock;
373 }
374
376 ret.pushKV("hash", block.hash.GetHex());
377 ret.pushKV("height", block.height);
378 return ret;
379 },
380 };
381}
382
384 return RPCHelpMan{
385 "waitforblockheight",
386 "Waits for (at least) block height and returns the height and "
387 "hash\nof the current tip.\n"
388 "\nReturns the current block on timeout or exit.\n",
389 {
391 "Block height to wait for."},
392 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0},
393 "Time in milliseconds to wait for a response. 0 indicates no "
394 "timeout."},
395 },
397 "",
398 "",
399 {
400 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
401 {RPCResult::Type::NUM, "height", "Block height"},
402 }},
403 RPCExamples{HelpExampleCli("waitforblockheight", "100 1000") +
404 HelpExampleRpc("waitforblockheight", "100, 1000")},
405 [&](const RPCHelpMan &self, const Config &config,
406 const JSONRPCRequest &request) -> UniValue {
407 int timeout = 0;
408
409 int height = request.params[0].getInt<int>();
410
411 if (!request.params[1].isNull()) {
412 timeout = request.params[1].getInt<int>();
413 }
414
415 CUpdatedBlock block;
416 {
418 if (timeout) {
419 cond_blockchange.wait_for(
420 lock, std::chrono::milliseconds(timeout),
422 return latestblock.height >= height ||
423 !IsRPCRunning();
424 });
425 } else {
426 cond_blockchange.wait(
427 lock,
429 return latestblock.height >= height ||
430 !IsRPCRunning();
431 });
432 }
433 block = latestblock;
434 }
436 ret.pushKV("hash", block.hash.GetHex());
437 ret.pushKV("height", block.height);
438 return ret;
439 },
440 };
441}
442
444 return RPCHelpMan{
445 "syncwithvalidationinterfacequeue",
446 "Waits for the validation interface queue to catch up on everything "
447 "that was there when we entered this function.\n",
448 {},
450 RPCExamples{HelpExampleCli("syncwithvalidationinterfacequeue", "") +
451 HelpExampleRpc("syncwithvalidationinterfacequeue", "")},
452 [&](const RPCHelpMan &self, const Config &config,
453 const JSONRPCRequest &request) -> UniValue {
455 return NullUniValue;
456 },
457 };
458}
459
461 return RPCHelpMan{
462 "getdifficulty",
463 "Returns the proof-of-work difficulty as a multiple of the minimum "
464 "difficulty.\n",
465 {},
467 "the proof-of-work difficulty as a multiple of the minimum "
468 "difficulty."},
469 RPCExamples{HelpExampleCli("getdifficulty", "") +
470 HelpExampleRpc("getdifficulty", "")},
471 [&](const RPCHelpMan &self, const Config &config,
472 const JSONRPCRequest &request) -> UniValue {
473 ChainstateManager &chainman = EnsureAnyChainman(request.context);
474 LOCK(cs_main);
475 return GetDifficulty(*CHECK_NONFATAL(chainman.ActiveTip()));
476 },
477 };
478}
479
481 return RPCHelpMan{
482 "getblockfrompeer",
483 "Attempt to fetch block from a given peer.\n"
484 "\nWe must have the header for this block, e.g. using submitheader.\n"
485 "The block will not have any undo data which can limit the usage of "
486 "the block data in a context where the undo data is needed.\n"
487 "Subsequent calls for the same block may cause the response from the "
488 "previous peer to be ignored.\n"
489 "\nReturns an empty JSON object if the request was successfully "
490 "scheduled.",
491 {
493 "The block hash to try to fetch"},
495 "The peer to fetch it from (see getpeerinfo for peer IDs)"},
496 },
497 RPCResult{RPCResult::Type::OBJ, "", /*optional=*/false, "", {}},
498 RPCExamples{HelpExampleCli("getblockfrompeer",
499 "\"00000000c937983704a73af28acdec37b049d214a"
500 "dbda81d7e2a3dd146f6ed09\" 0") +
501 HelpExampleRpc("getblockfrompeer",
502 "\"00000000c937983704a73af28acdec37b049d214a"
503 "dbda81d7e2a3dd146f6ed09\" 0")},
504 [&](const RPCHelpMan &self, const Config &config,
505 const JSONRPCRequest &request) -> UniValue {
506 const NodeContext &node = EnsureAnyNodeContext(request.context);
508 PeerManager &peerman = EnsurePeerman(node);
509
510 const BlockHash block_hash{
511 ParseHashV(request.params[0], "blockhash")};
512 const NodeId peer_id{request.params[1].getInt<int64_t>()};
513
514 const CBlockIndex *const index = WITH_LOCK(
515 cs_main,
516 return chainman.m_blockman.LookupBlockIndex(block_hash););
517
518 if (!index) {
519 throw JSONRPCError(RPC_MISC_ERROR, "Block header missing");
520 }
521
522 if (WITH_LOCK(::cs_main, return index->nStatus.hasData())) {
523 throw JSONRPCError(RPC_MISC_ERROR, "Block already downloaded");
524 }
525
526 if (const auto err{peerman.FetchBlock(config, peer_id, *index)}) {
527 throw JSONRPCError(RPC_MISC_ERROR, err.value());
528 }
529 return UniValue::VOBJ;
530 },
531 };
532}
533
535 return RPCHelpMan{
536 "getblockhash",
537 "Returns hash of block in best-block-chain at height provided.\n",
538 {
540 "The height index"},
541 },
542 RPCResult{RPCResult::Type::STR_HEX, "", "The block hash"},
543 RPCExamples{HelpExampleCli("getblockhash", "1000") +
544 HelpExampleRpc("getblockhash", "1000")},
545 [&](const RPCHelpMan &self, const Config &config,
546 const JSONRPCRequest &request) -> UniValue {
547 ChainstateManager &chainman = EnsureAnyChainman(request.context);
548 LOCK(cs_main);
549 const CChain &active_chain = chainman.ActiveChain();
550
551 int nHeight = request.params[0].getInt<int>();
552 if (nHeight < 0 || nHeight > active_chain.Height()) {
554 "Block height out of range");
555 }
556
557 const CBlockIndex *pblockindex = active_chain[nHeight];
558 return pblockindex->GetBlockHash().GetHex();
559 },
560 };
561}
562
564 return RPCHelpMan{
565 "getblockheader",
566 "If verbose is false, returns a string that is serialized, hex-encoded "
567 "data for blockheader 'hash'.\n"
568 "If verbose is true, returns an Object with information about "
569 "blockheader <hash>.\n",
570 {
572 "The block hash"},
573 {"verbose", RPCArg::Type::BOOL, RPCArg::Default{true},
574 "true for a json object, false for the hex-encoded data"},
575 },
576 {
577 RPCResult{
578 "for verbose = true",
580 "",
581 "",
582 {
584 "the block hash (same as provided)"},
585 {RPCResult::Type::NUM, "confirmations",
586 "The number of confirmations, or -1 if the block is not "
587 "on the main chain"},
588 {RPCResult::Type::NUM, "height",
589 "The block height or index"},
590 {RPCResult::Type::NUM, "version", "The block version"},
591 {RPCResult::Type::STR_HEX, "versionHex",
592 "The block version formatted in hexadecimal"},
593 {RPCResult::Type::STR_HEX, "merkleroot", "The merkle root"},
595 "The block time expressed in " + UNIX_EPOCH_TIME},
596 {RPCResult::Type::NUM_TIME, "mediantime",
597 "The median block time expressed in " + UNIX_EPOCH_TIME},
598 {RPCResult::Type::NUM, "nonce", "The nonce"},
599 {RPCResult::Type::STR_HEX, "bits", "The bits"},
600 {RPCResult::Type::NUM, "difficulty", "The difficulty"},
601 {RPCResult::Type::STR_HEX, "chainwork",
602 "Expected number of hashes required to produce the "
603 "current chain"},
604 {RPCResult::Type::NUM, "nTx",
605 "The number of transactions in the block"},
606 {RPCResult::Type::STR_HEX, "previousblockhash",
607 /* optional */ true,
608 "The hash of the previous block (if available)"},
609 {RPCResult::Type::STR_HEX, "nextblockhash",
610 /* optional */ true,
611 "The hash of the next block (if available)"},
612 }},
613 RPCResult{"for verbose=false", RPCResult::Type::STR_HEX, "",
614 "A string that is serialized, hex-encoded data for block "
615 "'hash'"},
616 },
617 RPCExamples{HelpExampleCli("getblockheader",
618 "\"00000000c937983704a73af28acdec37b049d214a"
619 "dbda81d7e2a3dd146f6ed09\"") +
620 HelpExampleRpc("getblockheader",
621 "\"00000000c937983704a73af28acdec37b049d214a"
622 "dbda81d7e2a3dd146f6ed09\"")},
623 [&](const RPCHelpMan &self, const Config &config,
624 const JSONRPCRequest &request) -> UniValue {
625 BlockHash hash(ParseHashV(request.params[0], "hash"));
626
627 bool fVerbose = true;
628 if (!request.params[1].isNull()) {
629 fVerbose = request.params[1].get_bool();
630 }
631
632 const CBlockIndex *pblockindex;
633 const CBlockIndex *tip;
634 {
635 ChainstateManager &chainman =
636 EnsureAnyChainman(request.context);
637 LOCK(cs_main);
638 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
639 tip = chainman.ActiveTip();
640 }
641
642 if (!pblockindex) {
644 "Block not found");
645 }
646
647 if (!fVerbose) {
648 DataStream ssBlock{};
649 ssBlock << pblockindex->GetBlockHeader();
650 std::string strHex = HexStr(ssBlock);
651 return strHex;
652 }
653
654 return blockheaderToJSON(*tip, *pblockindex);
655 },
656 };
657}
658
660 const CBlockIndex &blockindex) {
661 CBlock block;
662 {
663 LOCK(cs_main);
664 if (blockman.IsBlockPruned(blockindex)) {
666 "Block not available (pruned data)");
667 }
668 }
669
670 if (!blockman.ReadBlockFromDisk(block, blockindex)) {
671 // Block not found on disk. This could be because we have the block
672 // header in our index but not yet have the block or did not accept the
673 // block. Or if the block was pruned right after we released the lock
674 // above.
675 throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk");
676 }
677
678 return block;
679}
680
682 const CBlockIndex &blockindex) {
683 CBlockUndo blockUndo;
684
685 {
686 LOCK(cs_main);
687 if (blockman.IsBlockPruned(blockindex)) {
689 "Undo data not available (pruned data)");
690 }
691 }
692
693 if (!blockman.UndoReadFromDisk(blockUndo, blockindex)) {
694 throw JSONRPCError(RPC_MISC_ERROR, "Can't read undo data from disk");
695 }
696
697 return blockUndo;
698}
699
701 return RPCHelpMan{
702 "getblock",
703 "If verbosity is 0 or false, returns a string that is serialized, "
704 "hex-encoded data for block 'hash'.\n"
705 "If verbosity is 1 or true, returns an Object with information about "
706 "block <hash>.\n"
707 "If verbosity is 2, returns an Object with information about block "
708 "<hash> and information about each transaction.\n"
709 "If verbosity is 3, returns an Object with information about block "
710 "<hash> and information about each transaction, including prevout "
711 "information for inputs (only for unpruned blocks in the current best "
712 "chain).\n",
713 {
715 "The block hash"},
716 {"verbosity|verbose", RPCArg::Type::NUM, RPCArg::Default{1},
717 "0 for hex-encoded data, 1 for a json object, and 2 for json "
718 "object with transaction data",
720 },
721 {
722 RPCResult{"for verbosity = 0", RPCResult::Type::STR_HEX, "",
723 "A string that is serialized, hex-encoded data for block "
724 "'hash'"},
725 RPCResult{
726 "for verbosity = 1",
728 "",
729 "",
730 {
732 "the block hash (same as provided)"},
733 {RPCResult::Type::NUM, "confirmations",
734 "The number of confirmations, or -1 if the block is not "
735 "on the main chain"},
736 {RPCResult::Type::NUM, "size", "The block size"},
737 {RPCResult::Type::NUM, "height",
738 "The block height or index"},
739 {RPCResult::Type::NUM, "version", "The block version"},
740 {RPCResult::Type::STR_HEX, "versionHex",
741 "The block version formatted in hexadecimal"},
742 {RPCResult::Type::STR_HEX, "merkleroot", "The merkle root"},
744 "tx",
745 "The transaction ids",
746 {{RPCResult::Type::STR_HEX, "", "The transaction id"}}},
748 "The block time expressed in " + UNIX_EPOCH_TIME},
749 {RPCResult::Type::NUM_TIME, "mediantime",
750 "The median block time expressed in " + UNIX_EPOCH_TIME},
751 {RPCResult::Type::NUM, "nonce", "The nonce"},
752 {RPCResult::Type::STR_HEX, "bits", "The bits"},
753 {RPCResult::Type::NUM, "difficulty", "The difficulty"},
754 {RPCResult::Type::STR_HEX, "chainwork",
755 "Expected number of hashes required to produce the chain "
756 "up to this block (in hex)"},
757 {RPCResult::Type::NUM, "nTx",
758 "The number of transactions in the block"},
759 {RPCResult::Type::STR_HEX, "previousblockhash",
760 /* optional */ true,
761 "The hash of the previous block (if available)"},
762 {RPCResult::Type::STR_HEX, "nextblockhash",
763 /* optional */ true,
764 "The hash of the next block (if available)"},
765 }},
766 RPCResult{"for verbosity = 2",
768 "",
769 "",
770 {
772 "Same output as verbosity = 1"},
774 "tx",
775 "",
776 {
778 "",
779 "",
780 {
782 "The transactions in the format of the "
783 "getrawtransaction RPC. Different from "
784 "verbosity = 1 \"tx\" result"},
786 "The transaction fee in " +
788 ", omitted if block undo data is not "
789 "available"},
790 }},
791 }},
792 }},
793 },
795 HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d"
796 "214adbda81d7e2a3dd146f6ed09\"") +
797 HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d"
798 "214adbda81d7e2a3dd146f6ed09\"")},
799 [&](const RPCHelpMan &self, const Config &config,
800 const JSONRPCRequest &request) -> UniValue {
801 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
802
803 int verbosity = 1;
804 if (!request.params[1].isNull()) {
805 if (request.params[1].isNum()) {
806 verbosity = request.params[1].getInt<int>();
807 } else {
808 verbosity = request.params[1].get_bool() ? 1 : 0;
809 }
810 }
811
812 const CBlockIndex *pblockindex;
813 const CBlockIndex *tip;
814 ChainstateManager &chainman = EnsureAnyChainman(request.context);
815 {
816 LOCK(cs_main);
817 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
818 tip = chainman.ActiveTip();
819
820 if (!pblockindex) {
822 "Block not found");
823 }
824 }
825
826 const CBlock block =
827 GetBlockChecked(chainman.m_blockman, *pblockindex);
828
829 if (verbosity <= 0) {
831 ssBlock << block;
832 std::string strHex = HexStr(ssBlock);
833 return strHex;
834 }
835
836 TxVerbosity tx_verbosity;
837 if (verbosity == 1) {
838 tx_verbosity = TxVerbosity::SHOW_TXID;
839 } else if (verbosity == 2) {
840 tx_verbosity = TxVerbosity::SHOW_DETAILS;
841 } else {
843 }
844
845 return blockToJSON(chainman.m_blockman, block, *tip, *pblockindex,
846 tx_verbosity);
847 },
848 };
849}
850
851std::optional<int> GetPruneHeight(const BlockManager &blockman,
852 const CChain &chain) {
854
855 // Search for the last block missing block data or undo data. Don't let the
856 // search consider the genesis block, because the genesis block does not
857 // have undo data, but should not be considered pruned.
858 const CBlockIndex *first_block{chain[1]};
859 const CBlockIndex *chain_tip{chain.Tip()};
860
861 // If there are no blocks after the genesis block, or no blocks at all,
862 // nothing is pruned.
863 if (!first_block || !chain_tip) {
864 return std::nullopt;
865 }
866
867 // If the chain tip is pruned, everything is pruned.
868 if (!(chain_tip->nStatus.hasData() && chain_tip->nStatus.hasUndo())) {
869 return chain_tip->nHeight;
870 }
871
872 // Get first block with data, after the last block without data.
873 // This is the start of the unpruned range of blocks.
874 const CBlockIndex *first_unpruned{CHECK_NONFATAL(
875 blockman.GetFirstBlock(*chain_tip,
876 /*status_test=*/[](const BlockStatus &status) {
877 return status.hasData() && status.hasUndo();
878 }))};
879 if (first_unpruned == first_block) {
880 // All blocks between first_block and chain_tip have data, so nothing is
881 // pruned.
882 return std::nullopt;
883 }
884
885 // Block before the first unpruned block is the last pruned block.
886 return CHECK_NONFATAL(first_unpruned->pprev)->nHeight;
887}
888
890 return RPCHelpMan{
891 "pruneblockchain",
892 "",
893 {
895 "The block height to prune up to. May be set to a discrete "
896 "height, or to a " +
898 "\n"
899 " to prune blocks whose block time is at "
900 "least 2 hours older than the provided timestamp."},
901 },
902 RPCResult{RPCResult::Type::NUM, "", "Height of the last block pruned"},
903 RPCExamples{HelpExampleCli("pruneblockchain", "1000") +
904 HelpExampleRpc("pruneblockchain", "1000")},
905 [&](const RPCHelpMan &self, const Config &config,
906 const JSONRPCRequest &request) -> UniValue {
907 ChainstateManager &chainman = EnsureAnyChainman(request.context);
908 if (!chainman.m_blockman.IsPruneMode()) {
909 throw JSONRPCError(
911 "Cannot prune blocks because node is not in prune mode.");
912 }
913
914 LOCK(cs_main);
915 Chainstate &active_chainstate = chainman.ActiveChainstate();
916 CChain &active_chain = active_chainstate.m_chain;
917
918 int heightParam = request.params[0].getInt<int>();
919 if (heightParam < 0) {
921 "Negative block height.");
922 }
923
924 // Height value more than a billion is too high to be a block
925 // height, and too low to be a block time (corresponds to timestamp
926 // from Sep 2001).
927 if (heightParam > 1000000000) {
928 // Add a 2 hour buffer to include blocks which might have had
929 // old timestamps
930 const CBlockIndex *pindex = active_chain.FindEarliestAtLeast(
931 heightParam - TIMESTAMP_WINDOW, 0);
932 if (!pindex) {
934 "Could not find block with at least the "
935 "specified timestamp.");
936 }
937 heightParam = pindex->nHeight;
938 }
939
940 unsigned int height = (unsigned int)heightParam;
941 unsigned int chainHeight = (unsigned int)active_chain.Height();
942 if (chainHeight < config.GetChainParams().PruneAfterHeight()) {
944 "Blockchain is too short for pruning.");
945 } else if (height > chainHeight) {
946 throw JSONRPCError(
948 "Blockchain is shorter than the attempted prune height.");
949 } else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) {
951 "Attempt to prune blocks close to the tip. "
952 "Retaining the minimum number of blocks.\n");
953 height = chainHeight - MIN_BLOCKS_TO_KEEP;
954 }
955
956 PruneBlockFilesManual(active_chainstate, height);
957 return GetPruneHeight(chainman.m_blockman, active_chain)
958 .value_or(-1);
959 },
960 };
961}
962
963static CoinStatsHashType ParseHashType(const std::string &hash_type_input) {
964 if (hash_type_input == "hash_serialized") {
965 return CoinStatsHashType::HASH_SERIALIZED;
966 } else if (hash_type_input == "muhash") {
967 return CoinStatsHashType::MUHASH;
968 } else if (hash_type_input == "none") {
970 } else {
971 throw JSONRPCError(
973 strprintf("%s is not a valid hash_type", hash_type_input));
974 }
975}
976
978 return RPCHelpMan{
979 "gettxoutsetinfo",
980 "Returns statistics about the unspent transaction output set.\n"
981 "Note this call may take some time if you are not using "
982 "coinstatsindex.\n",
983 {
984 {"hash_type", RPCArg::Type::STR, RPCArg::Default{"hash_serialized"},
985 "Which UTXO set hash should be calculated. Options: "
986 "'hash_serialized' (the legacy algorithm), 'muhash', 'none'."},
988 "The block hash or height of the target height (only available "
989 "with coinstatsindex).",
991 .type_str = {"", "string or numeric"}}},
992 {"use_index", RPCArg::Type::BOOL, RPCArg::Default{true},
993 "Use coinstatsindex, if available."},
994 },
995 RPCResult{
997 "",
998 "",
999 {
1000 {RPCResult::Type::NUM, "height",
1001 "The current block height (index)"},
1002 {RPCResult::Type::STR_HEX, "bestblock",
1003 "The hash of the block at the tip of the chain"},
1004 {RPCResult::Type::NUM, "txouts",
1005 "The number of unspent transaction outputs"},
1006 {RPCResult::Type::NUM, "bogosize",
1007 "Database-independent, meaningless metric indicating "
1008 "the UTXO set size"},
1009 {RPCResult::Type::STR_HEX, "hash_serialized",
1010 /* optional */ true,
1011 "The serialized hash (only present if 'hash_serialized' "
1012 "hash_type is chosen)"},
1013 {RPCResult::Type::STR_HEX, "muhash", /* optional */ true,
1014 "The serialized hash (only present if 'muhash' "
1015 "hash_type is chosen)"},
1016 {RPCResult::Type::NUM, "transactions",
1017 "The number of transactions with unspent outputs (not "
1018 "available when coinstatsindex is used)"},
1019 {RPCResult::Type::NUM, "disk_size",
1020 "The estimated size of the chainstate on disk (not "
1021 "available when coinstatsindex is used)"},
1022 {RPCResult::Type::STR_AMOUNT, "total_amount",
1023 "The total amount"},
1024 {RPCResult::Type::STR_AMOUNT, "total_unspendable_amount",
1025 "The total amount of coins permanently excluded from the UTXO "
1026 "set (only available if coinstatsindex is used)"},
1028 "block_info",
1029 "Info on amounts in the block at this block height (only "
1030 "available if coinstatsindex is used)",
1031 {{RPCResult::Type::STR_AMOUNT, "prevout_spent",
1032 "Total amount of all prevouts spent in this block"},
1033 {RPCResult::Type::STR_AMOUNT, "coinbase",
1034 "Coinbase subsidy amount of this block"},
1035 {RPCResult::Type::STR_AMOUNT, "new_outputs_ex_coinbase",
1036 "Total amount of new outputs created by this block"},
1037 {RPCResult::Type::STR_AMOUNT, "unspendable",
1038 "Total amount of unspendable outputs created in this block"},
1040 "unspendables",
1041 "Detailed view of the unspendable categories",
1042 {
1043 {RPCResult::Type::STR_AMOUNT, "genesis_block",
1044 "The unspendable amount of the Genesis block subsidy"},
1046 "Transactions overridden by duplicates (no longer "
1047 "possible with BIP30)"},
1048 {RPCResult::Type::STR_AMOUNT, "scripts",
1049 "Amounts sent to scripts that are unspendable (for "
1050 "example OP_RETURN outputs)"},
1051 {RPCResult::Type::STR_AMOUNT, "unclaimed_rewards",
1052 "Fee rewards that miners did not claim in their "
1053 "coinbase transaction"},
1054 }}}},
1055 }},
1057 HelpExampleCli("gettxoutsetinfo", "") +
1058 HelpExampleCli("gettxoutsetinfo", R"("none")") +
1059 HelpExampleCli("gettxoutsetinfo", R"("none" 1000)") +
1061 "gettxoutsetinfo",
1062 R"("none" '"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"')") +
1063 HelpExampleRpc("gettxoutsetinfo", "") +
1064 HelpExampleRpc("gettxoutsetinfo", R"("none")") +
1065 HelpExampleRpc("gettxoutsetinfo", R"("none", 1000)") +
1067 "gettxoutsetinfo",
1068 R"("none", "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09")")},
1069 [&](const RPCHelpMan &self, const Config &config,
1070 const JSONRPCRequest &request) -> UniValue {
1072
1073 const CBlockIndex *pindex{nullptr};
1074 const CoinStatsHashType hash_type{
1075 request.params[0].isNull()
1076 ? CoinStatsHashType::HASH_SERIALIZED
1077 : ParseHashType(request.params[0].get_str())};
1078 bool index_requested =
1079 request.params[2].isNull() || request.params[2].get_bool();
1080
1081 NodeContext &node = EnsureAnyNodeContext(request.context);
1083 Chainstate &active_chainstate = chainman.ActiveChainstate();
1084 active_chainstate.ForceFlushStateToDisk();
1085
1086 CCoinsView *coins_view;
1087 BlockManager *blockman;
1088 {
1089 LOCK(::cs_main);
1090 coins_view = &active_chainstate.CoinsDB();
1091 blockman = &active_chainstate.m_blockman;
1092 pindex = blockman->LookupBlockIndex(coins_view->GetBestBlock());
1093 }
1094
1095 if (!request.params[1].isNull()) {
1096 if (!g_coin_stats_index) {
1098 "Querying specific block heights "
1099 "requires coinstatsindex");
1100 }
1101
1102 if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
1104 "hash_serialized hash type cannot be "
1105 "queried for a specific block");
1106 }
1107
1108 pindex = ParseHashOrHeight(request.params[1], chainman);
1109 }
1110
1111 if (index_requested && g_coin_stats_index) {
1112 if (!g_coin_stats_index->BlockUntilSyncedToCurrentChain()) {
1113 const IndexSummary summary{
1114 g_coin_stats_index->GetSummary()};
1115
1116 // If a specific block was requested and the index has
1117 // already synced past that height, we can return the data
1118 // already even though the index is not fully synced yet.
1119 if (pindex->nHeight > summary.best_block_height) {
1120 throw JSONRPCError(
1122 strprintf(
1123 "Unable to get data because coinstatsindex is "
1124 "still syncing. Current height: %d",
1125 summary.best_block_height));
1126 }
1127 }
1128 }
1129
1130 const std::optional<CCoinsStats> maybe_stats = GetUTXOStats(
1131 coins_view, *blockman, hash_type, node.rpc_interruption_point,
1132 pindex, index_requested);
1133 if (maybe_stats.has_value()) {
1134 const CCoinsStats &stats = maybe_stats.value();
1135 ret.pushKV("height", int64_t(stats.nHeight));
1136 ret.pushKV("bestblock", stats.hashBlock.GetHex());
1137 ret.pushKV("txouts", int64_t(stats.nTransactionOutputs));
1138 ret.pushKV("bogosize", int64_t(stats.nBogoSize));
1139 if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
1140 ret.pushKV("hash_serialized",
1141 stats.hashSerialized.GetHex());
1142 }
1143 if (hash_type == CoinStatsHashType::MUHASH) {
1144 ret.pushKV("muhash", stats.hashSerialized.GetHex());
1145 }
1146 CHECK_NONFATAL(stats.total_amount.has_value());
1147 ret.pushKV("total_amount", stats.total_amount.value());
1148 if (!stats.index_used) {
1149 ret.pushKV("transactions",
1150 static_cast<int64_t>(stats.nTransactions));
1151 ret.pushKV("disk_size", stats.nDiskSize);
1152 } else {
1153 ret.pushKV("total_unspendable_amount",
1155
1156 CCoinsStats prev_stats{};
1157 if (pindex->nHeight > 0) {
1158 const std::optional<CCoinsStats> maybe_prev_stats =
1159 GetUTXOStats(coins_view, *blockman, hash_type,
1160 node.rpc_interruption_point,
1161 pindex->pprev, index_requested);
1162 if (!maybe_prev_stats) {
1164 "Unable to read UTXO set");
1165 }
1166 prev_stats = maybe_prev_stats.value();
1167 }
1168
1169 UniValue block_info(UniValue::VOBJ);
1170 block_info.pushKV(
1171 "prevout_spent",
1173 prev_stats.total_prevout_spent_amount);
1174 block_info.pushKV("coinbase",
1175 stats.total_coinbase_amount -
1176 prev_stats.total_coinbase_amount);
1177 block_info.pushKV(
1178 "new_outputs_ex_coinbase",
1180 prev_stats.total_new_outputs_ex_coinbase_amount);
1181 block_info.pushKV("unspendable",
1183 prev_stats.total_unspendable_amount);
1184
1185 UniValue unspendables(UniValue::VOBJ);
1186 unspendables.pushKV(
1187 "genesis_block",
1189 prev_stats.total_unspendables_genesis_block);
1190 unspendables.pushKV(
1191 "bip30", stats.total_unspendables_bip30 -
1192 prev_stats.total_unspendables_bip30);
1193 unspendables.pushKV(
1194 "scripts", stats.total_unspendables_scripts -
1195 prev_stats.total_unspendables_scripts);
1196 unspendables.pushKV(
1197 "unclaimed_rewards",
1199 prev_stats.total_unspendables_unclaimed_rewards);
1200 block_info.pushKV("unspendables", std::move(unspendables));
1201
1202 ret.pushKV("block_info", std::move(block_info));
1203 }
1204 } else {
1206 "Unable to read UTXO set");
1207 }
1208 return ret;
1209 },
1210 };
1211}
1212
1214 return RPCHelpMan{
1215 "gettxout",
1216 "Returns details about an unspent transaction output.\n",
1217 {
1219 "The transaction id"},
1220 {"n", RPCArg::Type::NUM, RPCArg::Optional::NO, "vout number"},
1221 {"include_mempool", RPCArg::Type::BOOL, RPCArg::Default{true},
1222 "Whether to include the mempool. Note that an unspent output that "
1223 "is spent in the mempool won't appear."},
1224 },
1225 {
1226 RPCResult{"If the UTXO was not found", RPCResult::Type::NONE, "",
1227 ""},
1228 RPCResult{
1229 "Otherwise",
1231 "",
1232 "",
1233 {
1234 {RPCResult::Type::STR_HEX, "bestblock",
1235 "The hash of the block at the tip of the chain"},
1236 {RPCResult::Type::NUM, "confirmations",
1237 "The number of confirmations"},
1239 "The transaction value in " + Currency::get().ticker},
1241 "scriptPubKey",
1242 "",
1243 {
1244 {RPCResult::Type::STR_HEX, "asm", ""},
1245 {RPCResult::Type::STR_HEX, "hex", ""},
1246 {RPCResult::Type::NUM, "reqSigs",
1247 "Number of required signatures"},
1248 {RPCResult::Type::STR_HEX, "type",
1249 "The type, eg pubkeyhash"},
1251 "addresses",
1252 "array of eCash addresses",
1253 {{RPCResult::Type::STR, "address", "eCash address"}}},
1254 }},
1255 {RPCResult::Type::BOOL, "coinbase", "Coinbase or not"},
1256 }},
1257 },
1258 RPCExamples{"\nGet unspent transactions\n" +
1259 HelpExampleCli("listunspent", "") + "\nView the details\n" +
1260 HelpExampleCli("gettxout", "\"txid\" 1") +
1261 "\nAs a JSON-RPC call\n" +
1262 HelpExampleRpc("gettxout", "\"txid\", 1")},
1263 [&](const RPCHelpMan &self, const Config &config,
1264 const JSONRPCRequest &request) -> UniValue {
1265 NodeContext &node = EnsureAnyNodeContext(request.context);
1267 LOCK(cs_main);
1268
1270
1271 TxId txid(ParseHashV(request.params[0], "txid"));
1272 int n = request.params[1].getInt<int>();
1273 COutPoint out(txid, n);
1274 bool fMempool = true;
1275 if (!request.params[2].isNull()) {
1276 fMempool = request.params[2].get_bool();
1277 }
1278
1279 Coin coin;
1280 Chainstate &active_chainstate = chainman.ActiveChainstate();
1281 CCoinsViewCache *coins_view = &active_chainstate.CoinsTip();
1282
1283 if (fMempool) {
1284 const CTxMemPool &mempool = EnsureMemPool(node);
1285 LOCK(mempool.cs);
1286 CCoinsViewMemPool view(coins_view, mempool);
1287 if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
1288 return NullUniValue;
1289 }
1290 } else {
1291 if (!coins_view->GetCoin(out, coin)) {
1292 return NullUniValue;
1293 }
1294 }
1295
1296 const CBlockIndex *pindex =
1297 active_chainstate.m_blockman.LookupBlockIndex(
1298 coins_view->GetBestBlock());
1299 ret.pushKV("bestblock", pindex->GetBlockHash().GetHex());
1300 if (coin.GetHeight() == MEMPOOL_HEIGHT) {
1301 ret.pushKV("confirmations", 0);
1302 } else {
1303 ret.pushKV("confirmations",
1304 int64_t(pindex->nHeight - coin.GetHeight() + 1));
1305 }
1306 ret.pushKV("value", coin.GetTxOut().nValue);
1308 ScriptPubKeyToUniv(coin.GetTxOut().scriptPubKey, o, true);
1309 ret.pushKV("scriptPubKey", std::move(o));
1310 ret.pushKV("coinbase", coin.IsCoinBase());
1311
1312 return ret;
1313 },
1314 };
1315}
1316
1318 return RPCHelpMan{
1319 "verifychain",
1320 "Verifies blockchain database.\n",
1321 {
1322 {"checklevel", RPCArg::Type::NUM,
1324 strprintf("%d, range=0-4", DEFAULT_CHECKLEVEL)},
1325 strprintf("How thorough the block verification is:\n%s",
1327 {"nblocks", RPCArg::Type::NUM,
1329 "The number of blocks to check."},
1330 },
1332 "Verification finished successfully. If false, check "
1333 "debug.log for reason."},
1334 RPCExamples{HelpExampleCli("verifychain", "") +
1335 HelpExampleRpc("verifychain", "")},
1336 [&](const RPCHelpMan &self, const Config &config,
1337 const JSONRPCRequest &request) -> UniValue {
1338 const int check_level{request.params[0].isNull()
1340 : request.params[0].getInt<int>()};
1341 const int check_depth{request.params[1].isNull()
1343 : request.params[1].getInt<int>()};
1344
1345 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1346 LOCK(cs_main);
1347
1348 Chainstate &active_chainstate = chainman.ActiveChainstate();
1349 return CVerifyDB(chainman.GetNotifications())
1350 .VerifyDB(active_chainstate,
1351 active_chainstate.CoinsTip(), check_level,
1352 check_depth) == VerifyDBResult::SUCCESS;
1353 },
1354 };
1355}
1356
1358 return RPCHelpMan{
1359 "getblockchaininfo",
1360 "Returns an object containing various state info regarding blockchain "
1361 "processing.\n",
1362 {},
1363 RPCResult{
1365 "",
1366 "",
1367 {
1368 {RPCResult::Type::STR, "chain",
1369 "current network name (main, test, regtest)"},
1370 {RPCResult::Type::NUM, "blocks",
1371 "the height of the most-work fully-validated "
1372 "non-parked chain. The genesis block has height 0"},
1373 {RPCResult::Type::NUM, "headers",
1374 "the current number of headers we have validated"},
1375 {RPCResult::Type::NUM, "finalized_blockhash",
1376 "the hash of the avalanche finalized tip if any, otherwise "
1377 "the genesis block hash"},
1378 {RPCResult::Type::STR, "bestblockhash",
1379 "the hash of the currently best block"},
1380 {RPCResult::Type::NUM, "difficulty", "the current difficulty"},
1382 "The block time expressed in " + UNIX_EPOCH_TIME},
1383 {RPCResult::Type::NUM_TIME, "mediantime",
1384 "The median block time expressed in " + UNIX_EPOCH_TIME},
1385 {RPCResult::Type::NUM, "verificationprogress",
1386 "estimate of verification progress [0..1]"},
1387 {RPCResult::Type::BOOL, "initialblockdownload",
1388 "(debug information) estimate of whether this node is in "
1389 "Initial Block Download mode"},
1390 {RPCResult::Type::STR_HEX, "chainwork",
1391 "total amount of work in active chain, in hexadecimal"},
1392 {RPCResult::Type::NUM, "size_on_disk",
1393 "the estimated size of the block and undo files on disk"},
1394 {RPCResult::Type::BOOL, "pruned",
1395 "if the blocks are subject to pruning"},
1396 {RPCResult::Type::NUM, "pruneheight",
1397 "lowest-height complete block stored (only present if pruning "
1398 "is enabled)"},
1399 {RPCResult::Type::BOOL, "automatic_pruning",
1400 "whether automatic pruning is enabled (only present if "
1401 "pruning is enabled)"},
1402 {RPCResult::Type::NUM, "prune_target_size",
1403 "the target size used by pruning (only present if automatic "
1404 "pruning is enabled)"},
1405 {RPCResult::Type::STR, "warnings",
1406 "any network and blockchain warnings"},
1407 }},
1408 RPCExamples{HelpExampleCli("getblockchaininfo", "") +
1409 HelpExampleRpc("getblockchaininfo", "")},
1410 [&](const RPCHelpMan &self, const Config &config,
1411 const JSONRPCRequest &request) -> UniValue {
1412 const CChainParams &chainparams = config.GetChainParams();
1413
1414 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1415 LOCK(cs_main);
1416 Chainstate &active_chainstate = chainman.ActiveChainstate();
1417
1418 const CBlockIndex &tip{
1419 *CHECK_NONFATAL(active_chainstate.m_chain.Tip())};
1420 const int height{tip.nHeight};
1421
1423 obj.pushKV("chain", chainparams.GetChainTypeString());
1424 obj.pushKV("blocks", height);
1425 obj.pushKV("headers", chainman.m_best_header
1426 ? chainman.m_best_header->nHeight
1427 : -1);
1428 auto avalanche_finalized_tip{chainman.GetAvalancheFinalizedTip()};
1429 obj.pushKV("finalized_blockhash",
1430 avalanche_finalized_tip
1431 ? avalanche_finalized_tip->GetBlockHash().GetHex()
1432 : chainparams.GenesisBlock().GetHash().GetHex());
1433 obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex());
1434 obj.pushKV("difficulty", GetDifficulty(tip));
1435 obj.pushKV("time", tip.GetBlockTime());
1436 obj.pushKV("mediantime", tip.GetMedianTimePast());
1437 obj.pushKV(
1438 "verificationprogress",
1439 GuessVerificationProgress(chainman.GetParams().TxData(), &tip));
1440 obj.pushKV("initialblockdownload",
1441 chainman.IsInitialBlockDownload());
1442 obj.pushKV("chainwork", tip.nChainWork.GetHex());
1443 obj.pushKV("size_on_disk",
1445 obj.pushKV("pruned", chainman.m_blockman.IsPruneMode());
1446
1447 if (chainman.m_blockman.IsPruneMode()) {
1448 const auto prune_height{GetPruneHeight(
1449 chainman.m_blockman, active_chainstate.m_chain)};
1450 obj.pushKV("pruneheight",
1451 prune_height ? prune_height.value() + 1 : 0);
1452
1453 const bool automatic_pruning{
1454 chainman.m_blockman.GetPruneTarget() !=
1455 BlockManager::PRUNE_TARGET_MANUAL};
1456 obj.pushKV("automatic_pruning", automatic_pruning);
1457 if (automatic_pruning) {
1458 obj.pushKV("prune_target_size",
1459 chainman.m_blockman.GetPruneTarget());
1460 }
1461 }
1462
1463 obj.pushKV("warnings", GetWarnings(false).original);
1464 return obj;
1465 },
1466 };
1467}
1468
1471 bool operator()(const CBlockIndex *a, const CBlockIndex *b) const {
1472 // Make sure that unequal blocks with the same height do not compare
1473 // equal. Use the pointers themselves to make a distinction.
1474 if (a->nHeight != b->nHeight) {
1475 return (a->nHeight > b->nHeight);
1476 }
1477
1478 return a < b;
1479 }
1480};
1481
1483 return RPCHelpMan{
1484 "getchaintips",
1485 "Return information about all known tips in the block tree, including "
1486 "the main chain as well as orphaned branches.\n",
1487 {},
1488 RPCResult{
1490 "",
1491 "",
1493 "",
1494 "",
1495 {
1496 {RPCResult::Type::NUM, "height", "height of the chain tip"},
1497 {RPCResult::Type::STR_HEX, "hash", "block hash of the tip"},
1498 {RPCResult::Type::NUM, "branchlen",
1499 "zero for main chain, otherwise length of branch connecting "
1500 "the tip to the main chain"},
1501 {RPCResult::Type::STR, "status",
1502 "status of the chain, \"active\" for the main chain\n"
1503 "Possible values for status:\n"
1504 "1. \"invalid\" This branch contains at "
1505 "least one invalid block\n"
1506 "2. \"parked\" This branch contains at "
1507 "least one parked block\n"
1508 "3. \"headers-only\" Not all blocks for this "
1509 "branch are available, but the headers are valid\n"
1510 "4. \"valid-headers\" All blocks are available for "
1511 "this branch, but they were never fully validated\n"
1512 "5. \"valid-fork\" This branch is not part of "
1513 "the active chain, but is fully validated\n"
1514 "6. \"active\" This is the tip of the "
1515 "active main chain, which is certainly valid"},
1516 }}}},
1517 RPCExamples{HelpExampleCli("getchaintips", "") +
1518 HelpExampleRpc("getchaintips", "")},
1519 [&](const RPCHelpMan &self, const Config &config,
1520 const JSONRPCRequest &request) -> UniValue {
1521 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1522 LOCK(cs_main);
1523 CChain &active_chain = chainman.ActiveChain();
1524
1536 std::set<const CBlockIndex *, CompareBlocksByHeight> setTips;
1537 std::set<const CBlockIndex *> setOrphans;
1538 std::set<const CBlockIndex *> setPrevs;
1539
1540 for (const auto &[_, block_index] : chainman.BlockIndex()) {
1541 if (!active_chain.Contains(&block_index)) {
1542 setOrphans.insert(&block_index);
1543 setPrevs.insert(block_index.pprev);
1544 }
1545 }
1546
1547 for (std::set<const CBlockIndex *>::iterator it =
1548 setOrphans.begin();
1549 it != setOrphans.end(); ++it) {
1550 if (setPrevs.erase(*it) == 0) {
1551 setTips.insert(*it);
1552 }
1553 }
1554
1555 // Always report the currently active tip.
1556 setTips.insert(active_chain.Tip());
1557
1558 /* Construct the output array. */
1560 for (const CBlockIndex *block : setTips) {
1562 obj.pushKV("height", block->nHeight);
1563 obj.pushKV("hash", block->phashBlock->GetHex());
1564
1565 const int branchLen =
1566 block->nHeight - active_chain.FindFork(block)->nHeight;
1567 obj.pushKV("branchlen", branchLen);
1568
1569 std::string status;
1570 if (active_chain.Contains(block)) {
1571 // This block is part of the currently active chain.
1572 status = "active";
1573 } else if (block->nStatus.isInvalid()) {
1574 // This block or one of its ancestors is invalid.
1575 status = "invalid";
1576 } else if (block->nStatus.isOnParkedChain()) {
1577 // This block or one of its ancestors is parked.
1578 status = "parked";
1579 } else if (!block->HaveNumChainTxs()) {
1580 // This block cannot be connected because full block data
1581 // for it or one of its parents is missing.
1582 status = "headers-only";
1583 } else if (block->IsValid(BlockValidity::SCRIPTS)) {
1584 // This block is fully validated, but no longer part of the
1585 // active chain. It was probably the active block once, but
1586 // was reorganized.
1587 status = "valid-fork";
1588 } else if (block->IsValid(BlockValidity::TREE)) {
1589 // The headers for this block are valid, but it has not been
1590 // validated. It was probably never part of the most-work
1591 // chain.
1592 status = "valid-headers";
1593 } else {
1594 // No clue.
1595 status = "unknown";
1596 }
1597 obj.pushKV("status", status);
1598
1599 res.push_back(std::move(obj));
1600 }
1601
1602 return res;
1603 },
1604 };
1605}
1606
1608 return RPCHelpMan{
1609 "preciousblock",
1610 "Treats a block as if it were received before others with the same "
1611 "work.\n"
1612 "\nA later preciousblock call can override the effect of an earlier "
1613 "one.\n"
1614 "\nThe effects of preciousblock are not retained across restarts.\n",
1615 {
1617 "the hash of the block to mark as precious"},
1618 },
1620 RPCExamples{HelpExampleCli("preciousblock", "\"blockhash\"") +
1621 HelpExampleRpc("preciousblock", "\"blockhash\"")},
1622 [&](const RPCHelpMan &self, const Config &config,
1623 const JSONRPCRequest &request) -> UniValue {
1624 BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1625 CBlockIndex *pblockindex;
1626
1627 NodeContext &node = EnsureAnyNodeContext(request.context);
1629 {
1630 LOCK(cs_main);
1631 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
1632 if (!pblockindex) {
1634 "Block not found");
1635 }
1636 }
1637
1639 chainman.ActiveChainstate().PreciousBlock(state, pblockindex,
1640 node.avalanche.get());
1641
1642 if (!state.IsValid()) {
1644 }
1645
1646 // Block to make sure wallet/indexers sync before returning
1648
1649 return NullUniValue;
1650 },
1651 };
1652}
1653
1656 const BlockHash &block_hash) {
1658 CBlockIndex *pblockindex;
1659 {
1660 LOCK(chainman.GetMutex());
1661 pblockindex = chainman.m_blockman.LookupBlockIndex(block_hash);
1662 if (!pblockindex) {
1663 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1664 }
1665 }
1666 chainman.ActiveChainstate().InvalidateBlock(state, pblockindex);
1667
1668 if (state.IsValid()) {
1669 chainman.ActiveChainstate().ActivateBestChain(state, /*pblock=*/nullptr,
1670 avalanche);
1671 }
1672
1673 if (!state.IsValid()) {
1675 }
1676}
1677
1679 return RPCHelpMan{
1680 "invalidateblock",
1681 "Permanently marks a block as invalid, as if it violated a consensus "
1682 "rule.\n",
1683 {
1685 "the hash of the block to mark as invalid"},
1686 },
1688 RPCExamples{HelpExampleCli("invalidateblock", "\"blockhash\"") +
1689 HelpExampleRpc("invalidateblock", "\"blockhash\"")},
1690 [&](const RPCHelpMan &self, const Config &config,
1691 const JSONRPCRequest &request) -> UniValue {
1692 NodeContext &node = EnsureAnyNodeContext(request.context);
1694 const BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1695
1696 InvalidateBlock(chainman, node.avalanche.get(), hash);
1697 // Block to make sure wallet/indexers sync before returning
1699
1700 return NullUniValue;
1701 },
1702 };
1703}
1704
1706 return RPCHelpMan{
1707 "parkblock",
1708 "Marks a block as parked.\n",
1709 {
1711 "the hash of the block to park"},
1712 },
1714 RPCExamples{HelpExampleCli("parkblock", "\"blockhash\"") +
1715 HelpExampleRpc("parkblock", "\"blockhash\"")},
1716 [&](const RPCHelpMan &self, const Config &config,
1717 const JSONRPCRequest &request) -> UniValue {
1718 const std::string strHash = request.params[0].get_str();
1719 const BlockHash hash(uint256S(strHash));
1721
1722 NodeContext &node = EnsureAnyNodeContext(request.context);
1724 Chainstate &active_chainstate = chainman.ActiveChainstate();
1725 CBlockIndex *pblockindex = nullptr;
1726 {
1727 LOCK(cs_main);
1728 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
1729 if (!pblockindex) {
1731 "Block not found");
1732 }
1733
1734 if (active_chainstate.IsBlockAvalancheFinalized(pblockindex)) {
1735 // Reset avalanche finalization if we park a finalized
1736 // block.
1737 active_chainstate.ClearAvalancheFinalizedBlock();
1738 }
1739 }
1740
1741 active_chainstate.ParkBlock(state, pblockindex);
1742
1743 if (state.IsValid()) {
1744 active_chainstate.ActivateBestChain(state, /*pblock=*/nullptr,
1745 node.avalanche.get());
1746 }
1747
1748 if (!state.IsValid()) {
1750 }
1751
1752 // Block to make sure wallet/indexers sync before returning
1754
1755 return NullUniValue;
1756 },
1757 };
1758}
1759
1762 const BlockHash &block_hash) {
1763 {
1764 LOCK(chainman.GetMutex());
1765 CBlockIndex *pblockindex =
1766 chainman.m_blockman.LookupBlockIndex(block_hash);
1767 if (!pblockindex) {
1768 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
1769 }
1770
1771 chainman.ActiveChainstate().ResetBlockFailureFlags(pblockindex);
1772 chainman.RecalculateBestHeader();
1773 }
1774
1776 chainman.ActiveChainstate().ActivateBestChain(state, /*pblock=*/nullptr,
1777 avalanche);
1778
1779 if (!state.IsValid()) {
1781 }
1782}
1783
1785 return RPCHelpMan{
1786 "reconsiderblock",
1787 "Removes invalidity status of a block, its ancestors and its"
1788 "descendants, reconsider them for activation.\n"
1789 "This can be used to undo the effects of invalidateblock.\n",
1790 {
1792 "the hash of the block to reconsider"},
1793 },
1795 RPCExamples{HelpExampleCli("reconsiderblock", "\"blockhash\"") +
1796 HelpExampleRpc("reconsiderblock", "\"blockhash\"")},
1797 [&](const RPCHelpMan &self, const Config &config,
1798 const JSONRPCRequest &request) -> UniValue {
1799 NodeContext &node = EnsureAnyNodeContext(request.context);
1801 const BlockHash hash(ParseHashV(request.params[0], "blockhash"));
1802
1803 ReconsiderBlock(chainman, node.avalanche.get(), hash);
1804
1805 // Block to make sure wallet/indexers sync before returning
1807
1808 return NullUniValue;
1809 },
1810 };
1811}
1812
1814 return RPCHelpMan{
1815 "unparkblock",
1816 "Removes parked status of a block and its descendants, reconsider "
1817 "them for activation.\n"
1818 "This can be used to undo the effects of parkblock.\n",
1819 {
1821 "the hash of the block to unpark"},
1822 },
1824 RPCExamples{HelpExampleCli("unparkblock", "\"blockhash\"") +
1825 HelpExampleRpc("unparkblock", "\"blockhash\"")},
1826 [&](const RPCHelpMan &self, const Config &config,
1827 const JSONRPCRequest &request) -> UniValue {
1828 const std::string strHash = request.params[0].get_str();
1829 NodeContext &node = EnsureAnyNodeContext(request.context);
1831 const BlockHash hash(uint256S(strHash));
1832 Chainstate &active_chainstate = chainman.ActiveChainstate();
1833
1834 {
1835 LOCK(cs_main);
1836
1837 CBlockIndex *pblockindex =
1838 chainman.m_blockman.LookupBlockIndex(hash);
1839 if (!pblockindex) {
1841 "Block not found");
1842 }
1843
1844 if (!pblockindex->nStatus.isOnParkedChain()) {
1845 // Block to unpark is not parked so there is nothing to do.
1846 return NullUniValue;
1847 }
1848
1849 const CBlockIndex *tip = active_chainstate.m_chain.Tip();
1850 if (tip) {
1851 const CBlockIndex *ancestor =
1852 LastCommonAncestor(tip, pblockindex);
1853 if (active_chainstate.IsBlockAvalancheFinalized(ancestor)) {
1854 // Only reset avalanche finalization if we unpark a
1855 // block that might conflict with avalanche finalized
1856 // blocks.
1857 active_chainstate.ClearAvalancheFinalizedBlock();
1858 }
1859 }
1860
1861 active_chainstate.UnparkBlockAndChildren(pblockindex);
1862 }
1863
1865 active_chainstate.ActivateBestChain(state, /*pblock=*/nullptr,
1866 node.avalanche.get());
1867
1868 if (!state.IsValid()) {
1870 }
1871
1872 // Block to make sure wallet/indexers sync before returning
1874
1875 return NullUniValue;
1876 },
1877 };
1878}
1879
1881 return RPCHelpMan{
1882 "getchaintxstats",
1883 "Compute statistics about the total number and rate of transactions "
1884 "in the chain.\n",
1885 {
1886 {"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint{"one month"},
1887 "Size of the window in number of blocks"},
1888 {"blockhash", RPCArg::Type::STR_HEX,
1889 RPCArg::DefaultHint{"chain tip"},
1890 "The hash of the block that ends the window."},
1891 },
1892 RPCResult{
1894 "",
1895 "",
1896 {
1898 "The timestamp for the final block in the window, "
1899 "expressed in " +
1901 {RPCResult::Type::NUM, "txcount", /*optional=*/true,
1902 "The total number of transactions in the chain up to "
1903 "that point, if known. It may be unknown when using "
1904 "assumeutxo."},
1905 {RPCResult::Type::STR_HEX, "window_final_block_hash",
1906 "The hash of the final block in the window"},
1907 {RPCResult::Type::NUM, "window_final_block_height",
1908 "The height of the final block in the window."},
1909 {RPCResult::Type::NUM, "window_block_count",
1910 "Size of the window in number of blocks"},
1911 {RPCResult::Type::NUM, "window_interval",
1912 "The elapsed time in the window in seconds. Only "
1913 "returned if \"window_block_count\" is > 0"},
1914 {RPCResult::Type::NUM, "window_tx_count", /*optional=*/true,
1915 "The number of transactions in the window. Only "
1916 "returned if \"window_block_count\" is > 0 and if "
1917 "txcount exists for the start and end of the window."},
1918 {RPCResult::Type::NUM, "txrate", /*optional=*/true,
1919 "The average rate of transactions per second in the "
1920 "window. Only returned if \"window_interval\" is > 0 "
1921 "and if window_tx_count exists."},
1922 }},
1923 RPCExamples{HelpExampleCli("getchaintxstats", "") +
1924 HelpExampleRpc("getchaintxstats", "2016")},
1925 [&](const RPCHelpMan &self, const Config &config,
1926 const JSONRPCRequest &request) -> UniValue {
1927 ChainstateManager &chainman = EnsureAnyChainman(request.context);
1928 const CBlockIndex *pindex;
1929
1930 // By default: 1 month
1931 int blockcount =
1932 30 * 24 * 60 * 60 /
1933 config.GetChainParams().GetConsensus().nPowTargetSpacing;
1934
1935 if (request.params[1].isNull()) {
1936 LOCK(cs_main);
1937 pindex = chainman.ActiveTip();
1938 } else {
1939 BlockHash hash(ParseHashV(request.params[1], "blockhash"));
1940 LOCK(cs_main);
1941 pindex = chainman.m_blockman.LookupBlockIndex(hash);
1942 if (!pindex) {
1944 "Block not found");
1945 }
1946 if (!chainman.ActiveChain().Contains(pindex)) {
1948 "Block is not in main chain");
1949 }
1950 }
1951
1952 CHECK_NONFATAL(pindex != nullptr);
1953
1954 if (request.params[0].isNull()) {
1955 blockcount =
1956 std::max(0, std::min(blockcount, pindex->nHeight - 1));
1957 } else {
1958 blockcount = request.params[0].getInt<int>();
1959
1960 if (blockcount < 0 ||
1961 (blockcount > 0 && blockcount >= pindex->nHeight)) {
1963 "Invalid block count: "
1964 "should be between 0 and "
1965 "the block's height - 1");
1966 }
1967 }
1968
1969 const CBlockIndex &past_block{*CHECK_NONFATAL(
1970 pindex->GetAncestor(pindex->nHeight - blockcount))};
1971 const int64_t nTimeDiff{pindex->GetMedianTimePast() -
1972 past_block.GetMedianTimePast()};
1973
1975 ret.pushKV("time", pindex->GetBlockTime());
1976 if (pindex->nChainTx) {
1977 ret.pushKV("txcount", pindex->nChainTx);
1978 }
1979 ret.pushKV("window_final_block_hash",
1980 pindex->GetBlockHash().GetHex());
1981 ret.pushKV("window_final_block_height", pindex->nHeight);
1982 ret.pushKV("window_block_count", blockcount);
1983 if (blockcount > 0) {
1984 ret.pushKV("window_interval", nTimeDiff);
1985 if (pindex->nChainTx != 0 && past_block.nChainTx != 0) {
1986 unsigned int window_tx_count =
1987 pindex->nChainTx - past_block.nChainTx;
1988 ret.pushKV("window_tx_count", window_tx_count);
1989 if (nTimeDiff > 0) {
1990 ret.pushKV("txrate",
1991 double(window_tx_count) / nTimeDiff);
1992 }
1993 }
1994 }
1995
1996 return ret;
1997 },
1998 };
1999}
2000
2001template <typename T>
2002static T CalculateTruncatedMedian(std::vector<T> &scores) {
2003 size_t size = scores.size();
2004 if (size == 0) {
2005 return T();
2006 }
2007
2008 std::sort(scores.begin(), scores.end());
2009 if (size % 2 == 0) {
2010 return (scores[size / 2 - 1] + scores[size / 2]) / 2;
2011 } else {
2012 return scores[size / 2];
2013 }
2014}
2015
2016template <typename T> static inline bool SetHasKeys(const std::set<T> &set) {
2017 return false;
2018}
2019template <typename T, typename Tk, typename... Args>
2020static inline bool SetHasKeys(const std::set<T> &set, const Tk &key,
2021 const Args &...args) {
2022 return (set.count(key) != 0) || SetHasKeys(set, args...);
2023}
2024
2025// outpoint (needed for the utxo index) + nHeight + fCoinBase
2026static constexpr size_t PER_UTXO_OVERHEAD =
2027 sizeof(COutPoint) + sizeof(uint32_t) + sizeof(bool);
2028
2030 const auto &ticker = Currency::get().ticker;
2031 return RPCHelpMan{
2032 "getblockstats",
2033 "Compute per block statistics for a given window. All amounts are "
2034 "in " +
2035 ticker +
2036 ".\n"
2037 "It won't work for some heights with pruning.\n",
2038 {
2039 {"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::NO,
2040 "The block hash or height of the target block",
2042 .type_str = {"", "string or numeric"}}},
2043 {"stats",
2045 RPCArg::DefaultHint{"all values"},
2046 "Values to plot (see result below)",
2047 {
2049 "Selected statistic"},
2051 "Selected statistic"},
2052 },
2054 },
2055 RPCResult{
2057 "",
2058 "",
2059 {
2060 {RPCResult::Type::NUM, "avgfee", "Average fee in the block"},
2061 {RPCResult::Type::NUM, "avgfeerate",
2062 "Average feerate (in satoshis per virtual byte)"},
2063 {RPCResult::Type::NUM, "avgtxsize", "Average transaction size"},
2064 {RPCResult::Type::STR_HEX, "blockhash",
2065 "The block hash (to check for potential reorgs)"},
2066 {RPCResult::Type::NUM, "height", "The height of the block"},
2067 {RPCResult::Type::NUM, "ins",
2068 "The number of inputs (excluding coinbase)"},
2069 {RPCResult::Type::NUM, "maxfee", "Maximum fee in the block"},
2070 {RPCResult::Type::NUM, "maxfeerate",
2071 "Maximum feerate (in satoshis per virtual byte)"},
2072 {RPCResult::Type::NUM, "maxtxsize", "Maximum transaction size"},
2073 {RPCResult::Type::NUM, "medianfee",
2074 "Truncated median fee in the block"},
2075 {RPCResult::Type::NUM, "medianfeerate",
2076 "Truncated median feerate (in " + ticker + " per byte)"},
2077 {RPCResult::Type::NUM, "mediantime",
2078 "The block median time past"},
2079 {RPCResult::Type::NUM, "mediantxsize",
2080 "Truncated median transaction size"},
2081 {RPCResult::Type::NUM, "minfee", "Minimum fee in the block"},
2082 {RPCResult::Type::NUM, "minfeerate",
2083 "Minimum feerate (in satoshis per virtual byte)"},
2084 {RPCResult::Type::NUM, "mintxsize", "Minimum transaction size"},
2085 {RPCResult::Type::NUM, "outs", "The number of outputs"},
2086 {RPCResult::Type::NUM, "subsidy", "The block subsidy"},
2087 {RPCResult::Type::NUM, "time", "The block time"},
2088 {RPCResult::Type::NUM, "total_out",
2089 "Total amount in all outputs (excluding coinbase and thus "
2090 "reward [ie subsidy + totalfee])"},
2091 {RPCResult::Type::NUM, "total_size",
2092 "Total size of all non-coinbase transactions"},
2093 {RPCResult::Type::NUM, "totalfee", "The fee total"},
2094 {RPCResult::Type::NUM, "txs",
2095 "The number of transactions (including coinbase)"},
2096 {RPCResult::Type::NUM, "utxo_increase",
2097 "The increase/decrease in the number of unspent outputs"},
2098 {RPCResult::Type::NUM, "utxo_size_inc",
2099 "The increase/decrease in size for the utxo index (not "
2100 "discounting op_return and similar)"},
2101 }},
2104 "getblockstats",
2105 R"('"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"' '["minfeerate","avgfeerate"]')") +
2106 HelpExampleCli("getblockstats",
2107 R"(1000 '["minfeerate","avgfeerate"]')") +
2109 "getblockstats",
2110 R"("00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09", ["minfeerate","avgfeerate"])") +
2111 HelpExampleRpc("getblockstats",
2112 R"(1000, ["minfeerate","avgfeerate"])")},
2113 [&](const RPCHelpMan &self, const Config &config,
2114 const JSONRPCRequest &request) -> UniValue {
2115 ChainstateManager &chainman = EnsureAnyChainman(request.context);
2116 const CBlockIndex &pindex{*CHECK_NONFATAL(
2117 ParseHashOrHeight(request.params[0], chainman))};
2118
2119 std::set<std::string> stats;
2120 if (!request.params[1].isNull()) {
2121 const UniValue stats_univalue = request.params[1].get_array();
2122 for (unsigned int i = 0; i < stats_univalue.size(); i++) {
2123 const std::string stat = stats_univalue[i].get_str();
2124 stats.insert(stat);
2125 }
2126 }
2127
2128 const CBlock &block = GetBlockChecked(chainman.m_blockman, pindex);
2129 const CBlockUndo &blockUndo =
2130 GetUndoChecked(chainman.m_blockman, pindex);
2131
2132 // Calculate everything if nothing selected (default)
2133 const bool do_all = stats.size() == 0;
2134 const bool do_mediantxsize =
2135 do_all || stats.count("mediantxsize") != 0;
2136 const bool do_medianfee = do_all || stats.count("medianfee") != 0;
2137 const bool do_medianfeerate =
2138 do_all || stats.count("medianfeerate") != 0;
2139 const bool loop_inputs =
2140 do_all || do_medianfee || do_medianfeerate ||
2141 SetHasKeys(stats, "utxo_size_inc", "totalfee", "avgfee",
2142 "avgfeerate", "minfee", "maxfee", "minfeerate",
2143 "maxfeerate");
2144 const bool loop_outputs =
2145 do_all || loop_inputs || stats.count("total_out");
2146 const bool do_calculate_size =
2147 do_mediantxsize || loop_inputs ||
2148 SetHasKeys(stats, "total_size", "avgtxsize", "mintxsize",
2149 "maxtxsize");
2150
2151 const int64_t blockMaxSize = config.GetMaxBlockSize();
2152 Amount maxfee = Amount::zero();
2153 Amount maxfeerate = Amount::zero();
2154 Amount minfee = MAX_MONEY;
2155 Amount minfeerate = MAX_MONEY;
2156 Amount total_out = Amount::zero();
2157 Amount totalfee = Amount::zero();
2158 int64_t inputs = 0;
2159 int64_t maxtxsize = 0;
2160 int64_t mintxsize = blockMaxSize;
2161 int64_t outputs = 0;
2162 int64_t total_size = 0;
2163 int64_t utxo_size_inc = 0;
2164 std::vector<Amount> fee_array;
2165 std::vector<Amount> feerate_array;
2166 std::vector<int64_t> txsize_array;
2167
2168 for (size_t i = 0; i < block.vtx.size(); ++i) {
2169 const auto &tx = block.vtx.at(i);
2170 outputs += tx->vout.size();
2171 Amount tx_total_out = Amount::zero();
2172 if (loop_outputs) {
2173 for (const CTxOut &out : tx->vout) {
2174 tx_total_out += out.nValue;
2175 utxo_size_inc +=
2178 }
2179 }
2180
2181 if (tx->IsCoinBase()) {
2182 continue;
2183 }
2184
2185 // Don't count coinbase's fake input
2186 inputs += tx->vin.size();
2187 // Don't count coinbase reward
2188 total_out += tx_total_out;
2189
2190 int64_t tx_size = 0;
2191 if (do_calculate_size) {
2192 tx_size = tx->GetTotalSize();
2193 if (do_mediantxsize) {
2194 txsize_array.push_back(tx_size);
2195 }
2196 maxtxsize = std::max(maxtxsize, tx_size);
2197 mintxsize = std::min(mintxsize, tx_size);
2198 total_size += tx_size;
2199 }
2200
2201 if (loop_inputs) {
2202 Amount tx_total_in = Amount::zero();
2203 const auto &txundo = blockUndo.vtxundo.at(i - 1);
2204 for (const Coin &coin : txundo.vprevout) {
2205 const CTxOut &prevoutput = coin.GetTxOut();
2206
2207 tx_total_in += prevoutput.nValue;
2208 utxo_size_inc -=
2209 GetSerializeSize(prevoutput, PROTOCOL_VERSION) +
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:236
RPCHelpMan gettxout()
static RPCHelpMan getblock()
Definition: blockchain.cpp:700
static RPCHelpMan getdifficulty()
Definition: blockchain.cpp:460
static std::atomic< bool > g_scan_in_progress
static bool SetHasKeys(const std::set< T > &set)
static RPCHelpMan reconsiderblock()
static void InvalidateBlock(ChainstateManager &chainman, avalanche::Processor *const avalanche, const BlockHash &block_hash)
static T CalculateTruncatedMedian(std::vector< T > &scores)
static RPCHelpMan invalidateblock()
static int ComputeNextBlockAndDepth(const CBlockIndex &tip, const CBlockIndex &blockindex, const CBlockIndex *&next)
Definition: blockchain.cpp:103
static RPCHelpMan syncwithvalidationinterfacequeue()
Definition: blockchain.cpp:443
static CBlockUndo GetUndoChecked(BlockManager &blockman, const CBlockIndex &blockindex)
Definition: blockchain.cpp:681
static RPCHelpMan getchaintips()
static RPCHelpMan loadtxoutset()
static RPCHelpMan gettxoutsetinfo()
Definition: blockchain.cpp:977
static RPCHelpMan getchainstates()
std::tuple< std::unique_ptr< CCoinsViewCursor >, CCoinsStats, const CBlockIndex * > PrepareUTXOSnapshot(Chainstate &chainstate, const std::function< void()> &interruption_point)
static RPCHelpMan getblockstats()
static CoinStatsHashType ParseHashType(const std::string &hash_type_input)
Definition: blockchain.cpp:963
static RPCHelpMan preciousblock()
static constexpr size_t PER_UTXO_OVERHEAD
double GetDifficulty(const CBlockIndex &blockindex)
Calculate the difficulty for a given block index.
Definition: blockchain.cpp:87
static RPCHelpMan scantxoutset()
static std::condition_variable cond_blockchange
Definition: blockchain.cpp:69
static std::atomic< int > g_scan_progress
RAII object to prevent concurrency issue when scanning the txout set.
static CBlock GetBlockChecked(BlockManager &blockman, const CBlockIndex &blockindex)
Definition: blockchain.cpp:659
std::optional< int > GetPruneHeight(const BlockManager &blockman, const CChain &chain)
Definition: blockchain.cpp:851
static void ReconsiderBlock(ChainstateManager &chainman, avalanche::Processor *const avalanche, const BlockHash &block_hash)
static RPCHelpMan getblockfilter()
static RPCHelpMan getbestblockhash()
Definition: blockchain.cpp:237
RPCHelpMan getblockchaininfo()
static RPCHelpMan getchaintxstats()
UniValue CreateUTXOSnapshot(node::NodeContext &node, Chainstate &chainstate, AutoFile &afile, const fs::path &path, const fs::path &tmppath)
Test-only helper to create UTXO snapshots given a chainstate and a file handle.
static RPCHelpMan waitforblock()
Definition: blockchain.cpp:321
std::tuple< std::unique_ptr< CCoinsViewCursor >, CCoinsStats, const CBlockIndex * > PrepareUTXOSnapshot(Chainstate &chainstate, const std::function< void()> &interruption_point={}) EXCLUSIVE_LOCKS_REQUIRED(UniValue WriteUTXOSnapshot(Chainstate &chainstate, CCoinsViewCursor *pcursor, CCoinsStats *maybe_stats, const CBlockIndex *tip, AutoFile &afile, const fs::path &path, const fs::path &temppath, const std::function< void()> &interruption_point={})
static RPCHelpMan getblockfrompeer()
Definition: blockchain.cpp:480
static RPCHelpMan getblockhash()
Definition: blockchain.cpp:534
void RegisterBlockchainRPCCommands(CRPCTable &t)
static RPCHelpMan verifychain()
static std::atomic< bool > g_should_abort_scan
const std::vector< RPCResult > RPCHelpForChainstate
UniValue blockheaderToJSON(const CBlockIndex &tip, const CBlockIndex &blockindex)
Block header to JSON.
Definition: blockchain.cpp:146
RPCHelpMan unparkblock()
static RPCHelpMan waitforblockheight()
Definition: blockchain.cpp:383
static const CBlockIndex * ParseHashOrHeight(const UniValue &param, ChainstateManager &chainman)
Definition: blockchain.cpp:114
UniValue blockToJSON(BlockManager &blockman, const CBlock &block, const CBlockIndex &tip, const CBlockIndex &blockindex, TxVerbosity verbosity)
Block description to JSON.
Definition: blockchain.cpp:180
static RPCHelpMan pruneblockchain()
Definition: blockchain.cpp:889
static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange)
static RPCHelpMan getblockheader()
Definition: blockchain.cpp:563
RPCHelpMan parkblock()
static GlobalMutex cs_blockchange
Definition: blockchain.cpp:68
static RPCHelpMan dumptxoutset()
Serialize the UTXO set to a file for loading elsewhere.
static RPCHelpMan getblockcount()
Definition: blockchain.cpp:219
static RPCHelpMan waitfornewblock()
Definition: blockchain.cpp:264
void RPCNotifyBlockChange(const CBlockIndex *pindex)
Callback for when block tip changed.
Definition: blockchain.cpp:255
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
BlockFilterType
Definition: blockfilter.h:88
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
@ SCRIPTS
Scripts & signatures ok.
@ TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:112
static constexpr int64_t TIMESTAMP_WINDOW
Timestamp window used as a grace period by code that compares external timestamps (such as timestamps...
Definition: chain.h:36
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
#define CHECK_NONFATAL(condition)
Identity function.
Definition: check.h:53
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:525
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:515
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:557
int fclose()
Definition: streams.h:530
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:111
const std::vector< uint8_t > & GetEncodedFilter() const
Definition: blockfilter.h:134
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache)
Get a single filter header by block.
BlockHash GetHash() const
Definition: block.cpp:11
Definition: block.h:60
std::vector< CTransactionRef > vtx
Definition: block.h:63
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
bool IsValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
Definition: blockindex.h:191
uint256 hashMerkleRoot
Definition: blockindex.h:75
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
CBlockHeader GetBlockHeader() const
Definition: blockindex.h:117
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:51
uint32_t nTime
Definition: blockindex.h:76
uint32_t nNonce
Definition: blockindex.h:78
int64_t GetBlockTime() const
Definition: blockindex.h:160
int64_t GetMedianTimePast() const
Definition: blockindex.h:172
uint32_t nBits
Definition: blockindex.h:77
unsigned int nTx
Number of transactions in this block.
Definition: blockindex.h:55
int32_t nVersion
block header
Definition: blockindex.h:74
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:62
BlockHash GetBlockHash() const
Definition: blockindex.h:130
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Definition: blockindex.h:68
Undo information for a CBlock.
Definition: undo.h:73
std::vector< CTxUndo > vtxundo
Definition: undo.h:76
An in-memory indexed chain of blocks.
Definition: chain.h:138
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:154
CBlockIndex * FindEarliestAtLeast(int64_t nTime, int height) const
Find the earliest block with timestamp equal or greater than the given time and height equal or great...
Definition: chain.cpp:62
int Height() const
Return the maximal height in the chain.
Definition: chain.h:190
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:49
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:170
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
std::string GetChainTypeString() const
Return the chain type string.
Definition: chainparams.h:134
const CBlock & GenesisBlock() const
Definition: chainparams.h:112
const ChainTxData & TxData() const
Definition: chainparams.h:158
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:363
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:214
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:91
Cursor for iterating over CoinsView state.
Definition: coins.h:222
virtual void Next()=0
virtual bool Valid() const =0
virtual bool GetKey(COutPoint &key) const =0
virtual bool GetValue(Coin &coin) const =0
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:215
Abstract view on the open txout dataset.
Definition: coins.h:305
virtual BlockHash GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:16
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:647
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
GetCoin, returning whether it exists and is not spent.
Definition: txmempool.cpp:779
Definition: net.h:824
bool GetNetworkActive() const
Definition: net.h:916
void SetNetworkActive(bool active)
Definition: net.cpp:2355
RPC command dispatcher.
Definition: server.h:194
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:328
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:221
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:317
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:137
An output of a transaction.
Definition: transaction.h:128
CScript scriptPubKey
Definition: transaction.h:131
Amount nValue
Definition: transaction.h:130
Restore the UTXO in a Coin at a given COutPoint.
Definition: undo.h:62
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
Definition: validation.h:655
VerifyDBResult VerifyDB(Chainstate &chainstate, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:734
bool IsBlockAvalancheFinalized(const CBlockIndex *pindex) const EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Checks if a block is finalized by avalanche voting.
const std::optional< BlockHash > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
Definition: validation.h:841
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr, avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Find the best known block, and make it the tip of the block chain.
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:833
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:893
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:860
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
Definition: validation.h:890
void ForceFlushStateToDisk()
Unconditionally flush all changes to disk.
void UnparkBlockAndChildren(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove parked status from a block and its descendants.
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:867
bool AvalancheFinalizeBlock(CBlockIndex *pindex, avalanche::Processor &avalanche) EXCLUSIVE_LOCKS_REQUIRED(voi ClearAvalancheFinalizedBlock)() EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Mark a block as finalized by avalanche.
Definition: validation.h:996
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition: validation.h:791
bool ParkBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Park a block.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1186
node::BlockMap & BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:1463
SnapshotCompletionResult MaybeCompleteSnapshotValidation() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
Definition: validation.h:1437
kernel::Notifications & GetNotifications() const
Definition: validation.h:1294
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
Definition: validation.h:1318
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1444
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1441
const CChainParams & GetParams() const
Definition: validation.h:1279
const Consensus::Params & GetConsensus() const
Definition: validation.h:1282
const CBlockIndex * GetAvalancheFinalizedTip() const
util::Result< CBlockIndex * > ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1438
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1403
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1327
A UTXO entry.
Definition: coins.h:29
uint32_t GetHeight() const
Definition: coins.h:46
bool IsCoinBase() const
Definition: coins.h:47
CTxOut & GetTxOut()
Definition: coins.h:50
CoinsViewScanReserver()=default
Definition: config.h:19
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:174
Different type to mark Mutex at global scope.
Definition: sync.h:144
RAII class that disables the network in its constructor and enables it in its destructor.
NetworkDisable(CConnman &connman)
CConnman & m_connman
virtual std::optional< std::string > FetchBlock(const Config &config, NodeId peer_id, const CBlockIndex &block_index)=0
Attempt to manually fetch block from a given peer.
auto Arg(size_t i) const
Helper to get a required or default-valued request argument.
Definition: util.h:410
RAII class that temporarily rolls back the local chain in it's constructor and rolls it forward again...
avalanche::Processor *const m_avalanche
const CBlockIndex & m_invalidate_index
TemporaryRollback(ChainstateManager &chainman, avalanche::Processor *const avalanche, const CBlockIndex &index)
ChainstateManager & m_chainman
void push_back(UniValue val)
Definition: univalue.cpp:96
const std::string & get_str() const
@ VOBJ
Definition: univalue.h:31
@ VARR
Definition: univalue.h:32
bool isNull() const
Definition: univalue.h:104
size_t size() const
Definition: univalue.h:92
const std::vector< UniValue > & getValues() const
Int getInt() const
Definition: univalue.h:157
const UniValue & get_array() const
bool isNum() const
Definition: univalue.h:109
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:115
bool IsValid() const
Definition: validation.h:119
std::string GetRejectReason() const
Definition: validation.h:123
std::string ToString() const
Definition: validation.h:125
uint8_t * begin()
Definition: uint256.h:85
std::string ToString() const
Definition: uint256.h:80
std::string GetHex() const
Definition: uint256.cpp:16
std::string GetHex() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
std::string u8string() const
Definition: fs.h:72
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:107
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
Definition: blockstorage.h:362
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
bool IsPruneMode() const
Whether running in -prune mode.
Definition: blockstorage.h:359
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:30
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:41
256-bit opaque blob.
Definition: uint256.h:129
std::unique_ptr< CoinStatsIndex > g_coin_stats_index
The global UTXO set hash object.
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
Definition: core_write.cpp:194
TxVerbosity
Verbose level for block's transaction.
Definition: core_io.h:29
@ SHOW_DETAILS_AND_PREVOUT
The same as previous option with information about prevouts if available.
@ SHOW_TXID
Only TXID for each block's transaction.
@ SHOW_DETAILS
Include TXID, inputs, outputs, and other common block's transaction information.
void TxToUniv(const CTransaction &tx, const BlockHash &hashBlock, UniValue &entry, bool include_hex=true, const CTxUndo *txundo=nullptr, TxVerbosity verbosity=TxVerbosity::SHOW_DETAILS, std::function< bool(const CTxOut &)> is_change_func={})
Definition: core_write.cpp:221
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
int64_t NodeId
Definition: eviction.h:16
#define LogPrintLevel(category, level,...)
Definition: logging.h:437
#define LogPrint(category,...)
Definition: logging.h:452
unsigned int nHeight
static void pool cs
@ RPC
Definition: logging.h:76
@ NONE
Definition: logging.h:68
static path u8path(const std::string &utf8_str)
Definition: fs.h:90
static bool exists(const path &p)
Definition: fs.h:107
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:147
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:39
CoinStatsHashType
Definition: coinstats.h:24
Definition: init.h:31
std::optional< kernel::CCoinsStats > GetUTXOStats(CCoinsView *view, BlockManager &blockman, kernel::CoinStatsHashType hash_type, const std::function< void()> &interruption_point, const CBlockIndex *pindex, bool index_requested)
Calculate statistics about the unspent transaction output set.
Definition: coinstats.cpp:17
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:90
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
@ NODE_NETWORK_LIMITED
Definition: protocol.h:366
@ NODE_NETWORK
Definition: protocol.h:343
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:58
@ RPC_MISC_ERROR
General application defined errors std::exception thrown in command handling.
Definition: protocol.h:38
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
Definition: protocol.h:46
@ RPC_INTERNAL_ERROR
Definition: protocol.h:33
@ RPC_DATABASE_ERROR
Database error.
Definition: protocol.h:48
@ RPC_DESERIALIZATION_ERROR
Error parsing or validating structure in raw format.
Definition: protocol.h:50
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition: protocol.h:42
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:153
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:170
std::vector< CScript > EvalDescriptorStringOrObject(const UniValue &scanobject, FlatSigningProvider &provider)
Evaluate a descriptor given as a string, or as a {"desc":...,"range":...} object, with default range ...
Definition: util.cpp:1362
const std::string UNIX_EPOCH_TIME
String used to describe UNIX epoch time in documentation, factored out to a constant for consistency.
Definition: util.cpp:25
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded values (throws error if not hex).
Definition: util.cpp:76
@ SER_NETWORK
Definition: serialize.h:155
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1280
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1276
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:379
ChainstateManager & EnsureAnyChainman(const std::any &context)
Definition: server_util.cpp:59
NodeContext & EnsureAnyNodeContext(const std::any &context)
Definition: server_util.cpp:21
CTxMemPool & EnsureMemPool(const NodeContext &node)
Definition: server_util.cpp:29
PeerManager & EnsurePeerman(const NodeContext &node)
Definition: server_util.cpp:72
ChainstateManager & EnsureChainman(const NodeContext &node)
Definition: server_util.cpp:52
ArgsManager & EnsureArgsman(const NodeContext &node)
Definition: server_util.cpp:41
CConnman & EnsureConnman(const NodeContext &node)
Definition: server_util.cpp:63
ArgsManager & EnsureAnyArgsman(const std::any &context)
Definition: server_util.cpp:48
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
Definition: string.h:90
Definition: amount.h: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:64
Comparison function for sorting the getchaintips heads.
bool operator()(const CBlockIndex *a, const CBlockIndex *b) const
static const Currency & get()
Definition: amount.cpp:18
std::string ticker
Definition: amount.h: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:206
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
@ NO
Required arg.
UniValue Default
Default constant value.
Definition: util.h:208
std::string oneline_description
Should be empty unless it is supposed to override the auto-generated summary line.
Definition: util.h:143
bool skip_type_check
Definition: util.h:140
@ ELISION
Special type to denote elision (...)
@ NUM_TIME
Special numeric to denote unix epoch time.
@ STR_HEX
Special string with only hex chars.
@ STR_AMOUNT
Special string to represent a floating point amount.
A TxId is the identifier of a transaction.
Definition: txid.h:14
uint64_t nDiskSize
Definition: coinstats.h: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
Definition: tests.c:31
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
#define LOG_TIME_SECONDS(end_msg)
Definition: timer.h:103
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coins to signify they are only in the memory pool(since 0....
Definition: txmempool.h:55
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:143
const UniValue NullUniValue
Definition: univalue.cpp:16
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
double GuessVerificationProgress(const ChainTxData &data, const CBlockIndex *pindex)
Guess how far we are in the verification process at the given block index require cs_main if pindex h...
const std::vector< std::string > CHECKLEVEL_DOC
Documentation for argument 'checklevel'.
Definition: validation.cpp:102
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
AssertLockHeld(pool.cs)
static constexpr int DEFAULT_CHECKLEVEL
Definition: validation.h:102
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
Definition: validation.h:100
static const signed int DEFAULT_CHECKBLOCKS
Definition: validation.h:101
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
bilingual_str GetWarnings(bool verbose)
Format a string that describes several potential problems detected by the core.
Definition: warnings.cpp:41