Merge bitcoin/bitcoin#25760: rest: clean-up for mempool endpoints

acbea66589 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 acbea66589 - verified that just the error message was updated since da0c612c3d
  theStack:
    re-ACK acbea66589

Tree-SHA512: 35f6f0732a573fe8a6cdcc782f89ae3427a1de19f069a68c9c51bb525118c2b07e20303cbe19b9d4b7d1ad055d69c32def2d0fb8f886c851da562dd9ce33ad6a
This commit is contained in:
MacroFake 2022-08-05 16:43:23 +02:00
commit 7d3817b29a
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548

View File

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