Coverage Report

Created: 2024-10-21 15:10

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