Add UpdatedBlockTip signal to CMainSignals and CValidationInterface
[bitcoinplatinum.git] / src / rest.cpp
blob226e237fc649d75bfa4811fc902791768f6c45d9
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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 "primitives/block.h"
8 #include "primitives/transaction.h"
9 #include "main.h"
10 #include "httpserver.h"
11 #include "rpcserver.h"
12 #include "streams.h"
13 #include "sync.h"
14 #include "txmempool.h"
15 #include "utilstrencodings.h"
16 #include "version.h"
18 #include <boost/algorithm/string.hpp>
19 #include <boost/dynamic_bitset.hpp>
21 #include "univalue/univalue.h"
23 using namespace std;
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 nTxVer; // Don't call this nVersion, that name has a special meaning inside IMPLEMENT_SERIALIZE
46 uint32_t nHeight;
47 CTxOut out;
49 ADD_SERIALIZE_METHODS;
51 template <typename Stream, typename Operation>
52 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
54 READWRITE(nTxVer);
55 READWRITE(nHeight);
56 READWRITE(out);
60 extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
61 extern UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false);
62 extern UniValue mempoolInfoToJSON();
63 extern UniValue mempoolToJSON(bool fVerbose = false);
64 extern void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
65 extern UniValue blockheaderToJSON(const CBlockIndex* blockindex);
67 static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, string message)
69 req->WriteHeader("Content-Type", "text/plain");
70 req->WriteReply(status, message + "\r\n");
71 return false;
74 static enum RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
76 const std::string::size_type pos = strReq.rfind('.');
77 if (pos == std::string::npos)
79 param = strReq;
80 return rf_names[0].rf;
83 param = strReq.substr(0, pos);
84 const std::string suff(strReq, pos + 1);
86 for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
87 if (suff == rf_names[i].name)
88 return rf_names[i].rf;
90 /* If no suffix is found, return original string. */
91 param = strReq;
92 return rf_names[0].rf;
95 static string AvailableDataFormatsString()
97 string formats = "";
98 for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
99 if (strlen(rf_names[i].name) > 0) {
100 formats.append(".");
101 formats.append(rf_names[i].name);
102 formats.append(", ");
105 if (formats.length() > 0)
106 return formats.substr(0, formats.length() - 2);
108 return formats;
111 static bool ParseHashStr(const string& strReq, uint256& v)
113 if (!IsHex(strReq) || (strReq.size() != 64))
114 return false;
116 v.SetHex(strReq);
117 return true;
120 static bool CheckWarmup(HTTPRequest* req)
122 std::string statusmessage;
123 if (RPCIsInWarmup(&statusmessage))
124 return RESTERR(req, HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: " + statusmessage);
125 return true;
128 static bool rest_headers(HTTPRequest* req,
129 const std::string& strURIPart)
131 if (!CheckWarmup(req))
132 return false;
133 std::string param;
134 const RetFormat rf = ParseDataFormat(param, strURIPart);
135 vector<string> path;
136 boost::split(path, param, boost::is_any_of("/"));
138 if (path.size() != 2)
139 return RESTERR(req, HTTP_BAD_REQUEST, "No header count specified. Use /rest/headers/<count>/<hash>.<ext>.");
141 long count = strtol(path[0].c_str(), NULL, 10);
142 if (count < 1 || count > 2000)
143 return RESTERR(req, HTTP_BAD_REQUEST, "Header count out of range: " + path[0]);
145 string hashStr = path[1];
146 uint256 hash;
147 if (!ParseHashStr(hashStr, hash))
148 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
150 std::vector<const CBlockIndex *> headers;
151 headers.reserve(count);
153 LOCK(cs_main);
154 BlockMap::const_iterator it = mapBlockIndex.find(hash);
155 const CBlockIndex *pindex = (it != mapBlockIndex.end()) ? it->second : NULL;
156 while (pindex != NULL && chainActive.Contains(pindex)) {
157 headers.push_back(pindex);
158 if (headers.size() == (unsigned long)count)
159 break;
160 pindex = chainActive.Next(pindex);
164 CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
165 BOOST_FOREACH(const CBlockIndex *pindex, headers) {
166 ssHeader << pindex->GetBlockHeader();
169 switch (rf) {
170 case RF_BINARY: {
171 string binaryHeader = ssHeader.str();
172 req->WriteHeader("Content-Type", "application/octet-stream");
173 req->WriteReply(HTTP_OK, binaryHeader);
174 return true;
177 case RF_HEX: {
178 string strHex = HexStr(ssHeader.begin(), ssHeader.end()) + "\n";
179 req->WriteHeader("Content-Type", "text/plain");
180 req->WriteReply(HTTP_OK, strHex);
181 return true;
183 case RF_JSON: {
184 UniValue jsonHeaders(UniValue::VARR);
185 BOOST_FOREACH(const CBlockIndex *pindex, headers) {
186 jsonHeaders.push_back(blockheaderToJSON(pindex));
188 string strJSON = jsonHeaders.write() + "\n";
189 req->WriteHeader("Content-Type", "application/json");
190 req->WriteReply(HTTP_OK, strJSON);
191 return true;
193 default: {
194 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: .bin, .hex)");
198 // not reached
199 return true; // continue to process further HTTP reqs on this cxn
202 static bool rest_block(HTTPRequest* req,
203 const std::string& strURIPart,
204 bool showTxDetails)
206 if (!CheckWarmup(req))
207 return false;
208 std::string hashStr;
209 const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
211 uint256 hash;
212 if (!ParseHashStr(hashStr, hash))
213 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
215 CBlock block;
216 CBlockIndex* pblockindex = NULL;
218 LOCK(cs_main);
219 if (mapBlockIndex.count(hash) == 0)
220 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
222 pblockindex = mapBlockIndex[hash];
223 if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0)
224 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
226 if (!ReadBlockFromDisk(block, pblockindex))
227 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
230 CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
231 ssBlock << block;
233 switch (rf) {
234 case RF_BINARY: {
235 string binaryBlock = ssBlock.str();
236 req->WriteHeader("Content-Type", "application/octet-stream");
237 req->WriteReply(HTTP_OK, binaryBlock);
238 return true;
241 case RF_HEX: {
242 string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n";
243 req->WriteHeader("Content-Type", "text/plain");
244 req->WriteReply(HTTP_OK, strHex);
245 return true;
248 case RF_JSON: {
249 UniValue objBlock = blockToJSON(block, pblockindex, showTxDetails);
250 string strJSON = objBlock.write() + "\n";
251 req->WriteHeader("Content-Type", "application/json");
252 req->WriteReply(HTTP_OK, strJSON);
253 return true;
256 default: {
257 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
261 // not reached
262 return true; // continue to process further HTTP reqs on this cxn
265 static bool rest_block_extended(HTTPRequest* req, const std::string& strURIPart)
267 return rest_block(req, strURIPart, true);
270 static bool rest_block_notxdetails(HTTPRequest* req, const std::string& strURIPart)
272 return rest_block(req, strURIPart, false);
275 static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
277 if (!CheckWarmup(req))
278 return false;
279 std::string param;
280 const RetFormat rf = ParseDataFormat(param, strURIPart);
282 switch (rf) {
283 case RF_JSON: {
284 UniValue rpcParams(UniValue::VARR);
285 UniValue chainInfoObject = getblockchaininfo(rpcParams, false);
286 string strJSON = chainInfoObject.write() + "\n";
287 req->WriteHeader("Content-Type", "application/json");
288 req->WriteReply(HTTP_OK, strJSON);
289 return true;
291 default: {
292 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
296 // not reached
297 return true; // continue to process further HTTP reqs on this cxn
300 static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
302 if (!CheckWarmup(req))
303 return false;
304 std::string param;
305 const RetFormat rf = ParseDataFormat(param, strURIPart);
307 switch (rf) {
308 case RF_JSON: {
309 UniValue mempoolInfoObject = mempoolInfoToJSON();
311 string strJSON = mempoolInfoObject.write() + "\n";
312 req->WriteHeader("Content-Type", "application/json");
313 req->WriteReply(HTTP_OK, strJSON);
314 return true;
316 default: {
317 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
321 // not reached
322 return true; // continue to process further HTTP reqs on this cxn
325 static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPart)
327 if (!CheckWarmup(req))
328 return false;
329 std::string param;
330 const RetFormat rf = ParseDataFormat(param, strURIPart);
332 switch (rf) {
333 case RF_JSON: {
334 UniValue mempoolObject = mempoolToJSON(true);
336 string strJSON = mempoolObject.write() + "\n";
337 req->WriteHeader("Content-Type", "application/json");
338 req->WriteReply(HTTP_OK, strJSON);
339 return true;
341 default: {
342 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
346 // not reached
347 return true; // continue to process further HTTP reqs on this cxn
350 static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
352 if (!CheckWarmup(req))
353 return false;
354 std::string hashStr;
355 const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
357 uint256 hash;
358 if (!ParseHashStr(hashStr, hash))
359 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
361 CTransaction tx;
362 uint256 hashBlock = uint256();
363 if (!GetTransaction(hash, tx, hashBlock, true))
364 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
366 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
367 ssTx << tx;
369 switch (rf) {
370 case RF_BINARY: {
371 string binaryTx = ssTx.str();
372 req->WriteHeader("Content-Type", "application/octet-stream");
373 req->WriteReply(HTTP_OK, binaryTx);
374 return true;
377 case RF_HEX: {
378 string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n";
379 req->WriteHeader("Content-Type", "text/plain");
380 req->WriteReply(HTTP_OK, strHex);
381 return true;
384 case RF_JSON: {
385 UniValue objTx(UniValue::VOBJ);
386 TxToJSON(tx, hashBlock, objTx);
387 string strJSON = objTx.write() + "\n";
388 req->WriteHeader("Content-Type", "application/json");
389 req->WriteReply(HTTP_OK, strJSON);
390 return true;
393 default: {
394 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
398 // not reached
399 return true; // continue to process further HTTP reqs on this cxn
402 static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
404 if (!CheckWarmup(req))
405 return false;
406 std::string param;
407 const RetFormat rf = ParseDataFormat(param, strURIPart);
409 vector<string> uriParts;
410 if (param.length() > 1)
412 std::string strUriParams = param.substr(1);
413 boost::split(uriParts, strUriParams, boost::is_any_of("/"));
416 // throw exception in case of a empty request
417 std::string strRequestMutable = req->ReadBody();
418 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
419 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
421 bool fInputParsed = false;
422 bool fCheckMemPool = false;
423 vector<COutPoint> vOutPoints;
425 // parse/deserialize input
426 // input-format = output-format, rest/getutxos/bin requires binary input, gives binary output, ...
428 if (uriParts.size() > 0)
431 //inputs is sent over URI scheme (/rest/getutxos/checkmempool/txid1-n/txid2-n/...)
432 if (uriParts.size() > 0 && uriParts[0] == "checkmempool")
433 fCheckMemPool = true;
435 for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
437 uint256 txid;
438 int32_t nOutput;
439 std::string strTxid = uriParts[i].substr(0, uriParts[i].find("-"));
440 std::string strOutput = uriParts[i].substr(uriParts[i].find("-")+1);
442 if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid))
443 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Parse error");
445 txid.SetHex(strTxid);
446 vOutPoints.push_back(COutPoint(txid, (uint32_t)nOutput));
449 if (vOutPoints.size() > 0)
450 fInputParsed = true;
451 else
452 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
455 switch (rf) {
456 case RF_HEX: {
457 // convert hex to bin, continue then with bin part
458 std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable);
459 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
462 case RF_BINARY: {
463 try {
464 //deserialize only if user sent a request
465 if (strRequestMutable.size() > 0)
467 if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA
468 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Combination of URI scheme inputs and raw post data is not allowed");
470 CDataStream oss(SER_NETWORK, PROTOCOL_VERSION);
471 oss << strRequestMutable;
472 oss >> fCheckMemPool;
473 oss >> vOutPoints;
475 } catch (const std::ios_base::failure& e) {
476 // abort in case of unreadable binary data
477 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Parse error");
479 break;
482 case RF_JSON: {
483 if (!fInputParsed)
484 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
485 break;
487 default: {
488 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
492 // limit max outpoints
493 if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
494 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
496 // check spentness and form a bitmap (as well as a JSON capable human-readble string representation)
497 vector<unsigned char> bitmap;
498 vector<CCoin> outs;
499 std::string bitmapStringRepresentation;
500 boost::dynamic_bitset<unsigned char> hits(vOutPoints.size());
502 LOCK2(cs_main, mempool.cs);
504 CCoinsView viewDummy;
505 CCoinsViewCache view(&viewDummy);
507 CCoinsViewCache& viewChain = *pcoinsTip;
508 CCoinsViewMemPool viewMempool(&viewChain, mempool);
510 if (fCheckMemPool)
511 view.SetBackend(viewMempool); // switch cache backend to db+mempool in case user likes to query mempool
513 for (size_t i = 0; i < vOutPoints.size(); i++) {
514 CCoins coins;
515 uint256 hash = vOutPoints[i].hash;
516 if (view.GetCoins(hash, coins)) {
517 mempool.pruneSpent(hash, coins);
518 if (coins.IsAvailable(vOutPoints[i].n)) {
519 hits[i] = true;
520 // Safe to index into vout here because IsAvailable checked if it's off the end of the array, or if
521 // n is valid but points to an already spent output (IsNull).
522 CCoin coin;
523 coin.nTxVer = coins.nVersion;
524 coin.nHeight = coins.nHeight;
525 coin.out = coins.vout.at(vOutPoints[i].n);
526 assert(!coin.out.IsNull());
527 outs.push_back(coin);
531 bitmapStringRepresentation.append(hits[i] ? "1" : "0"); // form a binary string representation (human-readable for json output)
534 boost::to_block_range(hits, std::back_inserter(bitmap));
536 switch (rf) {
537 case RF_BINARY: {
538 // serialize data
539 // use exact same output as mentioned in Bip64
540 CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
541 ssGetUTXOResponse << chainActive.Height() << chainActive.Tip()->GetBlockHash() << bitmap << outs;
542 string ssGetUTXOResponseString = ssGetUTXOResponse.str();
544 req->WriteHeader("Content-Type", "application/octet-stream");
545 req->WriteReply(HTTP_OK, ssGetUTXOResponseString);
546 return true;
549 case RF_HEX: {
550 CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
551 ssGetUTXOResponse << chainActive.Height() << chainActive.Tip()->GetBlockHash() << bitmap << outs;
552 string strHex = HexStr(ssGetUTXOResponse.begin(), ssGetUTXOResponse.end()) + "\n";
554 req->WriteHeader("Content-Type", "text/plain");
555 req->WriteReply(HTTP_OK, strHex);
556 return true;
559 case RF_JSON: {
560 UniValue objGetUTXOResponse(UniValue::VOBJ);
562 // pack in some essentials
563 // use more or less the same output as mentioned in Bip64
564 objGetUTXOResponse.push_back(Pair("chainHeight", chainActive.Height()));
565 objGetUTXOResponse.push_back(Pair("chaintipHash", chainActive.Tip()->GetBlockHash().GetHex()));
566 objGetUTXOResponse.push_back(Pair("bitmap", bitmapStringRepresentation));
568 UniValue utxos(UniValue::VARR);
569 BOOST_FOREACH (const CCoin& coin, outs) {
570 UniValue utxo(UniValue::VOBJ);
571 utxo.push_back(Pair("txvers", (int32_t)coin.nTxVer));
572 utxo.push_back(Pair("height", (int32_t)coin.nHeight));
573 utxo.push_back(Pair("value", ValueFromAmount(coin.out.nValue)));
575 // include the script in a json output
576 UniValue o(UniValue::VOBJ);
577 ScriptPubKeyToJSON(coin.out.scriptPubKey, o, true);
578 utxo.push_back(Pair("scriptPubKey", o));
579 utxos.push_back(utxo);
581 objGetUTXOResponse.push_back(Pair("utxos", utxos));
583 // return json string
584 string strJSON = objGetUTXOResponse.write() + "\n";
585 req->WriteHeader("Content-Type", "application/json");
586 req->WriteReply(HTTP_OK, strJSON);
587 return true;
589 default: {
590 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
594 // not reached
595 return true; // continue to process further HTTP reqs on this cxn
598 static const struct {
599 const char* prefix;
600 bool (*handler)(HTTPRequest* req, const std::string& strReq);
601 } uri_prefixes[] = {
602 {"/rest/tx/", rest_tx},
603 {"/rest/block/notxdetails/", rest_block_notxdetails},
604 {"/rest/block/", rest_block_extended},
605 {"/rest/chaininfo", rest_chaininfo},
606 {"/rest/mempool/info", rest_mempool_info},
607 {"/rest/mempool/contents", rest_mempool_contents},
608 {"/rest/headers/", rest_headers},
609 {"/rest/getutxos", rest_getutxos},
612 bool StartREST()
614 for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++)
615 RegisterHTTPHandler(uri_prefixes[i].prefix, false, uri_prefixes[i].handler);
616 return true;
619 void InterruptREST()
623 void StopREST()
625 for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++)
626 UnregisterHTTPHandler(uri_prefixes[i].prefix, false);