Bitcoin Core Fuzz Coverage Report for wallet_tx_can_be_bumped

Coverage Report

Created: 2025-11-19 11:20

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