Changeset View
Changeset View
Standalone View
Standalone View
src/rest.cpp
Show First 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | |||||
static bool RESTERR(HTTPRequest *req, enum HTTPStatusCode status, | static bool RESTERR(HTTPRequest *req, enum HTTPStatusCode status, | ||||
std::string message) { | std::string message) { | ||||
req->WriteHeader("Content-Type", "text/plain"); | req->WriteHeader("Content-Type", "text/plain"); | ||||
req->WriteReply(status, message + "\r\n"); | req->WriteReply(status, message + "\r\n"); | ||||
return false; | return false; | ||||
} | } | ||||
/** | /** | ||||
* Get the node context mempool. | * Get the node context. | ||||
* | * | ||||
* Set the HTTP error and return nullptr if node context | * @param[in] req The HTTP request, whose status code will be set if node | ||||
* mempool is not found. | * context is not found. | ||||
* @returns Pointer to the node context or nullptr if not found. | |||||
*/ | |||||
static NodeContext *GetNodeContext(const util::Ref &context, HTTPRequest *req) { | |||||
NodeContext *node = | |||||
context.Has<NodeContext>() ? &context.Get<NodeContext>() : nullptr; | |||||
if (!node) { | |||||
RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, | |||||
strprintf("%s:%d (%s)\n" | |||||
"Internal bug detected: Node context not found!\n" | |||||
"You may report this issue here: %s\n", | |||||
__FILE__, __LINE__, __func__, PACKAGE_BUGREPORT)); | |||||
return nullptr; | |||||
} | |||||
return node; | |||||
} | |||||
/** | |||||
* Get the node context mempool. | |||||
* | * | ||||
* @param[in] req the HTTP request | * @param[in] req The HTTP request, whose status code will be set if node | ||||
* return pointer to the mempool or nullptr if no mempool found | * context mempool is not found. | ||||
* @returns Pointer to the mempool or nullptr if no mempool found. | |||||
*/ | */ | ||||
static CTxMemPool *GetMemPool(const util::Ref &context, HTTPRequest *req) { | static CTxMemPool *GetMemPool(const util::Ref &context, HTTPRequest *req) { | ||||
NodeContext *node = | NodeContext *node = | ||||
context.Has<NodeContext>() ? &context.Get<NodeContext>() : nullptr; | context.Has<NodeContext>() ? &context.Get<NodeContext>() : nullptr; | ||||
if (!node || !node->mempool) { | if (!node || !node->mempool) { | ||||
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found"); | RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found"); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 328 Lines • ▼ Show 20 Lines | static bool rest_tx(Config &config, const util::Ref &context, HTTPRequest *req, | ||||
} | } | ||||
const TxId txid(hash); | const TxId txid(hash); | ||||
if (g_txindex) { | if (g_txindex) { | ||||
g_txindex->BlockUntilSyncedToCurrentChain(); | g_txindex->BlockUntilSyncedToCurrentChain(); | ||||
} | } | ||||
CTransactionRef tx; | const NodeContext *const node = GetNodeContext(context, req); | ||||
if (!node) { | |||||
return false; | |||||
} | |||||
BlockHash hashBlock; | BlockHash hashBlock; | ||||
if (!GetTransaction(txid, tx, config.GetChainParams().GetConsensus(), | const CTransactionRef tx = | ||||
hashBlock)) { | GetTransaction(/* block_index */ nullptr, node->mempool, txid, | ||||
Params().GetConsensus(), hashBlock); | |||||
if (!tx) { | |||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); | return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); | ||||
} | } | ||||
switch (rf) { | switch (rf) { | ||||
case RetFormat::BINARY: { | case RetFormat::BINARY: { | ||||
CDataStream ssTx(SER_NETWORK, | CDataStream ssTx(SER_NETWORK, | ||||
PROTOCOL_VERSION | RPCSerializationFlags()); | PROTOCOL_VERSION | RPCSerializationFlags()); | ||||
ssTx << tx; | ssTx << tx; | ||||
▲ Show 20 Lines • Show All 343 Lines • Show Last 20 Lines |