Skip to content

Commit 0700847

Browse files
committed
rest: add listmempooltransactions to the REST API
Make the `listmempooltransactions` rpc available via the REST api
1 parent cb466f4 commit 0700847

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/rest.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,45 @@ static bool rest_deploymentinfo(const std::any& context, HTTPRequest* req, const
641641

642642
}
643643

644+
static bool rest_mempool_transactions(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
645+
{
646+
if (!CheckWarmup(req))
647+
return false;
648+
649+
std::string param;
650+
const RESTResponseFormat rf = ParseDataFormat(param, str_uri_part);
651+
if (param != "contents" && param != "info") {
652+
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid URI format. Expected /rest/mempool/transactions/<info|contents>.json");
653+
}
654+
655+
const CTxMemPool* mempool = GetMemPool(context, req);
656+
if (!mempool) return false;
657+
658+
switch (rf) {
659+
case RESTResponseFormat::JSON: {
660+
std::string str_json;
661+
std::string raw_sequence_start;
662+
const bool verbose = param == "contents";
663+
664+
try {
665+
raw_sequence_start = req->GetQueryParameter("sequence_start").value_or("0");
666+
} catch (const std::runtime_error& e) {
667+
return RESTERR(req, HTTP_BAD_REQUEST, e.what());
668+
}
669+
670+
const auto sequence_start{ToIntegral<uint64_t>(raw_sequence_start)};
671+
str_json = MempoolTxsToJSON(*mempool, verbose, sequence_start.value()).write() + "\n";
672+
673+
req->WriteHeader("Content-Type", "application/json");
674+
req->WriteReply(HTTP_OK, str_json);
675+
return true;
676+
}
677+
default: {
678+
return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: json)");
679+
}
680+
}
681+
}
682+
644683
static bool rest_mempool(const std::any& context, HTTPRequest* req, const std::string& str_uri_part)
645684
{
646685
if (!CheckWarmup(req))
@@ -1009,6 +1048,7 @@ static const struct {
10091048
{"/rest/blockfilterheaders/", rest_filter_header},
10101049
{"/rest/chaininfo", rest_chaininfo},
10111050
{"/rest/mempool/", rest_mempool},
1051+
{"/rest/mempool/transactions", rest_mempool_transactions},
10121052
{"/rest/headers/", rest_headers},
10131053
{"/rest/getutxos", rest_getutxos},
10141054
{"/rest/deploymentinfo/", rest_deploymentinfo},

0 commit comments

Comments
 (0)