Skip to content

Commit 7d3817b

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25760: rest: clean-up for mempool endpoints
acbea66 rest: clean-up for `mempool` endpoints (brunoerg) Pull request description: The functions `rest_mempool_info` and `rest_mempool_contents` are similar, the only difference between them is: `rest_mempool_info` uses `MempoolInfoToJSON` to get the mempool informations and `rest_mempool_contents` uses `MempoolToJSON`, for this reason this PR creates a new function to handle it and reduce duplicated code. Also, 1. Rename `strURIPart` to `str_uri_part`. 2. Rename `strJSON` to `str_json`. ACKs for top commit: stickies-v: re-ACK acbea66 - verified that just the error message was updated since bitcoin/bitcoin@da0c612 theStack: re-ACK acbea66 Tree-SHA512: 35f6f0732a573fe8a6cdcc782f89ae3427a1de19f069a68c9c51bb525118c2b07e20303cbe19b9d4b7d1ad055d69c32def2d0fb8f886c851da562dd9ce33ad6a
2 parents 006740b + acbea66 commit 7d3817b

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

src/rest.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -590,45 +590,31 @@ static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std:
590590
}
591591
}
592592

593-
static bool rest_mempool_info(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
593+
static bool rest_mempool(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
594594
{
595595
if (!CheckWarmup(req))
596596
return false;
597-
const CTxMemPool* mempool = GetMemPool(context, req);
598-
if (!mempool) return false;
599-
std::string param;
600-
const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
601-
602-
switch (rf) {
603-
case RESTResponseFormat::JSON: {
604-
UniValue mempoolInfoObject = MempoolInfoToJSON(*mempool);
605597

606-
std::string strJSON = mempoolInfoObject.write() + "\n";
607-
req->WriteHeader("Content-Type", "application/json");
608-
req->WriteReply(HTTP_OK, strJSON);
609-
return true;
610-
}
611-
default: {
612-
return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
613-
}
598+
std::string param;
599+
const RESTResponseFormat rf = ParseDataFormat(param, str_uri_part);
600+
if (param != "contents" && param != "info") {
601+
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/mempool/<info|contents>.json");
614602
}
615-
}
616603

617-
static bool rest_mempool_contents(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
618-
{
619-
if (!CheckWarmup(req)) return false;
620604
const CTxMemPool* mempool = GetMemPool(context, req);
621605
if (!mempool) return false;
622-
std::string param;
623-
const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
624606

625607
switch (rf) {
626608
case RESTResponseFormat::JSON: {
627-
UniValue mempoolObject = MempoolToJSON(*mempool, true);
609+
std::string str_json;
610+
if (param == "contents") {
611+
str_json = MempoolToJSON(*mempool, true).write() + "\n";
612+
} else {
613+
str_json = MempoolInfoToJSON(*mempool).write() + "\n";
614+
}
628615

629-
std::string strJSON = mempoolObject.write() + "\n";
630616
req->WriteHeader("Content-Type", "application/json");
631-
req->WriteReply(HTTP_OK, strJSON);
617+
req->WriteReply(HTTP_OK, str_json);
632618
return true;
633619
}
634620
default: {
@@ -946,8 +932,7 @@ static const struct {
946932
{"/rest/blockfilter/", rest_block_filter},
947933
{"/rest/blockfilterheaders/", rest_filter_header},
948934
{"/rest/chaininfo", rest_chaininfo},
949-
{"/rest/mempool/info", rest_mempool_info},
950-
{"/rest/mempool/contents", rest_mempool_contents},
935+
{"/rest/mempool/", rest_mempool},
951936
{"/rest/headers/", rest_headers},
952937
{"/rest/getutxos", rest_getutxos},
953938
{"/rest/blockhashbyheight/", rest_blockhash_by_height},

0 commit comments

Comments
 (0)