Coverage Report

Created: 2025-09-19 18:22

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