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 "primitives/block.h"
7 #include "primitives/transaction.h"
9 #include "httpserver.h"
10 #include "rpcserver.h"
13 #include "txmempool.h"
14 #include "utilstrencodings.h"
17 #include <boost/algorithm/string.hpp>
18 #include <boost/dynamic_bitset.hpp>
24 static const size_t MAX_GETUTXOS_OUTPOINTS = 15; //allow a max of 15 outpoints to be queried at once
44 uint32_t nTxVer; // Don't call this nVersion, that name has a special meaning inside IMPLEMENT_SERIALIZE
48 ADD_SERIALIZE_METHODS;
50 template <typename Stream, typename Operation>
51 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
59 extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
60 extern UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false);
61 extern UniValue mempoolInfoToJSON();
62 extern UniValue mempoolToJSON(bool fVerbose = false);
63 extern void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
64 extern UniValue blockheaderToJSON(const CBlockIndex* blockindex);
66 static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, string message)
68 req->WriteHeader("Content-Type", "text/plain");
69 req->WriteReply(status, message + "\r\n");
73 static enum RetFormat ParseDataFormat(vector<string>& params, const string& strReq)
75 boost::split(params, strReq, boost::is_any_of("."));
76 if (params.size() > 1) {
77 for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
78 if (params[1] == rf_names[i].name)
79 return rf_names[i].rf;
82 return rf_names[0].rf;
85 static string AvailableDataFormatsString()
88 for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
89 if (strlen(rf_names[i].name) > 0) {
91 formats.append(rf_names[i].name);
95 if (formats.length() > 0)
96 return formats.substr(0, formats.length() - 2);
101 static bool ParseHashStr(const string& strReq, uint256& v)
103 if (!IsHex(strReq) || (strReq.size() != 64))
110 static bool CheckWarmup(HTTPRequest* req)
112 std::string statusmessage;
113 if (RPCIsInWarmup(&statusmessage))
114 return RESTERR(req, HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: " + statusmessage);
118 static bool rest_headers(HTTPRequest* req,
119 const std::string& strURIPart)
121 if (!CheckWarmup(req))
123 vector<string> params;
124 const RetFormat rf = ParseDataFormat(params, strURIPart);
126 boost::split(path, params[0], boost::is_any_of("/"));
128 if (path.size() != 2)
129 return RESTERR(req, HTTP_BAD_REQUEST, "No header count specified. Use /rest/headers/<count>/<hash>.<ext>.");
131 long count = strtol(path[0].c_str(), NULL, 10);
132 if (count < 1 || count > 2000)
133 return RESTERR(req, HTTP_BAD_REQUEST, "Header count out of range: " + path[0]);
135 string hashStr = path[1];
137 if (!ParseHashStr(hashStr, hash))
138 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
140 std::vector<const CBlockIndex *> headers;
141 headers.reserve(count);
144 BlockMap::const_iterator it = mapBlockIndex.find(hash);
145 const CBlockIndex *pindex = (it != mapBlockIndex.end()) ? it->second : NULL;
146 while (pindex != NULL && chainActive.Contains(pindex)) {
147 headers.push_back(pindex);
148 if (headers.size() == (unsigned long)count)
150 pindex = chainActive.Next(pindex);
154 CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
155 BOOST_FOREACH(const CBlockIndex *pindex, headers) {
156 ssHeader << pindex->GetBlockHeader();
161 string binaryHeader = ssHeader.str();
162 req->WriteHeader("Content-Type", "application/octet-stream");
163 req->WriteReply(HTTP_OK, binaryHeader);
168 string strHex = HexStr(ssHeader.begin(), ssHeader.end()) + "\n";
169 req->WriteHeader("Content-Type", "text/plain");
170 req->WriteReply(HTTP_OK, strHex);
174 UniValue jsonHeaders(UniValue::VARR);
175 BOOST_FOREACH(const CBlockIndex *pindex, headers) {
176 jsonHeaders.push_back(blockheaderToJSON(pindex));
178 string strJSON = jsonHeaders.write() + "\n";
179 req->WriteHeader("Content-Type", "application/json");
180 req->WriteReply(HTTP_OK, strJSON);
184 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: .bin, .hex)");
189 return true; // continue to process further HTTP reqs on this cxn
192 static bool rest_block(HTTPRequest* req,
193 const std::string& strURIPart,
196 if (!CheckWarmup(req))
198 vector<string> params;
199 const RetFormat rf = ParseDataFormat(params, strURIPart);
201 string hashStr = params[0];
203 if (!ParseHashStr(hashStr, hash))
204 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
207 CBlockIndex* pblockindex = NULL;
210 if (mapBlockIndex.count(hash) == 0)
211 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
213 pblockindex = mapBlockIndex[hash];
214 if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0)
215 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
217 if (!ReadBlockFromDisk(block, pblockindex,1))
218 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
221 CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
226 string binaryBlock = ssBlock.str();
227 req->WriteHeader("Content-Type", "application/octet-stream");
228 req->WriteReply(HTTP_OK, binaryBlock);
233 string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n";
234 req->WriteHeader("Content-Type", "text/plain");
235 req->WriteReply(HTTP_OK, strHex);
240 UniValue objBlock = blockToJSON(block, pblockindex, showTxDetails);
241 string strJSON = objBlock.write() + "\n";
242 req->WriteHeader("Content-Type", "application/json");
243 req->WriteReply(HTTP_OK, strJSON);
248 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
253 return true; // continue to process further HTTP reqs on this cxn
256 static bool rest_block_extended(HTTPRequest* req, const std::string& strURIPart)
258 return rest_block(req, strURIPart, true);
261 static bool rest_block_notxdetails(HTTPRequest* req, const std::string& strURIPart)
263 return rest_block(req, strURIPart, false);
266 static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
268 if (!CheckWarmup(req))
270 vector<string> params;
271 const RetFormat rf = ParseDataFormat(params, strURIPart);
275 UniValue rpcParams(UniValue::VARR);
276 UniValue chainInfoObject = getblockchaininfo(rpcParams, false);
277 string strJSON = chainInfoObject.write() + "\n";
278 req->WriteHeader("Content-Type", "application/json");
279 req->WriteReply(HTTP_OK, strJSON);
283 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
288 return true; // continue to process further HTTP reqs on this cxn
291 static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
293 if (!CheckWarmup(req))
295 vector<string> params;
296 const RetFormat rf = ParseDataFormat(params, strURIPart);
300 UniValue mempoolInfoObject = mempoolInfoToJSON();
302 string strJSON = mempoolInfoObject.write() + "\n";
303 req->WriteHeader("Content-Type", "application/json");
304 req->WriteReply(HTTP_OK, strJSON);
308 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
313 return true; // continue to process further HTTP reqs on this cxn
316 static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPart)
318 if (!CheckWarmup(req))
320 vector<string> params;
321 const RetFormat rf = ParseDataFormat(params, strURIPart);
325 UniValue mempoolObject = mempoolToJSON(true);
327 string strJSON = mempoolObject.write() + "\n";
328 req->WriteHeader("Content-Type", "application/json");
329 req->WriteReply(HTTP_OK, strJSON);
333 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
338 return true; // continue to process further HTTP reqs on this cxn
341 static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
343 if (!CheckWarmup(req))
345 vector<string> params;
346 const RetFormat rf = ParseDataFormat(params, strURIPart);
348 string hashStr = params[0];
350 if (!ParseHashStr(hashStr, hash))
351 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
354 uint256 hashBlock = uint256();
355 if (!GetTransaction(hash, tx, hashBlock, true))
356 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
358 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
363 string binaryTx = ssTx.str();
364 req->WriteHeader("Content-Type", "application/octet-stream");
365 req->WriteReply(HTTP_OK, binaryTx);
370 string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n";
371 req->WriteHeader("Content-Type", "text/plain");
372 req->WriteReply(HTTP_OK, strHex);
377 UniValue objTx(UniValue::VOBJ);
378 TxToJSON(tx, hashBlock, objTx);
379 string strJSON = objTx.write() + "\n";
380 req->WriteHeader("Content-Type", "application/json");
381 req->WriteReply(HTTP_OK, strJSON);
386 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
391 return true; // continue to process further HTTP reqs on this cxn
394 static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
396 if (!CheckWarmup(req))
398 vector<string> params;
399 enum RetFormat rf = ParseDataFormat(params, strURIPart);
401 vector<string> uriParts;
402 if (params.size() > 0 && params[0].length() > 1)
404 std::string strUriParams = params[0].substr(1);
405 boost::split(uriParts, strUriParams, boost::is_any_of("/"));
408 // throw exception in case of an empty request
409 std::string strRequestMutable = req->ReadBody();
410 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
411 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
413 bool fInputParsed = false;
414 bool fCheckMemPool = false;
415 vector<COutPoint> vOutPoints;
417 // parse/deserialize input
418 // input-format = output-format, rest/getutxos/bin requires binary input, gives binary output, ...
420 if (uriParts.size() > 0)
423 //inputs is sent over URI scheme (/rest/getutxos/checkmempool/txid1-n/txid2-n/...)
424 if (uriParts.size() > 0 && uriParts[0] == "checkmempool")
425 fCheckMemPool = true;
427 for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
431 std::string strTxid = uriParts[i].substr(0, uriParts[i].find("-"));
432 std::string strOutput = uriParts[i].substr(uriParts[i].find("-")+1);
434 if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid))
435 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Parse error");
437 txid.SetHex(strTxid);
438 vOutPoints.push_back(COutPoint(txid, (uint32_t)nOutput));
441 if (vOutPoints.size() > 0)
444 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
449 // convert hex to bin, continue then with bin part
450 std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable);
451 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
456 //deserialize only if user sent a request
457 if (strRequestMutable.size() > 0)
459 if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA
460 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Combination of URI scheme inputs and raw post data is not allowed");
462 CDataStream oss(SER_NETWORK, PROTOCOL_VERSION);
463 oss << strRequestMutable;
464 oss >> fCheckMemPool;
467 } catch (const std::ios_base::failure& e) {
468 // abort in case of unreadable binary data
469 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Parse error");
476 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
480 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
484 // limit max outpoints
485 if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
486 return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
488 // check spentness and form a bitmap (as well as a JSON capable human-readable string representation)
489 vector<unsigned char> bitmap;
491 std::string bitmapStringRepresentation;
492 boost::dynamic_bitset<unsigned char> hits(vOutPoints.size());
494 LOCK2(cs_main, mempool.cs);
496 CCoinsView viewDummy;
497 CCoinsViewCache view(&viewDummy);
499 CCoinsViewCache& viewChain = *pcoinsTip;
500 CCoinsViewMemPool viewMempool(&viewChain, mempool);
503 view.SetBackend(viewMempool); // switch cache backend to db+mempool in case user likes to query mempool
505 for (size_t i = 0; i < vOutPoints.size(); i++) {
507 uint256 hash = vOutPoints[i].hash;
508 if (view.GetCoins(hash, coins)) {
509 mempool.pruneSpent(hash, coins);
510 if (coins.IsAvailable(vOutPoints[i].n)) {
512 // Safe to index into vout here because IsAvailable checked if it's off the end of the array, or if
513 // n is valid but points to an already spent output (IsNull).
515 coin.nTxVer = coins.nVersion;
516 coin.nHeight = coins.nHeight;
517 coin.out = coins.vout.at(vOutPoints[i].n);
518 assert(!coin.out.IsNull());
519 outs.push_back(coin);
523 bitmapStringRepresentation.append(hits[i] ? "1" : "0"); // form a binary string representation (human-readable for json output)
526 boost::to_block_range(hits, std::back_inserter(bitmap));
531 // use exact same output as mentioned in Bip64
532 CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
533 ssGetUTXOResponse << chainActive.Height() << chainActive.LastTip()->GetBlockHash() << bitmap << outs;
534 string ssGetUTXOResponseString = ssGetUTXOResponse.str();
536 req->WriteHeader("Content-Type", "application/octet-stream");
537 req->WriteReply(HTTP_OK, ssGetUTXOResponseString);
542 CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
543 ssGetUTXOResponse << chainActive.Height() << chainActive.LastTip()->GetBlockHash() << bitmap << outs;
544 string strHex = HexStr(ssGetUTXOResponse.begin(), ssGetUTXOResponse.end()) + "\n";
546 req->WriteHeader("Content-Type", "text/plain");
547 req->WriteReply(HTTP_OK, strHex);
552 UniValue objGetUTXOResponse(UniValue::VOBJ);
554 // pack in some essentials
555 // use more or less the same output as mentioned in Bip64
556 objGetUTXOResponse.push_back(Pair("chainHeight", chainActive.Height()));
557 objGetUTXOResponse.push_back(Pair("chaintipHash", chainActive.LastTip()->GetBlockHash().GetHex()));
558 objGetUTXOResponse.push_back(Pair("bitmap", bitmapStringRepresentation));
560 UniValue utxos(UniValue::VARR);
561 BOOST_FOREACH (const CCoin& coin, outs) {
562 UniValue utxo(UniValue::VOBJ);
563 utxo.push_back(Pair("txvers", (int32_t)coin.nTxVer));
564 utxo.push_back(Pair("height", (int32_t)coin.nHeight));
565 utxo.push_back(Pair("value", ValueFromAmount(coin.out.nValue)));
567 // include the script in a json output
568 UniValue o(UniValue::VOBJ);
569 ScriptPubKeyToJSON(coin.out.scriptPubKey, o, true);
570 utxo.push_back(Pair("scriptPubKey", o));
571 utxos.push_back(utxo);
573 objGetUTXOResponse.push_back(Pair("utxos", utxos));
575 // return json string
576 string strJSON = objGetUTXOResponse.write() + "\n";
577 req->WriteHeader("Content-Type", "application/json");
578 req->WriteReply(HTTP_OK, strJSON);
582 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
587 return true; // continue to process further HTTP reqs on this cxn
590 static const struct {
592 bool (*handler)(HTTPRequest* req, const std::string& strReq);
594 {"/rest/tx/", rest_tx},
595 {"/rest/block/notxdetails/", rest_block_notxdetails},
596 {"/rest/block/", rest_block_extended},
597 {"/rest/chaininfo", rest_chaininfo},
598 {"/rest/mempool/info", rest_mempool_info},
599 {"/rest/mempool/contents", rest_mempool_contents},
600 {"/rest/headers/", rest_headers},
601 {"/rest/getutxos", rest_getutxos},
606 for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++)
607 RegisterHTTPHandler(uri_prefixes[i].prefix, false, uri_prefixes[i].handler);
617 for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++)
618 UnregisterHTTPHandler(uri_prefixes[i].prefix, false);