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