LCOV - code coverage report
Current view: top level - src - rest.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 80.7 % 724 584
Test Date: 2025-12-25 05:18:09 Functions: 96.6 % 29 28
Branches: 44.7 % 1521 680

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

Generated by: LCOV version 2.0-1