From 1ab49b81cf32b6ef9e312a0a8ac45c68a3262f0d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 29 Jun 2020 10:04:08 +0200 Subject: [PATCH] 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 0000000000..5ffe328a4c --- /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 e9343b3348..e4fe021c3f 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 9b8e585b49..d663815e4c 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]