-
Notifications
You must be signed in to change notification settings - Fork 38.7k
rest: clean-up for mempool endpoints
#25760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
stickies-v
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK 029fad6
Alternatively, you could also only register /rest/mempool/ and inspect the str_uri_part? I think this would allow for slightly more code reduction.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
Nice, I had thought about it, but I think |
|
I don't think the current approach of registering both separately is unreasonable, so this is just a suggestion if you think that implementation makes sense - intuitively it would be my first approach. |
029fad6 to
001b45f
Compare
|
Nice, @stickies-v. Force-pushed addressing your suggestion. It is clearer having only |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I've left a few more suggestions in comments, but I think that's pretty much it from me.
Couldn't make suggested code, so here's the diff instead:
diff --git a/src/rest.cpp b/src/rest.cpp
index dd6fe8d8e..3f2ab98a8 100644
--- a/src/rest.cpp
+++ b/src/rest.cpp
@@ -594,20 +594,23 @@ static bool rest_mempool(const std::any& context, HTTPRequest* req, const std::s
{
if (!CheckWarmup(req))
return false;
- const CTxMemPool* mempool = GetMemPool(context, req);
- if (!mempool) return false;
+
std::string param;
const RESTResponseFormat rf = ParseDataFormat(param, str_uri_part);
+ if (param != "contents" && param != "info") {
+ return RESTERR(req, HTTP_BAD_REQUEST, "Error: invalid URI format. Expected /rest/mempool/<info|contents>.json");
+ }
+
+ const CTxMemPool* mempool = GetMemPool(context, req);
+ if (!mempool) return false;
switch (rf) {
case RESTResponseFormat::JSON: {
std::string str_json;
- if (str_uri_part == "contents.json") {
+ if (param == "contents") {
str_json = MempoolToJSON(*mempool, true).write() + "\n";
- } else if (str_uri_part == "info.json") {
- str_json = MempoolInfoToJSON(*mempool).write() + "\n";
} else {
- return RESTERR(req, HTTP_BAD_REQUEST, "Error: invalid URI format");
+ str_json = MempoolInfoToJSON(*mempool).write() + "\n";
}
req->WriteHeader("Content-Type", "application/json");001b45f to
da0c612
Compare
|
Thanks, @stickies-v for your review! Force-pushed addressing the latest suggestions. |
stickies-v
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK da0c612c3d69164da19332167c38bfcd1f9777a8 - nice work on removing all the duplicate code!
Just 1 nit that I didn't see earlier, sorry.
theStack
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice deduplication! 👌
Code-review ACK da0c612c3d69164da19332167c38bfcd1f9777a8
Happy to also re-ACK if you decide to take stickies' nit about removing the 'Error' prefix.
da0c612 to
acbea66
Compare
|
Force-pushed removing the |
stickies-v
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
theStack
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-ACK acbea66
jonatack
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK
| str_json = MempoolToJSON(*mempool, true).write() + "\n"; | ||
| } else { | ||
| str_json = MempoolInfoToJSON(*mempool).write() + "\n"; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe reduce temporaries and reassignments here.
case RESTResponseFormat::JSON: {
- std::string str_json;
- if (param == "contents") {
- str_json = MempoolToJSON(*mempool, true).write() + "\n";
- } else {
- str_json = MempoolInfoToJSON(*mempool).write() + "\n";
- }
-
req->WriteHeader("Content-Type", "application/json");
- req->WriteReply(HTTP_OK, str_json);
+ req->WriteReply(HTTP_OK, (param == "contents" ? MempoolToJSON(*mempool, /*verbose=*/true) : MempoolInfoToJSON(*mempool)).write() + "\n");
return true;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could make sense to avoid std::string copies in WriteReply, but maybe for a follow-up?
edit: I guess that's not possible, as libevent only accepts a (read-only) span, not the memory directly
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
The functions
rest_mempool_infoandrest_mempool_contentsare similar, the only difference between them is:rest_mempool_infousesMempoolInfoToJSONto get the mempool informations andrest_mempool_contentsusesMempoolToJSON, for this reason this PR creates a new function to handle it and reduce duplicated code.Also,
strURIParttostr_uri_part.strJSONtostr_json.