Skip to content

Commit 4d0373f

Browse files
MacroFakeknst
authored andcommitted
Merge 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@da0c612 theStack: re-ACK acbea66 Tree-SHA512: 35f6f0732a573fe8a6cdcc782f89ae3427a1de19f069a68c9c51bb525118c2b07e20303cbe19b9d4b7d1ad055d69c32def2d0fb8f886c851da562dd9ce33ad6a
1 parent 46a187a commit 4d0373f

File tree

1 file changed

+13
-31
lines changed

1 file changed

+13
-31
lines changed

src/rest.cpp

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -620,51 +620,34 @@ static bool rest_chaininfo(const CoreContext& context, HTTPRequest* req, const s
620620
}
621621
}
622622

623-
static bool rest_mempool_info(const CoreContext& context, HTTPRequest* req, const std::string& strURIPart)
623+
static bool rest_mempool(const CoreContext& context, HTTPRequest* req, const std::string& str_uri_part)
624624
{
625625
if (!CheckWarmup(req))
626626
return false;
627-
const CTxMemPool* mempool = GetMemPool(context, req);
628-
if (!mempool) return false;
629-
std::string param;
630-
const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
631627

632-
switch (rf) {
633-
case RESTResponseFormat::JSON: {
634-
const LLMQContext* llmq_ctx = GetLLMQContext(context, req);
635-
if (!llmq_ctx) return false;
636-
637-
UniValue mempoolInfoObject = MempoolInfoToJSON(*mempool, *llmq_ctx->isman);
638-
639-
std::string strJSON = mempoolInfoObject.write() + "\n";
640-
req->WriteHeader("Content-Type", "application/json");
641-
req->WriteReply(HTTP_OK, strJSON);
642-
return true;
643-
}
644-
default: {
645-
return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
646-
}
628+
std::string param;
629+
const RESTResponseFormat rf = ParseDataFormat(param, str_uri_part);
630+
if (param != "contents" && param != "info") {
631+
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/mempool/<info|contents>.json");
647632
}
648-
}
649633

650-
static bool rest_mempool_contents(const CoreContext& context, HTTPRequest* req, const std::string& strURIPart)
651-
{
652-
if (!CheckWarmup(req)) return false;
653634
const CTxMemPool* mempool = GetMemPool(context, req);
654635
if (!mempool) return false;
655-
std::string param;
656-
const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
657636

658637
switch (rf) {
659638
case RESTResponseFormat::JSON: {
660639
const LLMQContext* llmq_ctx = GetLLMQContext(context, req);
661640
if (!llmq_ctx) return false;
662641

663-
UniValue mempoolObject = MempoolToJSON(*mempool, llmq_ctx->isman.get(), true);
642+
std::string str_json;
643+
if (param == "contents") {
644+
str_json = MempoolToJSON(*mempool, llmq_ctx->isman.get(), true).write() + "\n";
645+
} else {
646+
str_json = MempoolInfoToJSON(*mempool, *llmq_ctx->isman).write() + "\n";
647+
}
664648

665-
std::string strJSON = mempoolObject.write() + "\n";
666649
req->WriteHeader("Content-Type", "application/json");
667-
req->WriteReply(HTTP_OK, strJSON);
650+
req->WriteReply(HTTP_OK, str_json);
668651
return true;
669652
}
670653
default: {
@@ -982,8 +965,7 @@ static const struct {
982965
{"/rest/blockfilter/", rest_block_filter},
983966
{"/rest/blockfilterheaders/", rest_filter_header},
984967
{"/rest/chaininfo", rest_chaininfo},
985-
{"/rest/mempool/info", rest_mempool_info},
986-
{"/rest/mempool/contents", rest_mempool_contents},
968+
{"/rest/mempool/", rest_mempool},
987969
{"/rest/headers/", rest_headers},
988970
{"/rest/getutxos", rest_getutxos},
989971
{"/rest/blockhashbyheight/", rest_blockhash_by_height},

0 commit comments

Comments
 (0)