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  auto hash{uint256::FromHex(hashStr)};
221  if (!hash) {
222  return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
223  }
224 
225  const CBlockIndex* tip = nullptr;
226  std::vector<const CBlockIndex*> headers;
227  headers.reserve(*parsed_count);
228  {
229  ChainstateManager* maybe_chainman = GetChainman(context, req);
230  if (!maybe_chainman) return false;
231  ChainstateManager& chainman = *maybe_chainman;
232  LOCK(cs_main);
233  CChain& active_chain = chainman.ActiveChain();
234  tip = active_chain.Tip();
235  const CBlockIndex* pindex{chainman.m_blockman.LookupBlockIndex(*hash)};
236  while (pindex != nullptr && active_chain.Contains(pindex)) {
237  headers.push_back(pindex);
238  if (headers.size() == *parsed_count) {
239  break;
240  }
241  pindex = active_chain.Next(pindex);
242  }
243  }
244 
245  switch (rf) {
247  DataStream ssHeader{};
248  for (const CBlockIndex *pindex : headers) {
249  ssHeader << pindex->GetBlockHeader();
250  }
251 
252  req->WriteHeader("Content-Type", "application/octet-stream");
253  req->WriteReply(HTTP_OK, ssHeader);
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  auto hash{uint256::FromHex(hashStr)};
295  if (!hash) {
296  return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
297  }
298 
299  FlatFilePos pos{};
300  const CBlockIndex* pblockindex = nullptr;
301  const CBlockIndex* tip = nullptr;
302  ChainstateManager* maybe_chainman = GetChainman(context, req);
303  if (!maybe_chainman) return false;
304  ChainstateManager& chainman = *maybe_chainman;
305  {
306  LOCK(cs_main);
307  tip = chainman.ActiveChain().Tip();
308  pblockindex = chainman.m_blockman.LookupBlockIndex(*hash);
309  if (!pblockindex) {
310  return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
311  }
312  if (chainman.m_blockman.IsBlockPruned(*pblockindex)) {
313  return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
314  }
315  pos = pblockindex->GetBlockPos();
316  }
317 
318  std::vector<uint8_t> block_data{};
319  if (!chainman.m_blockman.ReadRawBlockFromDisk(block_data, pos)) {
320  return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
321  }
322 
323  switch (rf) {
325  req->WriteHeader("Content-Type", "application/octet-stream");
326  req->WriteReply(HTTP_OK, std::as_bytes(std::span{block_data}));
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  auto block_hash{uint256::FromHex(raw_blockhash)};
396  if (!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  req->WriteHeader("Content-Type", "application/octet-stream");
455  req->WriteReply(HTTP_OK, ssHeader);
456  return true;
457  }
459  DataStream ssHeader{};
460  for (const uint256& header : filter_headers) {
461  ssHeader << header;
462  }
463 
464  std::string strHex = HexStr(ssHeader) + "\n";
465  req->WriteHeader("Content-Type", "text/plain");
466  req->WriteReply(HTTP_OK, strHex);
467  return true;
468  }
470  UniValue jsonHeaders(UniValue::VARR);
471  for (const uint256& header : filter_headers) {
472  jsonHeaders.push_back(header.GetHex());
473  }
474 
475  std::string strJSON = jsonHeaders.write() + "\n";
476  req->WriteHeader("Content-Type", "application/json");
477  req->WriteReply(HTTP_OK, strJSON);
478  return true;
479  }
480  default: {
481  return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
482  }
483  }
484 }
485 
486 static bool rest_block_filter(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
487 {
488  if (!CheckWarmup(req)) return false;
489 
490  std::string param;
491  const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
492 
493  // request is sent over URI scheme /rest/blockfilter/filtertype/blockhash
494  std::vector<std::string> uri_parts = SplitString(param, '/');
495  if (uri_parts.size() != 2) {
496  return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/blockfilter/<filtertype>/<blockhash>");
497  }
498 
499  auto block_hash{uint256::FromHex(uri_parts[1])};
500  if (!block_hash) {
501  return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + uri_parts[1]);
502  }
503 
504  BlockFilterType filtertype;
505  if (!BlockFilterTypeByName(uri_parts[0], filtertype)) {
506  return RESTERR(req, HTTP_BAD_REQUEST, "Unknown filtertype " + uri_parts[0]);
507  }
508 
509  BlockFilterIndex* index = GetBlockFilterIndex(filtertype);
510  if (!index) {
511  return RESTERR(req, HTTP_BAD_REQUEST, "Index is not enabled for filtertype " + uri_parts[0]);
512  }
513 
514  const CBlockIndex* block_index;
515  bool block_was_connected;
516  {
517  ChainstateManager* maybe_chainman = GetChainman(context, req);
518  if (!maybe_chainman) return false;
519  ChainstateManager& chainman = *maybe_chainman;
520  LOCK(cs_main);
521  block_index = chainman.m_blockman.LookupBlockIndex(*block_hash);
522  if (!block_index) {
523  return RESTERR(req, HTTP_NOT_FOUND, uri_parts[1] + " not found");
524  }
525  block_was_connected = block_index->IsValid(BLOCK_VALID_SCRIPTS);
526  }
527 
528  bool index_ready = index->BlockUntilSyncedToCurrentChain();
529 
530  BlockFilter filter;
531  if (!index->LookupFilter(block_index, filter)) {
532  std::string errmsg = "Filter not found.";
533 
534  if (!block_was_connected) {
535  errmsg += " Block was not connected to active chain.";
536  } else if (!index_ready) {
537  errmsg += " Block filters are still in the process of being indexed.";
538  } else {
539  errmsg += " This error is unexpected and indicates index corruption.";
540  }
541 
542  return RESTERR(req, HTTP_NOT_FOUND, errmsg);
543  }
544 
545  switch (rf) {
547  DataStream ssResp{};
548  ssResp << filter;
549 
550  req->WriteHeader("Content-Type", "application/octet-stream");
551  req->WriteReply(HTTP_OK, ssResp);
552  return true;
553  }
555  DataStream ssResp{};
556  ssResp << filter;
557 
558  std::string strHex = HexStr(ssResp) + "\n";
559  req->WriteHeader("Content-Type", "text/plain");
560  req->WriteReply(HTTP_OK, strHex);
561  return true;
562  }
565  ret.pushKV("filter", HexStr(filter.GetEncodedFilter()));
566  std::string strJSON = ret.write() + "\n";
567  req->WriteHeader("Content-Type", "application/json");
568  req->WriteReply(HTTP_OK, strJSON);
569  return true;
570  }
571  default: {
572  return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
573  }
574  }
575 }
576 
577 // A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
579 
580 static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
581 {
582  if (!CheckWarmup(req))
583  return false;
584  std::string param;
585  const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
586 
587  switch (rf) {
589  JSONRPCRequest jsonRequest;
590  jsonRequest.context = context;
591  jsonRequest.params = UniValue(UniValue::VARR);
592  UniValue chainInfoObject = getblockchaininfo().HandleRequest(jsonRequest);
593  std::string strJSON = chainInfoObject.write() + "\n";
594  req->WriteHeader("Content-Type", "application/json");
595  req->WriteReply(HTTP_OK, strJSON);
596  return true;
597  }
598  default: {
599  return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
600  }
601  }
602 }
603 
604 
606 
607 static bool rest_deploymentinfo(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
608 {
609  if (!CheckWarmup(req)) return false;
610 
611  std::string hash_str;
612  const RESTResponseFormat rf = ParseDataFormat(hash_str, str_uri_part);
613 
614  switch (rf) {
616  JSONRPCRequest jsonRequest;
617  jsonRequest.context = context;
618  jsonRequest.params = UniValue(UniValue::VARR);
619 
620  if (!hash_str.empty()) {
621  auto hash{uint256::FromHex(hash_str)};
622  if (!hash) {
623  return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hash_str);
624  }
625 
626  const ChainstateManager* chainman = GetChainman(context, req);
627  if (!chainman) return false;
628  if (!WITH_LOCK(::cs_main, return chainman->m_blockman.LookupBlockIndex(*hash))) {
629  return RESTERR(req, HTTP_BAD_REQUEST, "Block not found");
630  }
631 
632  jsonRequest.params.push_back(hash_str);
633  }
634 
635  req->WriteHeader("Content-Type", "application/json");
636  req->WriteReply(HTTP_OK, getdeploymentinfo().HandleRequest(jsonRequest).write() + "\n");
637  return true;
638  }
639  default: {
640  return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
641  }
642  }
643 
644 }
645 
646 static bool rest_mempool(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
647 {
648  if (!CheckWarmup(req))
649  return false;
650 
651  std::string param;
652  const RESTResponseFormat rf = ParseDataFormat(param, str_uri_part);
653  if (param != "contents" && param != "info") {
654  return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/mempool/<info|contents>.json");
655  }
656 
657  const CTxMemPool* mempool = GetMemPool(context, req);
658  if (!mempool) return false;
659 
660  switch (rf) {
662  std::string str_json;
663  if (param == "contents") {
664  std::string raw_verbose;
665  try {
666  raw_verbose = req->GetQueryParameter("verbose").value_or("true");
667  } catch (const std::runtime_error& e) {
668  return RESTERR(req, HTTP_BAD_REQUEST, e.what());
669  }
670  if (raw_verbose != "true" && raw_verbose != "false") {
671  return RESTERR(req, HTTP_BAD_REQUEST, "The \"verbose\" query parameter must be either \"true\" or \"false\".");
672  }
673  std::string raw_mempool_sequence;
674  try {
675  raw_mempool_sequence = req->GetQueryParameter("mempool_sequence").value_or("false");
676  } catch (const std::runtime_error& e) {
677  return RESTERR(req, HTTP_BAD_REQUEST, e.what());
678  }
679  if (raw_mempool_sequence != "true" && raw_mempool_sequence != "false") {
680  return RESTERR(req, HTTP_BAD_REQUEST, "The \"mempool_sequence\" query parameter must be either \"true\" or \"false\".");
681  }
682  const bool verbose{raw_verbose == "true"};
683  const bool mempool_sequence{raw_mempool_sequence == "true"};
684  if (verbose && mempool_sequence) {
685  return RESTERR(req, HTTP_BAD_REQUEST, "Verbose results cannot contain mempool sequence values. (hint: set \"verbose=false\")");
686  }
687  str_json = MempoolToJSON(*mempool, verbose, mempool_sequence).write() + "\n";
688  } else {
689  str_json = MempoolInfoToJSON(*mempool).write() + "\n";
690  }
691 
692  req->WriteHeader("Content-Type", "application/json");
693  req->WriteReply(HTTP_OK, str_json);
694  return true;
695  }
696  default: {
697  return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
698  }
699  }
700 }
701 
702 static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
703 {
704  if (!CheckWarmup(req))
705  return false;
706  std::string hashStr;
707  const RESTResponseFormat rf = ParseDataFormat(hashStr, strURIPart);
708 
709  auto hash{uint256::FromHex(hashStr)};
710  if (!hash) {
711  return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
712  }
713 
714  if (g_txindex) {
715  g_txindex->BlockUntilSyncedToCurrentChain();
716  }
717 
718  const NodeContext* const node = GetNodeContext(context, req);
719  if (!node) return false;
720  uint256 hashBlock = uint256();
721  const CTransactionRef tx{GetTransaction(/*block_index=*/nullptr, node->mempool.get(), *hash, hashBlock, node->chainman->m_blockman)};
722  if (!tx) {
723  return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
724  }
725 
726  switch (rf) {
728  DataStream ssTx;
729  ssTx << TX_WITH_WITNESS(tx);
730 
731  req->WriteHeader("Content-Type", "application/octet-stream");
732  req->WriteReply(HTTP_OK, ssTx);
733  return true;
734  }
735 
737  DataStream ssTx;
738  ssTx << TX_WITH_WITNESS(tx);
739 
740  std::string strHex = HexStr(ssTx) + "\n";
741  req->WriteHeader("Content-Type", "text/plain");
742  req->WriteReply(HTTP_OK, strHex);
743  return true;
744  }
745 
747  UniValue objTx(UniValue::VOBJ);
748  TxToUniv(*tx, /*block_hash=*/hashBlock, /*entry=*/ objTx);
749  std::string strJSON = objTx.write() + "\n";
750  req->WriteHeader("Content-Type", "application/json");
751  req->WriteReply(HTTP_OK, strJSON);
752  return true;
753  }
754 
755  default: {
756  return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
757  }
758  }
759 }
760 
761 static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
762 {
763  if (!CheckWarmup(req))
764  return false;
765  std::string param;
766  const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
767 
768  std::vector<std::string> uriParts;
769  if (param.length() > 1)
770  {
771  std::string strUriParams = param.substr(1);
772  uriParts = SplitString(strUriParams, '/');
773  }
774 
775  // throw exception in case of an empty request
776  std::string strRequestMutable = req->ReadBody();
777  if (strRequestMutable.length() == 0 && uriParts.size() == 0)
778  return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
779 
780  bool fInputParsed = false;
781  bool fCheckMemPool = false;
782  std::vector<COutPoint> vOutPoints;
783 
784  // parse/deserialize input
785  // input-format = output-format, rest/getutxos/bin requires binary input, gives binary output, ...
786 
787  if (uriParts.size() > 0)
788  {
789  //inputs is sent over URI scheme (/rest/getutxos/checkmempool/txid1-n/txid2-n/...)
790  if (uriParts[0] == "checkmempool") fCheckMemPool = true;
791 
792  for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
793  {
794  const auto txid_out{util::Split<std::string_view>(uriParts[i], '-')};
795  if (txid_out.size() != 2) {
796  return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
797  }
798  auto txid{Txid::FromHex(txid_out.at(0))};
799  auto output{ToIntegral<uint32_t>(txid_out.at(1))};
800 
801  if (!txid || !output) {
802  return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
803  }
804 
805  vOutPoints.emplace_back(*txid, *output);
806  }
807 
808  if (vOutPoints.size() > 0)
809  fInputParsed = true;
810  else
811  return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
812  }
813 
814  switch (rf) {
816  // convert hex to bin, continue then with bin part
817  std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable);
818  strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
819  [[fallthrough]];
820  }
821 
823  try {
824  //deserialize only if user sent a request
825  if (strRequestMutable.size() > 0)
826  {
827  if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA
828  return RESTERR(req, HTTP_BAD_REQUEST, "Combination of URI scheme inputs and raw post data is not allowed");
829 
830  DataStream oss{};
831  oss << strRequestMutable;
832  oss >> fCheckMemPool;
833  oss >> vOutPoints;
834  }
835  } catch (const std::ios_base::failure&) {
836  // abort in case of unreadable binary data
837  return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
838  }
839  break;
840  }
841 
843  if (!fInputParsed)
844  return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
845  break;
846  }
847  default: {
848  return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
849  }
850  }
851 
852  // limit max outpoints
853  if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
854  return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
855 
856  // check spentness and form a bitmap (as well as a JSON capable human-readable string representation)
857  std::vector<unsigned char> bitmap;
858  std::vector<CCoin> outs;
859  std::string bitmapStringRepresentation;
860  std::vector<bool> hits;
861  bitmap.resize((vOutPoints.size() + 7) / 8);
862  ChainstateManager* maybe_chainman = GetChainman(context, req);
863  if (!maybe_chainman) return false;
864  ChainstateManager& chainman = *maybe_chainman;
865  decltype(chainman.ActiveHeight()) active_height;
866  uint256 active_hash;
867  {
868  auto process_utxos = [&vOutPoints, &outs, &hits, &active_height, &active_hash, &chainman](const CCoinsView& view, const CTxMemPool* mempool) EXCLUSIVE_LOCKS_REQUIRED(chainman.GetMutex()) {
869  for (const COutPoint& vOutPoint : vOutPoints) {
870  Coin coin;
871  bool hit = (!mempool || !mempool->isSpent(vOutPoint)) && view.GetCoin(vOutPoint, coin);
872  hits.push_back(hit);
873  if (hit) outs.emplace_back(std::move(coin));
874  }
875  active_height = chainman.ActiveHeight();
876  active_hash = chainman.ActiveTip()->GetBlockHash();
877  };
878 
879  if (fCheckMemPool) {
880  const CTxMemPool* mempool = GetMemPool(context, req);
881  if (!mempool) return false;
882  // use db+mempool as cache backend in case user likes to query mempool
883  LOCK2(cs_main, mempool->cs);
884  CCoinsViewCache& viewChain = chainman.ActiveChainstate().CoinsTip();
885  CCoinsViewMemPool viewMempool(&viewChain, *mempool);
886  process_utxos(viewMempool, mempool);
887  } else {
888  LOCK(cs_main);
889  process_utxos(chainman.ActiveChainstate().CoinsTip(), nullptr);
890  }
891 
892  for (size_t i = 0; i < hits.size(); ++i) {
893  const bool hit = hits[i];
894  bitmapStringRepresentation.append(hit ? "1" : "0"); // form a binary string representation (human-readable for json output)
895  bitmap[i / 8] |= ((uint8_t)hit) << (i % 8);
896  }
897  }
898 
899  switch (rf) {
901  // serialize data
902  // use exact same output as mentioned in Bip64
903  DataStream ssGetUTXOResponse{};
904  ssGetUTXOResponse << active_height << active_hash << bitmap << outs;
905 
906  req->WriteHeader("Content-Type", "application/octet-stream");
907  req->WriteReply(HTTP_OK, ssGetUTXOResponse);
908  return true;
909  }
910 
912  DataStream ssGetUTXOResponse{};
913  ssGetUTXOResponse << active_height << active_hash << bitmap << outs;
914  std::string strHex = HexStr(ssGetUTXOResponse) + "\n";
915 
916  req->WriteHeader("Content-Type", "text/plain");
917  req->WriteReply(HTTP_OK, strHex);
918  return true;
919  }
920 
922  UniValue objGetUTXOResponse(UniValue::VOBJ);
923 
924  // pack in some essentials
925  // use more or less the same output as mentioned in Bip64
926  objGetUTXOResponse.pushKV("chainHeight", active_height);
927  objGetUTXOResponse.pushKV("chaintipHash", active_hash.GetHex());
928  objGetUTXOResponse.pushKV("bitmap", bitmapStringRepresentation);
929 
930  UniValue utxos(UniValue::VARR);
931  for (const CCoin& coin : outs) {
932  UniValue utxo(UniValue::VOBJ);
933  utxo.pushKV("height", (int32_t)coin.nHeight);
934  utxo.pushKV("value", ValueFromAmount(coin.out.nValue));
935 
936  // include the script in a json output
938  ScriptToUniv(coin.out.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
939  utxo.pushKV("scriptPubKey", std::move(o));
940  utxos.push_back(std::move(utxo));
941  }
942  objGetUTXOResponse.pushKV("utxos", std::move(utxos));
943 
944  // return json string
945  std::string strJSON = objGetUTXOResponse.write() + "\n";
946  req->WriteHeader("Content-Type", "application/json");
947  req->WriteReply(HTTP_OK, strJSON);
948  return true;
949  }
950  default: {
951  return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
952  }
953  }
954 }
955 
956 static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
957  const std::string& str_uri_part)
958 {
959  if (!CheckWarmup(req)) return false;
960  std::string height_str;
961  const RESTResponseFormat rf = ParseDataFormat(height_str, str_uri_part);
962 
963  int32_t blockheight = -1; // Initialization done only to prevent valgrind false positive, see https://github.com/bitcoin/bitcoin/pull/18785
964  if (!ParseInt32(height_str, &blockheight) || blockheight < 0) {
965  return RESTERR(req, HTTP_BAD_REQUEST, "Invalid height: " + SanitizeString(height_str));
966  }
967 
968  CBlockIndex* pblockindex = nullptr;
969  {
970  ChainstateManager* maybe_chainman = GetChainman(context, req);
971  if (!maybe_chainman) return false;
972  ChainstateManager& chainman = *maybe_chainman;
973  LOCK(cs_main);
974  const CChain& active_chain = chainman.ActiveChain();
975  if (blockheight > active_chain.Height()) {
976  return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
977  }
978  pblockindex = active_chain[blockheight];
979  }
980  switch (rf) {
982  DataStream ss_blockhash{};
983  ss_blockhash << pblockindex->GetBlockHash();
984  req->WriteHeader("Content-Type", "application/octet-stream");
985  req->WriteReply(HTTP_OK, ss_blockhash);
986  return true;
987  }
989  req->WriteHeader("Content-Type", "text/plain");
990  req->WriteReply(HTTP_OK, pblockindex->GetBlockHash().GetHex() + "\n");
991  return true;
992  }
994  req->WriteHeader("Content-Type", "application/json");
996  resp.pushKV("blockhash", pblockindex->GetBlockHash().GetHex());
997  req->WriteReply(HTTP_OK, resp.write() + "\n");
998  return true;
999  }
1000  default: {
1001  return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
1002  }
1003  }
1004 }
1005 
1006 static const struct {
1007  const char* prefix;
1008  bool (*handler)(const std::any& context, HTTPRequest* req, const std::string& strReq);
1009 } uri_prefixes[] = {
1010  {"/rest/tx/", rest_tx},
1011  {"/rest/block/notxdetails/", rest_block_notxdetails},
1012  {"/rest/block/", rest_block_extended},
1013  {"/rest/blockfilter/", rest_block_filter},
1014  {"/rest/blockfilterheaders/", rest_filter_header},
1015  {"/rest/chaininfo", rest_chaininfo},
1016  {"/rest/mempool/", rest_mempool},
1017  {"/rest/headers/", rest_headers},
1018  {"/rest/getutxos", rest_getutxos},
1019  {"/rest/deploymentinfo/", rest_deploymentinfo},
1020  {"/rest/deploymentinfo", rest_deploymentinfo},
1021  {"/rest/blockhashbyheight/", rest_blockhash_by_height},
1022 };
1023 
1024 void StartREST(const std::any& context)
1025 {
1026  for (const auto& up : uri_prefixes) {
1027  auto handler = [context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); };
1028  RegisterHTTPHandler(up.prefix, false, handler);
1029  }
1030 }
1031 
1033 {
1034 }
1035 
1036 void StopREST()
1037 {
1038  for (const auto& up : uri_prefixes) {
1039  UnregisterHTTPHandler(up.prefix, false);
1040  }
1041 }
int ret
#define PACKAGE_BUGREPORT
UniValue blockheaderToJSON(const CBlockIndex &tip, const CBlockIndex &blockindex)
Block header to JSON.
Definition: blockchain.cpp:137
UniValue blockToJSON(BlockManager &blockman, const CBlock &block, const CBlockIndex &tip, const CBlockIndex &blockindex, TxVerbosity verbosity)
Block description to JSON.
Definition: blockchain.cpp:166
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:833
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:388
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:871
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1118
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:1117
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
Definition: validation.h:1006
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1119
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1120
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1013
A UTXO entry.
Definition: coins.h:32
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
In-flight HTTP request.
Definition: httpserver.h:62
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:709
void WriteReply(int nStatus, std::string_view reply="")
Write HTTP reply.
Definition: httpserver.h:132
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:626
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:606
UniValue params
Definition: request.h:40
std::any context
Definition: request.h:45
UniValue HandleRequest(const JSONRPCRequest &request) const
Definition: util.cpp:633
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
static std::optional< transaction_identifier > FromHex(std::string_view hex)
256-bit opaque blob.
Definition: uint256.h:127
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:129
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: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:750
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:743
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:956
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:486
static bool rest_getutxos(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
Definition: rest.cpp:761
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:702
const char * prefix
Definition: rest.cpp:1007
void StartREST(const std::any &context)
Start HTTP REST subsystem.
Definition: rest.cpp:1024
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:1008
static const struct @10 uri_prefixes[]
void StopREST()
Stop HTTP REST subsystem.
Definition: rest.cpp:1036
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:580
void InterruptREST()
Interrupt RPC REST subsystem.
Definition: rest.cpp:1032
static bool rest_deploymentinfo(const std::any &context, HTTPRequest *req, const std::string &str_uri_part)
Definition: rest.cpp:607
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:646
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
#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:55
#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:1161
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.
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.