LCOV - code coverage report
Current view: top level - src - rest.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 0.0 % 723 0
Test Date: 2026-02-07 04:23:35 Functions: 0.0 % 29 0
Branches: 0.0 % 1517 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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 <flatfile.h>
      13                 :             : #include <httpserver.h>
      14                 :             : #include <index/blockfilterindex.h>
      15                 :             : #include <index/txindex.h>
      16                 :             : #include <node/blockstorage.h>
      17                 :             : #include <node/context.h>
      18                 :             : #include <primitives/block.h>
      19                 :             : #include <primitives/transaction.h>
      20                 :             : #include <rpc/blockchain.h>
      21                 :             : #include <rpc/mempool.h>
      22                 :             : #include <rpc/protocol.h>
      23                 :             : #include <rpc/server.h>
      24                 :             : #include <rpc/server_util.h>
      25                 :             : #include <streams.h>
      26                 :             : #include <sync.h>
      27                 :             : #include <txmempool.h>
      28                 :             : #include <undo.h>
      29                 :             : #include <util/any.h>
      30                 :             : #include <util/check.h>
      31                 :             : #include <util/strencodings.h>
      32                 :             : #include <validation.h>
      33                 :             : 
      34                 :             : #include <any>
      35                 :             : #include <vector>
      36                 :             : 
      37                 :             : #include <univalue.h>
      38                 :             : 
      39                 :             : using node::GetTransaction;
      40                 :             : using node::NodeContext;
      41                 :             : using util::SplitString;
      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 {
      47                 :             :     RESTResponseFormat rf;
      48                 :             :     const char* name;
      49                 :             : } rf_names[] = {
      50                 :             :       {RESTResponseFormat::UNDEF, ""},
      51                 :             :       {RESTResponseFormat::BINARY, "bin"},
      52                 :             :       {RESTResponseFormat::HEX, "hex"},
      53                 :             :       {RESTResponseFormat::JSON, "json"},
      54                 :             : };
      55                 :             : 
      56                 :           0 : struct CCoin {
      57                 :             :     uint32_t nHeight;
      58                 :             :     CTxOut out;
      59                 :             : 
      60                 :             :     CCoin() : nHeight(0) {}
      61                 :           0 :     explicit CCoin(Coin&& in) : nHeight(in.nHeight), out(std::move(in.out)) {}
      62                 :             : 
      63                 :           0 :     SERIALIZE_METHODS(CCoin, obj)
      64                 :             :     {
      65                 :           0 :         uint32_t nTxVerDummy = 0;
      66                 :           0 :         READWRITE(nTxVerDummy, obj.nHeight, obj.out);
      67                 :             :     }
      68                 :             : };
      69                 :             : 
      70                 :           0 : static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string message)
      71                 :             : {
      72   [ #  #  #  # ]:           0 :     req->WriteHeader("Content-Type", "text/plain");
      73   [ #  #  #  # ]:           0 :     req->WriteReply(status, message + "\r\n");
      74                 :           0 :     return false;
      75                 :             : }
      76                 :             : 
      77                 :             : /**
      78                 :             :  * Get the node context.
      79                 :             :  *
      80                 :             :  * @param[in]  req  The HTTP request, whose status code will be set if node
      81                 :             :  *                  context is not found.
      82                 :             :  * @returns         Pointer to the node context or nullptr if not found.
      83                 :             :  */
      84                 :           0 : static NodeContext* GetNodeContext(const std::any& context, HTTPRequest* req)
      85                 :             : {
      86                 :           0 :     auto node_context = util::AnyPtr<NodeContext>(context);
      87         [ #  # ]:           0 :     if (!node_context) {
      88         [ #  # ]:           0 :         RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, STR_INTERNAL_BUG("Node context not found!"));
      89                 :           0 :         return nullptr;
      90                 :             :     }
      91                 :             :     return node_context;
      92                 :             : }
      93                 :             : 
      94                 :             : /**
      95                 :             :  * Get the node context mempool.
      96                 :             :  *
      97                 :             :  * @param[in]  req The HTTP request, whose status code will be set if node
      98                 :             :  *                 context mempool is not found.
      99                 :             :  * @returns        Pointer to the mempool or nullptr if no mempool found.
     100                 :             :  */
     101                 :           0 : static CTxMemPool* GetMemPool(const std::any& context, HTTPRequest* req)
     102                 :             : {
     103                 :           0 :     auto node_context = util::AnyPtr<NodeContext>(context);
     104   [ #  #  #  # ]:           0 :     if (!node_context || !node_context->mempool) {
     105         [ #  # ]:           0 :         RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
     106                 :           0 :         return nullptr;
     107                 :             :     }
     108                 :             :     return node_context->mempool.get();
     109                 :             : }
     110                 :             : 
     111                 :             : /**
     112                 :             :  * Get the node context chainstatemanager.
     113                 :             :  *
     114                 :             :  * @param[in]  req The HTTP request, whose status code will be set if node
     115                 :             :  *                 context chainstatemanager is not found.
     116                 :             :  * @returns        Pointer to the chainstatemanager or nullptr if none found.
     117                 :             :  */
     118                 :           0 : static ChainstateManager* GetChainman(const std::any& context, HTTPRequest* req)
     119                 :             : {
     120                 :           0 :     auto node_context = util::AnyPtr<NodeContext>(context);
     121   [ #  #  #  # ]:           0 :     if (!node_context || !node_context->chainman) {
     122         [ #  # ]:           0 :         RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, STR_INTERNAL_BUG("Chainman disabled or instance not found!"));
     123                 :           0 :         return nullptr;
     124                 :             :     }
     125                 :             :     return node_context->chainman.get();
     126                 :             : }
     127                 :             : 
     128                 :           0 : RESTResponseFormat ParseDataFormat(std::string& param, const std::string& strReq)
     129                 :             : {
     130                 :             :     // Remove query string (if any, separated with '?') as it should not interfere with
     131                 :             :     // parsing param and data format
     132                 :           0 :     param = strReq.substr(0, strReq.rfind('?'));
     133                 :           0 :     const std::string::size_type pos_format{param.rfind('.')};
     134                 :             : 
     135                 :             :     // No format string is found
     136         [ #  # ]:           0 :     if (pos_format == std::string::npos) {
     137                 :             :         return RESTResponseFormat::UNDEF;
     138                 :             :     }
     139                 :             : 
     140                 :             :     // Match format string to available formats
     141                 :           0 :     const std::string suffix(param, pos_format + 1);
     142         [ #  # ]:           0 :     for (const auto& rf_name : rf_names) {
     143         [ #  # ]:           0 :         if (suffix == rf_name.name) {
     144         [ #  # ]:           0 :             param.erase(pos_format);
     145                 :           0 :             return rf_name.rf;
     146                 :             :         }
     147                 :             :     }
     148                 :             : 
     149                 :             :     // If no suffix is found, return RESTResponseFormat::UNDEF and original string without query string
     150                 :             :     return RESTResponseFormat::UNDEF;
     151                 :           0 : }
     152                 :             : 
     153                 :           0 : static std::string AvailableDataFormatsString()
     154                 :             : {
     155                 :           0 :     std::string formats;
     156         [ #  # ]:           0 :     for (const auto& rf_name : rf_names) {
     157         [ #  # ]:           0 :         if (strlen(rf_name.name) > 0) {
     158         [ #  # ]:           0 :             formats.append(".");
     159         [ #  # ]:           0 :             formats.append(rf_name.name);
     160         [ #  # ]:           0 :             formats.append(", ");
     161                 :             :         }
     162                 :             :     }
     163                 :             : 
     164   [ #  #  #  # ]:           0 :     if (formats.length() > 0)
     165         [ #  # ]:           0 :         return formats.substr(0, formats.length() - 2);
     166                 :             : 
     167                 :           0 :     return formats;
     168                 :           0 : }
     169                 :             : 
     170                 :           0 : static bool CheckWarmup(HTTPRequest* req)
     171                 :             : {
     172         [ #  # ]:           0 :     std::string statusmessage;
     173   [ #  #  #  # ]:           0 :     if (RPCIsInWarmup(&statusmessage))
     174   [ #  #  #  # ]:           0 :          return RESTERR(req, HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: " + statusmessage);
     175                 :             :     return true;
     176                 :           0 : }
     177                 :             : 
     178                 :           0 : static bool rest_headers(const std::any& context,
     179                 :             :                          HTTPRequest* req,
     180                 :             :                          const std::string& uri_part)
     181                 :             : {
     182         [ #  # ]:           0 :     if (!CheckWarmup(req))
     183                 :             :         return false;
     184         [ #  # ]:           0 :     std::string param;
     185         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
     186   [ #  #  #  # ]:           0 :     std::vector<std::string> path = SplitString(param, '/');
     187                 :             : 
     188         [ #  # ]:           0 :     std::string raw_count;
     189                 :           0 :     std::string hashStr;
     190   [ #  #  #  # ]:           0 :     if (path.size() == 2) {
     191                 :             :         // deprecated path: /rest/headers/<count>/<hash>
     192         [ #  # ]:           0 :         hashStr = path[1];
     193         [ #  # ]:           0 :         raw_count = path[0];
     194         [ #  # ]:           0 :     } else if (path.size() == 1) {
     195                 :             :         // new path with query parameter: /rest/headers/<hash>?count=<count>
     196         [ #  # ]:           0 :         hashStr = path[0];
     197                 :           0 :         try {
     198   [ #  #  #  #  :           0 :             raw_count = req->GetQueryParameter("count").value_or("5");
                   #  # ]
     199         [ -  - ]:           0 :         } catch (const std::runtime_error& e) {
     200   [ -  -  -  - ]:           0 :             return RESTERR(req, HTTP_BAD_REQUEST, e.what());
     201                 :           0 :         }
     202                 :             :     } else {
     203   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/headers/<hash>.<ext>?count=<count>");
     204                 :             :     }
     205                 :             : 
     206         [ #  # ]:           0 :     const auto parsed_count{ToIntegral<size_t>(raw_count)};
     207   [ #  #  #  #  :           0 :     if (!parsed_count.has_value() || *parsed_count < 1 || *parsed_count > MAX_REST_HEADERS_RESULTS) {
                   #  # ]
     208   [ #  #  #  # ]:           0 :         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));
     209                 :             :     }
     210                 :             : 
     211   [ #  #  #  # ]:           0 :     auto hash{uint256::FromHex(hashStr)};
     212         [ #  # ]:           0 :     if (!hash) {
     213   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
     214                 :             :     }
     215                 :             : 
     216                 :           0 :     const CBlockIndex* tip = nullptr;
     217                 :           0 :     std::vector<const CBlockIndex*> headers;
     218         [ #  # ]:           0 :     headers.reserve(*parsed_count);
     219         [ #  # ]:           0 :     ChainstateManager* maybe_chainman = GetChainman(context, req);
     220         [ #  # ]:           0 :     if (!maybe_chainman) return false;
     221                 :           0 :     ChainstateManager& chainman = *maybe_chainman;
     222                 :           0 :     {
     223         [ #  # ]:           0 :         LOCK(cs_main);
     224         [ #  # ]:           0 :         CChain& active_chain = chainman.ActiveChain();
     225         [ #  # ]:           0 :         tip = active_chain.Tip();
     226         [ #  # ]:           0 :         const CBlockIndex* pindex{chainman.m_blockman.LookupBlockIndex(*hash)};
     227   [ #  #  #  # ]:           0 :         while (pindex != nullptr && active_chain.Contains(pindex)) {
     228         [ #  # ]:           0 :             headers.push_back(pindex);
     229   [ #  #  #  # ]:           0 :             if (headers.size() == *parsed_count) {
     230                 :             :                 break;
     231                 :             :             }
     232                 :           0 :             pindex = active_chain.Next(pindex);
     233                 :             :         }
     234                 :           0 :     }
     235                 :             : 
     236   [ #  #  #  # ]:           0 :     switch (rf) {
     237                 :           0 :     case RESTResponseFormat::BINARY: {
     238                 :           0 :         DataStream ssHeader{};
     239         [ #  # ]:           0 :         for (const CBlockIndex *pindex : headers) {
     240         [ #  # ]:           0 :             ssHeader << pindex->GetBlockHeader();
     241                 :             :         }
     242                 :             : 
     243   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/octet-stream");
                   #  # ]
     244   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, ssHeader);
     245                 :           0 :         return true;
     246                 :           0 :     }
     247                 :             : 
     248                 :           0 :     case RESTResponseFormat::HEX: {
     249                 :           0 :         DataStream ssHeader{};
     250         [ #  # ]:           0 :         for (const CBlockIndex *pindex : headers) {
     251         [ #  # ]:           0 :             ssHeader << pindex->GetBlockHeader();
     252                 :             :         }
     253                 :             : 
     254   [ #  #  #  # ]:           0 :         std::string strHex = HexStr(ssHeader) + "\n";
     255   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "text/plain");
                   #  # ]
     256   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strHex);
     257                 :           0 :         return true;
     258                 :           0 :     }
     259                 :           0 :     case RESTResponseFormat::JSON: {
     260                 :           0 :         UniValue jsonHeaders(UniValue::VARR);
     261         [ #  # ]:           0 :         for (const CBlockIndex *pindex : headers) {
     262   [ #  #  #  # ]:           0 :             jsonHeaders.push_back(blockheaderToJSON(*tip, *pindex, chainman.GetConsensus().powLimit));
     263                 :             :         }
     264         [ #  # ]:           0 :         std::string strJSON = jsonHeaders.write() + "\n";
     265   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
     266   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strJSON);
     267                 :           0 :         return true;
     268                 :           0 :     }
     269                 :           0 :     default: {
     270   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
                   #  # ]
     271                 :             :     }
     272                 :             :     }
     273                 :           0 : }
     274                 :             : 
     275                 :             : /**
     276                 :             :  * Serialize spent outputs as a list of per-transaction CTxOut lists using binary format.
     277                 :             :  */
     278                 :           0 : static void SerializeBlockUndo(DataStream& stream, const CBlockUndo& block_undo)
     279                 :             : {
     280         [ #  # ]:           0 :     WriteCompactSize(stream, block_undo.vtxundo.size() + 1);
     281                 :           0 :     WriteCompactSize(stream, 0); // block_undo.vtxundo doesn't contain coinbase tx
     282         [ #  # ]:           0 :     for (const CTxUndo& tx_undo : block_undo.vtxundo) {
     283         [ #  # ]:           0 :         WriteCompactSize(stream, tx_undo.vprevout.size());
     284         [ #  # ]:           0 :         for (const Coin& coin : tx_undo.vprevout) {
     285                 :           0 :             coin.out.Serialize(stream);
     286                 :             :         }
     287                 :             :     }
     288                 :           0 : }
     289                 :             : 
     290                 :             : /**
     291                 :             :  * Serialize spent outputs as a list of per-transaction CTxOut lists using JSON format.
     292                 :             :  */
     293                 :           0 : static void BlockUndoToJSON(const CBlockUndo& block_undo, UniValue& result)
     294                 :             : {
     295         [ #  # ]:           0 :     result.push_back({UniValue::VARR}); // block_undo.vtxundo doesn't contain coinbase tx
     296         [ #  # ]:           0 :     for (const CTxUndo& tx_undo : block_undo.vtxundo) {
     297                 :           0 :         UniValue tx_prevouts(UniValue::VARR);
     298         [ #  # ]:           0 :         for (const Coin& coin : tx_undo.vprevout) {
     299                 :           0 :             UniValue prevout(UniValue::VOBJ);
     300   [ #  #  #  #  :           0 :             prevout.pushKV("value", ValueFromAmount(coin.out.nValue));
                   #  # ]
     301                 :             : 
     302                 :           0 :             UniValue script_pub_key(UniValue::VOBJ);
     303         [ #  # ]:           0 :             ScriptToUniv(coin.out.scriptPubKey, /*out=*/script_pub_key, /*include_hex=*/true, /*include_address=*/true);
     304   [ #  #  #  # ]:           0 :             prevout.pushKV("scriptPubKey", std::move(script_pub_key));
     305                 :             : 
     306         [ #  # ]:           0 :             tx_prevouts.push_back(std::move(prevout));
     307                 :           0 :         }
     308         [ #  # ]:           0 :         result.push_back(std::move(tx_prevouts));
     309                 :           0 :     }
     310                 :           0 : }
     311                 :             : 
     312                 :           0 : static bool rest_spent_txouts(const std::any& context, HTTPRequest* req, const std::string& uri_part)
     313                 :             : {
     314         [ #  # ]:           0 :     if (!CheckWarmup(req)) {
     315                 :             :         return false;
     316                 :             :     }
     317         [ #  # ]:           0 :     std::string param;
     318         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
     319   [ #  #  #  # ]:           0 :     std::vector<std::string> path = SplitString(param, '/');
     320                 :             : 
     321         [ #  # ]:           0 :     std::string hashStr;
     322   [ #  #  #  # ]:           0 :     if (path.size() == 1) {
     323                 :             :         // path with query parameter: /rest/spenttxouts/<hash>
     324         [ #  # ]:           0 :         hashStr = path[0];
     325                 :             :     } else {
     326   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/spenttxouts/<hash>.<ext>");
     327                 :             :     }
     328                 :             : 
     329   [ #  #  #  # ]:           0 :     auto hash{uint256::FromHex(hashStr)};
     330         [ #  # ]:           0 :     if (!hash) {
     331   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
     332                 :             :     }
     333                 :             : 
     334         [ #  # ]:           0 :     ChainstateManager* chainman = GetChainman(context, req);
     335         [ #  # ]:           0 :     if (!chainman) {
     336                 :             :         return false;
     337                 :             :     }
     338                 :             : 
     339   [ #  #  #  #  :           0 :     const CBlockIndex* pblockindex = WITH_LOCK(cs_main, return chainman->m_blockman.LookupBlockIndex(*hash));
                   #  # ]
     340         [ #  # ]:           0 :     if (!pblockindex) {
     341   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
     342                 :             :     }
     343                 :             : 
     344                 :           0 :     CBlockUndo block_undo;
     345   [ #  #  #  #  :           0 :     if (pblockindex->nHeight > 0 && !chainman->m_blockman.ReadBlockUndo(block_undo, *pblockindex)) {
                   #  # ]
     346   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_NOT_FOUND, hashStr + " undo not available");
     347                 :             :     }
     348                 :             : 
     349   [ #  #  #  # ]:           0 :     switch (rf) {
     350                 :           0 :     case RESTResponseFormat::BINARY: {
     351                 :           0 :         DataStream ssSpentResponse{};
     352         [ #  # ]:           0 :         SerializeBlockUndo(ssSpentResponse, block_undo);
     353   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/octet-stream");
                   #  # ]
     354   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, ssSpentResponse);
     355                 :           0 :         return true;
     356                 :           0 :     }
     357                 :             : 
     358                 :           0 :     case RESTResponseFormat::HEX: {
     359                 :           0 :         DataStream ssSpentResponse{};
     360         [ #  # ]:           0 :         SerializeBlockUndo(ssSpentResponse, block_undo);
     361   [ #  #  #  # ]:           0 :         const std::string strHex{HexStr(ssSpentResponse) + "\n"};
     362   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "text/plain");
                   #  # ]
     363   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strHex);
     364                 :           0 :         return true;
     365                 :           0 :     }
     366                 :             : 
     367                 :           0 :     case RESTResponseFormat::JSON: {
     368                 :           0 :         UniValue result(UniValue::VARR);
     369         [ #  # ]:           0 :         BlockUndoToJSON(block_undo, result);
     370         [ #  # ]:           0 :         std::string strJSON = result.write() + "\n";
     371   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
     372   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strJSON);
     373                 :           0 :         return true;
     374                 :           0 :     }
     375                 :             : 
     376                 :           0 :     default: {
     377   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
                   #  # ]
     378                 :             :     }
     379                 :             :     }
     380                 :           0 : }
     381                 :             : 
     382                 :             : /**
     383                 :             :  * This handler is used by multiple HTTP endpoints:
     384                 :             :  * - `/block/` via `rest_block_extended()`
     385                 :             :  * - `/block/notxdetails/` via `rest_block_notxdetails()`
     386                 :             :  * - `/blockpart/` via `rest_block_part()` (doesn't support JSON response, so `tx_verbosity` is unset)
     387                 :             :  */
     388                 :           0 : static bool rest_block(const std::any& context,
     389                 :             :                        HTTPRequest* req,
     390                 :             :                        const std::string& uri_part,
     391                 :             :                        std::optional<TxVerbosity> tx_verbosity,
     392                 :             :                        std::optional<std::pair<size_t, size_t>> block_part = std::nullopt)
     393                 :             : {
     394         [ #  # ]:           0 :     if (!CheckWarmup(req))
     395                 :             :         return false;
     396         [ #  # ]:           0 :     std::string hashStr;
     397         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(hashStr, uri_part);
     398                 :             : 
     399   [ #  #  #  # ]:           0 :     auto hash{uint256::FromHex(hashStr)};
     400         [ #  # ]:           0 :     if (!hash) {
     401   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
     402                 :             :     }
     403                 :             : 
     404                 :           0 :     FlatFilePos pos{};
     405                 :           0 :     const CBlockIndex* pblockindex = nullptr;
     406                 :           0 :     const CBlockIndex* tip = nullptr;
     407         [ #  # ]:           0 :     ChainstateManager* maybe_chainman = GetChainman(context, req);
     408         [ #  # ]:           0 :     if (!maybe_chainman) return false;
     409                 :           0 :     ChainstateManager& chainman = *maybe_chainman;
     410                 :           0 :     {
     411         [ #  # ]:           0 :         LOCK(cs_main);
     412   [ #  #  #  # ]:           0 :         tip = chainman.ActiveChain().Tip();
     413         [ #  # ]:           0 :         pblockindex = chainman.m_blockman.LookupBlockIndex(*hash);
     414         [ #  # ]:           0 :         if (!pblockindex) {
     415   [ #  #  #  # ]:           0 :             return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
     416                 :             :         }
     417         [ #  # ]:           0 :         if (!(pblockindex->nStatus & BLOCK_HAVE_DATA)) {
     418   [ #  #  #  # ]:           0 :             if (chainman.m_blockman.IsBlockPruned(*pblockindex)) {
     419   [ #  #  #  # ]:           0 :                 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
     420                 :             :             }
     421   [ #  #  #  # ]:           0 :             return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (not fully downloaded)");
     422                 :             :         }
     423         [ #  # ]:           0 :         pos = pblockindex->GetBlockPos();
     424                 :           0 :     }
     425                 :             : 
     426         [ #  # ]:           0 :     const auto block_data{chainman.m_blockman.ReadRawBlock(pos, block_part)};
     427         [ #  # ]:           0 :     if (!block_data) {
     428      [ #  #  # ]:           0 :         switch (block_data.error()) {
     429   [ #  #  #  # ]:           0 :         case node::ReadRawError::IO: return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "I/O error reading " + hashStr);
     430                 :           0 :         case node::ReadRawError::BadPartRange:
     431         [ #  # ]:           0 :             assert(block_part);
     432   [ #  #  #  # ]:           0 :             return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Bad block part offset/size %d/%d for %s", block_part->first, block_part->second, hashStr));
     433                 :             :         } // no default case, so the compiler can warn about missing cases
     434                 :           0 :         assert(false);
     435                 :             :     }
     436                 :             : 
     437   [ #  #  #  # ]:           0 :     switch (rf) {
     438                 :           0 :     case RESTResponseFormat::BINARY: {
     439   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/octet-stream");
                   #  # ]
     440   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, *block_data);
     441                 :             :         return true;
     442                 :             :     }
     443                 :             : 
     444                 :           0 :     case RESTResponseFormat::HEX: {
     445   [ #  #  #  # ]:           0 :         const std::string strHex{HexStr(*block_data) + "\n"};
     446   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "text/plain");
                   #  # ]
     447   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strHex);
     448                 :           0 :         return true;
     449                 :           0 :     }
     450                 :             : 
     451                 :           0 :     case RESTResponseFormat::JSON: {
     452         [ #  # ]:           0 :         if (tx_verbosity) {
     453                 :           0 :             CBlock block{};
     454   [ #  #  #  # ]:           0 :             SpanReader{*block_data} >> TX_WITH_WITNESS(block);
     455         [ #  # ]:           0 :             UniValue objBlock = blockToJSON(chainman.m_blockman, block, *tip, *pblockindex, *tx_verbosity, chainman.GetConsensus().powLimit);
     456         [ #  # ]:           0 :             std::string strJSON = objBlock.write() + "\n";
     457   [ #  #  #  #  :           0 :             req->WriteHeader("Content-Type", "application/json");
                   #  # ]
     458   [ #  #  #  # ]:           0 :             req->WriteReply(HTTP_OK, strJSON);
     459                 :           0 :             return true;
     460                 :           0 :         }
     461   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "JSON output is not supported for this request type");
     462                 :             :     }
     463                 :             : 
     464                 :           0 :     default: {
     465   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
                   #  # ]
     466                 :             :     }
     467                 :             :     }
     468                 :           0 : }
     469                 :             : 
     470                 :           0 : static bool rest_block_extended(const std::any& context, HTTPRequest* req, const std::string& uri_part)
     471                 :             : {
     472                 :           0 :     return rest_block(context, req, uri_part, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
     473                 :             : }
     474                 :             : 
     475                 :           0 : static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, const std::string& uri_part)
     476                 :             : {
     477                 :           0 :     return rest_block(context, req, uri_part, TxVerbosity::SHOW_TXID);
     478                 :             : }
     479                 :             : 
     480                 :           0 : static bool rest_block_part(const std::any& context, HTTPRequest* req, const std::string& uri_part)
     481                 :             : {
     482                 :           0 :     try {
     483   [ #  #  #  #  :           0 :         if (const auto opt_offset{ToIntegral<size_t>(req->GetQueryParameter("offset").value_or(""))}) {
             #  #  #  # ]
     484   [ #  #  #  #  :           0 :             if (const auto opt_size{ToIntegral<size_t>(req->GetQueryParameter("size").value_or(""))}) {
             #  #  #  # ]
     485         [ #  # ]:           0 :                 return rest_block(context, req, uri_part,
     486                 :             :                                   /*tx_verbosity=*/std::nullopt,
     487         [ #  # ]:           0 :                                   /*block_part=*/{{*opt_offset, *opt_size}});
     488                 :             :             } else {
     489   [ #  #  #  # ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, "Block part size missing or invalid");
     490                 :             :             }
     491                 :             :         } else {
     492   [ #  #  #  # ]:           0 :             return RESTERR(req, HTTP_BAD_REQUEST, "Block part offset missing or invalid");
     493                 :             :         }
     494         [ -  - ]:           0 :     } catch (const std::runtime_error& e) {
     495   [ -  -  -  - ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, e.what());
     496                 :           0 :     }
     497                 :             : }
     498                 :             : 
     499                 :           0 : static bool rest_filter_header(const std::any& context, HTTPRequest* req, const std::string& uri_part)
     500                 :             : {
     501         [ #  # ]:           0 :     if (!CheckWarmup(req)) return false;
     502                 :             : 
     503         [ #  # ]:           0 :     std::string param;
     504         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
     505                 :             : 
     506   [ #  #  #  # ]:           0 :     std::vector<std::string> uri_parts = SplitString(param, '/');
     507         [ #  # ]:           0 :     std::string raw_count;
     508                 :           0 :     std::string raw_blockhash;
     509   [ #  #  #  # ]:           0 :     if (uri_parts.size() == 3) {
     510                 :             :         // deprecated path: /rest/blockfilterheaders/<filtertype>/<count>/<blockhash>
     511         [ #  # ]:           0 :         raw_blockhash = uri_parts[2];
     512         [ #  # ]:           0 :         raw_count = uri_parts[1];
     513         [ #  # ]:           0 :     } else if (uri_parts.size() == 2) {
     514                 :             :         // new path with query parameter: /rest/blockfilterheaders/<filtertype>/<blockhash>?count=<count>
     515         [ #  # ]:           0 :         raw_blockhash = uri_parts[1];
     516                 :           0 :         try {
     517   [ #  #  #  #  :           0 :             raw_count = req->GetQueryParameter("count").value_or("5");
                   #  # ]
     518         [ -  - ]:           0 :         } catch (const std::runtime_error& e) {
     519   [ -  -  -  - ]:           0 :             return RESTERR(req, HTTP_BAD_REQUEST, e.what());
     520                 :           0 :         }
     521                 :             :     } else {
     522   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/blockfilterheaders/<filtertype>/<blockhash>.<ext>?count=<count>");
     523                 :             :     }
     524                 :             : 
     525         [ #  # ]:           0 :     const auto parsed_count{ToIntegral<size_t>(raw_count)};
     526   [ #  #  #  #  :           0 :     if (!parsed_count.has_value() || *parsed_count < 1 || *parsed_count > MAX_REST_HEADERS_RESULTS) {
                   #  # ]
     527   [ #  #  #  # ]:           0 :         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));
     528                 :             :     }
     529                 :             : 
     530   [ #  #  #  # ]:           0 :     auto block_hash{uint256::FromHex(raw_blockhash)};
     531         [ #  # ]:           0 :     if (!block_hash) {
     532   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + raw_blockhash);
     533                 :             :     }
     534                 :             : 
     535                 :           0 :     BlockFilterType filtertype;
     536   [ #  #  #  #  :           0 :     if (!BlockFilterTypeByName(uri_parts[0], filtertype)) {
                   #  # ]
     537   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Unknown filtertype " + uri_parts[0]);
     538                 :             :     }
     539                 :             : 
     540         [ #  # ]:           0 :     BlockFilterIndex* index = GetBlockFilterIndex(filtertype);
     541         [ #  # ]:           0 :     if (!index) {
     542   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Index is not enabled for filtertype " + uri_parts[0]);
     543                 :             :     }
     544                 :             : 
     545                 :           0 :     std::vector<const CBlockIndex*> headers;
     546         [ #  # ]:           0 :     headers.reserve(*parsed_count);
     547                 :           0 :     {
     548         [ #  # ]:           0 :         ChainstateManager* maybe_chainman = GetChainman(context, req);
     549         [ #  # ]:           0 :         if (!maybe_chainman) return false;
     550                 :           0 :         ChainstateManager& chainman = *maybe_chainman;
     551         [ #  # ]:           0 :         LOCK(cs_main);
     552         [ #  # ]:           0 :         CChain& active_chain = chainman.ActiveChain();
     553         [ #  # ]:           0 :         const CBlockIndex* pindex{chainman.m_blockman.LookupBlockIndex(*block_hash)};
     554   [ #  #  #  # ]:           0 :         while (pindex != nullptr && active_chain.Contains(pindex)) {
     555         [ #  # ]:           0 :             headers.push_back(pindex);
     556   [ #  #  #  # ]:           0 :             if (headers.size() == *parsed_count)
     557                 :             :                 break;
     558                 :           0 :             pindex = active_chain.Next(pindex);
     559                 :             :         }
     560                 :           0 :     }
     561                 :             : 
     562         [ #  # ]:           0 :     bool index_ready = index->BlockUntilSyncedToCurrentChain();
     563                 :             : 
     564                 :           0 :     std::vector<uint256> filter_headers;
     565         [ #  # ]:           0 :     filter_headers.reserve(*parsed_count);
     566         [ #  # ]:           0 :     for (const CBlockIndex* pindex : headers) {
     567                 :           0 :         uint256 filter_header;
     568   [ #  #  #  # ]:           0 :         if (!index->LookupFilterHeader(pindex, filter_header)) {
     569         [ #  # ]:           0 :             std::string errmsg = "Filter not found.";
     570                 :             : 
     571         [ #  # ]:           0 :             if (!index_ready) {
     572         [ #  # ]:           0 :                 errmsg += " Block filters are still in the process of being indexed.";
     573                 :             :             } else {
     574         [ #  # ]:           0 :                 errmsg += " This error is unexpected and indicates index corruption.";
     575                 :             :             }
     576                 :             : 
     577   [ #  #  #  # ]:           0 :             return RESTERR(req, HTTP_NOT_FOUND, errmsg);
     578                 :           0 :         }
     579         [ #  # ]:           0 :         filter_headers.push_back(filter_header);
     580                 :             :     }
     581                 :             : 
     582   [ #  #  #  # ]:           0 :     switch (rf) {
     583                 :           0 :     case RESTResponseFormat::BINARY: {
     584                 :           0 :         DataStream ssHeader{};
     585         [ #  # ]:           0 :         for (const uint256& header : filter_headers) {
     586         [ #  # ]:           0 :             ssHeader << header;
     587                 :             :         }
     588                 :             : 
     589   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/octet-stream");
                   #  # ]
     590   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, ssHeader);
     591                 :           0 :         return true;
     592                 :           0 :     }
     593                 :           0 :     case RESTResponseFormat::HEX: {
     594                 :           0 :         DataStream ssHeader{};
     595         [ #  # ]:           0 :         for (const uint256& header : filter_headers) {
     596         [ #  # ]:           0 :             ssHeader << header;
     597                 :             :         }
     598                 :             : 
     599   [ #  #  #  # ]:           0 :         std::string strHex = HexStr(ssHeader) + "\n";
     600   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "text/plain");
                   #  # ]
     601   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strHex);
     602                 :           0 :         return true;
     603                 :           0 :     }
     604                 :           0 :     case RESTResponseFormat::JSON: {
     605                 :           0 :         UniValue jsonHeaders(UniValue::VARR);
     606         [ #  # ]:           0 :         for (const uint256& header : filter_headers) {
     607   [ #  #  #  #  :           0 :             jsonHeaders.push_back(header.GetHex());
                   #  # ]
     608                 :             :         }
     609                 :             : 
     610         [ #  # ]:           0 :         std::string strJSON = jsonHeaders.write() + "\n";
     611   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
     612   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strJSON);
     613                 :           0 :         return true;
     614                 :           0 :     }
     615                 :           0 :     default: {
     616   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
                   #  # ]
     617                 :             :     }
     618                 :             :     }
     619                 :           0 : }
     620                 :             : 
     621                 :           0 : static bool rest_block_filter(const std::any& context, HTTPRequest* req, const std::string& uri_part)
     622                 :             : {
     623         [ #  # ]:           0 :     if (!CheckWarmup(req)) return false;
     624                 :             : 
     625         [ #  # ]:           0 :     std::string param;
     626         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
     627                 :             : 
     628                 :             :     // request is sent over URI scheme /rest/blockfilter/filtertype/blockhash
     629   [ #  #  #  # ]:           0 :     std::vector<std::string> uri_parts = SplitString(param, '/');
     630   [ #  #  #  # ]:           0 :     if (uri_parts.size() != 2) {
     631   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/blockfilter/<filtertype>/<blockhash>");
     632                 :             :     }
     633                 :             : 
     634   [ #  #  #  # ]:           0 :     auto block_hash{uint256::FromHex(uri_parts[1])};
     635         [ #  # ]:           0 :     if (!block_hash) {
     636   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + uri_parts[1]);
     637                 :             :     }
     638                 :             : 
     639                 :           0 :     BlockFilterType filtertype;
     640   [ #  #  #  #  :           0 :     if (!BlockFilterTypeByName(uri_parts[0], filtertype)) {
                   #  # ]
     641   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Unknown filtertype " + uri_parts[0]);
     642                 :             :     }
     643                 :             : 
     644         [ #  # ]:           0 :     BlockFilterIndex* index = GetBlockFilterIndex(filtertype);
     645         [ #  # ]:           0 :     if (!index) {
     646   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Index is not enabled for filtertype " + uri_parts[0]);
     647                 :             :     }
     648                 :             : 
     649                 :           0 :     const CBlockIndex* block_index;
     650                 :           0 :     bool block_was_connected;
     651                 :           0 :     {
     652         [ #  # ]:           0 :         ChainstateManager* maybe_chainman = GetChainman(context, req);
     653         [ #  # ]:           0 :         if (!maybe_chainman) return false;
     654                 :           0 :         ChainstateManager& chainman = *maybe_chainman;
     655         [ #  # ]:           0 :         LOCK(cs_main);
     656         [ #  # ]:           0 :         block_index = chainman.m_blockman.LookupBlockIndex(*block_hash);
     657         [ #  # ]:           0 :         if (!block_index) {
     658   [ #  #  #  #  :           0 :             return RESTERR(req, HTTP_NOT_FOUND, uri_parts[1] + " not found");
                   #  # ]
     659                 :             :         }
     660   [ #  #  #  # ]:           0 :         block_was_connected = block_index->IsValid(BLOCK_VALID_SCRIPTS);
     661                 :           0 :     }
     662                 :             : 
     663         [ #  # ]:           0 :     bool index_ready = index->BlockUntilSyncedToCurrentChain();
     664                 :             : 
     665         [ #  # ]:           0 :     BlockFilter filter;
     666   [ #  #  #  # ]:           0 :     if (!index->LookupFilter(block_index, filter)) {
     667         [ #  # ]:           0 :         std::string errmsg = "Filter not found.";
     668                 :             : 
     669         [ #  # ]:           0 :         if (!block_was_connected) {
     670         [ #  # ]:           0 :             errmsg += " Block was not connected to active chain.";
     671         [ #  # ]:           0 :         } else if (!index_ready) {
     672         [ #  # ]:           0 :             errmsg += " Block filters are still in the process of being indexed.";
     673                 :             :         } else {
     674         [ #  # ]:           0 :             errmsg += " This error is unexpected and indicates index corruption.";
     675                 :             :         }
     676                 :             : 
     677   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_NOT_FOUND, errmsg);
     678                 :           0 :     }
     679                 :             : 
     680   [ #  #  #  # ]:           0 :     switch (rf) {
     681                 :           0 :     case RESTResponseFormat::BINARY: {
     682                 :           0 :         DataStream ssResp{};
     683         [ #  # ]:           0 :         ssResp << filter;
     684                 :             : 
     685   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/octet-stream");
                   #  # ]
     686   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, ssResp);
     687                 :           0 :         return true;
     688                 :           0 :     }
     689                 :           0 :     case RESTResponseFormat::HEX: {
     690                 :           0 :         DataStream ssResp{};
     691         [ #  # ]:           0 :         ssResp << filter;
     692                 :             : 
     693   [ #  #  #  # ]:           0 :         std::string strHex = HexStr(ssResp) + "\n";
     694   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "text/plain");
                   #  # ]
     695   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strHex);
     696                 :           0 :         return true;
     697                 :           0 :     }
     698                 :           0 :     case RESTResponseFormat::JSON: {
     699                 :           0 :         UniValue ret(UniValue::VOBJ);
     700   [ #  #  #  #  :           0 :         ret.pushKV("filter", HexStr(filter.GetEncodedFilter()));
          #  #  #  #  #  
                      # ]
     701         [ #  # ]:           0 :         std::string strJSON = ret.write() + "\n";
     702   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
     703   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strJSON);
     704                 :           0 :         return true;
     705                 :           0 :     }
     706                 :           0 :     default: {
     707   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
                   #  # ]
     708                 :             :     }
     709                 :             :     }
     710                 :           0 : }
     711                 :             : 
     712                 :             : // A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
     713                 :             : RPCHelpMan getblockchaininfo();
     714                 :             : 
     715                 :           0 : static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std::string& uri_part)
     716                 :             : {
     717         [ #  # ]:           0 :     if (!CheckWarmup(req))
     718                 :             :         return false;
     719         [ #  # ]:           0 :     std::string param;
     720         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
     721                 :             : 
     722         [ #  # ]:           0 :     switch (rf) {
     723                 :           0 :     case RESTResponseFormat::JSON: {
     724                 :           0 :         JSONRPCRequest jsonRequest;
     725         [ #  # ]:           0 :         jsonRequest.context = context;
     726                 :           0 :         jsonRequest.params = UniValue(UniValue::VARR);
     727   [ #  #  #  # ]:           0 :         UniValue chainInfoObject = getblockchaininfo().HandleRequest(jsonRequest);
     728         [ #  # ]:           0 :         std::string strJSON = chainInfoObject.write() + "\n";
     729   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
     730   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strJSON);
     731                 :           0 :         return true;
     732                 :           0 :     }
     733                 :           0 :     default: {
     734   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
     735                 :             :     }
     736                 :             :     }
     737                 :           0 : }
     738                 :             : 
     739                 :             : 
     740                 :             : RPCHelpMan getdeploymentinfo();
     741                 :             : 
     742                 :           0 : static bool rest_deploymentinfo(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
     743                 :             : {
     744         [ #  # ]:           0 :     if (!CheckWarmup(req)) return false;
     745                 :             : 
     746         [ #  # ]:           0 :     std::string hash_str;
     747         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(hash_str, str_uri_part);
     748                 :             : 
     749         [ #  # ]:           0 :     switch (rf) {
     750                 :           0 :     case RESTResponseFormat::JSON: {
     751                 :           0 :         JSONRPCRequest jsonRequest;
     752         [ #  # ]:           0 :         jsonRequest.context = context;
     753                 :           0 :         jsonRequest.params = UniValue(UniValue::VARR);
     754                 :             : 
     755         [ #  # ]:           0 :         if (!hash_str.empty()) {
     756   [ #  #  #  # ]:           0 :             auto hash{uint256::FromHex(hash_str)};
     757         [ #  # ]:           0 :             if (!hash) {
     758   [ #  #  #  # ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hash_str);
     759                 :             :             }
     760                 :             : 
     761         [ #  # ]:           0 :             const ChainstateManager* chainman = GetChainman(context, req);
     762         [ #  # ]:           0 :             if (!chainman) return false;
     763   [ #  #  #  #  :           0 :             if (!WITH_LOCK(::cs_main, return chainman->m_blockman.LookupBlockIndex(*hash))) {
             #  #  #  # ]
     764   [ #  #  #  # ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, "Block not found");
     765                 :             :             }
     766                 :             : 
     767   [ #  #  #  # ]:           0 :             jsonRequest.params.push_back(hash_str);
     768                 :             :         }
     769                 :             : 
     770   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
     771   [ #  #  #  #  :           0 :         req->WriteReply(HTTP_OK, getdeploymentinfo().HandleRequest(jsonRequest).write() + "\n");
          #  #  #  #  #  
                      # ]
     772                 :           0 :         return true;
     773                 :           0 :     }
     774                 :           0 :     default: {
     775   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
     776                 :             :     }
     777                 :             :     }
     778                 :             : 
     779                 :           0 : }
     780                 :             : 
     781                 :           0 : static bool rest_mempool(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
     782                 :             : {
     783         [ #  # ]:           0 :     if (!CheckWarmup(req))
     784                 :             :         return false;
     785                 :             : 
     786         [ #  # ]:           0 :     std::string param;
     787         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(param, str_uri_part);
     788   [ #  #  #  # ]:           0 :     if (param != "contents" && param != "info") {
     789   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/mempool/<info|contents>.json");
     790                 :             :     }
     791                 :             : 
     792         [ #  # ]:           0 :     const CTxMemPool* mempool = GetMemPool(context, req);
     793         [ #  # ]:           0 :     if (!mempool) return false;
     794                 :             : 
     795         [ #  # ]:           0 :     switch (rf) {
     796                 :           0 :     case RESTResponseFormat::JSON: {
     797         [ #  # ]:           0 :         std::string str_json;
     798         [ #  # ]:           0 :         if (param == "contents") {
     799         [ #  # ]:           0 :             std::string raw_verbose;
     800                 :           0 :             try {
     801   [ #  #  #  #  :           0 :                 raw_verbose = req->GetQueryParameter("verbose").value_or("true");
                   #  # ]
     802         [ -  - ]:           0 :             } catch (const std::runtime_error& e) {
     803   [ -  -  -  - ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, e.what());
     804                 :           0 :             }
     805   [ #  #  #  # ]:           0 :             if (raw_verbose != "true" && raw_verbose != "false") {
     806   [ #  #  #  # ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, "The \"verbose\" query parameter must be either \"true\" or \"false\".");
     807                 :             :             }
     808         [ #  # ]:           0 :             std::string raw_mempool_sequence;
     809                 :           0 :             try {
     810   [ #  #  #  #  :           0 :                 raw_mempool_sequence = req->GetQueryParameter("mempool_sequence").value_or("false");
                   #  # ]
     811         [ -  - ]:           0 :             } catch (const std::runtime_error& e) {
     812   [ -  -  -  - ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, e.what());
     813                 :           0 :             }
     814   [ #  #  #  # ]:           0 :             if (raw_mempool_sequence != "true" && raw_mempool_sequence != "false") {
     815   [ #  #  #  # ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, "The \"mempool_sequence\" query parameter must be either \"true\" or \"false\".");
     816                 :             :             }
     817                 :           0 :             const bool verbose{raw_verbose == "true"};
     818                 :           0 :             const bool mempool_sequence{raw_mempool_sequence == "true"};
     819         [ #  # ]:           0 :             if (verbose && mempool_sequence) {
     820   [ #  #  #  # ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, "Verbose results cannot contain mempool sequence values. (hint: set \"verbose=false\")");
     821                 :             :             }
     822   [ #  #  #  # ]:           0 :             str_json = MempoolToJSON(*mempool, verbose, mempool_sequence).write() + "\n";
     823                 :           0 :         } else {
     824   [ #  #  #  # ]:           0 :             str_json = MempoolInfoToJSON(*mempool).write() + "\n";
     825                 :             :         }
     826                 :             : 
     827   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
     828   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, str_json);
     829                 :             :         return true;
     830                 :           0 :     }
     831                 :           0 :     default: {
     832   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
     833                 :             :     }
     834                 :             :     }
     835                 :           0 : }
     836                 :             : 
     837                 :           0 : static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string& uri_part)
     838                 :             : {
     839         [ #  # ]:           0 :     if (!CheckWarmup(req))
     840                 :             :         return false;
     841         [ #  # ]:           0 :     std::string hashStr;
     842         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(hashStr, uri_part);
     843                 :             : 
     844   [ #  #  #  # ]:           0 :     auto hash{Txid::FromHex(hashStr)};
     845         [ #  # ]:           0 :     if (!hash) {
     846   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
     847                 :             :     }
     848                 :             : 
     849         [ #  # ]:           0 :     if (g_txindex) {
     850         [ #  # ]:           0 :         g_txindex->BlockUntilSyncedToCurrentChain();
     851                 :             :     }
     852                 :             : 
     853         [ #  # ]:           0 :     const NodeContext* const node = GetNodeContext(context, req);
     854         [ #  # ]:           0 :     if (!node) return false;
     855                 :           0 :     uint256 hashBlock = uint256();
     856         [ #  # ]:           0 :     const CTransactionRef tx{GetTransaction(/*block_index=*/nullptr, node->mempool.get(), *hash,  node->chainman->m_blockman, hashBlock)};
     857         [ #  # ]:           0 :     if (!tx) {
     858   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
     859                 :             :     }
     860                 :             : 
     861   [ #  #  #  # ]:           0 :     switch (rf) {
     862                 :           0 :     case RESTResponseFormat::BINARY: {
     863                 :           0 :         DataStream ssTx;
     864         [ #  # ]:           0 :         ssTx << TX_WITH_WITNESS(tx);
     865                 :             : 
     866   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/octet-stream");
                   #  # ]
     867   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, ssTx);
     868                 :           0 :         return true;
     869                 :           0 :     }
     870                 :             : 
     871                 :           0 :     case RESTResponseFormat::HEX: {
     872                 :           0 :         DataStream ssTx;
     873         [ #  # ]:           0 :         ssTx << TX_WITH_WITNESS(tx);
     874                 :             : 
     875   [ #  #  #  # ]:           0 :         std::string strHex = HexStr(ssTx) + "\n";
     876   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "text/plain");
                   #  # ]
     877   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strHex);
     878                 :           0 :         return true;
     879                 :           0 :     }
     880                 :             : 
     881                 :           0 :     case RESTResponseFormat::JSON: {
     882                 :           0 :         UniValue objTx(UniValue::VOBJ);
     883         [ #  # ]:           0 :         TxToUniv(*tx, /*block_hash=*/hashBlock, /*entry=*/ objTx);
     884         [ #  # ]:           0 :         std::string strJSON = objTx.write() + "\n";
     885   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
     886   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strJSON);
     887                 :           0 :         return true;
     888                 :           0 :     }
     889                 :             : 
     890                 :           0 :     default: {
     891   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
                   #  # ]
     892                 :             :     }
     893                 :             :     }
     894                 :           0 : }
     895                 :             : 
     896                 :           0 : static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::string& uri_part)
     897                 :             : {
     898         [ #  # ]:           0 :     if (!CheckWarmup(req))
     899                 :             :         return false;
     900         [ #  # ]:           0 :     std::string param;
     901         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(param, uri_part);
     902                 :             : 
     903                 :           0 :     std::vector<std::string> uriParts;
     904   [ #  #  #  # ]:           0 :     if (param.length() > 1)
     905                 :             :     {
     906         [ #  # ]:           0 :         std::string strUriParams = param.substr(1);
     907   [ #  #  #  # ]:           0 :         uriParts = SplitString(strUriParams, '/');
     908                 :           0 :     }
     909                 :             : 
     910                 :             :     // throw exception in case of an empty request
     911         [ #  # ]:           0 :     std::string strRequestMutable = req->ReadBody();
     912   [ #  #  #  #  :           0 :     if (strRequestMutable.length() == 0 && uriParts.size() == 0)
                   #  # ]
     913   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
     914                 :             : 
     915                 :           0 :     bool fInputParsed = false;
     916                 :           0 :     bool fCheckMemPool = false;
     917                 :           0 :     std::vector<COutPoint> vOutPoints;
     918                 :             : 
     919                 :             :     // parse/deserialize input
     920                 :             :     // input-format = output-format, rest/getutxos/bin requires binary input, gives binary output, ...
     921                 :             : 
     922   [ #  #  #  # ]:           0 :     if (uriParts.size() > 0)
     923                 :             :     {
     924                 :             :         //inputs is sent over URI scheme (/rest/getutxos/checkmempool/txid1-n/txid2-n/...)
     925         [ #  # ]:           0 :         if (uriParts[0] == "checkmempool") fCheckMemPool = true;
     926                 :             : 
     927   [ #  #  #  #  :           0 :         for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
                   #  # ]
     928                 :             :         {
     929   [ #  #  #  # ]:           0 :             const auto txid_out{util::Split<std::string_view>(uriParts[i], '-')};
     930   [ #  #  #  # ]:           0 :             if (txid_out.size() != 2) {
     931   [ #  #  #  # ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
     932                 :             :             }
     933   [ #  #  #  # ]:           0 :             auto txid{Txid::FromHex(txid_out.at(0))};
     934         [ #  # ]:           0 :             auto output{ToIntegral<uint32_t>(txid_out.at(1))};
     935                 :             : 
     936   [ #  #  #  # ]:           0 :             if (!txid || !output) {
     937   [ #  #  #  # ]:           0 :                 return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
     938                 :             :             }
     939                 :             : 
     940         [ #  # ]:           0 :             vOutPoints.emplace_back(*txid, *output);
     941                 :           0 :         }
     942                 :             : 
     943   [ #  #  #  # ]:           0 :         if (vOutPoints.size() > 0)
     944                 :             :             fInputParsed = true;
     945                 :             :         else
     946   [ #  #  #  # ]:           0 :             return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
     947                 :             :     }
     948                 :             : 
     949   [ #  #  #  # ]:           0 :     switch (rf) {
     950                 :           0 :     case RESTResponseFormat::HEX: {
     951                 :             :         // convert hex to bin, continue then with bin part
     952   [ #  #  #  # ]:           0 :         std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable);
     953         [ #  # ]:           0 :         strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
     954                 :           0 :         [[fallthrough]];
     955                 :           0 :     }
     956                 :             : 
     957                 :           0 :     case RESTResponseFormat::BINARY: {
     958                 :           0 :         try {
     959                 :             :             //deserialize only if user sent a request
     960   [ #  #  #  # ]:           0 :             if (strRequestMutable.size() > 0)
     961                 :             :             {
     962         [ #  # ]:           0 :                 if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA
     963   [ #  #  #  # ]:           0 :                     return RESTERR(req, HTTP_BAD_REQUEST, "Combination of URI scheme inputs and raw post data is not allowed");
     964                 :             : 
     965                 :           0 :                 DataStream oss{};
     966         [ #  # ]:           0 :                 oss << strRequestMutable;
     967         [ #  # ]:           0 :                 oss >> fCheckMemPool;
     968         [ #  # ]:           0 :                 oss >> vOutPoints;
     969                 :           0 :             }
     970         [ -  - ]:           0 :         } catch (const std::ios_base::failure&) {
     971                 :             :             // abort in case of unreadable binary data
     972   [ -  -  -  - ]:           0 :             return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
     973                 :           0 :         }
     974                 :             :         break;
     975                 :             :     }
     976                 :             : 
     977                 :           0 :     case RESTResponseFormat::JSON: {
     978         [ #  # ]:           0 :         if (!fInputParsed)
     979   [ #  #  #  # ]:           0 :             return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
     980                 :             :         break;
     981                 :             :     }
     982                 :           0 :     default: {
     983   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
                   #  # ]
     984                 :             :     }
     985                 :             :     }
     986                 :             : 
     987                 :             :     // limit max outpoints
     988   [ #  #  #  # ]:           0 :     if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
     989   [ #  #  #  # ]:           0 :         return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
     990                 :             : 
     991                 :             :     // check spentness and form a bitmap (as well as a JSON capable human-readable string representation)
     992                 :           0 :     std::vector<unsigned char> bitmap;
     993                 :           0 :     std::vector<CCoin> outs;
     994         [ #  # ]:           0 :     std::string bitmapStringRepresentation;
     995                 :           0 :     std::vector<bool> hits;
     996         [ #  # ]:           0 :     bitmap.resize((vOutPoints.size() + 7) / 8);
     997         [ #  # ]:           0 :     ChainstateManager* maybe_chainman = GetChainman(context, req);
     998         [ #  # ]:           0 :     if (!maybe_chainman) return false;
     999                 :           0 :     ChainstateManager& chainman = *maybe_chainman;
    1000                 :           0 :     decltype(chainman.ActiveHeight()) active_height;
    1001                 :           0 :     uint256 active_hash;
    1002                 :           0 :     {
    1003                 :           0 :         auto process_utxos = [&vOutPoints, &outs, &hits, &active_height, &active_hash, &chainman](const CCoinsView& view, const CTxMemPool* mempool) EXCLUSIVE_LOCKS_REQUIRED(chainman.GetMutex()) {
    1004         [ #  # ]:           0 :             for (const COutPoint& vOutPoint : vOutPoints) {
    1005   [ #  #  #  # ]:           0 :                 auto coin = !mempool || !mempool->isSpent(vOutPoint) ? view.GetCoin(vOutPoint) : std::nullopt;
    1006         [ #  # ]:           0 :                 hits.push_back(coin.has_value());
    1007   [ #  #  #  # ]:           0 :                 if (coin) outs.emplace_back(std::move(*coin));
    1008                 :           0 :             }
    1009                 :           0 :             active_height = chainman.ActiveHeight();
    1010                 :           0 :             active_hash = chainman.ActiveTip()->GetBlockHash();
    1011                 :           0 :         };
    1012                 :             : 
    1013         [ #  # ]:           0 :         if (fCheckMemPool) {
    1014         [ #  # ]:           0 :             const CTxMemPool* mempool = GetMemPool(context, req);
    1015         [ #  # ]:           0 :             if (!mempool) return false;
    1016                 :             :             // use db+mempool as cache backend in case user likes to query mempool
    1017   [ #  #  #  # ]:           0 :             LOCK2(cs_main, mempool->cs);
    1018   [ #  #  #  # ]:           0 :             CCoinsViewCache& viewChain = chainman.ActiveChainstate().CoinsTip();
    1019         [ #  # ]:           0 :             CCoinsViewMemPool viewMempool(&viewChain, *mempool);
    1020         [ #  # ]:           0 :             process_utxos(viewMempool, mempool);
    1021   [ #  #  #  # ]:           0 :         } else {
    1022         [ #  # ]:           0 :             LOCK(cs_main);
    1023   [ #  #  #  #  :           0 :             process_utxos(chainman.ActiveChainstate().CoinsTip(), nullptr);
                   #  # ]
    1024                 :           0 :         }
    1025                 :             : 
    1026         [ #  # ]:           0 :         for (size_t i = 0; i < hits.size(); ++i) {
    1027         [ #  # ]:           0 :             const bool hit = hits[i];
    1028   [ #  #  #  # ]:           0 :             bitmapStringRepresentation.append(hit ? "1" : "0"); // form a binary string representation (human-readable for json output)
    1029                 :           0 :             bitmap[i / 8] |= ((uint8_t)hit) << (i % 8);
    1030                 :             :         }
    1031                 :             :     }
    1032                 :             : 
    1033   [ #  #  #  # ]:           0 :     switch (rf) {
    1034                 :           0 :     case RESTResponseFormat::BINARY: {
    1035                 :             :         // serialize data
    1036                 :             :         // use exact same output as mentioned in Bip64
    1037                 :           0 :         DataStream ssGetUTXOResponse{};
    1038   [ #  #  #  #  :           0 :         ssGetUTXOResponse << active_height << active_hash << bitmap << outs;
             #  #  #  # ]
    1039                 :             : 
    1040   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/octet-stream");
                   #  # ]
    1041   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, ssGetUTXOResponse);
    1042                 :           0 :         return true;
    1043                 :           0 :     }
    1044                 :             : 
    1045                 :           0 :     case RESTResponseFormat::HEX: {
    1046                 :           0 :         DataStream ssGetUTXOResponse{};
    1047   [ #  #  #  #  :           0 :         ssGetUTXOResponse << active_height << active_hash << bitmap << outs;
             #  #  #  # ]
    1048   [ #  #  #  # ]:           0 :         std::string strHex = HexStr(ssGetUTXOResponse) + "\n";
    1049                 :             : 
    1050   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "text/plain");
                   #  # ]
    1051   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strHex);
    1052                 :           0 :         return true;
    1053                 :           0 :     }
    1054                 :             : 
    1055                 :           0 :     case RESTResponseFormat::JSON: {
    1056                 :           0 :         UniValue objGetUTXOResponse(UniValue::VOBJ);
    1057                 :             : 
    1058                 :             :         // pack in some essentials
    1059                 :             :         // use more or less the same output as mentioned in Bip64
    1060   [ #  #  #  #  :           0 :         objGetUTXOResponse.pushKV("chainHeight", active_height);
                   #  # ]
    1061   [ #  #  #  #  :           0 :         objGetUTXOResponse.pushKV("chaintipHash", active_hash.GetHex());
             #  #  #  # ]
    1062   [ #  #  #  #  :           0 :         objGetUTXOResponse.pushKV("bitmap", bitmapStringRepresentation);
                   #  # ]
    1063                 :             : 
    1064                 :           0 :         UniValue utxos(UniValue::VARR);
    1065         [ #  # ]:           0 :         for (const CCoin& coin : outs) {
    1066                 :           0 :             UniValue utxo(UniValue::VOBJ);
    1067   [ #  #  #  #  :           0 :             utxo.pushKV("height", (int32_t)coin.nHeight);
                   #  # ]
    1068   [ #  #  #  #  :           0 :             utxo.pushKV("value", ValueFromAmount(coin.out.nValue));
                   #  # ]
    1069                 :             : 
    1070                 :             :             // include the script in a json output
    1071                 :           0 :             UniValue o(UniValue::VOBJ);
    1072         [ #  # ]:           0 :             ScriptToUniv(coin.out.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
    1073   [ #  #  #  # ]:           0 :             utxo.pushKV("scriptPubKey", std::move(o));
    1074         [ #  # ]:           0 :             utxos.push_back(std::move(utxo));
    1075                 :           0 :         }
    1076   [ #  #  #  # ]:           0 :         objGetUTXOResponse.pushKV("utxos", std::move(utxos));
    1077                 :             : 
    1078                 :             :         // return json string
    1079         [ #  # ]:           0 :         std::string strJSON = objGetUTXOResponse.write() + "\n";
    1080   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
    1081   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, strJSON);
    1082                 :           0 :         return true;
    1083                 :           0 :     }
    1084                 :           0 :     default: {
    1085   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
                   #  # ]
    1086                 :             :     }
    1087                 :             :     }
    1088                 :           0 : }
    1089                 :             : 
    1090                 :           0 : static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
    1091                 :             :                        const std::string& str_uri_part)
    1092                 :             : {
    1093         [ #  # ]:           0 :     if (!CheckWarmup(req)) return false;
    1094         [ #  # ]:           0 :     std::string height_str;
    1095         [ #  # ]:           0 :     const RESTResponseFormat rf = ParseDataFormat(height_str, str_uri_part);
    1096                 :             : 
    1097         [ #  # ]:           0 :     const auto blockheight{ToIntegral<int32_t>(height_str)};
    1098   [ #  #  #  # ]:           0 :     if (!blockheight || *blockheight < 0) {
    1099   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_BAD_REQUEST, "Invalid height: " + SanitizeString(height_str, SAFE_CHARS_URI));
             #  #  #  # ]
    1100                 :             :     }
    1101                 :             : 
    1102                 :           0 :     CBlockIndex* pblockindex = nullptr;
    1103                 :           0 :     {
    1104         [ #  # ]:           0 :         ChainstateManager* maybe_chainman = GetChainman(context, req);
    1105         [ #  # ]:           0 :         if (!maybe_chainman) return false;
    1106                 :           0 :         ChainstateManager& chainman = *maybe_chainman;
    1107         [ #  # ]:           0 :         LOCK(cs_main);
    1108         [ #  # ]:           0 :         const CChain& active_chain = chainman.ActiveChain();
    1109   [ #  #  #  # ]:           0 :         if (*blockheight > active_chain.Height()) {
    1110   [ #  #  #  #  :           0 :             return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
                   #  # ]
    1111                 :             :         }
    1112         [ #  # ]:           0 :         pblockindex = active_chain[*blockheight];
    1113                 :           0 :     }
    1114   [ #  #  #  # ]:           0 :     switch (rf) {
    1115                 :           0 :     case RESTResponseFormat::BINARY: {
    1116                 :           0 :         DataStream ss_blockhash{};
    1117         [ #  # ]:           0 :         ss_blockhash << pblockindex->GetBlockHash();
    1118   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/octet-stream");
                   #  # ]
    1119   [ #  #  #  # ]:           0 :         req->WriteReply(HTTP_OK, ss_blockhash);
    1120                 :           0 :         return true;
    1121                 :           0 :     }
    1122                 :           0 :     case RESTResponseFormat::HEX: {
    1123   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "text/plain");
                   #  # ]
    1124   [ #  #  #  #  :           0 :         req->WriteReply(HTTP_OK, pblockindex->GetBlockHash().GetHex() + "\n");
                   #  # ]
    1125                 :           0 :         return true;
    1126                 :             :     }
    1127                 :           0 :     case RESTResponseFormat::JSON: {
    1128   [ #  #  #  #  :           0 :         req->WriteHeader("Content-Type", "application/json");
                   #  # ]
    1129                 :           0 :         UniValue resp = UniValue(UniValue::VOBJ);
    1130   [ #  #  #  #  :           0 :         resp.pushKV("blockhash", pblockindex->GetBlockHash().GetHex());
             #  #  #  # ]
    1131   [ #  #  #  #  :           0 :         req->WriteReply(HTTP_OK, resp.write() + "\n");
                   #  # ]
    1132                 :           0 :         return true;
    1133                 :           0 :     }
    1134                 :           0 :     default: {
    1135   [ #  #  #  #  :           0 :         return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
                   #  # ]
    1136                 :             :     }
    1137                 :             :     }
    1138                 :           0 : }
    1139                 :             : 
    1140                 :             : static const struct {
    1141                 :             :     const char* prefix;
    1142                 :             :     bool (*handler)(const std::any& context, HTTPRequest* req, const std::string& strReq);
    1143                 :             : } uri_prefixes[] = {
    1144                 :             :     {"/rest/tx/", rest_tx},
    1145                 :             :     {"/rest/block/notxdetails/", rest_block_notxdetails},
    1146                 :             :     {"/rest/block/", rest_block_extended},
    1147                 :             :     {"/rest/blockpart/", rest_block_part},
    1148                 :             :     {"/rest/blockfilter/", rest_block_filter},
    1149                 :             :     {"/rest/blockfilterheaders/", rest_filter_header},
    1150                 :             :     {"/rest/chaininfo", rest_chaininfo},
    1151                 :             :     {"/rest/mempool/", rest_mempool},
    1152                 :             :     {"/rest/headers/", rest_headers},
    1153                 :             :     {"/rest/getutxos", rest_getutxos},
    1154                 :             :     {"/rest/deploymentinfo/", rest_deploymentinfo},
    1155                 :             :     {"/rest/deploymentinfo", rest_deploymentinfo},
    1156                 :             :     {"/rest/blockhashbyheight/", rest_blockhash_by_height},
    1157                 :             :     {"/rest/spenttxouts/", rest_spent_txouts},
    1158                 :             : };
    1159                 :             : 
    1160                 :           0 : void StartREST(const std::any& context)
    1161                 :             : {
    1162         [ #  # ]:           0 :     for (const auto& up : uri_prefixes) {
    1163   [ #  #  #  # ]:           0 :         auto handler = [context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); };
    1164   [ #  #  #  #  :           0 :         RegisterHTTPHandler(up.prefix, false, handler);
                   #  # ]
    1165                 :           0 :     }
    1166                 :           0 : }
    1167                 :             : 
    1168                 :           0 : void InterruptREST()
    1169                 :             : {
    1170                 :           0 : }
    1171                 :             : 
    1172                 :           0 : void StopREST()
    1173                 :             : {
    1174         [ #  # ]:           0 :     for (const auto& up : uri_prefixes) {
    1175         [ #  # ]:           0 :         UnregisterHTTPHandler(up.prefix, false);
    1176                 :             :     }
    1177                 :           0 : }
        

Generated by: LCOV version 2.0-1