[Qt] hide bump context menu action if tx already has been bumped
[bitcoinplatinum.git] / src / rest.cpp
blob7537ed45027f510020b2ffdf76f3688ecbec2a78
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 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)
54 READWRITE(nTxVer);
55 READWRITE(nHeight);
56 READWRITE(out);
60 static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string message)
62 req->WriteHeader("Content-Type", "text/plain");
63 req->WriteReply(status, message + "\r\n");
64 return false;
67 static enum RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
69 const std::string::size_type pos = strReq.rfind('.');
70 if (pos == std::string::npos)
72 param = strReq;
73 return rf_names[0].rf;
76 param = strReq.substr(0, pos);
77 const std::string suff(strReq, pos + 1);
79 for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
80 if (suff == rf_names[i].name)
81 return rf_names[i].rf;
83 /* If no suffix is found, return original string. */
84 param = strReq;
85 return rf_names[0].rf;
88 static std::string AvailableDataFormatsString()
90 std::string formats = "";
91 for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
92 if (strlen(rf_names[i].name) > 0) {
93 formats.append(".");
94 formats.append(rf_names[i].name);
95 formats.append(", ");
98 if (formats.length() > 0)
99 return formats.substr(0, formats.length() - 2);
101 return formats;
104 static bool ParseHashStr(const std::string& strReq, uint256& v)
106 if (!IsHex(strReq) || (strReq.size() != 64))
107 return false;
109 v.SetHex(strReq);
110 return true;
113 static bool CheckWarmup(HTTPRequest* req)
115 std::string statusmessage;
116 if (RPCIsInWarmup(&statusmessage))
117 return RESTERR(req, HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: " + statusmessage);
118 return true;
121 static bool rest_headers(HTTPRequest* req,
122 const std::string& strURIPart)
124 if (!CheckWarmup(req))
125 return false;
126 std::string param;
127 const RetFormat rf = ParseDataFormat(param, strURIPart);
128 std::vector<std::string> path;
129 boost::split(path, param, boost::is_any_of("/"));
131 if (path.size() != 2)
132 return RESTERR(req, HTTP_BAD_REQUEST, "No header count specified. Use /rest/headers/<count>/<hash>.<ext>.");
134 long count = strtol(path[0].c_str(), NULL, 10);
135 if (count < 1 || count > 2000)
136 return RESTERR(req, HTTP_BAD_REQUEST, "Header count out of range: " + path[0]);
138 std::string hashStr = path[1];
139 uint256 hash;
140 if (!ParseHashStr(hashStr, hash))
141 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
143 std::vector<const CBlockIndex *> headers;
144 headers.reserve(count);
146 LOCK(cs_main);
147 BlockMap::const_iterator it = mapBlockIndex.find(hash);
148 const CBlockIndex *pindex = (it != mapBlockIndex.end()) ? it->second : NULL;
149 while (pindex != NULL && chainActive.Contains(pindex)) {
150 headers.push_back(pindex);
151 if (headers.size() == (unsigned long)count)
152 break;
153 pindex = chainActive.Next(pindex);
157 CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
158 BOOST_FOREACH(const CBlockIndex *pindex, headers) {
159 ssHeader << pindex->GetBlockHeader();
162 switch (rf) {
163 case RF_BINARY: {
164 std::string binaryHeader = ssHeader.str();
165 req->WriteHeader("Content-Type", "application/octet-stream");
166 req->WriteReply(HTTP_OK, binaryHeader);
167 return true;
170 case RF_HEX: {
171 std::string strHex = HexStr(ssHeader.begin(), ssHeader.end()) + "\n";
172 req->WriteHeader("Content-Type", "text/plain");
173 req->WriteReply(HTTP_OK, strHex);
174 return true;
176 case RF_JSON: {
177 UniValue jsonHeaders(UniValue::VARR);
178 BOOST_FOREACH(const CBlockIndex *pindex, headers) {
179 jsonHeaders.push_back(blockheaderToJSON(pindex));
181 std::string strJSON = jsonHeaders.write() + "\n";
182 req->WriteHeader("Content-Type", "application/json");
183 req->WriteReply(HTTP_OK, strJSON);
184 return true;
186 default: {
187 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: .bin, .hex)");
191 // not reached
192 return true; // continue to process further HTTP reqs on this cxn
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 = NULL;
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() + ")");
254 // not reached
255 return true; // continue to process further HTTP reqs on this cxn
258 static bool rest_block_extended(HTTPRequest* req, const std::string& strURIPart)
260 return rest_block(req, strURIPart, true);
263 static bool rest_block_notxdetails(HTTPRequest* req, const std::string& strURIPart)
265 return rest_block(req, strURIPart, false);
268 // A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
269 UniValue getblockchaininfo(const JSONRPCRequest& request);
271 static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
273 if (!CheckWarmup(req))
274 return false;
275 std::string param;
276 const RetFormat rf = ParseDataFormat(param, strURIPart);
278 switch (rf) {
279 case RF_JSON: {
280 JSONRPCRequest jsonRequest;
281 jsonRequest.params = UniValue(UniValue::VARR);
282 UniValue chainInfoObject = getblockchaininfo(jsonRequest);
283 std::string strJSON = chainInfoObject.write() + "\n";
284 req->WriteHeader("Content-Type", "application/json");
285 req->WriteReply(HTTP_OK, strJSON);
286 return true;
288 default: {
289 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
293 // not reached
294 return true; // continue to process further HTTP reqs on this cxn
297 static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
299 if (!CheckWarmup(req))
300 return false;
301 std::string param;
302 const RetFormat rf = ParseDataFormat(param, strURIPart);
304 switch (rf) {
305 case RF_JSON: {
306 UniValue mempoolInfoObject = mempoolInfoToJSON();
308 std::string strJSON = mempoolInfoObject.write() + "\n";
309 req->WriteHeader("Content-Type", "application/json");
310 req->WriteReply(HTTP_OK, strJSON);
311 return true;
313 default: {
314 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
318 // not reached
319 return true; // continue to process further HTTP reqs on this cxn
322 static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPart)
324 if (!CheckWarmup(req))
325 return false;
326 std::string param;
327 const RetFormat rf = ParseDataFormat(param, strURIPart);
329 switch (rf) {
330 case RF_JSON: {
331 UniValue mempoolObject = mempoolToJSON(true);
333 std::string strJSON = mempoolObject.write() + "\n";
334 req->WriteHeader("Content-Type", "application/json");
335 req->WriteReply(HTTP_OK, strJSON);
336 return true;
338 default: {
339 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
343 // not reached
344 return true; // continue to process further HTTP reqs on this cxn
347 static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
349 if (!CheckWarmup(req))
350 return false;
351 std::string hashStr;
352 const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
354 uint256 hash;
355 if (!ParseHashStr(hashStr, hash))
356 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
358 CTransactionRef tx;
359 uint256 hashBlock = uint256();
360 if (!GetTransaction(hash, tx, Params().GetConsensus(), hashBlock, true))
361 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
363 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
364 ssTx << tx;
366 switch (rf) {
367 case RF_BINARY: {
368 std::string binaryTx = ssTx.str();
369 req->WriteHeader("Content-Type", "application/octet-stream");
370 req->WriteReply(HTTP_OK, binaryTx);
371 return true;
374 case RF_HEX: {
375 std::string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n";
376 req->WriteHeader("Content-Type", "text/plain");
377 req->WriteReply(HTTP_OK, strHex);
378 return true;
381 case RF_JSON: {
382 UniValue objTx(UniValue::VOBJ);
383 TxToUniv(*tx, hashBlock, objTx);
384 std::string strJSON = objTx.write() + "\n";
385 req->WriteHeader("Content-Type", "application/json");
386 req->WriteReply(HTTP_OK, strJSON);
387 return true;
390 default: {
391 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
395 // not reached
396 return true; // continue to process further HTTP reqs on this cxn
399 static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
401 if (!CheckWarmup(req))
402 return false;
403 std::string param;
404 const RetFormat rf = ParseDataFormat(param, strURIPart);
406 std::vector<std::string> uriParts;
407 if (param.length() > 1)
409 std::string strUriParams = param.substr(1);
410 boost::split(uriParts, strUriParams, boost::is_any_of("/"));
413 // throw exception in case of a empty request
414 std::string strRequestMutable = req->ReadBody();
415 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
416 return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
418 bool fInputParsed = false;
419 bool fCheckMemPool = false;
420 std::vector<COutPoint> vOutPoints;
422 // parse/deserialize input
423 // input-format = output-format, rest/getutxos/bin requires binary input, gives binary output, ...
425 if (uriParts.size() > 0)
428 //inputs is sent over URI scheme (/rest/getutxos/checkmempool/txid1-n/txid2-n/...)
429 if (uriParts.size() > 0 && uriParts[0] == "checkmempool")
430 fCheckMemPool = true;
432 for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
434 uint256 txid;
435 int32_t nOutput;
436 std::string strTxid = uriParts[i].substr(0, uriParts[i].find("-"));
437 std::string strOutput = uriParts[i].substr(uriParts[i].find("-")+1);
439 if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid))
440 return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
442 txid.SetHex(strTxid);
443 vOutPoints.push_back(COutPoint(txid, (uint32_t)nOutput));
446 if (vOutPoints.size() > 0)
447 fInputParsed = true;
448 else
449 return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
452 switch (rf) {
453 case RF_HEX: {
454 // convert hex to bin, continue then with bin part
455 std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable);
456 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
459 case RF_BINARY: {
460 try {
461 //deserialize only if user sent a request
462 if (strRequestMutable.size() > 0)
464 if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA
465 return RESTERR(req, HTTP_BAD_REQUEST, "Combination of URI scheme inputs and raw post data is not allowed");
467 CDataStream oss(SER_NETWORK, PROTOCOL_VERSION);
468 oss << strRequestMutable;
469 oss >> fCheckMemPool;
470 oss >> vOutPoints;
472 } catch (const std::ios_base::failure& e) {
473 // abort in case of unreadable binary data
474 return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
476 break;
479 case RF_JSON: {
480 if (!fInputParsed)
481 return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
482 break;
484 default: {
485 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
489 // limit max outpoints
490 if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
491 return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
493 // check spentness and form a bitmap (as well as a JSON capable human-readable string representation)
494 std::vector<unsigned char> bitmap;
495 std::vector<CCoin> outs;
496 std::string bitmapStringRepresentation;
497 std::vector<bool> hits;
498 bitmap.resize((vOutPoints.size() + 7) / 8);
500 LOCK2(cs_main, mempool.cs);
502 CCoinsView viewDummy;
503 CCoinsViewCache view(&viewDummy);
505 CCoinsViewCache& viewChain = *pcoinsTip;
506 CCoinsViewMemPool viewMempool(&viewChain, mempool);
508 if (fCheckMemPool)
509 view.SetBackend(viewMempool); // switch cache backend to db+mempool in case user likes to query mempool
511 for (size_t i = 0; i < vOutPoints.size(); i++) {
512 CCoins coins;
513 uint256 hash = vOutPoints[i].hash;
514 bool hit = false;
515 if (view.GetCoins(hash, coins)) {
516 mempool.pruneSpent(hash, coins);
517 if (coins.IsAvailable(vOutPoints[i].n)) {
518 hit = true;
519 // Safe to index into vout here because IsAvailable checked if it's off the end of the array, or if
520 // n is valid but points to an already spent output (IsNull).
521 CCoin coin;
522 coin.nTxVer = coins.nVersion;
523 coin.nHeight = coins.nHeight;
524 coin.out = coins.vout.at(vOutPoints[i].n);
525 assert(!coin.out.IsNull());
526 outs.push_back(coin);
530 hits.push_back(hit);
531 bitmapStringRepresentation.append(hit ? "1" : "0"); // form a binary string representation (human-readable for json output)
532 bitmap[i / 8] |= ((uint8_t)hit) << (i % 8);
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 std::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 std::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 ScriptPubKeyToUniv(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 std::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);