rpc, cli: add network in/out connections to getnetworkinfo and -getinfo #19405

pull jonatack wants to merge 3 commits into bitcoin:master from jonatack:in-and-out-connections changing 5 files +44 −9
  1. jonatack commented at 9:00 am on June 29, 2020: member

    This is basic info that is present in the GUI that I’ve been wishing to have exposed via the RPC and CLI without needing a bash workaround or script. For human users it would also be useful to have it in -getinfo.

    bitcoin-cli getnetworkinfo

    0  "connections": 15,
    1  "connections_in": 6,
    2  "connections_out": 9,
    

    bitcoin-cli -getinfo

    0  "connections": {
    1    "in": 6,
    2    "out": 9,
    3    "total": 15
    4  },
    

    Update the tests, RPC help, and release notes for the changes. Also fixup the getnettotals timemillis help while touching rpc/net.cpp.


    Reviewers can manually test this PR by building from source, launching bitcoind, and then running bitcoin-cli -getinfo, bitcoin-cli getnetworkinfo, bitcoin-cli help getnetworkinfo, and bitcoin-cli help getnettotals (for the UNIX epoch time change).

  2. fanquake added the label RPC/REST/ZMQ on Jun 29, 2020
  3. in src/rpc/net.cpp:486 in e7c7809a09 outdated
    483@@ -484,6 +484,8 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request)
    484                         {RPCResult::Type::BOOL, "localrelay", "true if transaction relay is requested from peers"},
    485                         {RPCResult::Type::NUM, "timeoffset", "the time offset"},
    486                         {RPCResult::Type::NUM, "connections", "the number of connections"},
    


    jonatack commented at 11:01 am on June 29, 2020:
    Possibly change this line to “the total number of connections”.
  4. laanwj commented at 1:52 pm on June 29, 2020: member

    This is info I’ve been wishing to have for some time – probably there is an RPC I’m overlooking? But for human users it seems good to have it in -getinfo.

    Yeah, I’ve personally always used a stats script that goes through getpeerinfo and categorizes them by in/out × ipv4/ipv6/tor.

    0in:  ipv4 139 ipv6  20 tor   0
    1out: ipv4  10 ipv6   0 tor   0
    

    Seems like something that could easily be done client-side in bitcoin-cli itself. But no objections to adding this server-side, it’s a small code change.

  5. practicalswift commented at 2:00 pm on June 29, 2020: contributor
    Concept ACK
  6. jonatack force-pushed on Jun 29, 2020
  7. jonatack commented at 3:14 pm on June 29, 2020: member

    Re-pushed with release notes.

    Yeah, I’ve personally always used a stats script that goes through getpeerinfo and categorizes them by in/out × ipv4/ipv6/tor.

    0in:  ipv4 139 ipv6  20 tor   0
    1out: ipv4  10 ipv6   0 tor   0
    

    Seems like something that could easily be done client-side in bitcoin-cli itself. But no objections to adding this server-side, it’s a small code change.

    That’s pretty nice. In terms of vertical space for human use, something like that might be better as string values

    0  "connections": 149,
    1  "in": "ipv4 139 ipv6 20 tor 0",
    2  "out": "ipv4 10 ipv6  0 tor 0",
    

    rather than a JSON object

     0  "connections": {
     1    "in": {
     2      "ipv4": 139,
     3      "ipv6": 20,
     4      "tor": 0
     5    },
     6    "out": {
     7      "ipv4": 10,
     8      "ipv6": 0,
     9      "tor": 0
    10    },
    11    "total": 149
    12  },
    

    We could perhaps create a new CLI -netinfo option to provide a more detailed report of the network peer connections.

  8. in src/rpc/net.cpp:410 in 9290a98843 outdated
    406@@ -407,7 +407,7 @@ static UniValue getnettotals(const JSONRPCRequest& request)
    407                    {
    408                        {RPCResult::Type::NUM, "totalbytesrecv", "Total bytes received"},
    409                        {RPCResult::Type::NUM, "totalbytessent", "Total bytes sent"},
    410-                       {RPCResult::Type::NUM_TIME, "timemillis", "Current UNIX time in milliseconds"},
    411+                       {RPCResult::Type::NUM_TIME, "timemillis", UNIX_EPOCH_TIME + " in milliseconds"},
    


    MarcoFalke commented at 5:27 pm on June 29, 2020:
    nit: Any reason to remove “Current “?

    jonatack commented at 5:44 pm on June 29, 2020:

    nit: Any reason to remove “Current “?

    Without any other qualifier I thought it was self-understood but don’t mind adding back “Current” if reviewers think it’s unclear.


    jonatack commented at 6:25 pm on June 29, 2020:
    Re-added while retouching to fix the extra line.
  9. in src/rpc/net.cpp:539 in 9290a98843 outdated
    532@@ -531,7 +533,10 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request)
    533     obj.pushKV("timeoffset",    GetTimeOffset());
    534     if (node.connman) {
    535         obj.pushKV("networkactive", node.connman->GetNetworkActive());
    536-        obj.pushKV("connections",   (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL));
    537+        obj.pushKV("connections", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL));
    538+        obj.pushKV("connections_in", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_IN));
    539+        obj.pushKV("connections_out", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_OUT));
    540+
    


    MarcoFalke commented at 5:27 pm on June 29, 2020:
    nit: Any reason to add an empty line?

    jonatack commented at 6:10 pm on June 29, 2020:
    That’s strange and inadvertant. Will remove.

    jonatack commented at 6:26 pm on June 29, 2020:
    Thanks – removed.
  10. MarcoFalke commented at 5:28 pm on June 29, 2020: member
    Concept ACK, but I am wondering if instead the connection type (#19316) should be returned?
  11. jonatack commented at 6:01 pm on June 29, 2020: member

    Concept ACK, but I am wondering if instead the connection type (#19316) should be returned?

    That PR seems orthogonal, as it proposes to detail the outbounds into various subtypes, but the basic distinction remains between inbound and outbound connections.

    I reckon that exposing to users and RPC clients the counts of the outbound subtypes in addition to in/out/total would be enough detail and screen space to warrant a separate client-side -netinfo option in a future PR that could also provide @laanwj’s ip4/6/tor breakdown.

  12. jonatack force-pushed on Jun 29, 2020
  13. jonatack commented at 6:27 pm on June 29, 2020: member
    Addressed @MarcoFalke’s suggestions. Thanks everyone. Rebased per git diff 9290a98 94a792cc.
  14. jnewbery commented at 1:35 pm on July 1, 2020: member

    I have no objection to adding this to getnetworkinfo, but just noting that jq is your friend here:

    0bitcoin-cli getpeerinfo | jq "group_by(.inbound) | map({Inbound: .[0].inbound, Count: length}) | .[]"
    1{
    2  "Inbound": false,
    3  "Count": 9
    4}
    5{
    6  "Inbound": true,
    7  "Count": 6
    8}
    
  15. willcl-ark commented at 2:25 pm on July 2, 2020: member

    tACK.

    Changes seem simple-enough and are working (and passing tests) on MacOS 10.15.

    Also pleased to learn jnewbery’s advanced jq pipe from above :)

  16. eriknylund commented at 9:22 am on July 15, 2020: contributor

    ACK 94a792cc19f26754ac5d60a1b733d4b520f7d0c7

    Ran unit tests and extended functional tests, all pass. Did not run fuzz tests. Started bitcoind on a clean datadir and ran the bitcoin-cli commands to verify the new output:

    0$ ./bitcoind -datadir=/tmp/pr19405
    

    Tested -getinfo

     0$ ./bitcoin-cli -datadir=/tmp/pr19405 -getinfo
     1{
     2  "version": 209900,
     3  "blocks": 0,
     4  "headers": 192000,
     5  "verificationprogress": 1.827923226519457e-09,
     6  "timeoffset": 0,
     7  "connections": {
     8    "in": 0,
     9    "out": 6,
    10    "total": 6
    11  },
    12  "proxy": "",
    13  "difficulty": 1,
    14  "chain": "main",
    15  "keypoolsize": 1000,
    16  "paytxfee": 0.00000000,
    17  "balance": 0.00000000,
    18  "relayfee": 0.00001000,
    19  "warnings": "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications"
    20}
    

    Tested getnetworkinfo:

     0$ ./bitcoin-cli -datadir=/tmp/pr19405 getnetworkinfo
     1{
     2  "version": 209900,
     3  "subversion": "/Satoshi:0.20.99/",
     4  "protocolversion": 70015,
     5  "localservices": "0000000000000409",
     6  "localservicesnames": [
     7    "NETWORK",
     8    "WITNESS",
     9    "NETWORK_LIMITED"
    10  ],
    11  "localrelay": true,
    12  "timeoffset": 0,
    13  "networkactive": true,
    14  "connections": 7,
    15  "connections_in": 0,
    16  "connections_out": 7,
    17  "networks": [
    18    {
    19      "name": "ipv4",
    20      "limited": false,
    21      "reachable": true,
    22      "proxy": "",
    23      "proxy_randomize_credentials": false
    24    },
    25    {
    26      "name": "ipv6",
    27      "limited": false,
    28      "reachable": true,
    29      "proxy": "",
    30      "proxy_randomize_credentials": false
    31    },
    32    {
    33      "name": "onion",
    34      "limited": true,
    35      "reachable": false,
    36      "proxy": "",
    37      "proxy_randomize_credentials": false
    38    }
    39  ],
    40  "relayfee": 0.00001000,
    41  "incrementalfee": 0.00001000,
    42  "localaddresses": [
    43  ],
    44  "warnings": "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications"
    45}
    

    Verified that the help contains the new fields as well:

     0$ ./bitcoin-cli -datadir=/tmp/pr19405 help getnetworkinfo
     1getnetworkinfo
     2Returns an object containing various state info regarding P2P networking.
     3
     4Result:
     5{                                                    (json object)
     6  "version" : n,                                     (numeric) the server version
     7  "subversion" : "str",                              (string) the server subversion string
     8  "protocolversion" : n,                             (numeric) the protocol version
     9  "localservices" : "hex",                           (string) the services we offer to the network
    10  "localservicesnames" : [                           (json array) the services we offer to the network, in human-readable form
    11    "str",                                           (string) the service name
    12    ...
    13  ],
    14  "localrelay" : true|false,                         (boolean) true if transaction relay is requested from peers
    15  "timeoffset" : n,                                  (numeric) the time offset
    16  "connections" : n,                                 (numeric) the total number of connections
    17  "connections_in" : n,                              (numeric) the number of inbound connections
    18  "connections_out" : n,                             (numeric) the number of outbound connections
    19  "networkactive" : true|false,                      (boolean) whether p2p networking is enabled
    20  "networks" : [                                     (json array) information per network
    21    {                                                (json object)
    22      "name" : "str",                                (string) network (ipv4, ipv6 or onion)
    23      "limited" : true|false,                        (boolean) is the network limited using -onlynet?
    24      "reachable" : true|false,                      (boolean) is the network reachable?
    25      "proxy" : "str",                               (string) ("host:port") the proxy that is used for this network, or empty if none
    26      "proxy_randomize_credentials" : true|false     (boolean) Whether randomized credentials are used
    27    },
    28    ...
    29  ],
    30  "relayfee" : n,                                    (numeric) minimum relay fee for transactions in BTC/kB
    31  "incrementalfee" : n,                              (numeric) minimum fee increment for mempool limiting or BIP 125 replacement in BTC/kB
    32  "localaddresses" : [                               (json array) list of local addresses
    33    {                                                (json object)
    34      "address" : "str",                             (string) network address
    35      "port" : n,                                    (numeric) network port
    36      "score" : n                                    (numeric) relative score
    37    },
    38    ...
    39  ],
    40  "warnings" : "str"                                 (string) any network and blockchain warnings
    41}
    42`
    
  17. in test/functional/rpc_net.py:105 in 94a792cc19 outdated
     96@@ -97,21 +97,27 @@ def _test_getnettotals(self):
     97             assert_greater_than_or_equal(after['bytessent_per_msg'].get('ping', 0), before['bytessent_per_msg'].get('ping', 0) + 32)
     98 
     99     def _test_getnetworkinfo(self):
    100-        assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
    101-        assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
    102+        self.log.info("Test getnetworkinfo")
    


    eriknylund commented at 9:36 am on July 15, 2020:
    I’m unsure about what this part is doing, if we just close all connection right after? Could this part be left out?

    jonatack commented at 11:03 am on July 15, 2020:
    Thanks for reviewing, @eriknylund – much appreciated. This code at line 100 adds informational logging to the test output at the start of this _test_getnetworkinfo() test function (e.g. what you see when you run test/functional/rpc_net.py), then updates the rest of the test function to cover the two fields added in this PR while making one less call to getnetworkinfo().

    jonatack commented at 11:04 am on July 15, 2020:
    (If I misunderstood your question, let me know.)

    eriknylund commented at 12:21 pm on July 15, 2020:
    Sorry if my question was unclear I’ll try to rephrase it. I realize I should have used the comment function better to hilight all the lines and not only the beginning of the block which was misleading - sorry about that. I’m trying to figure out why the asserts on line 102-105. I think the ones on lines 116-120 are enough to test, as line 110 sets the precondition for this test. Perhaps I’m missing something obvious?

    jonatack commented at 1:12 pm on July 15, 2020:
    Oh ok. I agree that the test could be marginally improved or refactored, but it is also fine as-is – if anything, faster than before – and not worth invalidating review for.

    eriknylund commented at 1:36 pm on July 15, 2020:
    Thanks for taking your time to explain. I agree it’s probably not worth fixing, just wanted to make sure I understood your intent with those lines asserting the entry of this test method - if there’s something obvious I’m missing or could learn with regards to how tests are structured and setup here.
  18. eriknylund approved
  19. DrahtBot commented at 9:23 pm on July 18, 2020: member

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #19771 (net: Replace enum CConnMan::NumConnections with enum class ConnectionDirection by luke-jr)
    • #17167 (Allow whitelisting outgoing connections by luke-jr)

    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.

  20. DrahtBot added the label Needs rebase on Jul 23, 2020
  21. jonatack commented at 5:01 pm on July 23, 2020: member

    Current status:

    • 1 Concept ACK
    • 2 “No objections”
    • 2 Tested ACKs

    Is anything else needed? The changes are simple and have test coverage.

  22. jonatack closed this on Jul 23, 2020

  23. luke-jr referenced this in commit 2709f14877 on Aug 15, 2020
  24. luke-jr referenced this in commit f97fe2b116 on Aug 15, 2020
  25. jonatack reopened this on Aug 19, 2020

  26. darosior commented at 8:38 am on August 20, 2020: member
    Concept ACK
  27. jonatack force-pushed on Aug 20, 2020
  28. jonatack commented at 9:35 am on August 20, 2020: member
    Rebased and re-opened per IRC discussion yesterday with shesek at http://www.erisian.com.au/bitcoin-core-dev/log-2020-08-19.html#l-375. Proposing with both getnetworkinfo and -getinfo, open to adjust per review feedback.
  29. DrahtBot removed the label Needs rebase on Aug 20, 2020
  30. DrahtBot added the label Needs rebase on Aug 24, 2020
  31. Add in/out connections to rpc getnetworkinfo 1ab49b81cf
  32. UNIX_EPOCH_TIME fixup in rpc getnettotals d9cc13e88d
  33. Add in/out connections to cli -getinfo 581b343d5b
  34. jonatack force-pushed on Aug 24, 2020
  35. jonatack commented at 4:44 pm on August 24, 2020: member
    Rebased. Testing and review welcome! :smiling_face_with_three_hearts:
  36. DrahtBot removed the label Needs rebase on Aug 24, 2020
  37. eriknylund commented at 7:24 pm on August 24, 2020: contributor

    ACK 581b343d5bf517510ab0236583ca96628751177d

    Repeated steps from #19405 (comment) on the re-opened and rebased PR. Tests and extended functional tests pass.

    Tested bitcoin-cli -getinfo, bitcoin-cli getnetworkinfo and bitcoin-cli help getnetworkinfo and the output is unchanged from the previous test. Also tested the UNIX epoch time fixup in bitcoin-cli help getnettotals this looks alright as well.

  38. eriknylund commented at 5:57 am on August 27, 2020: contributor
    @shesek have you had a chance to test and review to see if this works for your purposes?
  39. benthecarman commented at 6:33 am on August 27, 2020: contributor
    tACK 581b343
  40. willcl-ark commented at 11:05 am on August 28, 2020: member
    tACK for 581b343d5bf517510ab0236583ca96628751177d, this time rebased onto master at 862fde88be706adb20a211178253636442c3ae00.
  41. shesek commented at 2:42 pm on August 28, 2020: contributor
    tACK 581b343. This provides what I needed, thanks!
  42. eriknylund commented at 2:47 pm on August 28, 2020: contributor

    tACK 581b343. This provides what I needed, thanks!

    Thanks @shesek!

  43. n-thumann commented at 10:35 am on September 4, 2020: contributor
    tACK 581b343 on master at a0a422c, ran unit & functional tests and and confirmed changes on an existing datadir ✌️
  44. n-thumann approved
  45. eriknylund commented at 10:38 am on September 4, 2020: contributor

    tACK 581b343 on master at a0a422c, ran unit & functional tests and and confirmed changes on an existing datadir ✌️

    Thanks for reviewing @n-thumann and happy to hear that you tested on an existing datadir, which covers the other side of my test! 👍

  46. laanwj merged this on Sep 4, 2020
  47. laanwj closed this on Sep 4, 2020

  48. jonatack deleted the branch on Sep 4, 2020
  49. sidhujag referenced this in commit 2eceb2b906 on Sep 9, 2020
  50. fanquake referenced this in commit a33651866c on Sep 15, 2020
  51. sidhujag referenced this in commit 7c9738a42a on Sep 15, 2020
  52. Fabcien referenced this in commit fa47e97498 on Sep 23, 2021
  53. DrahtBot locked this on Aug 16, 2022

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: 2024-07-05 19:13 UTC

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