Bitcoin Core 29.99.0
P2P Digital Currency
rest.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2022 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 <bitcoin-build-config.h> // IWYU pragma: keep
7
8#include <rest.h>
9
10#include <blockfilter.h>
11#include <chain.h>
12#include <chainparams.h>
13#include <core_io.h>
14#include <flatfile.h>
15#include <httpserver.h>
17#include <index/txindex.h>
18#include <node/blockstorage.h>
19#include <node/context.h>
20#include <primitives/block.h>
22#include <rpc/blockchain.h>
23#include <rpc/mempool.h>
24#include <rpc/protocol.h>
25#include <rpc/server.h>
26#include <rpc/server_util.h>
27#include <streams.h>
28#include <sync.h>
29#include <txmempool.h>
30#include <undo.h>
31#include <util/any.h>
32#include <util/check.h>
33#include <util/strencodings.h>
34#include <validation.h>
35
36#include <any>
37#include <vector>
38
39#include <univalue.h>
40
44
45static const size_t MAX_GETUTXOS_OUTPOINTS = 15; //allow a max of 15 outpoints to be queried at once
46static constexpr unsigned int MAX_REST_HEADERS_RESULTS = 2000;
47
48static const struct {
50 const char* name;
51} rf_names[] = {
56};
57
58struct CCoin {
59 uint32_t nHeight;
61
62 CCoin() : nHeight(0) {}
63 explicit CCoin(Coin&& in) : nHeight(in.nHeight), out(std::move(in.out)) {}
64
66 {
67 uint32_t nTxVerDummy = 0;
68 READWRITE(nTxVerDummy, obj.nHeight, obj.out);
69 }
70};
71
72static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string message)
73{
74 req->WriteHeader("Content-Type", "text/plain");
75 req->WriteReply(status, message + "\r\n");
76 return false;
77}
78
86static NodeContext* GetNodeContext(const std::any& context, HTTPRequest* req)
87{
88 auto node_context = util::AnyPtr<NodeContext>(context);
89 if (!node_context) {
91 strprintf("%s:%d (%s)\n"
92 "Internal bug detected: Node context not found!\n"
93 "You may report this issue here: %s\n",
94 __FILE__, __LINE__, __func__, CLIENT_BUGREPORT));
95 return nullptr;
96 }
97 return node_context;
98}
99
107static CTxMemPool* GetMemPool(const std::any& context, HTTPRequest* req)
108{
109 auto node_context = util::AnyPtr<NodeContext>(context);
110 if (!node_context || !node_context->mempool) {
111 RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
112 return nullptr;
113 }
114 return node_context->mempool.get();
115}
116
124static ChainstateManager* GetChainman(const std::any& context, HTTPRequest* req)
125{
126 auto node_context = util::AnyPtr<NodeContext>(context);
127 if (!node_context || !node_context->chainman) {
129 strprintf("%s:%d (%s)\n"
130 "Internal bug detected: Chainman disabled or instance not found!\n"
131 "You may report this issue here: %s\n",
132 __FILE__, __LINE__, __func__, CLIENT_BUGREPORT));
133 return nullptr;
134 }
135 return node_context->chainman.get();
136}
137
138RESTResponseFormat ParseDataFormat(std::string& param, const std::string& strReq)
139{
140 // Remove query string (if any, separated with '?') as it should not interfere with
141 // parsing param and data format
142 param = strReq.substr(0, strReq.rfind('?'));
143 const std::string::size_type pos_format{param.rfind('.')};
144
145 // No format string is found
146 if (pos_format == std::string::npos) {
148 }
149
150 // Match format string to available formats
151 const std::string suffix(param, pos_format + 1);
152 for (const auto& rf_name : rf_names) {
153 if (suffix == rf_name.name) {
154 param.erase(pos_format);
155 return rf_name.rf;
156 }
157 }
158
159 // If no suffix is found, return RESTResponseFormat::UNDEF and original string without query string
161}
162
163static std::string AvailableDataFormatsString()
164{
165 std::string formats;
166 for (const auto& rf_name : rf_names) {
167 if (strlen(rf_name.name) > 0) {
168 formats.append(".");
169 formats.append(rf_name.name);
170 formats.append(", ");
171 }
172 }
173
174 if (formats.length() > 0)
175 return formats.substr(0, formats.length() - 2);
176
177 return formats;
178}
179
180static bool CheckWarmup(HTTPRequest* req)
181{
182 std::string statusmessage;
183 if (RPCIsInWarmup(&statusmessage))
184 return RESTERR(req, HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: " + statusmessage);
185 return true;
186}
187
188static bool rest_headers(const std::any& context,
189 HTTPRequest* req,
190 const std::string& uri_part)
191{
192 if (!CheckWarmup(req))
193 return false;
194 std::string param;
195 const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
196 std::vector<std::string> path = SplitString(param, '/');
197
198 std::string raw_count;
199 std::string hashStr;
200 if (path.size() == 2) {
201 // deprecated path: /rest/headers/<count>/<hash>
202 hashStr = path[1];
203 raw_count = path[0];
204 } else if (path.size() == 1) {
205 // new path with query parameter: /rest/headers/<hash>?count=<count>
206 hashStr = path[0];
207 try {
208 raw_count = req->GetQueryParameter("count").value_or("5");
209 } catch (const std::runtime_error& e) {
210 return RESTERR(req, HTTP_BAD_REQUEST, e.what());
211 }
212 } else {
213 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/headers/<hash>.<ext>?count=<count>");
214 }
215
216 const auto parsed_count{ToIntegral<size_t>(raw_count)};
217 if (!parsed_count.has_value() || *parsed_count < 1 || *parsed_count > MAX_REST_HEADERS_RESULTS) {
218 return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Header count is invalid or out of acceptable range (1-%u): %s", MAX_REST_HEADERS_RESULTS, raw_count));
219 }
220
221 auto hash{uint256::FromHex(hashStr)};
222 if (!hash) {
223 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
224 }
225
226 const CBlockIndex* tip = nullptr;
227 std::vector<const CBlockIndex*> headers;
228 headers.reserve(*parsed_count);
229 ChainstateManager* maybe_chainman = GetChainman(context, req);
230 if (!maybe_chainman) return false;
231 ChainstateManager& chainman = *maybe_chainman;
232 {
233 LOCK(cs_main);
234 CChain& active_chain = chainman.ActiveChain();
235 tip = active_chain.Tip();
236 const CBlockIndex* pindex{chainman.m_blockman.LookupBlockIndex(*hash)};
237 while (pindex != nullptr && active_chain.Contains(pindex)) {
238 headers.push_back(pindex);
239 if (headers.size() == *parsed_count) {
240 break;
241 }
242 pindex = active_chain.Next(pindex);
243 }
244 }
245
246 switch (rf) {
248 DataStream ssHeader{};
249 for (const CBlockIndex *pindex : headers) {
250 ssHeader << pindex->GetBlockHeader();
251 }
252
253 req->WriteHeader("Content-Type", "application/octet-stream");
254 req->WriteReply(HTTP_OK, ssHeader);
255 return true;
256 }
257
259 DataStream ssHeader{};
260 for (const CBlockIndex *pindex : headers) {
261 ssHeader << pindex->GetBlockHeader();
262 }
263
264 std::string strHex = HexStr(ssHeader) + "\n";
265 req->WriteHeader("Content-Type", "text/plain");
266 req->WriteReply(HTTP_OK, strHex);
267 return true;
268 }
270 UniValue jsonHeaders(UniValue::VARR);
271 for (const CBlockIndex *pindex : headers) {
272 jsonHeaders.push_back(blockheaderToJSON(*tip, *pindex, chainman.GetConsensus().powLimit));
273 }
274 std::string strJSON = jsonHeaders.write() + "\n";
275 req->WriteHeader("Content-Type", "application/json");
276 req->WriteReply(HTTP_OK, strJSON);
277 return true;
278 }
279 default: {
280 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
281 }
282 }
283}
284
288static void SerializeBlockUndo(DataStream& stream, const CBlockUndo& block_undo)
289{
290 WriteCompactSize(stream, block_undo.vtxundo.size() + 1);
291 WriteCompactSize(stream, 0); // block_undo.vtxundo doesn't contain coinbase tx
292 for (const CTxUndo& tx_undo : block_undo.vtxundo) {
293 WriteCompactSize(stream, tx_undo.vprevout.size());
294 for (const Coin& coin : tx_undo.vprevout) {
295 coin.out.Serialize(stream);
296 }
297 }
298}
299
303static void BlockUndoToJSON(const CBlockUndo& block_undo, UniValue& result)
304{
305 result.push_back({UniValue::VARR}); // block_undo.vtxundo doesn't contain coinbase tx
306 for (const CTxUndo& tx_undo : block_undo.vtxundo) {
307 UniValue tx_prevouts(UniValue::VARR);
308 for (const Coin& coin : tx_undo.vprevout) {
309 UniValue prevout(UniValue::VOBJ);
310 prevout.pushKV("value", ValueFromAmount(coin.out.nValue));
311
312 UniValue script_pub_key(UniValue::VOBJ);
313 ScriptToUniv(coin.out.scriptPubKey, /*out=*/script_pub_key, /*include_hex=*/true, /*include_address=*/true);
314 prevout.pushKV("scriptPubKey", std::move(script_pub_key));
315
316 tx_prevouts.push_back(std::move(prevout));
317 }
318 result.push_back(std::move(tx_prevouts));
319 }
320}
321
322static bool rest_spent_txouts(const std::any& context, HTTPRequest* req, const std::string& uri_part)
323{
324 if (!CheckWarmup(req)) {
325 return false;
326 }
327 std::string param;
328 const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
329 std::vector<std::string> path = SplitString(param, '/');
330
331 std::string hashStr;
332 if (path.size() == 1) {
333 // path with query parameter: /rest/spenttxouts/<hash>
334 hashStr = path[0];
335 } else {
336 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/spenttxouts/<hash>.<ext>");
337 }
338
339 auto hash{uint256::FromHex(hashStr)};
340 if (!hash) {
341 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
342 }
343
344 ChainstateManager* chainman = GetChainman(context, req);
345 if (!chainman) {
346 return false;
347 }
348
349 const CBlockIndex* pblockindex = WITH_LOCK(cs_main, return chainman->m_blockman.LookupBlockIndex(*hash));
350 if (!pblockindex) {
351 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
352 }
353
354 CBlockUndo block_undo;
355 if (pblockindex->nHeight > 0 && !chainman->m_blockman.ReadBlockUndo(block_undo, *pblockindex)) {
356 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " undo not available");
357 }
358
359 switch (rf) {
361 DataStream ssSpentResponse{};
362 SerializeBlockUndo(ssSpentResponse, block_undo);
363 req->WriteHeader("Content-Type", "application/octet-stream");
364 req->WriteReply(HTTP_OK, ssSpentResponse);
365 return true;
366 }
367
369 DataStream ssSpentResponse{};
370 SerializeBlockUndo(ssSpentResponse, block_undo);
371 const std::string strHex{HexStr(ssSpentResponse) + "\n"};
372 req->WriteHeader("Content-Type", "text/plain");
373 req->WriteReply(HTTP_OK, strHex);
374 return true;
375 }
376
378 UniValue result(UniValue::VARR);
379 BlockUndoToJSON(block_undo, result);
380 std::string strJSON = result.write() + "\n";
381 req->WriteHeader("Content-Type", "application/json");
382 req->WriteReply(HTTP_OK, strJSON);
383 return true;
384 }
385
386 default: {
387 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
388 }
389 }
390}
391
392static bool rest_block(const std::any& context,
393 HTTPRequest* req,
394 const std::string& uri_part,
395 TxVerbosity tx_verbosity)
396{
397 if (!CheckWarmup(req))
398 return false;
399 std::string hashStr;
400 const RESTResponseFormat rf = ParseDataFormat(hashStr, uri_part);
401
402 auto hash{uint256::FromHex(hashStr)};
403 if (!hash) {
404 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
405 }
406
407 FlatFilePos pos{};
408 const CBlockIndex* pblockindex = nullptr;
409 const CBlockIndex* tip = nullptr;
410 ChainstateManager* maybe_chainman = GetChainman(context, req);
411 if (!maybe_chainman) return false;
412 ChainstateManager& chainman = *maybe_chainman;
413 {
414 LOCK(cs_main);
415 tip = chainman.ActiveChain().Tip();
416 pblockindex = chainman.m_blockman.LookupBlockIndex(*hash);
417 if (!pblockindex) {
418 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
419 }
420 if (!(pblockindex->nStatus & BLOCK_HAVE_DATA)) {
421 if (chainman.m_blockman.IsBlockPruned(*pblockindex)) {
422 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
423 }
424 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (not fully downloaded)");
425 }
426 pos = pblockindex->GetBlockPos();
427 }
428
429 std::vector<std::byte> block_data{};
430 if (!chainman.m_blockman.ReadRawBlock(block_data, pos)) {
431 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
432 }
433
434 switch (rf) {
436 req->WriteHeader("Content-Type", "application/octet-stream");
437 req->WriteReply(HTTP_OK, block_data);
438 return true;
439 }
440
442 const std::string strHex{HexStr(block_data) + "\n"};
443 req->WriteHeader("Content-Type", "text/plain");
444 req->WriteReply(HTTP_OK, strHex);
445 return true;
446 }
447
449 CBlock block{};
450 DataStream block_stream{block_data};
451 block_stream >> TX_WITH_WITNESS(block);
452 UniValue objBlock = blockToJSON(chainman.m_blockman, block, *tip, *pblockindex, tx_verbosity, chainman.GetConsensus().powLimit);
453 std::string strJSON = objBlock.write() + "\n";
454 req->WriteHeader("Content-Type", "application/json");
455 req->WriteReply(HTTP_OK, strJSON);
456 return true;
457 }
458
459 default: {
460 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
461 }
462 }
463}
464
465static bool rest_block_extended(const std::any& context, HTTPRequest* req, const std::string& uri_part)
466{
467 return rest_block(context, req, uri_part, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
468}
469
470static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, const std::string& uri_part)
471{
472 return rest_block(context, req, uri_part, TxVerbosity::SHOW_TXID);
473}
474
475static bool rest_filter_header(const std::any& context, HTTPRequest* req, const std::string& uri_part)
476{
477 if (!CheckWarmup(req)) return false;
478
479 std::string param;
480 const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
481
482 std::vector<std::string> uri_parts = SplitString(param, '/');
483 std::string raw_count;
484 std::string raw_blockhash;
485 if (uri_parts.size() == 3) {
486 // deprecated path: /rest/blockfilterheaders/<filtertype>/<count>/<blockhash>
487 raw_blockhash = uri_parts[2];
488 raw_count = uri_parts[1];
489 } else if (uri_parts.size() == 2) {
490 // new path with query parameter: /rest/blockfilterheaders/<filtertype>/<blockhash>?count=<count>
491 raw_blockhash = uri_parts[1];
492 try {
493 raw_count = req->GetQueryParameter("count").value_or("5");
494 } catch (const std::runtime_error& e) {
495 return RESTERR(req, HTTP_BAD_REQUEST, e.what());
496 }
497 } else {
498 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/blockfilterheaders/<filtertype>/<blockhash>.<ext>?count=<count>");
499 }
500
501 const auto parsed_count{ToIntegral<size_t>(raw_count)};
502 if (!parsed_count.has_value() || *parsed_count < 1 || *parsed_count > MAX_REST_HEADERS_RESULTS) {
503 return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Header count is invalid or out of acceptable range (1-%u): %s", MAX_REST_HEADERS_RESULTS, raw_count));
504 }
505
506 auto block_hash{uint256::FromHex(raw_blockhash)};
507 if (!block_hash) {
508 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + raw_blockhash);
509 }
510
511 BlockFilterType filtertype;
512 if (!BlockFilterTypeByName(uri_parts[0], filtertype)) {
513 return RESTERR(req, HTTP_BAD_REQUEST, "Unknown filtertype " + uri_parts[0]);
514 }
515
516 BlockFilterIndex* index = GetBlockFilterIndex(filtertype);
517 if (!index) {
518 return RESTERR(req, HTTP_BAD_REQUEST, "Index is not enabled for filtertype " + uri_parts[0]);
519 }
520
521 std::vector<const CBlockIndex*> headers;
522 headers.reserve(*parsed_count);
523 {
524 ChainstateManager* maybe_chainman = GetChainman(context, req);
525 if (!maybe_chainman) return false;
526 ChainstateManager& chainman = *maybe_chainman;
527 LOCK(cs_main);
528 CChain& active_chain = chainman.ActiveChain();
529 const CBlockIndex* pindex{chainman.m_blockman.LookupBlockIndex(*block_hash)};
530 while (pindex != nullptr && active_chain.Contains(pindex)) {
531 headers.push_back(pindex);
532 if (headers.size() == *parsed_count)
533 break;
534 pindex = active_chain.Next(pindex);
535 }
536 }
537
538 bool index_ready = index->BlockUntilSyncedToCurrentChain();
539
540 std::vector<uint256> filter_headers;
541 filter_headers.reserve(*parsed_count);
542 for (const CBlockIndex* pindex : headers) {
543 uint256 filter_header;
544 if (!index->LookupFilterHeader(pindex, filter_header)) {
545 std::string errmsg = "Filter not found.";
546
547 if (!index_ready) {
548 errmsg += " Block filters are still in the process of being indexed.";
549 } else {
550 errmsg += " This error is unexpected and indicates index corruption.";
551 }
552
553 return RESTERR(req, HTTP_NOT_FOUND, errmsg);
554 }
555 filter_headers.push_back(filter_header);
556 }
557
558 switch (rf) {
560 DataStream ssHeader{};
561 for (const uint256& header : filter_headers) {
562 ssHeader << header;
563 }
564
565 req->WriteHeader("Content-Type", "application/octet-stream");
566 req->WriteReply(HTTP_OK, ssHeader);
567 return true;
568 }
570 DataStream ssHeader{};
571 for (const uint256& header : filter_headers) {
572 ssHeader << header;
573 }
574
575 std::string strHex = HexStr(ssHeader) + "\n";
576 req->WriteHeader("Content-Type", "text/plain");
577 req->WriteReply(HTTP_OK, strHex);
578 return true;
579 }
581 UniValue jsonHeaders(UniValue::VARR);
582 for (const uint256& header : filter_headers) {
583 jsonHeaders.push_back(header.GetHex());
584 }
585
586 std::string strJSON = jsonHeaders.write() + "\n";
587 req->WriteHeader("Content-Type", "application/json");
588 req->WriteReply(HTTP_OK, strJSON);
589 return true;
590 }
591 default: {
592 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
593 }
594 }
595}
596
597static bool rest_block_filter(const std::any& context, HTTPRequest* req, const std::string& uri_part)
598{
599 if (!CheckWarmup(req)) return false;
600
601 std::string param;
602 const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
603
604 // request is sent over URI scheme /rest/blockfilter/filtertype/blockhash
605 std::vector<std::string> uri_parts = SplitString(param, '/');
606 if (uri_parts.size() != 2) {
607 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/blockfilter/<filtertype>/<blockhash>");
608 }
609
610 auto block_hash{uint256::FromHex(uri_parts[1])};
611 if (!block_hash) {
612 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + uri_parts[1]);
613 }
614
615 BlockFilterType filtertype;
616 if (!BlockFilterTypeByName(uri_parts[0], filtertype)) {
617 return RESTERR(req, HTTP_BAD_REQUEST, "Unknown filtertype " + uri_parts[0]);
618 }
619
620 BlockFilterIndex* index = GetBlockFilterIndex(filtertype);
621 if (!index) {
622 return RESTERR(req, HTTP_BAD_REQUEST, "Index is not enabled for filtertype " + uri_parts[0]);
623 }
624
625 const CBlockIndex* block_index;
626 bool block_was_connected;
627 {
628 ChainstateManager* maybe_chainman = GetChainman(context, req);
629 if (!maybe_chainman) return false;
630 ChainstateManager& chainman = *maybe_chainman;
631 LOCK(cs_main);
632 block_index = chainman.m_blockman.LookupBlockIndex(*block_hash);
633 if (!block_index) {
634 return RESTERR(req, HTTP_NOT_FOUND, uri_parts[1] + " not found");
635 }
636 block_was_connected = block_index->IsValid(BLOCK_VALID_SCRIPTS);
637 }
638
639 bool index_ready = index->BlockUntilSyncedToCurrentChain();
640
641 BlockFilter filter;
642 if (!index->LookupFilter(block_index, filter)) {
643 std::string errmsg = "Filter not found.";
644
645 if (!block_was_connected) {
646 errmsg += " Block was not connected to active chain.";
647 } else if (!index_ready) {
648 errmsg += " Block filters are still in the process of being indexed.";
649 } else {
650 errmsg += " This error is unexpected and indicates index corruption.";
651 }
652
653 return RESTERR(req, HTTP_NOT_FOUND, errmsg);
654 }
655
656 switch (rf) {
658 DataStream ssResp{};
659 ssResp << filter;
660
661 req->WriteHeader("Content-Type", "application/octet-stream");
662 req->WriteReply(HTTP_OK, ssResp);
663 return true;
664 }
666 DataStream ssResp{};
667 ssResp << filter;
668
669 std::string strHex = HexStr(ssResp) + "\n";
670 req->WriteHeader("Content-Type", "text/plain");
671 req->WriteReply(HTTP_OK, strHex);
672 return true;
673 }
676 ret.pushKV("filter", HexStr(filter.GetEncodedFilter()));
677 std::string strJSON = ret.write() + "\n";
678 req->WriteHeader("Content-Type", "application/json");
679 req->WriteReply(HTTP_OK, strJSON);
680 return true;
681 }
682 default: {
683 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
684 }
685 }
686}
687
688// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
690
691static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std::string& uri_part)
692{
693 if (!CheckWarmup(req))
694 return false;
695 std::string param;
696 const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
697
698 switch (rf) {
700 JSONRPCRequest jsonRequest;
701 jsonRequest.context = context;
702 jsonRequest.params = UniValue(UniValue::VARR);
703 UniValue chainInfoObject = getblockchaininfo().HandleRequest(jsonRequest);
704 std::string strJSON = chainInfoObject.write() + "\n";
705 req->WriteHeader("Content-Type", "application/json");
706 req->WriteReply(HTTP_OK, strJSON);
707 return true;
708 }
709 default: {
710 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
711 }
712 }
713}
714
715
717
718static bool rest_deploymentinfo(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
719{
720 if (!CheckWarmup(req)) return false;
721
722 std::string hash_str;
723 const RESTResponseFormat rf = ParseDataFormat(hash_str, str_uri_part);
724
725 switch (rf) {
727 JSONRPCRequest jsonRequest;
728 jsonRequest.context = context;
729 jsonRequest.params = UniValue(UniValue::VARR);
730
731 if (!hash_str.empty()) {
732 auto hash{uint256::FromHex(hash_str)};
733 if (!hash) {
734 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hash_str);
735 }
736
737 const ChainstateManager* chainman = GetChainman(context, req);
738 if (!chainman) return false;
739 if (!WITH_LOCK(::cs_main, return chainman->m_blockman.LookupBlockIndex(*hash))) {
740 return RESTERR(req, HTTP_BAD_REQUEST, "Block not found");
741 }
742
743 jsonRequest.params.push_back(hash_str);
744 }
745
746 req->WriteHeader("Content-Type", "application/json");
747 req->WriteReply(HTTP_OK, getdeploymentinfo().HandleRequest(jsonRequest).write() + "\n");
748 return true;
749 }
750 default: {
751 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
752 }
753 }
754
755}
756
757static bool rest_mempool(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
758{
759 if (!CheckWarmup(req))
760 return false;
761
762 std::string param;
763 const RESTResponseFormat rf = ParseDataFormat(param, str_uri_part);
764 if (param != "contents" && param != "info") {
765 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/mempool/<info|contents>.json");
766 }
767
768 const CTxMemPool* mempool = GetMemPool(context, req);
769 if (!mempool) return false;
770
771 switch (rf) {
773 std::string str_json;
774 if (param == "contents") {
775 std::string raw_verbose;
776 try {
777 raw_verbose = req->GetQueryParameter("verbose").value_or("true");
778 } catch (const std::runtime_error& e) {
779 return RESTERR(req, HTTP_BAD_REQUEST, e.what());
780 }
781 if (raw_verbose != "true" && raw_verbose != "false") {
782 return RESTERR(req, HTTP_BAD_REQUEST, "The \"verbose\" query parameter must be either \"true\" or \"false\".");
783 }
784 std::string raw_mempool_sequence;
785 try {
786 raw_mempool_sequence = req->GetQueryParameter("mempool_sequence").value_or("false");
787 } catch (const std::runtime_error& e) {
788 return RESTERR(req, HTTP_BAD_REQUEST, e.what());
789 }
790 if (raw_mempool_sequence != "true" && raw_mempool_sequence != "false") {
791 return RESTERR(req, HTTP_BAD_REQUEST, "The \"mempool_sequence\" query parameter must be either \"true\" or \"false\".");
792 }
793 const bool verbose{raw_verbose == "true"};
794 const bool mempool_sequence{raw_mempool_sequence == "true"};
795 if (verbose && mempool_sequence) {
796 return RESTERR(req, HTTP_BAD_REQUEST, "Verbose results cannot contain mempool sequence values. (hint: set \"verbose=false\")");
797 }
798 str_json = MempoolToJSON(*mempool, verbose, mempool_sequence).write() + "\n";
799 } else {
800 str_json = MempoolInfoToJSON(*mempool).write() + "\n";
801 }
802
803 req->WriteHeader("Content-Type", "application/json");
804 req->WriteReply(HTTP_OK, str_json);
805 return true;
806 }
807 default: {
808 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
809 }
810 }
811}
812
813static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string& uri_part)
814{
815 if (!CheckWarmup(req))
816 return false;
817 std::string hashStr;
818 const RESTResponseFormat rf = ParseDataFormat(hashStr, uri_part);
819
820 auto hash{Txid::FromHex(hashStr)};
821 if (!hash) {
822 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
823 }
824
825 if (g_txindex) {
826 g_txindex->BlockUntilSyncedToCurrentChain();
827 }
828
829 const NodeContext* const node = GetNodeContext(context, req);
830 if (!node) return false;
831 uint256 hashBlock = uint256();
832 const CTransactionRef tx{GetTransaction(/*block_index=*/nullptr, node->mempool.get(), *hash, hashBlock, node->chainman->m_blockman)};
833 if (!tx) {
834 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
835 }
836
837 switch (rf) {
839 DataStream ssTx;
840 ssTx << TX_WITH_WITNESS(tx);
841
842 req->WriteHeader("Content-Type", "application/octet-stream");
843 req->WriteReply(HTTP_OK, ssTx);
844 return true;
845 }
846
848 DataStream ssTx;
849 ssTx << TX_WITH_WITNESS(tx);
850
851 std::string strHex = HexStr(ssTx) + "\n";
852 req->WriteHeader("Content-Type", "text/plain");
853 req->WriteReply(HTTP_OK, strHex);
854 return true;
855 }
856
859 TxToUniv(*tx, /*block_hash=*/hashBlock, /*entry=*/ objTx);
860 std::string strJSON = objTx.write() + "\n";
861 req->WriteHeader("Content-Type", "application/json");
862 req->WriteReply(HTTP_OK, strJSON);
863 return true;
864 }
865
866 default: {
867 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
868 }
869 }
870}
871
872static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::string& uri_part)
873{
874 if (!CheckWarmup(req))
875 return false;
876 std::string param;
877 const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
878
879 std::vector<std::string> uriParts;
880 if (param.length() > 1)
881 {
882 std::string strUriParams = param.substr(1);
883 uriParts = SplitString(strUriParams, '/');
884 }
885
886 // throw exception in case of an empty request
887 std::string strRequestMutable = req->ReadBody();
888 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
889 return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
890
891 bool fInputParsed = false;
892 bool fCheckMemPool = false;
893 std::vector<COutPoint> vOutPoints;
894
895 // parse/deserialize input
896 // input-format = output-format, rest/getutxos/bin requires binary input, gives binary output, ...
897
898 if (uriParts.size() > 0)
899 {
900 //inputs is sent over URI scheme (/rest/getutxos/checkmempool/txid1-n/txid2-n/...)
901 if (uriParts[0] == "checkmempool") fCheckMemPool = true;
902
903 for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
904 {
905 const auto txid_out{util::Split<std::string_view>(uriParts[i], '-')};
906 if (txid_out.size() != 2) {
907 return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
908 }
909 auto txid{Txid::FromHex(txid_out.at(0))};
910 auto output{ToIntegral<uint32_t>(txid_out.at(1))};
911
912 if (!txid || !output) {
913 return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
914 }
915
916 vOutPoints.emplace_back(*txid, *output);
917 }
918
919 if (vOutPoints.size() > 0)
920 fInputParsed = true;
921 else
922 return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
923 }
924
925 switch (rf) {
927 // convert hex to bin, continue then with bin part
928 std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable);
929 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
930 [[fallthrough]];
931 }
932
934 try {
935 //deserialize only if user sent a request
936 if (strRequestMutable.size() > 0)
937 {
938 if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA
939 return RESTERR(req, HTTP_BAD_REQUEST, "Combination of URI scheme inputs and raw post data is not allowed");
940
941 DataStream oss{};
942 oss << strRequestMutable;
943 oss >> fCheckMemPool;
944 oss >> vOutPoints;
945 }
946 } catch (const std::ios_base::failure&) {
947 // abort in case of unreadable binary data
948 return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
949 }
950 break;
951 }
952
954 if (!fInputParsed)
955 return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
956 break;
957 }
958 default: {
959 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
960 }
961 }
962
963 // limit max outpoints
964 if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
965 return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
966
967 // check spentness and form a bitmap (as well as a JSON capable human-readable string representation)
968 std::vector<unsigned char> bitmap;
969 std::vector<CCoin> outs;
970 std::string bitmapStringRepresentation;
971 std::vector<bool> hits;
972 bitmap.resize((vOutPoints.size() + 7) / 8);
973 ChainstateManager* maybe_chainman = GetChainman(context, req);
974 if (!maybe_chainman) return false;
975 ChainstateManager& chainman = *maybe_chainman;
976 decltype(chainman.ActiveHeight()) active_height;
977 uint256 active_hash;
978 {
979 auto process_utxos = [&vOutPoints, &outs, &hits, &active_height, &active_hash, &chainman](const CCoinsView& view, const CTxMemPool* mempool) EXCLUSIVE_LOCKS_REQUIRED(chainman.GetMutex()) {
980 for (const COutPoint& vOutPoint : vOutPoints) {
981 auto coin = !mempool || !mempool->isSpent(vOutPoint) ? view.GetCoin(vOutPoint) : std::nullopt;
982 hits.push_back(coin.has_value());
983 if (coin) outs.emplace_back(std::move(*coin));
984 }
985 active_height = chainman.ActiveHeight();
986 active_hash = chainman.ActiveTip()->GetBlockHash();
987 };
988
989 if (fCheckMemPool) {
990 const CTxMemPool* mempool = GetMemPool(context, req);
991 if (!mempool) return false;
992 // use db+mempool as cache backend in case user likes to query mempool
993 LOCK2(cs_main, mempool->cs);
994 CCoinsViewCache& viewChain = chainman.ActiveChainstate().CoinsTip();
995 CCoinsViewMemPool viewMempool(&viewChain, *mempool);
996 process_utxos(viewMempool, mempool);
997 } else {
998 LOCK(cs_main);
999 process_utxos(chainman.ActiveChainstate().CoinsTip(), nullptr);
1000 }
1001
1002 for (size_t i = 0; i < hits.size(); ++i) {
1003 const bool hit = hits[i];
1004 bitmapStringRepresentation.append(hit ? "1" : "0"); // form a binary string representation (human-readable for json output)
1005 bitmap[i / 8] |= ((uint8_t)hit) << (i % 8);
1006 }
1007 }
1008
1009 switch (rf) {
1011 // serialize data
1012 // use exact same output as mentioned in Bip64
1013 DataStream ssGetUTXOResponse{};
1014 ssGetUTXOResponse << active_height << active_hash << bitmap << outs;
1015
1016 req->WriteHeader("Content-Type", "application/octet-stream");
1017 req->WriteReply(HTTP_OK, ssGetUTXOResponse);
1018 return true;
1019 }
1020
1022 DataStream ssGetUTXOResponse{};
1023 ssGetUTXOResponse << active_height << active_hash << bitmap << outs;
1024 std::string strHex = HexStr(ssGetUTXOResponse) + "\n";
1025
1026 req->WriteHeader("Content-Type", "text/plain");
1027 req->WriteReply(HTTP_OK, strHex);
1028 return true;
1029 }
1030
1032 UniValue objGetUTXOResponse(UniValue::VOBJ);
1033
1034 // pack in some essentials
1035 // use more or less the same output as mentioned in Bip64
1036 objGetUTXOResponse.pushKV("chainHeight", active_height);
1037 objGetUTXOResponse.pushKV("chaintipHash", active_hash.GetHex());
1038 objGetUTXOResponse.pushKV("bitmap", bitmapStringRepresentation);
1039
1040 UniValue utxos(UniValue::VARR);
1041 for (const CCoin& coin : outs) {
1043 utxo.pushKV("height", (int32_t)coin.nHeight);
1044 utxo.pushKV("value", ValueFromAmount(coin.out.nValue));
1045
1046 // include the script in a json output
1048 ScriptToUniv(coin.out.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
1049 utxo.pushKV("scriptPubKey", std::move(o));
1050 utxos.push_back(std::move(utxo));
1051 }
1052 objGetUTXOResponse.pushKV("utxos", std::move(utxos));
1053
1054 // return json string
1055 std::string strJSON = objGetUTXOResponse.write() + "\n";
1056 req->WriteHeader("Content-Type", "application/json");
1057 req->WriteReply(HTTP_OK, strJSON);
1058 return true;
1059 }
1060 default: {
1061 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
1062 }
1063 }
1064}
1065
1066static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
1067 const std::string& str_uri_part)
1068{
1069 if (!CheckWarmup(req)) return false;
1070 std::string height_str;
1071 const RESTResponseFormat rf = ParseDataFormat(height_str, str_uri_part);
1072
1073 const auto blockheight{ToIntegral<int32_t>(height_str)};
1074 if (!blockheight || *blockheight < 0) {
1075 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid height: " + SanitizeString(height_str, SAFE_CHARS_URI));
1076 }
1077
1078 CBlockIndex* pblockindex = nullptr;
1079 {
1080 ChainstateManager* maybe_chainman = GetChainman(context, req);
1081 if (!maybe_chainman) return false;
1082 ChainstateManager& chainman = *maybe_chainman;
1083 LOCK(cs_main);
1084 const CChain& active_chain = chainman.ActiveChain();
1085 if (*blockheight > active_chain.Height()) {
1086 return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
1087 }
1088 pblockindex = active_chain[*blockheight];
1089 }
1090 switch (rf) {
1092 DataStream ss_blockhash{};
1093 ss_blockhash << pblockindex->GetBlockHash();
1094 req->WriteHeader("Content-Type", "application/octet-stream");
1095 req->WriteReply(HTTP_OK, ss_blockhash);
1096 return true;
1097 }
1099 req->WriteHeader("Content-Type", "text/plain");
1100 req->WriteReply(HTTP_OK, pblockindex->GetBlockHash().GetHex() + "\n");
1101 return true;
1102 }
1104 req->WriteHeader("Content-Type", "application/json");
1106 resp.pushKV("blockhash", pblockindex->GetBlockHash().GetHex());
1107 req->WriteReply(HTTP_OK, resp.write() + "\n");
1108 return true;
1109 }
1110 default: {
1111 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
1112 }
1113 }
1114}
1115
1116static const struct {
1117 const char* prefix;
1118 bool (*handler)(const std::any& context, HTTPRequest* req, const std::string& strReq);
1119} uri_prefixes[] = {
1120 {"/rest/tx/", rest_tx},
1121 {"/rest/block/notxdetails/", rest_block_notxdetails},
1122 {"/rest/block/", rest_block_extended},
1123 {"/rest/blockfilter/", rest_block_filter},
1124 {"/rest/blockfilterheaders/", rest_filter_header},
1125 {"/rest/chaininfo", rest_chaininfo},
1126 {"/rest/mempool/", rest_mempool},
1127 {"/rest/headers/", rest_headers},
1128 {"/rest/getutxos", rest_getutxos},
1129 {"/rest/deploymentinfo/", rest_deploymentinfo},
1130 {"/rest/deploymentinfo", rest_deploymentinfo},
1131 {"/rest/blockhashbyheight/", rest_blockhash_by_height},
1132 {"/rest/spenttxouts/", rest_spent_txouts},
1134
1135void StartREST(const std::any& context)
1136{
1137 for (const auto& up : uri_prefixes) {
1138 auto handler = [context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); };
1139 RegisterHTTPHandler(up.prefix, false, handler);
1140 }
1141}
1142
1144{
1145}
1146
1148{
1149 for (const auto& up : uri_prefixes) {
1150 UnregisterHTTPHandler(up.prefix, false);
1151 }
1152}
int ret
UniValue blockToJSON(BlockManager &blockman, const CBlock &block, const CBlockIndex &tip, const CBlockIndex &blockindex, TxVerbosity verbosity, const uint256 pow_limit)
Block description to JSON.
Definition: blockchain.cpp:181
UniValue blockheaderToJSON(const CBlockIndex &tip, const CBlockIndex &blockindex, const uint256 pow_limit)
Block header to JSON.
Definition: blockchain.cpp:151
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
BlockFilterType
Definition: blockfilter.h:93
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
@ BLOCK_VALID_SCRIPTS
Scripts & signatures ok.
Definition: chain.h:115
@ BLOCK_HAVE_DATA
full block available in blk*.dat
Definition: chain.h:121
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:115
const std::vector< unsigned char > & GetEncodedFilter() const LIFETIMEBOUND
Definition: blockfilter.h:138
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.
Definition: block.h:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:141
bool IsValid(enum BlockStatus nUpTo) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
Definition: chain.h:295
uint256 GetBlockHash() const
Definition: chain.h:243
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:153
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: chain.h:208
Undo information for a CBlock.
Definition: undo.h:63
std::vector< CTxUndo > vtxundo
Definition: undo.h:65
An in-memory indexed chain of blocks.
Definition: chain.h:417
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:433
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
Definition: chain.h:453
int Height() const
Return the maximal height in the chain.
Definition: chain.h:462
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:447
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:363
Abstract view on the open txout dataset.
Definition: coins.h:310
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:919
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:281
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:367
An output of a transaction.
Definition: transaction.h:150
CScript scriptPubKey
Definition: transaction.h:153
CAmount nValue
Definition: transaction.h:152
Undo information for a CTransaction.
Definition: undo.h:53
std::vector< Coin > vprevout
Definition: undo.h:56
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:898
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:1122
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
Definition: validation.h:1031
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1125
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1124
const Consensus::Params & GetConsensus() const
Definition: validation.h:1007
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1123
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1037
A UTXO entry.
Definition: coins.h:33
CTxOut out
unspent transaction output
Definition: coins.h:36
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:130
In-flight HTTP request.
Definition: httpserver.h:71
std::optional< std::string > GetQueryParameter(const std::string &key) const
Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not...
Definition: httpserver.cpp:725
void WriteReply(int nStatus, std::string_view reply="")
Write HTTP reply.
Definition: httpserver.h:141
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:642
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:622
UniValue params
Definition: request.h:57
std::any context
Definition: request.h:62
UniValue HandleRequest(const JSONRPCRequest &request) const
Definition: util.cpp:638
void push_back(UniValue val)
Definition: univalue.cpp:104
@ VOBJ
Definition: univalue.h:24
@ VARR
Definition: univalue.h:24
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:126
std::string GetHex() const
Definition: uint256.cpp:11
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool ReadBlockUndo(CBlockUndo &blockundo, const CBlockIndex &index) const
bool ReadRawBlock(std::vector< std::byte > &block, const FlatFilePos &pos) const
static std::optional< transaction_identifier > FromHex(std::string_view hex)
256-bit opaque blob.
Definition: uint256.h:196
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:198
void TxToUniv(const CTransaction &tx, const uint256 &block_hash, UniValue &entry, bool include_hex=true, const CTxUndo *txundo=nullptr, TxVerbosity verbosity=TxVerbosity::SHOW_DETAILS)
Definition: core_write.cpp:171
TxVerbosity
Verbose level for block's transaction.
Definition: core_io.h:28
@ SHOW_DETAILS_AND_PREVOUT
The same as previous option with information about prevouts if available.
@ SHOW_TXID
Only TXID for each block's transaction.
void ScriptToUniv(const CScript &script, UniValue &out, bool include_hex=true, bool include_address=false, const SigningProvider *provider=nullptr)
Definition: core_write.cpp:150
UniValue ValueFromAmount(const CAmount amount)
Definition: core_write.cpp:26
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:766
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:759
Definition: messages.h:20
CTransactionRef GetTransaction(const CBlockIndex *const block_index, const CTxMemPool *const mempool, const Txid &hash, uint256 &hashBlock, const BlockManager &blockman)
Return transaction with a given hash.
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:148
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
static constexpr unsigned int MAX_REST_HEADERS_RESULTS
Definition: rest.cpp:46
static bool rest_headers(const std::any &context, HTTPRequest *req, const std::string &uri_part)
Definition: rest.cpp:188
static void SerializeBlockUndo(DataStream &stream, const CBlockUndo &block_undo)
Serialize spent outputs as a list of per-transaction CTxOut lists using binary format.
Definition: rest.cpp:288
static bool rest_block_extended(const std::any &context, HTTPRequest *req, const std::string &uri_part)
Definition: rest.cpp:465
static bool rest_blockhash_by_height(const std::any &context, HTTPRequest *req, const std::string &str_uri_part)
Definition: rest.cpp:1066
RESTResponseFormat rf
Definition: rest.cpp:49
static bool rest_block_filter(const std::any &context, HTTPRequest *req, const std::string &uri_part)
Definition: rest.cpp:597
static bool rest_block(const std::any &context, HTTPRequest *req, const std::string &uri_part, TxVerbosity tx_verbosity)
Definition: rest.cpp:392
const char * prefix
Definition: rest.cpp:1117
static bool rest_block_notxdetails(const std::any &context, HTTPRequest *req, const std::string &uri_part)
Definition: rest.cpp:470
void StartREST(const std::any &context)
Start HTTP REST subsystem.
Definition: rest.cpp:1135
static bool rest_filter_header(const std::any &context, HTTPRequest *req, const std::string &uri_part)
Definition: rest.cpp:475
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1118
static bool rest_getutxos(const std::any &context, HTTPRequest *req, const std::string &uri_part)
Definition: rest.cpp:872
static bool rest_tx(const std::any &context, HTTPRequest *req, const std::string &uri_part)
Definition: rest.cpp:813
static const struct @10 uri_prefixes[]
void StopREST()
Stop HTTP REST subsystem.
Definition: rest.cpp:1147
RPCHelpMan getdeploymentinfo()
const char * name
Definition: rest.cpp:50
RPCHelpMan getblockchaininfo()
void InterruptREST()
Interrupt RPC REST subsystem.
Definition: rest.cpp:1143
static bool rest_deploymentinfo(const std::any &context, HTTPRequest *req, const std::string &str_uri_part)
Definition: rest.cpp:718
static bool RESTERR(HTTPRequest *req, enum HTTPStatusCode status, std::string message)
Definition: rest.cpp:72
static bool rest_mempool(const std::any &context, HTTPRequest *req, const std::string &str_uri_part)
Definition: rest.cpp:757
static const struct @9 rf_names[]
static bool CheckWarmup(HTTPRequest *req)
Definition: rest.cpp:180
static ChainstateManager * GetChainman(const std::any &context, HTTPRequest *req)
Get the node context chainstatemanager.
Definition: rest.cpp:124
static void BlockUndoToJSON(const CBlockUndo &block_undo, UniValue &result)
Serialize spent outputs as a list of per-transaction CTxOut lists using JSON format.
Definition: rest.cpp:303
static bool rest_chaininfo(const std::any &context, HTTPRequest *req, const std::string &uri_part)
Definition: rest.cpp:691
static bool rest_spent_txouts(const std::any &context, HTTPRequest *req, const std::string &uri_part)
Definition: rest.cpp:322
RESTResponseFormat ParseDataFormat(std::string &param, const std::string &strReq)
Parse a URI to get the data format and URI without data format and query string.
Definition: rest.cpp:138
static CTxMemPool * GetMemPool(const std::any &context, HTTPRequest *req)
Get the node context mempool.
Definition: rest.cpp:107
static const size_t MAX_GETUTXOS_OUTPOINTS
Definition: rest.cpp:45
static std::string AvailableDataFormatsString()
Definition: rest.cpp:163
static NodeContext * GetNodeContext(const std::any &context, HTTPRequest *req)
Get the node context.
Definition: rest.cpp:86
RESTResponseFormat
Definition: rest.h:10
UniValue MempoolInfoToJSON(const CTxMemPool &pool)
Mempool information to JSON.
Definition: mempool.cpp:676
UniValue MempoolToJSON(const CTxMemPool &pool, bool verbose, bool include_mempool_sequence)
Mempool to JSON.
Definition: mempool.cpp:346
HTTPStatusCode
HTTP status codes.
Definition: protocol.h:11
@ HTTP_BAD_REQUEST
Definition: protocol.h:14
@ HTTP_OK
Definition: protocol.h:12
@ HTTP_SERVICE_UNAVAILABLE
Definition: protocol.h:20
@ HTTP_NOT_FOUND
Definition: protocol.h:17
@ HTTP_INTERNAL_SERVER_ERROR
Definition: protocol.h:19
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Definition: serialize.h:1088
#define READWRITE(...)
Definition: serialize.h:145
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:333
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:68
@ SAFE_CHARS_URI
Chars allowed in URIs (RFC 3986)
Definition: strencodings.h:35
Definition: rest.cpp:58
CTxOut out
Definition: rest.cpp:60
CCoin(Coin &&in)
Definition: rest.cpp:63
uint32_t nHeight
Definition: rest.cpp:59
CCoin()
Definition: rest.cpp:62
SERIALIZE_METHODS(CCoin, obj)
Definition: rest.cpp:65
uint256 powLimit
Proof of work parameters.
Definition: params.h:111
NodeContext struct containing references to chain state and connection state.
Definition: context.h:56
#define LOCK2(cs1, cs2)
Definition: sync.h:260
#define LOCK(cs)
Definition: sync.h:259
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:290
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.
Definition: txindex.cpp:17
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.