19 #include <boost/algorithm/string.hpp> 20 #include <boost/dynamic_bitset.hpp> 52 template <
typename Stream,
typename Operation>
53 inline void SerializationOp(Stream& s, Operation ser_action,
int nType,
int nVersion)
77 const std::string::size_type pos = strReq.rfind(
'.');
78 if (pos == std::string::npos)
84 param = strReq.substr(0, pos);
85 const std::string suff(strReq, pos + 1);
103 formats.append(
", ");
106 if (formats.length() > 0)
107 return formats.substr(0, formats.length() - 2);
114 if (!
IsHex(strReq) || (strReq.size() != 64))
123 std::string statusmessage;
130 const std::string& strURIPart)
137 boost::split(path, param, boost::is_any_of(
"/"));
139 if (path.size() != 2)
142 long count = strtol(path[0].c_str(), NULL, 10);
143 if (count < 1 || count > 2000)
146 string hashStr = path[1];
151 std::vector<const CBlockIndex *> headers;
152 headers.reserve(
count);
158 headers.push_back(pindex);
159 if (headers.size() == (
unsigned long)
count)
166 BOOST_FOREACH(
const CBlockIndex *pindex, headers) {
172 string binaryHeader = ssHeader.
str();
173 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
186 BOOST_FOREACH(
const CBlockIndex *pindex, headers) {
189 string strJSON = jsonHeaders.
write() +
"\n";
190 req->
WriteHeader(
"Content-Type",
"application/json");
204 const std::string& strURIPart,
236 string binaryBlock = ssBlock.
str();
237 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
251 string strJSON = objBlock.
write() +
"\n";
252 req->
WriteHeader(
"Content-Type",
"application/json");
287 string strJSON = chainInfoObject.
write() +
"\n";
288 req->
WriteHeader(
"Content-Type",
"application/json");
312 string strJSON = mempoolInfoObject.
write() +
"\n";
313 req->
WriteHeader(
"Content-Type",
"application/json");
337 string strJSON = mempoolObject.
write() +
"\n";
338 req->
WriteHeader(
"Content-Type",
"application/json");
372 string binaryTx = ssTx.
str();
373 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
388 string strJSON = objTx.
write() +
"\n";
389 req->
WriteHeader(
"Content-Type",
"application/json");
410 vector<string> uriParts;
411 if (param.length() > 1)
413 std::string strUriParams = param.substr(1);
414 boost::split(uriParts, strUriParams, boost::is_any_of(
"/"));
418 std::string strRequestMutable = req->
ReadBody();
419 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
422 bool fInputParsed =
false;
423 bool fCheckMemPool =
false;
424 vector<COutPoint> vOutPoints;
429 if (uriParts.size() > 0)
433 if (uriParts.size() > 0 && uriParts[0] ==
"checkmempool")
434 fCheckMemPool =
true;
436 for (
size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
440 std::string strTxid = uriParts[i].substr(0, uriParts[i].find(
"-"));
441 std::string strOutput = uriParts[i].substr(uriParts[i].find(
"-")+1);
447 vOutPoints.push_back(
COutPoint(txid, (uint32_t)nOutput));
450 if (vOutPoints.size() > 0)
459 std::vector<unsigned char> strRequestV =
ParseHex(strRequestMutable);
460 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
466 if (strRequestMutable.size() > 0)
472 oss << strRequestMutable;
473 oss >> fCheckMemPool;
476 }
catch (
const std::ios_base::failure& e) {
498 vector<unsigned char> bitmap;
500 std::string bitmapStringRepresentation;
501 boost::dynamic_bitset<unsigned char> hits(vOutPoints.size());
514 for (
size_t i = 0; i < vOutPoints.size(); i++) {
516 uint256 hash = vOutPoints[i].hash;
526 coin.
out = coins.
vout.at(vOutPoints[i].
n);
528 outs.push_back(coin);
532 bitmapStringRepresentation.append(hits[i] ?
"1" :
"0");
535 boost::to_block_range(hits, std::back_inserter(bitmap));
543 string ssGetUTXOResponseString = ssGetUTXOResponse.
str();
545 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
553 string strHex =
HexStr(ssGetUTXOResponse.
begin(), ssGetUTXOResponse.
end()) +
"\n";
567 objGetUTXOResponse.
push_back(
Pair(
"bitmap", bitmapStringRepresentation));
570 BOOST_FOREACH (
const CCoin& coin, outs) {
585 string strJSON = objGetUTXOResponse.
write() +
"\n";
586 req->
WriteHeader(
"Content-Type",
"application/json");
599 static const struct {
bool(* handler)(HTTPRequest *req, const std::string &strReq)
static bool CheckWarmup(HTTPRequest *req)
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
static bool rest_mempool_info(HTTPRequest *req, const std::string &strURIPart)
static bool rest_chaininfo(HTTPRequest *req, const std::string &strURIPart)
CBlockHeader GetBlockHeader() const
static string AvailableDataFormatsString()
bool IsAvailable(unsigned int nPos) const
check whether a particular output is still available
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
HTTPStatusCode
HTTP status codes.
static bool rest_tx(HTTPRequest *req, const std::string &strURIPart)
const_iterator end() const
bool IsHex(const string &str)
bool push_back(const UniValue &val)
UniValue mempoolToJSON(bool fVerbose=false)
UniValue blockToJSON(const CBlock &block, const CBlockIndex *blockindex, bool txDetails=false)
UniValue ValueFromAmount(const CAmount &amount)
static bool rest_headers(HTTPRequest *req, const std::string &strURIPart)
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
bool Contains(const CBlockIndex *pindex) const
static bool rest_block(HTTPRequest *req, const std::string &strURIPart, bool showTxDetails)
CCoinsViewCache * pcoinsTip
std::vector< CTxOut > vout
unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are d...
static bool rest_getutxos(HTTPRequest *req, const std::string &strURIPart)
bool ParseInt32(const std::string &str, int32_t *out)
static bool rest_mempool_contents(HTTPRequest *req, const std::string &strURIPart)
const_iterator begin() const
void WriteReply(int nStatus, const std::string &strReply="")
void TxToJSON(const CTransaction &tx, const uint256 hashBlock, UniValue &entry)
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
vector< unsigned char > ParseHex(const char *psz)
void SetBackend(CCoinsView &viewIn)
UniValue blockheaderToJSON(const CBlockIndex *blockindex)
static std::pair< std::string, UniValue > Pair(const char *cKey, const char *cVal)
static const size_t MAX_GETUTXOS_OUTPOINTS
CBlockIndex * Next(const CBlockIndex *pindex) const
const CChainParams & Params()
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
static const int PROTOCOL_VERSION
static const struct @20 rf_names[]
unsigned int nStatus
Verification status of this block. See enum BlockStatus.
uint256 GetBlockHash() const
std::string GetHex() const
bool RPCIsInWarmup(std::string *outStatus)
static const struct @21 uri_prefixes[]
void pruneSpent(const uint256 &hash, CCoins &coins)
CBlockIndex * Tip() const
void WriteHeader(const std::string &hdr, const std::string &value)
static bool RESTERR(HTTPRequest *req, enum HTTPStatusCode status, string message)
UniValue mempoolInfoToJSON()
void ScriptPubKeyToJSON(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
bool GetTransaction(const uint256 &hash, CTransaction &txOut, const Consensus::Params &consensusParams, uint256 &hashBlock, bool fAllowSlow)
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, const Consensus::Params &consensusParams)
void SetHex(const char *psz)
static bool ParseHashStr(const string &strReq, uint256 &v)
static bool rest_block_notxdetails(HTTPRequest *req, const std::string &strURIPart)
static enum RetFormat ParseDataFormat(std::string ¶m, const std::string &strReq)
UniValue getblockchaininfo(const UniValue ¶ms, bool fHelp)
int nHeight
at which height this transaction was included in the active block chain
static bool rest_block_extended(HTTPRequest *req, const std::string &strURIPart)