rpc: add optional nodeid param to filter getpeerinfo #32741

pull waketraindev wants to merge 1 commits into bitcoin:master from waketraindev:2025-06-getpeerinfo-filterid changing 4 files +25 −1
  1. waketraindev commented at 9:51 pm on June 12, 2025: none
    Adds a nodeid parameter to getpeerinfo to filter results by peer id. This especially helpful when using bitcoin-cli or console to investigate something from debug.log I often find it annoying to manually search through the full peer list just to get info on one node, this makes that much easier.
  2. DrahtBot commented at 9:51 pm on June 12, 2025: contributor

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

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32741.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

  3. DrahtBot added the label RPC/REST/ZMQ on Jun 12, 2025
  4. in src/rpc/net.cpp:126 in 6285d75ae9 outdated
    121@@ -122,7 +122,9 @@ static RPCHelpMan getpeerinfo()
    122     return RPCHelpMan{
    123         "getpeerinfo",
    124         "Returns data about each connected network peer as a json array of objects.",
    125-        {},
    126+        {
    127+            {"nodeid", RPCArg::Type::NUM, RPCArg::Default{-1}, "Filter results to the peer with this nodeid. Specify -1 to return all peers."}
    


    maflcko commented at 6:57 am on June 13, 2025:
    this should be optional, not a magic default value

    waketraindev commented at 12:29 pm on June 13, 2025:
    Updated to use Optional::OMITTED to avoid relying on magic default value. Thanks!
  5. in src/rpc/net.cpp:208 in 6285d75ae9 outdated
    203             + HelpExampleRpc("getpeerinfo", "")
    204+            + HelpExampleRpc("getpeerinfo", "7")
    205         },
    206         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    207 {
    208+    const int filterPeerId = request.params[0].isNull() ? -1 : request.params[0].getInt<int>();
    


    maflcko commented at 6:57 am on June 13, 2025:
    nit: snake_case for new code

    maflcko commented at 6:58 am on June 13, 2025:
    Also, you can use the self.Arg to get the value.

    waketraindev commented at 12:31 pm on June 13, 2025:
    Squashed commits. Renamed to filter_id. Evaluating how to best use self.Arg with NodeId. Will follow up

    waketraindev commented at 2:21 pm on June 13, 2025:

    Also, you can use the self.Arg to get the value.

    Would this approach be preferable to avoid using a sentinel value like -1?

    0const UniValue* nodeid_arg = self.MaybeArg<UniValue>("nodeid");
    1const std::optional<NodeId> filter_id = nodeid_arg ? std::make_optional(nodeid_arg->getInt<NodeId>()) : std::nullopt;
    
    0if (filter_id && stats.nodeid != *filter_id) continue;
    1...
    2if (filter_id && stats.nodeid == *filter_id) break;
    

    Would it be acceptable to touch util.cpp for this to add a int64_t template for MaybeArg or stick to the sentinel approach?

    0TMPL_INST(nullptr, std::optional<int64_t>, maybe_arg ? std::optional{maybe_arg->getInt<int64_t>()} : std::nullopt;);
    
  6. maflcko commented at 7:00 am on June 13, 2025: member

    Please squash your commits according to https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits

    Also, missing tests?

  7. 0xB10C commented at 9:17 am on June 13, 2025: contributor
    I’m frequently using something like bitcoin-cli getpeerinfo | jq '[.[] | select(.id == 24430)]' for this. Doesn’t work in the GUI console though.
  8. waketraindev force-pushed on Jun 13, 2025
  9. waketraindev commented at 1:20 pm on June 13, 2025: none
    Added test coverage with nodeid filter in test_getpeerinfo, including a check for nonexistent ids. Let me know if you’d prefer this in a separate test method or different cases
  10. waketraindev force-pushed on Jun 13, 2025
  11. DrahtBot added the label CI failed on Jun 13, 2025
  12. DrahtBot commented at 1:22 pm on June 13, 2025: contributor

    🚧 At least one of the CI tasks failed. Task lint: https://github.com/bitcoin/bitcoin/runs/44049534572 LLM reason (✨ experimental): The CI failure is caused by lint errors, specifically a trailing whitespace issue detected during the lint check.

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

  13. waketraindev force-pushed on Jun 13, 2025
  14. waketraindev force-pushed on Jun 13, 2025
  15. rpc: add optional "nodeid" filter to getpeerinfo with test coverage 53c5ce915c
  16. waketraindev force-pushed on Jun 13, 2025
  17. waketraindev force-pushed on Jun 13, 2025
  18. waketraindev requested review from maflcko on Jun 13, 2025
  19. DrahtBot removed the label CI failed on Jun 13, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-06-15 06:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me