Skip to content

Commit 23d3ae7

Browse files
committed
Merge #19405: rpc, cli: add network in/out connections to getnetworkinfo and -getinfo
581b343 Add in/out connections to cli -getinfo (Jon Atack) d9cc13e UNIX_EPOCH_TIME fixup in rpc getnettotals (Jon Atack) 1ab49b8 Add in/out connections to rpc getnetworkinfo (Jon Atack) Pull request description: 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` ``` "connections": 15, "connections_in": 6, "connections_out": 9, ``` `bitcoin-cli -getinfo` ``` "connections": { "in": 6, "out": 9, "total": 15 }, ``` 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](https://jonatack.github.io/articles/how-to-compile-bitcoin-core-and-run-the-tests), 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). ACKs for top commit: eriknylund: > tACK [581b343](bitcoin/bitcoin@581b343) on master at [a0a422c](bitcoin/bitcoin@a0a422c), ran unit & functional tests and and confirmed changes on an existing datadir ✌️ benthecarman: tACK `581b343` willcl-ark: tACK for 581b343, this time rebased onto master at 862fde8. shesek: tACK `581b343`. This provides what I needed, thanks! n-thumann: tACK 581b343 on master at a0a422c, ran unit & functional tests and and confirmed changes on an existing datadir ✌️ Tree-SHA512: 08dd3ac8fefae401bd8253ff3ac027603c528eeccba53cedcb127771316173a7052fce44af8fa33ac98ebc4cf2a2b11cdefd949995d55e9b9a5942b876d00dc5
2 parents 99a8eb6 + 581b343 commit 23d3ae7

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

doc/release-notes-19405.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Updated RPCs
2+
3+
- `getnetworkinfo` now returns two new fields, `connections_in` and
4+
`connections_out`, that provide the number of inbound and outbound peer
5+
connections. These new fields are in addition to the existing `connections`
6+
field, which returns the total number of peer connections. (#19405)
7+
8+
## CLI
9+
10+
- The `connections` field of `bitcoin-cli -getinfo` is expanded to return a JSON
11+
object with `in`, `out` and `total` numbers of peer connections. It previously
12+
returned a single integer value for the total number of peer connections. (#19405)

src/bitcoin-cli.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,13 @@ class GetinfoRequestHandler: public BaseRequestHandler
271271
result.pushKV("headers", batch[ID_BLOCKCHAININFO]["result"]["headers"]);
272272
result.pushKV("verificationprogress", batch[ID_BLOCKCHAININFO]["result"]["verificationprogress"]);
273273
result.pushKV("timeoffset", batch[ID_NETWORKINFO]["result"]["timeoffset"]);
274-
result.pushKV("connections", batch[ID_NETWORKINFO]["result"]["connections"]);
274+
275+
UniValue connections(UniValue::VOBJ);
276+
connections.pushKV("in", batch[ID_NETWORKINFO]["result"]["connections_in"]);
277+
connections.pushKV("out", batch[ID_NETWORKINFO]["result"]["connections_out"]);
278+
connections.pushKV("total", batch[ID_NETWORKINFO]["result"]["connections"]);
279+
result.pushKV("connections", connections);
280+
275281
result.pushKV("proxy", batch[ID_NETWORKINFO]["result"]["networks"][0]["proxy"]);
276282
result.pushKV("difficulty", batch[ID_BLOCKCHAININFO]["result"]["difficulty"]);
277283
result.pushKV("chain", UniValue(batch[ID_BLOCKCHAININFO]["result"]["chain"]));

src/rpc/net.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ static UniValue getnettotals(const JSONRPCRequest& request)
414414
{
415415
{RPCResult::Type::NUM, "totalbytesrecv", "Total bytes received"},
416416
{RPCResult::Type::NUM, "totalbytessent", "Total bytes sent"},
417-
{RPCResult::Type::NUM_TIME, "timemillis", "Current UNIX time in milliseconds"},
417+
{RPCResult::Type::NUM_TIME, "timemillis", "Current " + UNIX_EPOCH_TIME + " in milliseconds"},
418418
{RPCResult::Type::OBJ, "uploadtarget", "",
419419
{
420420
{RPCResult::Type::NUM, "timeframe", "Length of the measuring timeframe in seconds"},
@@ -490,7 +490,9 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request)
490490
}},
491491
{RPCResult::Type::BOOL, "localrelay", "true if transaction relay is requested from peers"},
492492
{RPCResult::Type::NUM, "timeoffset", "the time offset"},
493-
{RPCResult::Type::NUM, "connections", "the number of connections"},
493+
{RPCResult::Type::NUM, "connections", "the total number of connections"},
494+
{RPCResult::Type::NUM, "connections_in", "the number of inbound connections"},
495+
{RPCResult::Type::NUM, "connections_out", "the number of outbound connections"},
494496
{RPCResult::Type::BOOL, "networkactive", "whether p2p networking is enabled"},
495497
{RPCResult::Type::ARR, "networks", "information per network",
496498
{
@@ -538,7 +540,9 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request)
538540
obj.pushKV("timeoffset", GetTimeOffset());
539541
if (node.connman) {
540542
obj.pushKV("networkactive", node.connman->GetNetworkActive());
541-
obj.pushKV("connections", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL));
543+
obj.pushKV("connections", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL));
544+
obj.pushKV("connections_in", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_IN));
545+
obj.pushKV("connections_out", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_OUT));
542546
}
543547
obj.pushKV("networks", GetNetworksInfo());
544548
obj.pushKV("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()));

test/functional/interface_bitcoin_cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ def run_test(self):
7171
assert_equal(cli_get_info['blocks'], blockchain_info['blocks'])
7272
assert_equal(cli_get_info['headers'], blockchain_info['headers'])
7373
assert_equal(cli_get_info['timeoffset'], network_info['timeoffset'])
74-
assert_equal(cli_get_info['connections'], network_info['connections'])
74+
assert_equal(
75+
cli_get_info['connections'],
76+
{
77+
'in': network_info['connections_in'],
78+
'out': network_info['connections_out'],
79+
'total': network_info['connections']
80+
}
81+
)
7582
assert_equal(cli_get_info['proxy'], network_info['networks'][0]['proxy'])
7683
assert_equal(cli_get_info['difficulty'], blockchain_info['difficulty'])
7784
assert_equal(cli_get_info['chain'], blockchain_info['chain'])

test/functional/rpc_net.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ def test_getnettotals(self):
102102

103103
def test_getnetworkinfo(self):
104104
self.log.info("Test getnetworkinfo")
105-
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
106-
assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
105+
info = self.nodes[0].getnetworkinfo()
106+
assert_equal(info['networkactive'], True)
107+
assert_equal(info['connections'], 2)
108+
assert_equal(info['connections_in'], 1)
109+
assert_equal(info['connections_out'], 1)
107110

108111
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']):
109112
self.nodes[0].setnetworkactive(state=False)
@@ -117,8 +120,11 @@ def test_getnetworkinfo(self):
117120
connect_nodes(self.nodes[0], 1)
118121
connect_nodes(self.nodes[1], 0)
119122

120-
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
121-
assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
123+
info = self.nodes[0].getnetworkinfo()
124+
assert_equal(info['networkactive'], True)
125+
assert_equal(info['connections'], 2)
126+
assert_equal(info['connections_in'], 1)
127+
assert_equal(info['connections_out'], 1)
122128

123129
# check the `servicesnames` field
124130
network_info = [node.getnetworkinfo() for node in self.nodes]

0 commit comments

Comments
 (0)