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.
7 #include "chainparams.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"
17 #include "txmempool.h"
18 #include "utilstrencodings.h"
21 #include <boost/algorithm/string.hpp>
25 static const size_t MAX_GETUTXOS_OUTPOINTS
= 15; //allow a max of 15 outpoints to be queried at once
45 uint32_t nTxVer
; // Don't call this nVersion, that name has a special meaning inside IMPLEMENT_SERIALIZE
49 ADD_SERIALIZE_METHODS
;
51 template <typename Stream
, typename Operation
>
52 inline void SerializationOp(Stream
& s
, Operation ser_action
)
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");
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
)
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. */
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) {
94 formats
.append(rf_names
[i
].name
);
98 if (formats
.length() > 0)
99 return formats
.substr(0, formats
.length() - 2);
104 static bool ParseHashStr(const std::string
& strReq
, uint256
& v
)
106 if (!IsHex(strReq
) || (strReq
.size() != 64))
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
);
121 static bool rest_headers(HTTPRequest
* req
,
122 const std::string
& strURIPart
)
124 if (!CheckWarmup(req
))
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];
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
);
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
)
153 pindex
= chainActive
.Next(pindex
);
157 CDataStream
ssHeader(SER_NETWORK
, PROTOCOL_VERSION
);
158 BOOST_FOREACH(const CBlockIndex
*pindex
, headers
) {
159 ssHeader
<< pindex
->GetBlockHeader();
164 std::string binaryHeader
= ssHeader
.str();
165 req
->WriteHeader("Content-Type", "application/octet-stream");
166 req
->WriteReply(HTTP_OK
, binaryHeader
);
171 std::string strHex
= HexStr(ssHeader
.begin(), ssHeader
.end()) + "\n";
172 req
->WriteHeader("Content-Type", "text/plain");
173 req
->WriteReply(HTTP_OK
, strHex
);
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
);
187 return RESTERR(req
, HTTP_NOT_FOUND
, "output format not found (available: .bin, .hex)");
192 return true; // continue to process further HTTP reqs on this cxn
195 static bool rest_block(HTTPRequest
* req
,
196 const std::string
& strURIPart
,
199 if (!CheckWarmup(req
))
202 const RetFormat rf
= ParseDataFormat(hashStr
, strURIPart
);
205 if (!ParseHashStr(hashStr
, hash
))
206 return RESTERR(req
, HTTP_BAD_REQUEST
, "Invalid hash: " + hashStr
);
209 CBlockIndex
* pblockindex
= NULL
;
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());
228 std::string binaryBlock
= ssBlock
.str();
229 req
->WriteHeader("Content-Type", "application/octet-stream");
230 req
->WriteReply(HTTP_OK
, binaryBlock
);
235 std::string strHex
= HexStr(ssBlock
.begin(), ssBlock
.end()) + "\n";
236 req
->WriteHeader("Content-Type", "text/plain");
237 req
->WriteReply(HTTP_OK
, strHex
);
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
);
250 return RESTERR(req
, HTTP_NOT_FOUND
, "output format not found (available: " + AvailableDataFormatsString() + ")");
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
))
276 const RetFormat rf
= ParseDataFormat(param
, strURIPart
);
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
);
289 return RESTERR(req
, HTTP_NOT_FOUND
, "output format not found (available: json)");
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
))
302 const RetFormat rf
= ParseDataFormat(param
, strURIPart
);
306 UniValue mempoolInfoObject
= mempoolInfoToJSON();
308 std::string strJSON
= mempoolInfoObject
.write() + "\n";
309 req
->WriteHeader("Content-Type", "application/json");
310 req
->WriteReply(HTTP_OK
, strJSON
);
314 return RESTERR(req
, HTTP_NOT_FOUND
, "output format not found (available: json)");
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
))
327 const RetFormat rf
= ParseDataFormat(param
, strURIPart
);
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
);
339 return RESTERR(req
, HTTP_NOT_FOUND
, "output format not found (available: json)");
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
))
352 const RetFormat rf
= ParseDataFormat(hashStr
, strURIPart
);
355 if (!ParseHashStr(hashStr
, hash
))
356 return RESTERR(req
, HTTP_BAD_REQUEST
, "Invalid hash: " + hashStr
);
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());
368 std::string binaryTx
= ssTx
.str();
369 req
->WriteHeader("Content-Type", "application/octet-stream");
370 req
->WriteReply(HTTP_OK
, binaryTx
);
375 std::string strHex
= HexStr(ssTx
.begin(), ssTx
.end()) + "\n";
376 req
->WriteHeader("Content-Type", "text/plain");
377 req
->WriteReply(HTTP_OK
, strHex
);
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
);
391 return RESTERR(req
, HTTP_NOT_FOUND
, "output format not found (available: " + AvailableDataFormatsString() + ")");
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
))
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
++)
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)
449 return RESTERR(req
, HTTP_BAD_REQUEST
, "Error: empty request");
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());
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
;
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");
481 return RESTERR(req
, HTTP_BAD_REQUEST
, "Error: empty request");
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
);
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
++) {
513 uint256 hash
= vOutPoints
[i
].hash
;
515 if (view
.GetCoins(hash
, coins
)) {
516 mempool
.pruneSpent(hash
, coins
);
517 if (coins
.IsAvailable(vOutPoints
[i
].n
)) {
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).
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
);
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);
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
);
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
);
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
);
590 return RESTERR(req
, HTTP_NOT_FOUND
, "output format not found (available: " + AvailableDataFormatsString() + ")");
595 return true; // continue to process further HTTP reqs on this cxn
598 static const struct {
600 bool (*handler
)(HTTPRequest
* req
, const std::string
& strReq
);
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
},
614 for (unsigned int i
= 0; i
< ARRAYLEN(uri_prefixes
); i
++)
615 RegisterHTTPHandler(uri_prefixes
[i
].prefix
, false, uri_prefixes
[i
].handler
);
625 for (unsigned int i
= 0; i
< ARRAYLEN(uri_prefixes
); i
++)
626 UnregisterHTTPHandler(uri_prefixes
[i
].prefix
, false);