22 #include <boost/algorithm/string.hpp> 26 static const size_t MAX_GETUTXOS_OUTPOINTS = 15;
52 explicit CCoin(
Coin&& in) : nHeight(in.nHeight), out(
std::move(in.out)) {}
54 template <
typename Stream,
typename Operation>
57 uint32_t nTxVerDummy = 0;
71 static enum RetFormat ParseDataFormat(std::string& param,
const std::string& strReq)
73 const std::string::size_type pos = strReq.rfind(
'.');
74 if (pos == std::string::npos)
77 return rf_names[0].rf;
80 param = strReq.substr(0, pos);
81 const std::string suff(strReq, pos + 1);
83 for (
unsigned int i = 0; i <
ARRAYLEN(rf_names); i++)
84 if (suff == rf_names[i].name)
85 return rf_names[i].rf;
89 return rf_names[0].rf;
92 static std::string AvailableDataFormatsString()
94 std::string formats =
"";
95 for (
unsigned int i = 0; i <
ARRAYLEN(rf_names); i++)
96 if (strlen(rf_names[i].name) > 0) {
98 formats.append(rf_names[i].name);
102 if (formats.length() > 0)
103 return formats.substr(0, formats.length() - 2);
110 if (!
IsHex(strReq) || (strReq.size() != 64))
119 std::string statusmessage;
126 const std::string& strURIPart)
128 if (!CheckWarmup(req))
131 const RetFormat rf = ParseDataFormat(param, strURIPart);
132 std::vector<std::string> path;
133 boost::split(path, param, boost::is_any_of(
"/"));
135 if (path.size() != 2)
136 return RESTERR(req,
HTTP_BAD_REQUEST,
"No header count specified. Use /rest/headers/<count>/<hash>.<ext>.");
138 long count = strtol(path[0].c_str(),
nullptr, 10);
139 if (count < 1 || count > 2000)
140 return RESTERR(req,
HTTP_BAD_REQUEST,
"Header count out of range: " + path[0]);
142 std::string hashStr = path[1];
147 std::vector<const CBlockIndex *> headers;
148 headers.reserve(count);
154 headers.push_back(pindex);
155 if (headers.size() == (
unsigned long)count)
163 ssHeader << pindex->GetBlockHeader();
168 std::string binaryHeader = ssHeader.
str();
169 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
175 std::string strHex =
HexStr(ssHeader.
begin(), ssHeader.
end()) +
"\n";
185 std::string strJSON = jsonHeaders.
write() +
"\n";
186 req->
WriteHeader(
"Content-Type",
"application/json");
191 return RESTERR(req,
HTTP_NOT_FOUND,
"output format not found (available: .bin, .hex)");
197 const std::string& strURIPart,
200 if (!CheckWarmup(req))
203 const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
218 return RESTERR(req,
HTTP_NOT_FOUND, hashStr +
" not available (pruned data)");
229 std::string binaryBlock = ssBlock.
str();
230 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
236 std::string strHex =
HexStr(ssBlock.
begin(), ssBlock.
end()) +
"\n";
244 std::string strJSON = objBlock.
write() +
"\n";
245 req->
WriteHeader(
"Content-Type",
"application/json");
251 return RESTERR(req,
HTTP_NOT_FOUND,
"output format not found (available: " + AvailableDataFormatsString() +
")");
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);
269 static bool rest_chaininfo(
HTTPRequest* req,
const std::string& strURIPart)
271 if (!CheckWarmup(req))
274 const RetFormat rf = ParseDataFormat(param, strURIPart);
281 std::string strJSON = chainInfoObject.
write() +
"\n";
282 req->
WriteHeader(
"Content-Type",
"application/json");
287 return RESTERR(req,
HTTP_NOT_FOUND,
"output format not found (available: json)");
292 static bool rest_mempool_info(
HTTPRequest* req,
const std::string& strURIPart)
294 if (!CheckWarmup(req))
297 const RetFormat rf = ParseDataFormat(param, strURIPart);
303 std::string strJSON = mempoolInfoObject.
write() +
"\n";
304 req->
WriteHeader(
"Content-Type",
"application/json");
309 return RESTERR(req,
HTTP_NOT_FOUND,
"output format not found (available: json)");
314 static bool rest_mempool_contents(
HTTPRequest* req,
const std::string& strURIPart)
316 if (!CheckWarmup(req))
319 const RetFormat rf = ParseDataFormat(param, strURIPart);
325 std::string strJSON = mempoolObject.
write() +
"\n";
326 req->
WriteHeader(
"Content-Type",
"application/json");
331 return RESTERR(req,
HTTP_NOT_FOUND,
"output format not found (available: json)");
336 static bool rest_tx(
HTTPRequest* req,
const std::string& strURIPart)
338 if (!CheckWarmup(req))
341 const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
357 std::string binaryTx = ssTx.
str();
358 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
373 std::string strJSON = objTx.
write() +
"\n";
374 req->
WriteHeader(
"Content-Type",
"application/json");
380 return RESTERR(req,
HTTP_NOT_FOUND,
"output format not found (available: " + AvailableDataFormatsString() +
")");
385 static bool rest_getutxos(
HTTPRequest* req,
const std::string& strURIPart)
387 if (!CheckWarmup(req))
390 const RetFormat rf = ParseDataFormat(param, strURIPart);
392 std::vector<std::string> uriParts;
393 if (param.length() > 1)
395 std::string strUriParams = param.substr(1);
396 boost::split(uriParts, strUriParams, boost::is_any_of(
"/"));
400 std::string strRequestMutable = req->
ReadBody();
401 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
404 bool fInputParsed =
false;
405 bool fCheckMemPool =
false;
406 std::vector<COutPoint> vOutPoints;
411 if (uriParts.size() > 0)
414 if (uriParts[0] ==
"checkmempool") fCheckMemPool =
true;
416 for (
size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
420 std::string strTxid = uriParts[i].substr(0, uriParts[i].find(
"-"));
421 std::string strOutput = uriParts[i].substr(uriParts[i].find(
"-")+1);
427 vOutPoints.push_back(
COutPoint(txid, (uint32_t)nOutput));
430 if (vOutPoints.size() > 0)
439 std::vector<unsigned char> strRequestV =
ParseHex(strRequestMutable);
440 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
446 if (strRequestMutable.size() > 0)
449 return RESTERR(req,
HTTP_BAD_REQUEST,
"Combination of URI scheme inputs and raw post data is not allowed");
452 oss << strRequestMutable;
453 oss >> fCheckMemPool;
456 }
catch (
const std::ios_base::failure& e) {
469 return RESTERR(req,
HTTP_NOT_FOUND,
"output format not found (available: " + AvailableDataFormatsString() +
")");
474 if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
475 return RESTERR(req,
HTTP_BAD_REQUEST,
strprintf(
"Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
478 std::vector<unsigned char> bitmap;
479 std::vector<CCoin> outs;
480 std::string bitmapStringRepresentation;
481 std::vector<bool> hits;
482 bitmap.resize((vOutPoints.size() + 7) / 8);
495 for (
size_t i = 0; i < vOutPoints.size(); i++) {
500 outs.emplace_back(std::move(coin));
504 bitmapStringRepresentation.append(hit ?
"1" :
"0");
505 bitmap[i / 8] |= ((uint8_t)hit) << (i % 8);
515 std::string ssGetUTXOResponseString = ssGetUTXOResponse.
str();
517 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
525 std::string strHex =
HexStr(ssGetUTXOResponse.
begin(), ssGetUTXOResponse.
end()) +
"\n";
539 objGetUTXOResponse.
push_back(Pair(
"bitmap", bitmapStringRepresentation));
542 for (
const CCoin& coin : outs) {
544 utxo.
push_back(Pair(
"height", (int32_t)coin.nHeight));
553 objGetUTXOResponse.
push_back(Pair(
"utxos", utxos));
556 std::string strJSON = objGetUTXOResponse.
write() +
"\n";
557 req->
WriteHeader(
"Content-Type",
"application/json");
562 return RESTERR(req,
HTTP_NOT_FOUND,
"output format not found (available: " + AvailableDataFormatsString() +
")");
567 static const struct {
571 {
"/rest/tx/", rest_tx},
572 {
"/rest/block/notxdetails/", rest_block_notxdetails},
573 {
"/rest/block/", rest_block_extended},
574 {
"/rest/chaininfo", rest_chaininfo},
575 {
"/rest/mempool/info", rest_mempool_info},
576 {
"/rest/mempool/contents", rest_mempool_contents},
577 {
"/rest/headers/", rest_headers},
578 {
"/rest/getutxos", rest_getutxos},
583 for (
unsigned int i = 0; i <
ARRAYLEN(uri_prefixes); i++)
594 for (
unsigned int i = 0; i <
ARRAYLEN(uri_prefixes); i++)
bool(* handler)(HTTPRequest *req, const std::string &strReq)
uint32_t nStatus
Verification status of this block. See enum BlockStatus.
void SetBackend(CCoinsView &viewIn)
bool fHavePruned
Pruning-related variables and constants.
bool isSpent(const COutPoint &outpoint)
UniValue mempoolInfoToJSON()
Mempool information to JSON.
int Height() const
Return the maximal height in the chain.
CCriticalSection cs_main
Global state.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
HTTPStatusCode
HTTP status codes.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
UniValue blockheaderToJSON(const CBlockIndex *blockindex)
Block header to JSON.
Double ended buffer combining vector and stream-like interfaces.
UniValue ValueFromAmount(const CAmount &amount, const int8_t units)
std::shared_ptr< const CTransaction > CTransactionRef
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
uint256 GetBlockHash() const
CCoinsViewCache * pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
bool push_back(const UniValue &val)
Abstract view on the open txout dataset.
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
UniValue getblockchaininfo(const JSONRPCRequest &request)
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
bool IsHex(const std::string &str)
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
An output of a transaction.
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
An outpoint - a combination of a transaction hash and an index n into its vout.
UniValue mempoolToJSON(bool fVerbose)
Mempool to JSON.
bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus::Params &consensusParams, uint256 &hashBlock, bool fAllowSlow)
Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock...
uint256 ParseHashStr(const std::string &, const std::string &strName)
const_iterator end() const
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
const_iterator begin() const
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
void StopREST()
Stop HTTP REST subsystem.
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
int RPCSerializationFlags()
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
std::string GetHex() const
bool RPCIsInWarmup(std::string *outStatus)
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
std::string ReadBody()
Read request body.
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, UniValue &entry, bool include_hex=true, int serialize_flags=0)
void SerializationOp(Stream &s, Operation ser_action)
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
void InterruptREST()
Interrupt RPC REST subsystem.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
full block available in blk*.dat
UniValue blockToJSON(const CBlock &block, const CBlockIndex *blockindex, bool txDetails)
Block description to JSON.
void SetHex(const char *psz)
CCoinsView that brings transactions from a memorypool into view.
unsigned int nTx
Number of transactions in this block.
bool StartREST()
Start HTTP REST subsystem.
std::vector< unsigned char > ParseHex(const char *psz)