26 #include <boost/algorithm/string.hpp> 54 explicit CCoin(
Coin&& in) : nHeight(in.nHeight), out(
std::move(in.out)) {}
58 uint32_t nTxVerDummy = 0;
59 READWRITE(nTxVerDummy, obj.nHeight, obj.out);
83 "Internal bug detected: Node context not found!\n" 84 "You may report this issue here: %s\n",
110 const std::string::size_type pos = strReq.rfind(
'.');
111 if (pos == std::string::npos)
117 param = strReq.substr(0, pos);
118 const std::string suff(strReq, pos + 1);
136 formats.append(
", ");
139 if (formats.length() > 0)
140 return formats.substr(0, formats.length() - 2);
147 std::string statusmessage;
155 const std::string& strURIPart)
161 std::vector<std::string> path;
162 boost::split(path, param, boost::is_any_of(
"/"));
164 if (path.size() != 2)
167 long count = strtol(path[0].c_str(),
nullptr, 10);
168 if (count < 1 || count > 2000)
171 std::string hashStr = path[1];
177 std::vector<const CBlockIndex *> headers;
178 headers.reserve(count);
183 while (pindex !=
nullptr && ::
ChainActive().Contains(pindex)) {
184 headers.push_back(pindex);
185 if (headers.size() == (
unsigned long)count)
195 ssHeader << pindex->GetBlockHeader();
198 std::string binaryHeader = ssHeader.
str();
199 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
207 ssHeader << pindex->GetBlockHeader();
210 std::string strHex =
HexStr(ssHeader) +
"\n";
220 std::string strJSON = jsonHeaders.
write() +
"\n";
221 req->
WriteHeader(
"Content-Type",
"application/json");
232 const std::string& strURIPart,
266 std::string binaryBlock = ssBlock.
str();
267 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
275 std::string strHex =
HexStr(ssBlock) +
"\n";
283 std::string strJSON = objBlock.
write() +
"\n";
284 req->
WriteHeader(
"Content-Type",
"application/json");
320 std::string strJSON = chainInfoObject.
write() +
"\n";
321 req->
WriteHeader(
"Content-Type",
"application/json");
336 if (!mempool)
return false;
344 std::string strJSON = mempoolInfoObject.
write() +
"\n";
345 req->
WriteHeader(
"Content-Type",
"application/json");
359 if (!mempool)
return false;
367 std::string strJSON = mempoolObject.
write() +
"\n";
368 req->
WriteHeader(
"Content-Type",
"application/json");
390 g_txindex->BlockUntilSyncedToCurrentChain();
394 if (!node)
return false;
406 std::string binaryTx = ssTx.
str();
407 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
416 std::string strHex =
HexStr(ssTx) +
"\n";
425 std::string strJSON = objTx.
write() +
"\n";
426 req->
WriteHeader(
"Content-Type",
"application/json");
444 std::vector<std::string> uriParts;
445 if (param.length() > 1)
447 std::string strUriParams = param.substr(1);
448 boost::split(uriParts, strUriParams, boost::is_any_of(
"/"));
452 std::string strRequestMutable = req->
ReadBody();
453 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
456 bool fInputParsed =
false;
457 bool fCheckMemPool =
false;
458 std::vector<COutPoint> vOutPoints;
463 if (uriParts.size() > 0)
466 if (uriParts[0] ==
"checkmempool") fCheckMemPool =
true;
468 for (
size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
472 std::string strTxid = uriParts[i].substr(0, uriParts[i].find(
'-'));
473 std::string strOutput = uriParts[i].substr(uriParts[i].find(
'-')+1);
479 vOutPoints.push_back(
COutPoint(txid, (uint32_t)nOutput));
482 if (vOutPoints.size() > 0)
491 std::vector<unsigned char> strRequestV =
ParseHex(strRequestMutable);
492 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
498 if (strRequestMutable.size() > 0)
504 oss << strRequestMutable;
505 oss >> fCheckMemPool;
508 }
catch (
const std::ios_base::failure&) {
530 std::vector<unsigned char> bitmap;
531 std::vector<CCoin> outs;
532 std::string bitmapStringRepresentation;
533 std::vector<bool> hits;
534 bitmap.resize((vOutPoints.size() + 7) / 8);
536 auto process_utxos = [&vOutPoints, &outs, &hits](
const CCoinsView& view,
const CTxMemPool& mempool) {
537 for (
const COutPoint& vOutPoint : vOutPoints) {
539 bool hit = !mempool.isSpent(vOutPoint) && view.GetCoin(vOutPoint, coin);
541 if (hit) outs.emplace_back(std::move(coin));
547 if (!mempool)
return false;
552 process_utxos(viewMempool, *mempool);
558 for (
size_t i = 0; i < hits.size(); ++i) {
559 const bool hit = hits[i];
560 bitmapStringRepresentation.append(hit ?
"1" :
"0");
561 bitmap[i / 8] |= ((uint8_t)hit) << (i % 8);
571 std::string ssGetUTXOResponseString = ssGetUTXOResponse.
str();
573 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
581 std::string strHex =
HexStr(ssGetUTXOResponse) +
"\n";
594 objGetUTXOResponse.
pushKV(
"chaintipHash", ::
ChainActive().Tip()->GetBlockHash().GetHex());
595 objGetUTXOResponse.
pushKV(
"bitmap", bitmapStringRepresentation);
598 for (
const CCoin& coin : outs) {
600 utxo.
pushKV(
"height", (int32_t)coin.nHeight);
606 utxo.
pushKV(
"scriptPubKey", o);
609 objGetUTXOResponse.
pushKV(
"utxos", utxos);
612 std::string strJSON = objGetUTXOResponse.
write() +
"\n";
613 req->
WriteHeader(
"Content-Type",
"application/json");
624 const std::string& str_uri_part)
627 std::string height_str;
630 int32_t blockheight = -1;
631 if (!
ParseInt32(height_str, &blockheight) || blockheight < 0) {
647 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
657 req->
WriteHeader(
"Content-Type",
"application/json");
669 static const struct {
std::shared_ptr< const CTransaction > CTransactionRef
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
bool(* handler)(const util::Ref &context, HTTPRequest *req, const std::string &strReq)
static bool rest_block_extended(const util::Ref &context, HTTPRequest *req, const std::string &strURIPart)
static bool CheckWarmup(HTTPRequest *req)
static NodeContext * GetNodeContext(const util::Ref &context, HTTPRequest *req)
Get the node context.
static RetFormat ParseDataFormat(std::string ¶m, const std::string &strReq)
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, UniValue &entry, bool include_hex=true, int serialize_flags=0, const CTxUndo *txundo=nullptr)
CChain & ChainActive()
Please prefer the identical ChainstateManager::ActiveChain.
std::unique_ptr< CTxMemPool > mempool
static bool rest_block_notxdetails(const util::Ref &context, HTTPRequest *req, const std::string &strURIPart)
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
static std::string AvailableDataFormatsString()
UniValue HandleRequest(const JSONRPCRequest &request)
#define ARRAYLEN(array)
Utilities for converting data from/to strings.
std::vector< unsigned char > ParseHex(const char *psz)
int Height() const
Return the maximal height in the chain.
UniValue ValueFromAmount(const CAmount &amount)
HTTPStatusCode
HTTP status codes.
UniValue blockheaderToJSON(const CBlockIndex *tip, const CBlockIndex *blockindex)
Block header to JSON.
static const struct @10 uri_prefixes[]
Double ended buffer combining vector and stream-like interfaces.
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
SERIALIZE_METHODS(CCoin, obj)
uint256 GetBlockHash() const
static bool rest_block(HTTPRequest *req, const std::string &strURIPart, bool showTxDetails)
bool push_back(const UniValue &val)
static bool RESTERR(HTTPRequest *req, enum HTTPStatusCode status, std::string message)
NodeContext struct containing references to chain state and connection state.
Abstract view on the open txout dataset.
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
static bool rest_blockhash_by_height(const util::Ref &context, HTTPRequest *req, const std::string &str_uri_part)
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.
static bool rest_mempool_contents(const util::Ref &context, HTTPRequest *req, const std::string &strURIPart)
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)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
static bool rest_mempool_info(const util::Ref &context, HTTPRequest *req, const std::string &strURIPart)
bool pushKV(const std::string &key, const UniValue &val)
An output of a transaction.
static bool rest_getutxos(const util::Ref &context, HTTPRequest *req, const std::string &strURIPart)
An outpoint - a combination of a transaction hash and an index n into its vout.
static CTxMemPool * GetMemPool(const util::Ref &context, HTTPRequest *req)
Get the node context mempool.
static const struct @9 rf_names[]
UniValue blockToJSON(const CBlock &block, const CBlockIndex *tip, const CBlockIndex *blockindex, bool txDetails)
Block description to JSON.
Type-safe dynamic reference.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
static const size_t MAX_GETUTXOS_OUTPOINTS
CBlockIndex * LookupBlockIndex(const uint256 &hash)
RPCHelpMan getblockchaininfo()
CChainState & ChainstateActive()
Please prefer the identical ChainstateManager::ActiveChainstate.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
static bool rest_headers(const util::Ref &context, HTTPRequest *req, const std::string &strURIPart)
UniValue MempoolInfoToJSON(const CTxMemPool &pool)
Mempool information to JSON.
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.
bool IsBlockPruned(const CBlockIndex *pblockindex)
Check whether the block associated with this index entry is pruned or not.
int RPCSerializationFlags()
static const int PROTOCOL_VERSION
network protocol versioning
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)
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
std::string ReadBody()
Read request body.
UniValue MempoolToJSON(const CTxMemPool &pool, bool verbose, bool include_mempool_sequence)
Mempool to JSON.
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
const Consensus::Params & GetConsensus() const
void InterruptREST()
Interrupt RPC REST subsystem.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
void SetHex(const char *psz)
static bool rest_chaininfo(const util::Ref &context, HTTPRequest *req, const std::string &strURIPart)
CCoinsView that brings transactions from a mempool into view.
CTransactionRef GetTransaction(const CBlockIndex *const block_index, const CTxMemPool *const mempool, const uint256 &hash, const Consensus::Params &consensusParams, uint256 &hashBlock)
Return transaction from the block at block_index.
static bool rest_tx(const util::Ref &context, HTTPRequest *req, const std::string &strURIPart)
#define PACKAGE_BUGREPORT
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
void StartREST(const util::Ref &context)
Start HTTP REST subsystem.