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