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