scripted-diff: rename assert_raises_jsonrpc to assert_raises_rpc error
[bitcoinplatinum.git] / src / rest.cpp
blob0b2c843d5f9986acf3e7e701c971a85888841a7d
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #include "chain.h"
7 #include "chainparams.h"
8 #include "core_io.h"
9 #include "primitives/block.h"
10 #include "primitives/transaction.h"
11 #include "validation.h"
12 #include "httpserver.h"
13 #include "rpc/blockchain.h"
14 #include "rpc/server.h"
15 #include "streams.h"
16 #include "sync.h"
17 #include "txmempool.h"
18 #include "utilstrencodings.h"
19 #include "version.h"
21 #include <boost/algorithm/string.hpp>
23 #include <univalue.h>
25 static const size_t MAX_GETUTXOS_OUTPOINTS = 15; //allow a max of 15 outpoints to be queried at once
27 enum RetFormat {
28 RF_UNDEF,
29 RF_BINARY,
30 RF_HEX,
31 RF_JSON,
34 static const struct {
35 enum RetFormat rf;
36 const char* name;
37 } rf_names[] = {
38 {RF_UNDEF, ""},
39 {RF_BINARY, "bin"},
40 {RF_HEX, "hex"},
41 {RF_JSON, "json"},
44 struct CCoin {
45 uint32_t nHeight;
46 CTxOut out;
48 ADD_SERIALIZE_METHODS;
50 CCoin() : nHeight(0) {}
51 explicit CCoin(Coin&& in) : nHeight(in.nHeight), out(std::move(in.out)) {}
53 template <typename Stream, typename Operation>
54 inline void SerializationOp(Stream& s, Operation ser_action)
56 uint32_t nTxVerDummy = 0;
57 READWRITE(nTxVerDummy);
58 READWRITE(nHeight);
59 READWRITE(out);
63 static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string message)
65 req->WriteHeader("Content-Type", "text/plain");
66 req->WriteReply(status, message + "\r\n");
67 return false;
70 static enum RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
72 const std::string::size_type pos = strReq.rfind('.');
73 if (pos == std::string::npos)
75 param = strReq;
76 return rf_names[0].rf;
79 param = strReq.substr(0, pos);
80 const std::string suff(strReq, pos + 1);
82 for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
83 if (suff == rf_names[i].name)
84 return rf_names[i].rf;
86 /* If no suffix is found, return original string. */
87 param = strReq;
88 return rf_names[0].rf;
91 static std::string AvailableDataFormatsString()
93 std::string formats = "";
94 for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
95 if (strlen(rf_names[i].name) > 0) {
96 formats.append(".");
97 formats.append(rf_names[i].name);
98 formats.append(", ");
101 if (formats.length() > 0)
102 return formats.substr(0, formats.length() - 2);
104 return formats;
107 static bool ParseHashStr(const std::string& strReq, uint256& v)
109 if (!IsHex(strReq) || (strReq.size() != 64))
110 return false;
112 v.SetHex(strReq);
113 return true;
116 static bool CheckWarmup(HTTPRequest* req)
118 std::string statusmessage;
119 if (RPCIsInWarmup(&statusmessage))
120 return RESTERR(req, HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: " + statusmessage);
121 return true;
124 static bool rest_headers(HTTPRequest* req,
125 const std::string& strURIPart)
127 if (!CheckWarmup(req))
128 return false;
129 std::string param;
130 const RetFormat rf = ParseDataFormat(param, strURIPart);
131 std::vector<std::string> path;
132 boost::split(path, param, boost::is_any_of("/"));
134 if (path.size() != 2)
135 return RESTERR(req, HTTP_BAD_REQUEST, "No header count specified. Use /rest/headers/<count>/<hash>.<ext>.");
137 long count = strtol(path[0].c_str(), nullptr, 10);
138 if (count < 1 || count > 2000)
139 return RESTERR(req, HTTP_BAD_REQUEST, "Header count out of range: " + path[0]);
141 std::string hashStr = path[1];
142 uint256 hash;
143 if (!ParseHashStr(hashStr, hash))
144 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
146 std::vector<const CBlockIndex *> headers;
147 headers.reserve(count);
149 LOCK(cs_main);
150 BlockMap::const_iterator it = mapBlockIndex.find(hash);
151 const CBlockIndex *pindex = (it != mapBlockIndex.end()) ? it->second : nullptr;
152 while (pindex != nullptr && chainActive.Contains(pindex)) {
153 headers.push_back(pindex);
154 if (headers.size() == (unsigned long)count)
155 break;
156 pindex = chainActive.Next(pindex);
160 CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
161 for (const CBlockIndex *pindex : headers) {
162 ssHeader << pindex->GetBlockHeader();
165 switch (rf) {
166 case RF_BINARY: {
167 std::string binaryHeader = ssHeader.str();
168 req->WriteHeader("Content-Type", "application/octet-stream");
169 req->WriteReply(HTTP_OK, binaryHeader);
170 return true;
173 case RF_HEX: {
174 std::string strHex = HexStr(ssHeader.begin(), ssHeader.end()) + "\n";
175 req->WriteHeader("Content-Type", "text/plain");
176 req->WriteReply(HTTP_OK, strHex);
177 return true;
179 case RF_JSON: {
180 UniValue jsonHeaders(UniValue::VARR);
181 for (const CBlockIndex *pindex : headers) {
182 jsonHeaders.push_back(blockheaderToJSON(pindex));
184 std::string strJSON = jsonHeaders.write() + "\n";
185 req->WriteHeader("Content-Type", "application/json");
186 req->WriteReply(HTTP_OK, strJSON);
187 return true;
189 default: {
190 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: .bin, .hex)");
195 static bool rest_block(HTTPRequest* req,
196 const std::string& strURIPart,
197 bool showTxDetails)
199 if (!CheckWarmup(req))
200 return false;
201 std::string hashStr;
202 const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
204 uint256 hash;
205 if (!ParseHashStr(hashStr, hash))
206 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
208 CBlock block;
209 CBlockIndex* pblockindex = nullptr;
211 LOCK(cs_main);
212 if (mapBlockIndex.count(hash) == 0)
213 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
215 pblockindex = mapBlockIndex[hash];
216 if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0)
217 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
219 if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus()))
220 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
223 CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
224 ssBlock << block;
226 switch (rf) {
227 case RF_BINARY: {
228 std::string binaryBlock = ssBlock.str();
229 req->WriteHeader("Content-Type", "application/octet-stream");
230 req->WriteReply(HTTP_OK, binaryBlock);
231 return true;
234 case RF_HEX: {
235 std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n";
236 req->WriteHeader("Content-Type", "text/plain");
237 req->WriteReply(HTTP_OK, strHex);
238 return true;
241 case RF_JSON: {
242 UniValue objBlock = blockToJSON(block, pblockindex, showTxDetails);
243 std::string strJSON = objBlock.write() + "\n";
244 req->WriteHeader("Content-Type", "application/json");
245 req->WriteReply(HTTP_OK, strJSON);
246 return true;
249 default: {
250 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
255 static bool rest_block_extended(HTTPRequest* req, const std::string& strURIPart)
257 return rest_block(req, strURIPart, true);
260 static bool rest_block_notxdetails(HTTPRequest* req, const std::string& strURIPart)
262 return rest_block(req, strURIPart, false);
265 // A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
266 UniValue getblockchaininfo(const JSONRPCRequest& request);
268 static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
270 if (!CheckWarmup(req))
271 return false;
272 std::string param;
273 const RetFormat rf = ParseDataFormat(param, strURIPart);
275 switch (rf) {
276 case RF_JSON: {
277 JSONRPCRequest jsonRequest;
278 jsonRequest.params = UniValue(UniValue::VARR);
279 UniValue chainInfoObject = getblockchaininfo(jsonRequest);
280 std::string strJSON = chainInfoObject.write() + "\n";
281 req->WriteHeader("Content-Type", "application/json");
282 req->WriteReply(HTTP_OK, strJSON);
283 return true;
285 default: {
286 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
291 static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
293 if (!CheckWarmup(req))
294 return false;
295 std::string param;
296 const RetFormat rf = ParseDataFormat(param, strURIPart);
298 switch (rf) {
299 case RF_JSON: {
300 UniValue mempoolInfoObject = mempoolInfoToJSON();
302 std::string strJSON = mempoolInfoObject.write() + "\n";
303 req->WriteHeader("Content-Type", "application/json");
304 req->WriteReply(HTTP_OK, strJSON);
305 return true;
307 default: {
308 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
313 static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPart)
315 if (!CheckWarmup(req))
316 return false;
317 std::string param;
318 const RetFormat rf = ParseDataFormat(param, strURIPart);
320 switch (rf) {
321 case RF_JSON: {
322 UniValue mempoolObject = mempoolToJSON(true);
324 std::string strJSON = mempoolObject.write() + "\n";
325 req->WriteHeader("Content-Type", "application/json");
326 req->WriteReply(HTTP_OK, strJSON);
327 return true;
329 default: {
330 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
335 static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
337 if (!CheckWarmup(req))
338 return false;
339 std::string hashStr;
340 const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
342 uint256 hash;
343 if (!ParseHashStr(hashStr, hash))
344 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
346 CTransactionRef tx;
347 uint256 hashBlock = uint256();
348 if (!GetTransaction(hash, tx, Params().GetConsensus(), hashBlock, true))
349 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
351 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
352 ssTx << tx;
354 switch (rf) {
355 case RF_BINARY: {
356 std::string binaryTx = ssTx.str();
357 req->WriteHeader("Content-Type", "application/octet-stream");
358 req->WriteReply(HTTP_OK, binaryTx);
359 return true;
362 case RF_HEX: {
363 std::string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n";
364 req->WriteHeader("Content-Type", "text/plain");
365 req->WriteReply(HTTP_OK, strHex);
366 return true;
369 case RF_JSON: {
370 UniValue objTx(UniValue::VOBJ);
371 TxToUniv(*tx, hashBlock, objTx);
372 std::string strJSON = objTx.write() + "\n";
373 req->WriteHeader("Content-Type", "application/json");
374 req->WriteReply(HTTP_OK, strJSON);
375 return true;
378 default: {
379 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
384 static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
386 if (!CheckWarmup(req))
387 return false;
388 std::string param;
389 const RetFormat rf = ParseDataFormat(param, strURIPart);
391 std::vector<std::string> uriParts;
392 if (param.length() > 1)
394 std::string strUriParams = param.substr(1);
395 boost::split(uriParts, strUriParams, boost::is_any_of("/"));
398 // throw exception in case of an empty request
399 std::string strRequestMutable = req->ReadBody();
400 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
401 return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
403 bool fInputParsed = false;
404 bool fCheckMemPool = false;
405 std::vector<COutPoint> vOutPoints;
407 // parse/deserialize input
408 // input-format = output-format, rest/getutxos/bin requires binary input, gives binary output, ...
410 if (uriParts.size() > 0)
413 //inputs is sent over URI scheme (/rest/getutxos/checkmempool/txid1-n/txid2-n/...)
414 if (uriParts.size() > 0 && uriParts[0] == "checkmempool")
415 fCheckMemPool = true;
417 for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
419 uint256 txid;
420 int32_t nOutput;
421 std::string strTxid = uriParts[i].substr(0, uriParts[i].find("-"));
422 std::string strOutput = uriParts[i].substr(uriParts[i].find("-")+1);
424 if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid))
425 return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
427 txid.SetHex(strTxid);
428 vOutPoints.push_back(COutPoint(txid, (uint32_t)nOutput));
431 if (vOutPoints.size() > 0)
432 fInputParsed = true;
433 else
434 return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
437 switch (rf) {
438 case RF_HEX: {
439 // convert hex to bin, continue then with bin part
440 std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable);
441 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
444 case RF_BINARY: {
445 try {
446 //deserialize only if user sent a request
447 if (strRequestMutable.size() > 0)
449 if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA
450 return RESTERR(req, HTTP_BAD_REQUEST, "Combination of URI scheme inputs and raw post data is not allowed");
452 CDataStream oss(SER_NETWORK, PROTOCOL_VERSION);
453 oss << strRequestMutable;
454 oss >> fCheckMemPool;
455 oss >> vOutPoints;
457 } catch (const std::ios_base::failure& e) {
458 // abort in case of unreadable binary data
459 return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
461 break;
464 case RF_JSON: {
465 if (!fInputParsed)
466 return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
467 break;
469 default: {
470 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
474 // limit max outpoints
475 if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
476 return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
478 // check spentness and form a bitmap (as well as a JSON capable human-readable string representation)
479 std::vector<unsigned char> bitmap;
480 std::vector<CCoin> outs;
481 std::string bitmapStringRepresentation;
482 std::vector<bool> hits;
483 bitmap.resize((vOutPoints.size() + 7) / 8);
485 LOCK2(cs_main, mempool.cs);
487 CCoinsView viewDummy;
488 CCoinsViewCache view(&viewDummy);
490 CCoinsViewCache& viewChain = *pcoinsTip;
491 CCoinsViewMemPool viewMempool(&viewChain, mempool);
493 if (fCheckMemPool)
494 view.SetBackend(viewMempool); // switch cache backend to db+mempool in case user likes to query mempool
496 for (size_t i = 0; i < vOutPoints.size(); i++) {
497 bool hit = false;
498 Coin coin;
499 if (view.GetCoin(vOutPoints[i], coin) && !mempool.isSpent(vOutPoints[i])) {
500 hit = true;
501 outs.emplace_back(std::move(coin));
504 hits.push_back(hit);
505 bitmapStringRepresentation.append(hit ? "1" : "0"); // form a binary string representation (human-readable for json output)
506 bitmap[i / 8] |= ((uint8_t)hit) << (i % 8);
510 switch (rf) {
511 case RF_BINARY: {
512 // serialize data
513 // use exact same output as mentioned in Bip64
514 CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
515 ssGetUTXOResponse << chainActive.Height() << chainActive.Tip()->GetBlockHash() << bitmap << outs;
516 std::string ssGetUTXOResponseString = ssGetUTXOResponse.str();
518 req->WriteHeader("Content-Type", "application/octet-stream");
519 req->WriteReply(HTTP_OK, ssGetUTXOResponseString);
520 return true;
523 case RF_HEX: {
524 CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
525 ssGetUTXOResponse << chainActive.Height() << chainActive.Tip()->GetBlockHash() << bitmap << outs;
526 std::string strHex = HexStr(ssGetUTXOResponse.begin(), ssGetUTXOResponse.end()) + "\n";
528 req->WriteHeader("Content-Type", "text/plain");
529 req->WriteReply(HTTP_OK, strHex);
530 return true;
533 case RF_JSON: {
534 UniValue objGetUTXOResponse(UniValue::VOBJ);
536 // pack in some essentials
537 // use more or less the same output as mentioned in Bip64
538 objGetUTXOResponse.push_back(Pair("chainHeight", chainActive.Height()));
539 objGetUTXOResponse.push_back(Pair("chaintipHash", chainActive.Tip()->GetBlockHash().GetHex()));
540 objGetUTXOResponse.push_back(Pair("bitmap", bitmapStringRepresentation));
542 UniValue utxos(UniValue::VARR);
543 for (const CCoin& coin : outs) {
544 UniValue utxo(UniValue::VOBJ);
545 utxo.push_back(Pair("height", (int32_t)coin.nHeight));
546 utxo.push_back(Pair("value", ValueFromAmount(coin.out.nValue)));
548 // include the script in a json output
549 UniValue o(UniValue::VOBJ);
550 ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true);
551 utxo.push_back(Pair("scriptPubKey", o));
552 utxos.push_back(utxo);
554 objGetUTXOResponse.push_back(Pair("utxos", utxos));
556 // return json string
557 std::string strJSON = objGetUTXOResponse.write() + "\n";
558 req->WriteHeader("Content-Type", "application/json");
559 req->WriteReply(HTTP_OK, strJSON);
560 return true;
562 default: {
563 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
568 static const struct {
569 const char* prefix;
570 bool (*handler)(HTTPRequest* req, const std::string& strReq);
571 } uri_prefixes[] = {
572 {"/rest/tx/", rest_tx},
573 {"/rest/block/notxdetails/", rest_block_notxdetails},
574 {"/rest/block/", rest_block_extended},
575 {"/rest/chaininfo", rest_chaininfo},
576 {"/rest/mempool/info", rest_mempool_info},
577 {"/rest/mempool/contents", rest_mempool_contents},
578 {"/rest/headers/", rest_headers},
579 {"/rest/getutxos", rest_getutxos},
582 bool StartREST()
584 for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++)
585 RegisterHTTPHandler(uri_prefixes[i].prefix, false, uri_prefixes[i].handler);
586 return true;
589 void InterruptREST()
593 void StopREST()
595 for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++)
596 UnregisterHTTPHandler(uri_prefixes[i].prefix, false);