 |
Bitcoin Core
21.99.0
P2P Digital Currency
|
Go to the documentation of this file.
28 #include <boost/algorithm/string.hpp>
60 uint32_t nTxVerDummy = 0;
61 READWRITE(nTxVerDummy, obj.nHeight, obj.out);
81 auto node_context = util::AnyPtr<NodeContext>(context);
85 "Internal bug detected: Node context not found!\n"
86 "You may report this issue here: %s\n",
102 auto node_context = util::AnyPtr<NodeContext>(context);
103 if (!node_context || !node_context->mempool) {
107 return node_context->mempool.get();
112 const std::string::size_type pos = strReq.rfind(
'.');
113 if (pos == std::string::npos)
119 param = strReq.substr(0, pos);
120 const std::string suff(strReq, pos + 1);
122 for (
const auto& rf_name :
rf_names) {
123 if (suff == rf_name.name)
135 for (
const auto& rf_name :
rf_names) {
136 if (strlen(rf_name.name) > 0) {
138 formats.append(rf_name.name);
139 formats.append(
", ");
143 if (formats.length() > 0)
144 return formats.substr(0, formats.length() - 2);
151 std::string statusmessage;
159 const std::string& strURIPart)
165 std::vector<std::string> path;
166 boost::split(path, param, boost::is_any_of(
"/"));
168 if (path.size() != 2)
171 long count = strtol(path[0].c_str(),
nullptr, 10);
172 if (count < 1 || count > 2000)
175 std::string hashStr = path[1];
181 std::vector<const CBlockIndex *> headers;
182 headers.reserve(
count);
187 tip = active_chain.
Tip();
188 const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
189 while (pindex !=
nullptr && active_chain.
Contains(pindex)) {
190 headers.push_back(pindex);
191 if (headers.size() == (
unsigned long)
count)
193 pindex = active_chain.
Next(pindex);
201 ssHeader << pindex->GetBlockHeader();
204 std::string binaryHeader = ssHeader.
str();
205 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
213 ssHeader << pindex->GetBlockHeader();
216 std::string strHex =
HexStr(ssHeader) +
"\n";
226 std::string strJSON = jsonHeaders.
write() +
"\n";
227 req->
WriteHeader(
"Content-Type",
"application/json");
239 const std::string& strURIPart,
258 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
274 std::string binaryBlock = ssBlock.
str();
275 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
283 std::string strHex =
HexStr(ssBlock) +
"\n";
291 std::string strJSON = objBlock.
write() +
"\n";
292 req->
WriteHeader(
"Content-Type",
"application/json");
305 return rest_block(context, req, strURIPart,
true);
310 return rest_block(context, req, strURIPart,
false);
329 std::string strJSON = chainInfoObject.
write() +
"\n";
330 req->
WriteHeader(
"Content-Type",
"application/json");
345 if (!mempool)
return false;
353 std::string strJSON = mempoolInfoObject.
write() +
"\n";
354 req->
WriteHeader(
"Content-Type",
"application/json");
368 if (!mempool)
return false;
376 std::string strJSON = mempoolObject.
write() +
"\n";
377 req->
WriteHeader(
"Content-Type",
"application/json");
399 g_txindex->BlockUntilSyncedToCurrentChain();
403 if (!
node)
return false;
415 std::string binaryTx = ssTx.
str();
416 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
425 std::string strHex =
HexStr(ssTx) +
"\n";
434 std::string strJSON = objTx.
write() +
"\n";
435 req->
WriteHeader(
"Content-Type",
"application/json");
453 std::vector<std::string> uriParts;
454 if (param.length() > 1)
456 std::string strUriParams = param.substr(1);
457 boost::split(uriParts, strUriParams, boost::is_any_of(
"/"));
461 std::string strRequestMutable = req->
ReadBody();
462 if (strRequestMutable.length() == 0 && uriParts.size() == 0)
465 bool fInputParsed =
false;
466 bool fCheckMemPool =
false;
467 std::vector<COutPoint> vOutPoints;
472 if (uriParts.size() > 0)
475 if (uriParts[0] ==
"checkmempool") fCheckMemPool =
true;
477 for (
size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
481 std::string strTxid = uriParts[i].substr(0, uriParts[i].find(
'-'));
482 std::string strOutput = uriParts[i].substr(uriParts[i].find(
'-')+1);
488 vOutPoints.push_back(
COutPoint(txid, (uint32_t)nOutput));
491 if (vOutPoints.size() > 0)
500 std::vector<unsigned char> strRequestV =
ParseHex(strRequestMutable);
501 strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
507 if (strRequestMutable.size() > 0)
513 oss << strRequestMutable;
514 oss >> fCheckMemPool;
517 }
catch (
const std::ios_base::failure&) {
539 std::vector<unsigned char> bitmap;
540 std::vector<CCoin> outs;
541 std::string bitmapStringRepresentation;
542 std::vector<bool> hits;
543 bitmap.resize((vOutPoints.size() + 7) / 8);
546 auto process_utxos = [&vOutPoints, &outs, &hits](
const CCoinsView& view,
const CTxMemPool& mempool) {
547 for (
const COutPoint& vOutPoint : vOutPoints) {
549 bool hit = !mempool.isSpent(vOutPoint) && view.GetCoin(vOutPoint, coin);
551 if (hit) outs.emplace_back(std::move(coin));
557 if (!mempool)
return false;
562 process_utxos(viewMempool, *mempool);
568 for (
size_t i = 0; i < hits.size(); ++i) {
569 const bool hit = hits[i];
570 bitmapStringRepresentation.append(hit ?
"1" :
"0");
571 bitmap[i / 8] |= ((uint8_t)hit) << (i % 8);
581 std::string ssGetUTXOResponseString = ssGetUTXOResponse.
str();
583 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
591 std::string strHex =
HexStr(ssGetUTXOResponse) +
"\n";
605 objGetUTXOResponse.
pushKV(
"bitmap", bitmapStringRepresentation);
608 for (
const CCoin& coin : outs) {
610 utxo.
pushKV(
"height", (int32_t)coin.nHeight);
616 utxo.
pushKV(
"scriptPubKey", o);
619 objGetUTXOResponse.
pushKV(
"utxos", utxos);
622 std::string strJSON = objGetUTXOResponse.
write() +
"\n";
623 req->
WriteHeader(
"Content-Type",
"application/json");
634 const std::string& str_uri_part)
637 std::string height_str;
640 int32_t blockheight = -1;
641 if (!
ParseInt32(height_str, &blockheight) || blockheight < 0) {
650 if (blockheight > active_chain.
Height()) {
653 pblockindex = active_chain[blockheight];
659 req->
WriteHeader(
"Content-Type",
"application/octet-stream");
669 req->
WriteHeader(
"Content-Type",
"application/json");
681 static const struct {
static std::string AvailableDataFormatsString()
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
static bool rest_chaininfo(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
static RetFormat ParseDataFormat(std::string ¶m, const std::string &strReq)
@ HTTP_SERVICE_UNAVAILABLE
std::vector< unsigned char > ParseHex(const char *psz)
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, bool include_addresses, UniValue &entry, bool include_hex=true, int serialize_flags=0, const CTxUndo *txundo=nullptr)
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
#define PACKAGE_BUGREPORT
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
HTTPStatusCode
HTTP status codes.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
void StartREST(const std::any &context)
Start HTTP REST subsystem.
bool IsHex(const std::string &str)
UniValue MempoolInfoToJSON(const CTxMemPool &pool)
Mempool information to JSON.
static bool CheckWarmup(HTTPRequest *req)
ChainstateManager & EnsureAnyChainman(const std::any &context)
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
CChainState & ActiveChainstate() const
The most-work chain.
std::shared_ptr< const CTransaction > CTransactionRef
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
static bool rest_getutxos(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
bool pushKV(const std::string &key, const UniValue &val)
UniValue ValueFromAmount(const CAmount amount)
CChain & ActiveChain() const
Abstract view on the open txout dataset.
static bool rest_tx(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
static bool rest_block(const std::any &context, HTTPRequest *req, const std::string &strURIPart, bool showTxDetails)
UniValue blockheaderToJSON(const CBlockIndex *tip, const CBlockIndex *blockindex)
Block header to JSON.
static bool rest_mempool_contents(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.
An output of a transaction.
UniValue HandleRequest(const JSONRPCRequest &request) const
static bool rest_headers(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
std::string ReadBody()
Read request body.
static bool rest_block_notxdetails(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
std::string GetHex() const
static CTxMemPool * GetMemPool(const std::any &context, HTTPRequest *req)
Get the node context mempool.
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
RPCHelpMan getblockchaininfo()
void StopREST()
Stop HTTP REST subsystem.
static bool rest_block_extended(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
static bool rest_blockhash_by_height(const std::any &context, HTTPRequest *req, const std::string &str_uri_part)
static bool RESTERR(HTTPRequest *req, enum HTTPStatusCode status, std::string message)
static bool rest_mempool_info(const std::any &context, HTTPRequest *req, const std::string &strURIPart)
uint256 GetBlockHash() const
SERIALIZE_METHODS(CCoin, obj)
int Height() const
Return the maximal height in the chain.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
UniValue MempoolToJSON(const CTxMemPool &pool, bool verbose, bool include_mempool_sequence)
Mempool to JSON.
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
An in-memory indexed chain of blocks.
static const struct @10 uri_prefixes[]
CCoinsView that adds a memory cache for transactions to another CCoinsView.
static const size_t MAX_GETUTXOS_OUTPOINTS
int RPCSerializationFlags()
CCoinsView that brings transactions from a mempool into view.
bool push_back(const UniValue &val)
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex, bool include_addresses)
const CChainParams & Params()
Return the currently selected parameters.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Double ended buffer combining vector and stream-like interfaces.
static NodeContext * GetNodeContext(const std::any &context, HTTPRequest *req)
Get the node context.
An outpoint - a combination of a transaction hash and an index n into its vout.
static const struct @9 rf_names[]
bool RPCIsInWarmup(std::string *outStatus)
NodeContext struct containing references to chain state and connection state.
The block chain is a tree shaped structure starting with the genesis block at the root,...
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
bool IsBlockPruned(const CBlockIndex *pblockindex)
Check whether the block associated with this index entry is pruned or not.
void SetHex(const char *psz)
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
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 InterruptREST()
Interrupt RPC REST subsystem.
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.
UniValue blockToJSON(const CBlock &block, const CBlockIndex *tip, const CBlockIndex *blockindex, bool txDetails)
Block description to JSON.
static const int PROTOCOL_VERSION
network protocol versioning
@ HTTP_INTERNAL_SERVER_ERROR