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