diff options
author | MacroFake <falke.marco@gmail.com> | 2022-08-05 16:43:23 +0200 |
---|---|---|
committer | MacroFake <falke.marco@gmail.com> | 2022-08-05 16:43:29 +0200 |
commit | 7d3817b29a2ad6a1ca5bae6c342472bc74e2a7fe (patch) | |
tree | 126738f3f2ed9529f19fe0b38b72835effed11a9 | |
parent | 006740b6f6475ed6ae08803c60dd82027602695b (diff) | |
parent | acbea66589100fe6ef726f4b2a92ec26132ef17b (diff) | |
download | bitcoin-7d3817b29a2ad6a1ca5bae6c342472bc74e2a7fe.tar.xz |
Merge bitcoin/bitcoin#25760: rest: clean-up for `mempool` endpoints
acbea66589100fe6ef726f4b2a92ec26132ef17b 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 acbea66589100fe6ef726f4b2a92ec26132ef17b - verified that just the error message was updated since https://github.com/bitcoin/bitcoin/commit/da0c612c3d69164da19332167c38bfcd1f9777a8
theStack:
re-ACK acbea66589100fe6ef726f4b2a92ec26132ef17b
Tree-SHA512: 35f6f0732a573fe8a6cdcc782f89ae3427a1de19f069a68c9c51bb525118c2b07e20303cbe19b9d4b7d1ad055d69c32def2d0fb8f886c851da562dd9ce33ad6a
-rw-r--r-- | src/rest.cpp | 41 |
1 files changed, 13 insertions, 28 deletions
diff --git a/src/rest.cpp b/src/rest.cpp index 43c248b03b..85815a9c8b 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -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; - 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"; - 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)"); - } + 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"); } -} -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 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->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}, |