From 1ab49b81cf32b6ef9e312a0a8ac45c68a3262f0d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 29 Jun 2020 10:04:08 +0200 Subject: [PATCH 1/3] Add in/out connections to rpc getnetworkinfo --- doc/release-notes-19405.md | 6 ++++++ src/rpc/net.cpp | 8 ++++++-- test/functional/rpc_net.py | 14 ++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 doc/release-notes-19405.md diff --git a/doc/release-notes-19405.md b/doc/release-notes-19405.md new file mode 100644 index 00000000000..5ffe328a4c7 --- /dev/null +++ b/doc/release-notes-19405.md @@ -0,0 +1,6 @@ +## Updated RPCs + +- `getnetworkinfo` now returns two new fields, `connections_in` and + `connections_out`, that provide the number of inbound and outbound peer + connections. These new fields are in addition to the existing `connections` + field, which returns the total number of peer connections. (#19405) diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index e9343b3348a..e4fe021c3fe 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -490,7 +490,9 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request) }}, {RPCResult::Type::BOOL, "localrelay", "true if transaction relay is requested from peers"}, {RPCResult::Type::NUM, "timeoffset", "the time offset"}, - {RPCResult::Type::NUM, "connections", "the number of connections"}, + {RPCResult::Type::NUM, "connections", "the total number of connections"}, + {RPCResult::Type::NUM, "connections_in", "the number of inbound connections"}, + {RPCResult::Type::NUM, "connections_out", "the number of outbound connections"}, {RPCResult::Type::BOOL, "networkactive", "whether p2p networking is enabled"}, {RPCResult::Type::ARR, "networks", "information per network", { @@ -538,7 +540,9 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request) obj.pushKV("timeoffset", GetTimeOffset()); if (node.connman) { obj.pushKV("networkactive", node.connman->GetNetworkActive()); - obj.pushKV("connections", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL)); + obj.pushKV("connections", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL)); + obj.pushKV("connections_in", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_IN)); + obj.pushKV("connections_out", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_OUT)); } obj.pushKV("networks", GetNetworksInfo()); obj.pushKV("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())); diff --git a/test/functional/rpc_net.py b/test/functional/rpc_net.py index 9b8e585b492..d663815e4c5 100755 --- a/test/functional/rpc_net.py +++ b/test/functional/rpc_net.py @@ -103,8 +103,11 @@ class NetTest(BitcoinTestFramework): def test_getnetworkinfo(self): self.log.info("Test getnetworkinfo") - assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) - assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) + info = self.nodes[0].getnetworkinfo() + assert_equal(info['networkactive'], True) + assert_equal(info['connections'], 2) + assert_equal(info['connections_in'], 1) + assert_equal(info['connections_out'], 1) with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']): self.nodes[0].setnetworkactive(state=False) @@ -118,8 +121,11 @@ class NetTest(BitcoinTestFramework): connect_nodes(self.nodes[0], 1) connect_nodes(self.nodes[1], 0) - assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True) - assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) + info = self.nodes[0].getnetworkinfo() + assert_equal(info['networkactive'], True) + assert_equal(info['connections'], 2) + assert_equal(info['connections_in'], 1) + assert_equal(info['connections_out'], 1) # check the `servicesnames` field network_info = [node.getnetworkinfo() for node in self.nodes] From d9cc13e88d096c1a171159c01cbb96444f7f8d7f Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 29 Jun 2020 10:31:25 +0200 Subject: [PATCH 2/3] UNIX_EPOCH_TIME fixup in rpc getnettotals --- src/rpc/net.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index e4fe021c3fe..f7722533d4e 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -414,7 +414,7 @@ static UniValue getnettotals(const JSONRPCRequest& request) { {RPCResult::Type::NUM, "totalbytesrecv", "Total bytes received"}, {RPCResult::Type::NUM, "totalbytessent", "Total bytes sent"}, - {RPCResult::Type::NUM_TIME, "timemillis", "Current UNIX time in milliseconds"}, + {RPCResult::Type::NUM_TIME, "timemillis", "Current " + UNIX_EPOCH_TIME + " in milliseconds"}, {RPCResult::Type::OBJ, "uploadtarget", "", { {RPCResult::Type::NUM, "timeframe", "Length of the measuring timeframe in seconds"}, From 581b343d5bf517510ab0236583ca96628751177d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 29 Jun 2020 10:05:15 +0200 Subject: [PATCH 3/3] Add in/out connections to cli -getinfo --- doc/release-notes-19405.md | 6 ++++++ src/bitcoin-cli.cpp | 8 +++++++- test/functional/interface_bitcoin_cli.py | 9 ++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/doc/release-notes-19405.md b/doc/release-notes-19405.md index 5ffe328a4c7..14f2a81c7a5 100644 --- a/doc/release-notes-19405.md +++ b/doc/release-notes-19405.md @@ -4,3 +4,9 @@ `connections_out`, that provide the number of inbound and outbound peer connections. These new fields are in addition to the existing `connections` field, which returns the total number of peer connections. (#19405) + +## CLI + +- The `connections` field of `bitcoin-cli -getinfo` is expanded to return a JSON + object with `in`, `out` and `total` numbers of peer connections. It previously + returned a single integer value for the total number of peer connections. (#19405) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index cf52b710cb2..437251a02e2 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -271,7 +271,13 @@ public: result.pushKV("headers", batch[ID_BLOCKCHAININFO]["result"]["headers"]); result.pushKV("verificationprogress", batch[ID_BLOCKCHAININFO]["result"]["verificationprogress"]); result.pushKV("timeoffset", batch[ID_NETWORKINFO]["result"]["timeoffset"]); - result.pushKV("connections", batch[ID_NETWORKINFO]["result"]["connections"]); + + UniValue connections(UniValue::VOBJ); + connections.pushKV("in", batch[ID_NETWORKINFO]["result"]["connections_in"]); + connections.pushKV("out", batch[ID_NETWORKINFO]["result"]["connections_out"]); + connections.pushKV("total", batch[ID_NETWORKINFO]["result"]["connections"]); + result.pushKV("connections", connections); + result.pushKV("proxy", batch[ID_NETWORKINFO]["result"]["networks"][0]["proxy"]); result.pushKV("difficulty", batch[ID_BLOCKCHAININFO]["result"]["difficulty"]); result.pushKV("chain", UniValue(batch[ID_BLOCKCHAININFO]["result"]["chain"])); diff --git a/test/functional/interface_bitcoin_cli.py b/test/functional/interface_bitcoin_cli.py index 80003aca0d1..81c007c27bd 100755 --- a/test/functional/interface_bitcoin_cli.py +++ b/test/functional/interface_bitcoin_cli.py @@ -71,7 +71,14 @@ class TestBitcoinCli(BitcoinTestFramework): assert_equal(cli_get_info['blocks'], blockchain_info['blocks']) assert_equal(cli_get_info['headers'], blockchain_info['headers']) assert_equal(cli_get_info['timeoffset'], network_info['timeoffset']) - assert_equal(cli_get_info['connections'], network_info['connections']) + assert_equal( + cli_get_info['connections'], + { + 'in': network_info['connections_in'], + 'out': network_info['connections_out'], + 'total': network_info['connections'] + } + ) assert_equal(cli_get_info['proxy'], network_info['networks'][0]['proxy']) assert_equal(cli_get_info['difficulty'], blockchain_info['difficulty']) assert_equal(cli_get_info['chain'], blockchain_info['chain'])