Skip to content

Conversation

brunoerg
Copy link
Contributor

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.

Copy link
Contributor

@stickies-v stickies-v left a 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.

@DrahtBot
Copy link
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25756 (rest: Remove support for a number of -deprecatedrest options by stickies-v)
  • #25755 (rest: Use from_blockhash and txdetails query parameters by stickies-v)
  • #25754 (rest: Extend HTTPRequest interface to support querying path (parameters) by stickies-v)
  • #25753 (rest: Move format string from path-like parameter to query parameter by stickies-v)
  • #25412 (rest: add /deploymentinfo endpoint by brunoerg)
  • #21422 (Add feerate histogram to getmempoolinfo by kiminuo)

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.

@brunoerg
Copy link
Contributor Author

brunoerg commented Aug 2, 2022

Alternatively, you could also only register /rest/mempool/ and inspect the str_uri_part? I think this would allow for slightly more code reduction.

Nice, I had thought about it, but I think str_uri_part wouldn't get me the whole URI but only (in this case) the format, like: '.json'. Am I missing something?

@stickies-v
Copy link
Contributor

http_request_cb passes everything after the endpoint prefix to str_uri_part. If you register the endpoints with prefix /rest/mempool/<info|content>, then indeed only .json would get passed to str_uri_part. If however you just register /rest/mempool/, then everything after that i.e. <info|content>.json would get passed. (plug: #25754 is trying to clean up this interface)

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.

@brunoerg brunoerg force-pushed the 2022-07-rest-improvements branch from 029fad6 to 001b45f Compare August 3, 2022 00:32
@brunoerg
Copy link
Contributor Author

brunoerg commented Aug 3, 2022

Nice, @stickies-v. Force-pushed addressing your suggestion.

It is clearer having only /rest/mempool/, and then check the str_uri_part.

Copy link
Contributor

@stickies-v stickies-v left a 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");

@brunoerg brunoerg force-pushed the 2022-07-rest-improvements branch from 001b45f to da0c612 Compare August 3, 2022 18:22
@brunoerg
Copy link
Contributor Author

brunoerg commented Aug 3, 2022

Thanks, @stickies-v for your review! Force-pushed addressing the latest suggestions.

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK da0c612 - nice work on removing all the duplicate code!

Just 1 nit that I didn't see earlier, sorry.

Copy link
Contributor

@theStack theStack left a 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 da0c612

Happy to also re-ACK if you decide to take stickies' nit about removing the 'Error' prefix.

@brunoerg brunoerg force-pushed the 2022-07-rest-improvements branch from da0c612 to acbea66 Compare August 5, 2022 13:28
@brunoerg
Copy link
Contributor Author

brunoerg commented Aug 5, 2022

Force-pushed removing the Error: prefix. Thanks, @stickies-v.

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK acbea66 - verified that just the error message was updated since da0c612

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK acbea66

Copy link
Member

@jonatack jonatack left a 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";
}
Copy link
Member

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;

Copy link
Member

@maflcko maflcko Aug 5, 2022

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

@maflcko maflcko merged commit 7d3817b into bitcoin:master Aug 5, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 5, 2022
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
@bitcoin bitcoin locked and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants