Skip to content

Commit 79a3b59

Browse files
committed
Merge #20120: net, rpc, test, bugfix: update GetNetworkName, GetNetworksInfo, regression tests
7b5bd31 test: add getnetworkinfo network name regression tests (Jon Atack) 9a75e1e rpc: update GetNetworksInfo() to not return unsupported networks (Jon Atack) ba8997f net: update GetNetworkName() with all enum Network cases (Jon Atack) Pull request description: Following up on the BIP155 addrv2 changes, and starting with 7be6ff6 in #19845, RPC getnetworkinfo began returning networks with empty names. <details><summary><code>getnetworkinfo</code> on current master</summary><p> ``` "networks": [ { "name": "ipv4", "limited": false, "reachable": true, "proxy": "", "proxy_randomize_credentials": false }, { "name": "ipv6", "limited": false, "reachable": true, "proxy": "", "proxy_randomize_credentials": false }, { "name": "onion", "limited": false, "reachable": true, "proxy": "127.0.0.1:9050", "proxy_randomize_credentials": true }, { "name": "", "limited": false, "reachable": true, "proxy": "", "proxy_randomize_credentials": false }, { "name": "", "limited": false, "reachable": true, "proxy": "", "proxy_randomize_credentials": false } ], ``` </p></details> <details><summary><code>getnetworkinfo</code> on this branch</summary><p> ``` "networks": [ { "name": "ipv4", "limited": false, "reachable": true, "proxy": "", "proxy_randomize_credentials": false }, { "name": "ipv6", "limited": false, "reachable": true, "proxy": "", "proxy_randomize_credentials": false }, { "name": "onion", "limited": false, "reachable": true, "proxy": "127.0.0.1:9050", "proxy_randomize_credentials": true } ], ``` </p></details> This patch: - updates `GetNetworkName()` to the current Network enum - updates `getNetworksInfo()` to ignore as-yet unsupported networks - adds regression tests ACKs for top commit: hebasto: re-ACK 7b5bd31 vasild: ACK 7b5bd31 Tree-SHA512: 8f12363eb430e6f45e59e3b1d69c2f2eb5ead254ce7a67547d116c3b70138d763157335a3c85b51f684a3b3b502c6aace4f6fa60ac3b988cf7a9475a7423c4d7
2 parents 4fd37d0 + 7b5bd31 commit 79a3b59

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

src/netbase.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,20 @@ enum Network ParseNetwork(const std::string& net_in) {
5252
return NET_UNROUTABLE;
5353
}
5454

55-
std::string GetNetworkName(enum Network net) {
56-
switch(net)
57-
{
55+
std::string GetNetworkName(enum Network net)
56+
{
57+
switch (net) {
58+
case NET_UNROUTABLE: return "unroutable";
5859
case NET_IPV4: return "ipv4";
5960
case NET_IPV6: return "ipv6";
6061
case NET_ONION: return "onion";
61-
default: return "";
62-
}
62+
case NET_I2P: return "i2p";
63+
case NET_CJDNS: return "cjdns";
64+
case NET_INTERNAL: return "internal";
65+
case NET_MAX: assert(false);
66+
} // no default case, so the compiler can warn about missing cases
67+
68+
assert(false);
6369
}
6470

6571
bool static LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)

src/rpc/net.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,9 @@ static RPCHelpMan getnettotals()
497497
static UniValue GetNetworksInfo()
498498
{
499499
UniValue networks(UniValue::VARR);
500-
for(int n=0; n<NET_MAX; ++n)
501-
{
500+
for (int n = 0; n < NET_MAX; ++n) {
502501
enum Network network = static_cast<enum Network>(n);
503-
if(network == NET_UNROUTABLE || network == NET_INTERNAL)
504-
continue;
502+
if (network == NET_UNROUTABLE || network == NET_I2P || network == NET_CJDNS || network == NET_INTERNAL) continue;
505503
proxyType proxy;
506504
UniValue obj(UniValue::VOBJ);
507505
GetProxy(network, proxy);

test/functional/feature_proxy.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
addnode connect to IPv6
2727
addnode connect to onion
2828
addnode connect to generic DNS name
29+
30+
- Test getnetworkinfo for each node
2931
"""
3032

3133
import socket
@@ -41,12 +43,16 @@
4143
from test_framework.netutil import test_ipv6_local
4244

4345
RANGE_BEGIN = PORT_MIN + 2 * PORT_RANGE # Start after p2p and rpc ports
44-
# From GetNetworkName() in netbase.cpp:
45-
NET_UNROUTABLE = ""
46+
47+
# Networks returned by RPC getpeerinfo, defined in src/netbase.cpp::GetNetworkName()
48+
NET_UNROUTABLE = "unroutable"
4649
NET_IPV4 = "ipv4"
4750
NET_IPV6 = "ipv6"
4851
NET_ONION = "onion"
4952

53+
# Networks returned by RPC getnetworkinfo, defined in src/rpc/net.cpp::GetNetworksInfo()
54+
NETWORKS = frozenset({NET_IPV4, NET_IPV6, NET_ONION})
55+
5056

5157
class ProxyTest(BitcoinTestFramework):
5258
def set_test_params(self):
@@ -84,14 +90,14 @@ def setup_nodes(self):
8490
self.serv3 = Socks5Server(self.conf3)
8591
self.serv3.start()
8692

87-
# Note: proxies are not used to connect to local nodes
88-
# this is because the proxy to use is based on CService.GetNetwork(), which return NET_UNROUTABLE for localhost
93+
# Note: proxies are not used to connect to local nodes. This is because the proxy to
94+
# use is based on CService.GetNetwork(), which returns NET_UNROUTABLE for localhost.
8995
args = [
9096
['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-proxyrandomize=1'],
9197
['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-onion=%s:%i' % (self.conf2.addr),'-proxyrandomize=0'],
9298
['-listen', '-proxy=%s:%i' % (self.conf2.addr),'-proxyrandomize=1'],
9399
[]
94-
]
100+
]
95101
if self.have_ipv6:
96102
args[3] = ['-listen', '-proxy=[%s]:%i' % (self.conf3.addr),'-proxyrandomize=0', '-noonion']
97103
self.add_nodes(self.num_nodes, extra_args=args)
@@ -189,30 +195,34 @@ def networks_dict(d):
189195
r[x['name']] = x
190196
return r
191197

192-
# test RPC getnetworkinfo
198+
self.log.info("Test RPC getnetworkinfo")
193199
n0 = networks_dict(self.nodes[0].getnetworkinfo())
194-
for net in ['ipv4','ipv6','onion']:
200+
assert_equal(NETWORKS, n0.keys())
201+
for net in NETWORKS:
195202
assert_equal(n0[net]['proxy'], '%s:%i' % (self.conf1.addr))
196203
assert_equal(n0[net]['proxy_randomize_credentials'], True)
197204
assert_equal(n0['onion']['reachable'], True)
198205

199206
n1 = networks_dict(self.nodes[1].getnetworkinfo())
200-
for net in ['ipv4','ipv6']:
207+
assert_equal(NETWORKS, n1.keys())
208+
for net in ['ipv4', 'ipv6']:
201209
assert_equal(n1[net]['proxy'], '%s:%i' % (self.conf1.addr))
202210
assert_equal(n1[net]['proxy_randomize_credentials'], False)
203211
assert_equal(n1['onion']['proxy'], '%s:%i' % (self.conf2.addr))
204212
assert_equal(n1['onion']['proxy_randomize_credentials'], False)
205213
assert_equal(n1['onion']['reachable'], True)
206214

207215
n2 = networks_dict(self.nodes[2].getnetworkinfo())
208-
for net in ['ipv4','ipv6','onion']:
216+
assert_equal(NETWORKS, n2.keys())
217+
for net in NETWORKS:
209218
assert_equal(n2[net]['proxy'], '%s:%i' % (self.conf2.addr))
210219
assert_equal(n2[net]['proxy_randomize_credentials'], True)
211220
assert_equal(n2['onion']['reachable'], True)
212221

213222
if self.have_ipv6:
214223
n3 = networks_dict(self.nodes[3].getnetworkinfo())
215-
for net in ['ipv4','ipv6']:
224+
assert_equal(NETWORKS, n3.keys())
225+
for net in NETWORKS:
216226
assert_equal(n3[net]['proxy'], '[%s]:%i' % (self.conf3.addr))
217227
assert_equal(n3[net]['proxy_randomize_credentials'], False)
218228
assert_equal(n3['onion']['reachable'], False)

0 commit comments

Comments
 (0)