63 uint32_t nTxVerDummy = 0;
64 READWRITE(nTxVerDummy, obj.nHeight, obj.out);
84 auto node_context = util::AnyPtr<NodeContext>(
context);
88 "Internal bug detected: Node context not found!\n" 89 "You may report this issue here: %s\n",
105 auto node_context = util::AnyPtr<NodeContext>(
context);
106 if (!node_context || !node_context->mempool) {
110 return node_context->mempool.get();
122 auto node_context = util::AnyPtr<NodeContext>(
context);
123 if (!node_context || !node_context->chainman) {
126 "Internal bug detected: Chainman disabled or instance not found!\n" 127 "You may report this issue here: %s\n",
131 return node_context->chainman.get();
138 param = strReq.substr(0, strReq.rfind(
'?'));
139 const std::string::size_type pos_format{param.rfind(
'.')};
142 if (pos_format == std::string::npos) {
147 const std::string suffix(param, pos_format + 1);
148 for (
const auto& rf_name :
rf_names) {
149 if (suffix == rf_name.name) {
150 param.erase(pos_format);
162 for (
const auto& rf_name :
rf_names) {
163 if (strlen(rf_name.name) > 0) {
165 formats.append(rf_name.name);
166 formats.append(
", ");
170 if (formats.length() > 0)
171 return formats.substr(0, formats.length() - 2);
178 std::string statusmessage;
186 const std::string& strURIPart)
192 std::vector<std::string> path =
SplitString(param,
'/');
194 std::string raw_count;
196 if (path.size() == 2) {
200 }
else if (path.size() == 1) {
205 }
catch (
const std::runtime_error& e) {
209 return RESTERR(req,
HTTP_BAD_REQUEST,
"Invalid URI format. Expected /rest/headers/<hash>.<ext>?count=<count>");
212 const auto parsed_count{ToIntegral<size_t>(raw_count)};
222 std::vector<const CBlockIndex*> headers;
223 headers.reserve(*parsed_count);
226 if (!maybe_chainman)
return false;
230 tip = active_chain.
Tip();
232 while (pindex !=
nullptr && active_chain.
Contains(pindex)) {
233 headers.push_back(pindex);
234 if (headers.size() == *parsed_count) {
237 pindex = active_chain.
Next(pindex);
245 ssHeader << pindex->GetBlockHeader();
248 std::string binaryHeader = ssHeader.
str();
249 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
257 ssHeader << pindex->GetBlockHeader();
260 std::string strHex =
HexStr(ssHeader) +
"\n";
270 std::string strJSON = jsonHeaders.
write() +
"\n";
271 req->
WriteHeader(
"Content-Type",
"application/json");
283 const std::string& strURIPart,
299 if (!maybe_chainman)
return false;
309 if (chainman.
m_blockman.IsBlockPruned(pblockindex))
320 std::string binaryBlock = ssBlock.
str();
321 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
329 std::string strHex =
HexStr(ssBlock) +
"\n";
337 std::string strJSON = objBlock.
write() +
"\n";
338 req->
WriteHeader(
"Content-Type",
"application/json");
366 std::vector<std::string> uri_parts =
SplitString(param,
'/');
367 std::string raw_count;
368 std::string raw_blockhash;
369 if (uri_parts.size() == 3) {
371 raw_blockhash = uri_parts[2];
372 raw_count = uri_parts[1];
373 }
else if (uri_parts.size() == 2) {
375 raw_blockhash = uri_parts[1];
378 }
catch (
const std::runtime_error& e) {
382 return RESTERR(req,
HTTP_BAD_REQUEST,
"Invalid URI format. Expected /rest/blockfilterheaders/<filtertype>/<blockhash>.<ext>?count=<count>");
385 const auto parsed_count{ToIntegral<size_t>(raw_count)};
405 std::vector<const CBlockIndex*> headers;
406 headers.reserve(*parsed_count);
409 if (!maybe_chainman)
return false;
414 while (pindex !=
nullptr && active_chain.
Contains(pindex)) {
415 headers.push_back(pindex);
416 if (headers.size() == *parsed_count)
418 pindex = active_chain.
Next(pindex);
422 bool index_ready = index->BlockUntilSyncedToCurrentChain();
424 std::vector<uint256> filter_headers;
425 filter_headers.reserve(*parsed_count);
429 std::string errmsg =
"Filter not found.";
432 errmsg +=
" Block filters are still in the process of being indexed.";
434 errmsg +=
" This error is unexpected and indicates index corruption.";
439 filter_headers.push_back(filter_header);
445 for (
const uint256& header : filter_headers) {
449 std::string binaryHeader = ssHeader.str();
450 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
456 for (
const uint256& header : filter_headers) {
460 std::string strHex =
HexStr(ssHeader) +
"\n";
467 for (
const uint256& header : filter_headers) {
471 std::string strJSON = jsonHeaders.
write() +
"\n";
472 req->
WriteHeader(
"Content-Type",
"application/json");
490 std::vector<std::string> uri_parts =
SplitString(param,
'/');
491 if (uri_parts.size() != 2) {
492 return RESTERR(req,
HTTP_BAD_REQUEST,
"Invalid URI format. Expected /rest/blockfilter/<filtertype>/<blockhash>");
511 bool block_was_connected;
514 if (!maybe_chainman)
return false;
524 bool index_ready = index->BlockUntilSyncedToCurrentChain();
528 std::string errmsg =
"Filter not found.";
530 if (!block_was_connected) {
531 errmsg +=
" Block was not connected to active chain.";
532 }
else if (!index_ready) {
533 errmsg +=
" Block filters are still in the process of being indexed.";
535 errmsg +=
" This error is unexpected and indicates index corruption.";
546 std::string binaryResp = ssResp.str();
547 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
555 std::string strHex =
HexStr(ssResp) +
"\n";
563 std::string strJSON =
ret.write() +
"\n";
564 req->
WriteHeader(
"Content-Type",
"application/json");
590 std::string strJSON = chainInfoObject.
write() +
"\n";
591 req->
WriteHeader(
"Content-Type",
"application/json");
608 if (param !=
"contents" && param !=
"info") {
613 if (!mempool)
return false;
617 std::string str_json;
618 if (param ==
"contents") {
624 req->
WriteHeader(
"Content-Type",
"application/json");
646 g_txindex->BlockUntilSyncedToCurrentChain();
650 if (!
node)
return false;
662 std::string binaryTx = ssTx.
str();
663 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
672 std::string strHex =
HexStr(ssTx) +
"\n";
681 std::string strJSON = objTx.
write() +
"\n";
682 req->
WriteHeader(
"Content-Type",
"application/json");
700 std::vector<std::string> uriParts;
701 if (param.length() > 1)
703 std::string strUriParams = param.substr(1);
708 std::string strRequestMutable = req->
ReadBody();
709 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
712 bool fInputParsed =
false;
713 bool fCheckMemPool =
false;
714 std::vector<COutPoint> vOutPoints;
719 if (uriParts.size() > 0)
722 if (uriParts[0] ==
"checkmempool") fCheckMemPool =
true;
724 for (
size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
728 std::string strTxid = uriParts[i].substr(0, uriParts[i].find(
'-'));
729 std::string strOutput = uriParts[i].substr(uriParts[i].find(
'-')+1);
735 vOutPoints.push_back(
COutPoint(txid, (uint32_t)nOutput));
738 if (vOutPoints.size() > 0)
747 std::vector<unsigned char> strRequestV =
ParseHex(strRequestMutable);
748 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
755 if (strRequestMutable.size() > 0)
761 oss << strRequestMutable;
762 oss >> fCheckMemPool;
765 }
catch (
const std::ios_base::failure&) {
787 std::vector<unsigned char> bitmap;
788 std::vector<CCoin> outs;
789 std::string bitmapStringRepresentation;
790 std::vector<bool> hits;
791 bitmap.resize((vOutPoints.size() + 7) / 8);
793 if (!maybe_chainman)
return false;
799 for (
const COutPoint& vOutPoint : vOutPoints) {
801 bool hit = (!mempool || !mempool->isSpent(vOutPoint)) && view.GetCoin(vOutPoint, coin);
803 if (hit) outs.emplace_back(std::move(coin));
811 if (!mempool)
return false;
816 process_utxos(viewMempool, mempool);
822 for (
size_t i = 0; i < hits.size(); ++i) {
823 const bool hit = hits[i];
824 bitmapStringRepresentation.append(hit ?
"1" :
"0");
825 bitmap[i / 8] |= ((uint8_t)hit) << (i % 8);
834 ssGetUTXOResponse << active_height << active_hash << bitmap << outs;
835 std::string ssGetUTXOResponseString = ssGetUTXOResponse.
str();
837 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
844 ssGetUTXOResponse << active_height << active_hash << bitmap << outs;
845 std::string strHex =
HexStr(ssGetUTXOResponse) +
"\n";
857 objGetUTXOResponse.
pushKV(
"chainHeight", active_height);
858 objGetUTXOResponse.
pushKV(
"chaintipHash", active_hash.GetHex());
859 objGetUTXOResponse.
pushKV(
"bitmap", bitmapStringRepresentation);
862 for (
const CCoin& coin : outs) {
864 utxo.
pushKV(
"height", (int32_t)coin.nHeight);
870 utxo.
pushKV(
"scriptPubKey", o);
873 objGetUTXOResponse.
pushKV(
"utxos", utxos);
876 std::string strJSON = objGetUTXOResponse.
write() +
"\n";
877 req->
WriteHeader(
"Content-Type",
"application/json");
888 const std::string& str_uri_part)
891 std::string height_str;
894 int32_t blockheight = -1;
895 if (!
ParseInt32(height_str, &blockheight) || blockheight < 0) {
902 if (!maybe_chainman)
return false;
906 if (blockheight > active_chain.
Height()) {
909 pblockindex = active_chain[blockheight];
915 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
925 req->
WriteHeader(
"Content-Type",
"application/json");
937 static const struct {
std::shared_ptr< const CTransaction > CTransactionRef
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
static ChainstateManager * GetChainman(const std::any &context, HTTPRequest *req)
Get the node context chainstatemanager.
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
void push_back(UniValue val)
static bool CheckWarmup(HTTPRequest *req)
The same as previous option with information about prevouts if available.
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
UniValue blockToJSON(BlockManager &blockman, const CBlock &block, const CBlockIndex *tip, const CBlockIndex *blockindex, TxVerbosity verbosity)
Block description to JSON.
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
TxVerbosity
Verbose level for block's transaction.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
static bool rest_getutxos(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
An in-memory indexed chain of blocks.
static std::string AvailableDataFormatsString()
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
static bool rest_tx(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
RESTResponseFormat ParseDataFormat(std::string ¶m, const std::string &strReq)
Parse a URI to get the data format and URI without data format and query string.
int Height() const
Return the maximal height in the chain.
bool IsHex(std::string_view str)
HTTPStatusCode
HTTP status codes.
UniValue blockheaderToJSON(const CBlockIndex *tip, const CBlockIndex *blockindex)
Block header to JSON.
static const struct @10 uri_prefixes[]
std::optional< std::string > GetQueryParameter(const std::string &key) const
Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not...
Double ended buffer combining vector and stream-like interfaces.
static bool rest_headers(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
CTransactionRef GetTransaction(const CBlockIndex *const block_index, const CTxMemPool *const mempool, const uint256 &hash, const Consensus::Params &consensusParams, uint256 &hashBlock)
Return transaction with a given hash.
std::vector< std::string > SplitString(std::string_view str, char sep)
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
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_mempool(const std::any &context, HTTPRequest *req, const std::string &str_uri_part)
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
NodeContext struct containing references to chain state and connection state.
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
static bool rest_block_extended(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
static bool RESTERR(HTTPRequest *req, enum HTTPStatusCode status, std::string message)
static bool rest_block(const std::any &context, HTTPRequest *req, const std::string &strURIPart, TxVerbosity tx_verbosity)
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
Abstract view on the open txout dataset.
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache)
Get a single filter header by block.
Complete block filter struct as defined in BIP 157.
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...
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
An output of a transaction.
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
An outpoint - a combination of a transaction hash and an index n into its vout.
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
static const struct @9 rf_names[]
bool ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
static bool rest_blockhash_by_height(const std::any &context, HTTPRequest *req, const std::string &str_uri_part)
static bool rest_block_notxdetails(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
bool IsValid(enum BlockStatus nUpTo=BLOCK_VALID_TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
static CTxMemPool * GetMemPool(const std::any &context, HTTPRequest *req)
Get the node context mempool.
static const size_t MAX_GETUTXOS_OUTPOINTS
const CChainParams & GetParams() const
RPCHelpMan getblockchaininfo()
static bool rest_filter_header(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
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.
Only TXID for each block's transaction.
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.
void pushKV(std::string key, UniValue val)
int RPCSerializationFlags()
static const int PROTOCOL_VERSION
network protocol versioning
static NodeContext * GetNodeContext(const std::any &context, HTTPRequest *req)
Get the node context.
static bool rest_block_filter(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
std::string GetHex() const
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
UniValue MempoolToJSON(const CTxMemPool &pool, bool verbose, bool include_mempool_sequence)
Mempool to JSON.
UniValue ValueFromAmount(const CAmount amount)
bool RPCIsInWarmup(std::string *outStatus)
std::string ReadBody()
Read request body.
void ScriptToUniv(const CScript &script, UniValue &out, bool include_hex=true, bool include_address=false)
UniValue MempoolInfoToJSON(const CTxMemPool &pool)
Mempool information to JSON.
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
const std::vector< unsigned char > & GetEncodedFilter() const LIFETIMEBOUND
const Consensus::Params & GetConsensus() const
Chainstate & ActiveChainstate() const
The most-work chain.
void InterruptREST()
Interrupt RPC REST subsystem.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
UniValue HandleRequest(const JSONRPCRequest &request) const
void SetHex(const char *psz)
CCoinsView that brings transactions from a mempool into view.
void TxToUniv(const CTransaction &tx, const uint256 &block_hash, UniValue &entry, bool include_hex=true, int serialize_flags=0, const CTxUndo *txundo=nullptr, TxVerbosity verbosity=TxVerbosity::SHOW_DETAILS)
#define PACKAGE_BUGREPORT
static constexpr unsigned int MAX_REST_HEADERS_RESULTS
static bool rest_chaininfo(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
void StartREST(const std::any &context)
Start HTTP REST subsystem.