rest: clean-up for `mempool` endpoints

24.x
brunoerg 2 years ago
parent 1d89fc695a
commit acbea66589

@ -590,45 +590,31 @@ static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std:
} }
} }
static bool rest_mempool_info(const std::any& context, HTTPRequest* req, const std::string& strURIPart) static bool rest_mempool(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
{ {
if (!CheckWarmup(req)) if (!CheckWarmup(req))
return false; return false;
const CTxMemPool* mempool = GetMemPool(context, req);
if (!mempool) return false;
std::string param;
const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
switch (rf) {
case RESTResponseFormat::JSON: {
UniValue mempoolInfoObject = MempoolInfoToJSON(*mempool);
std::string strJSON = mempoolInfoObject.write() + "\n"; std::string param;
req->WriteHeader("Content-Type", "application/json"); const RESTResponseFormat rf = ParseDataFormat(param, str_uri_part);
req->WriteReply(HTTP_OK, strJSON); if (param != "contents" && param != "info") {
return true; return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/mempool/<info|contents>.json");
}
default: {
return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
}
} }
}
static bool rest_mempool_contents(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
{
if (!CheckWarmup(req)) return false;
const CTxMemPool* mempool = GetMemPool(context, req); const CTxMemPool* mempool = GetMemPool(context, req);
if (!mempool) return false; if (!mempool) return false;
std::string param;
const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
switch (rf) { switch (rf) {
case RESTResponseFormat::JSON: { case RESTResponseFormat::JSON: {
UniValue mempoolObject = MempoolToJSON(*mempool, true); std::string str_json;
if (param == "contents") {
str_json = MempoolToJSON(*mempool, true).write() + "\n";
} else {
str_json = MempoolInfoToJSON(*mempool).write() + "\n";
}
std::string strJSON = mempoolObject.write() + "\n";
req->WriteHeader("Content-Type", "application/json"); req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strJSON); req->WriteReply(HTTP_OK, str_json);
return true; return true;
} }
default: { default: {
@ -946,8 +932,7 @@ static const struct {
{"/rest/blockfilter/", rest_block_filter}, {"/rest/blockfilter/", rest_block_filter},
{"/rest/blockfilterheaders/", rest_filter_header}, {"/rest/blockfilterheaders/", rest_filter_header},
{"/rest/chaininfo", rest_chaininfo}, {"/rest/chaininfo", rest_chaininfo},
{"/rest/mempool/info", rest_mempool_info}, {"/rest/mempool/", rest_mempool},
{"/rest/mempool/contents", rest_mempool_contents},
{"/rest/headers/", rest_headers}, {"/rest/headers/", rest_headers},
{"/rest/getutxos", rest_getutxos}, {"/rest/getutxos", rest_getutxos},
{"/rest/blockhashbyheight/", rest_blockhash_by_height}, {"/rest/blockhashbyheight/", rest_blockhash_by_height},

Loading…
Cancel
Save