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