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