Skip to content

Commit

Permalink
rpc, test: expose CNodeStats network in RPC getpeerinfo
Browse files Browse the repository at this point in the history
Summary:
This is a backport of [[bitcoin/bitcoin#20002 | core#20002]] [2/5]
bitcoin/bitcoin@4938a10

Depends on D10502

Test Plan: `ninja check-functional`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10503
  • Loading branch information
jonatack authored and PiRK committed Nov 24, 2021
1 parent 58dc89e commit 0e151b8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ static RPCHelpMan getpeerinfo() {
"(ip:port) Bind address of the connection to the peer"},
{RPCResult::Type::STR, "addrlocal",
"(ip:port) Local address as reported by the peer"},
{RPCResult::Type::STR, "network",
"Network (ipv4, ipv6, or onion) the peer connected "
"through"},
{RPCResult::Type::NUM, "mapped_as",
"The AS in the BGP route to the peer used for "
"diversifying\n"
Expand Down Expand Up @@ -223,12 +226,13 @@ static RPCHelpMan getpeerinfo() {
bool fStateStats = GetNodeStateStats(stats.nodeid, statestats);
obj.pushKV("id", stats.nodeid);
obj.pushKV("addr", stats.addrName);
if (!(stats.addrLocal.empty())) {
obj.pushKV("addrlocal", stats.addrLocal);
}
if (stats.addrBind.IsValid()) {
obj.pushKV("addrbind", stats.addrBind.ToString());
}
if (!(stats.addrLocal.empty())) {
obj.pushKV("addrlocal", stats.addrLocal);
}
obj.pushKV("network", stats.m_network);
if (stats.m_mapped_as != 0) {
obj.pushKV("mapped_as", uint64_t(stats.m_mapped_as));
}
Expand Down
44 changes: 33 additions & 11 deletions test/functional/feature_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
- proxy on IPv6
- Create various proxies (as threads)
- Create bitcoinds that connect to them
- Manipulate the bitcoinds using addnode (onetry) an observe effects
- Create nodes that connect to them
- Manipulate the peer connections using addnode (onetry) and observe effects
- Test the getpeerinfo `network` field for the peer
addnode connect to IPv4
addnode connect to IPv6
Expand All @@ -41,6 +42,11 @@
from test_framework.util import PORT_MIN, PORT_RANGE, assert_equal

RANGE_BEGIN = PORT_MIN + 2 * PORT_RANGE # Start after p2p and rpc ports
# From GetNetworkName() in netbase.cpp:
NET_UNROUTABLE = ""
NET_IPV4 = "ipv4"
NET_IPV6 = "ipv6"
NET_ONION = "onion"


class ProxyTest(BitcoinTestFramework):
Expand Down Expand Up @@ -102,10 +108,17 @@ def setup_nodes(self):
self.add_nodes(self.num_nodes, extra_args=args)
self.start_nodes()

def network_test(self, node, addr, network):
for peer in node.getpeerinfo():
if peer["addr"] == addr:
assert_equal(peer["network"], network)

def node_test(self, node, proxies, auth, test_onion=True):
rv = []
# Test: outgoing IPv4 connection through node
node.addnode("15.61.23.23:1234", "onetry")
addr = "15.61.23.23:1234"
self.log.debug(
"Test: outgoing IPv4 connection through node for address {}".format(addr))
node.addnode(addr, "onetry")
cmd = proxies[0].queue.get()
assert isinstance(cmd, Socks5Command)
# Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME,
Expand All @@ -117,11 +130,13 @@ def node_test(self, node, proxies, auth, test_onion=True):
assert_equal(cmd.username, None)
assert_equal(cmd.password, None)
rv.append(cmd)
self.network_test(node, addr, network=NET_IPV4)

if self.have_ipv6:
# Test: outgoing IPv6 connection through node
node.addnode(
"[1233:3432:2434:2343:3234:2345:6546:4534]:5443", "onetry")
addr = "[1233:3432:2434:2343:3234:2345:6546:4534]:5443"
self.log.debug(
"Test: outgoing IPv6 connection through node for address {}".format(addr))
node.addnode(addr, "onetry")
cmd = proxies[1].queue.get()
assert isinstance(cmd, Socks5Command)
# Note: bitcoind's SOCKS5 implementation only sends atyp
Expand All @@ -133,10 +148,13 @@ def node_test(self, node, proxies, auth, test_onion=True):
assert_equal(cmd.username, None)
assert_equal(cmd.password, None)
rv.append(cmd)
self.network_test(node, addr, network=NET_IPV6)

if test_onion:
# Test: outgoing onion connection through node
node.addnode("bitcoinostk4e4re.onion:8333", "onetry")
addr = "bitcoinostk4e4re.onion:8333"
self.log.debug(
"Test: outgoing onion connection through node for address {}".format(addr))
node.addnode(addr, "onetry")
cmd = proxies[2].queue.get()
assert isinstance(cmd, Socks5Command)
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
Expand All @@ -146,9 +164,12 @@ def node_test(self, node, proxies, auth, test_onion=True):
assert_equal(cmd.username, None)
assert_equal(cmd.password, None)
rv.append(cmd)
self.network_test(node, addr, network=NET_ONION)

# Test: outgoing DNS name connection through node
node.addnode("node.noumenon:8333", "onetry")
addr = "node.noumenon:8333"
self.log.debug(
"Test: outgoing DNS name connection through node for address {}".format(addr))
node.addnode(addr, "onetry")
cmd = proxies[3].queue.get()
assert isinstance(cmd, Socks5Command)
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
Expand All @@ -158,6 +179,7 @@ def node_test(self, node, proxies, auth, test_onion=True):
assert_equal(cmd.username, None)
assert_equal(cmd.password, None)
rv.append(cmd)
self.network_test(node, addr, network=NET_UNROUTABLE)

return rv

Expand Down

0 comments on commit 0e151b8

Please sign in to comment.