Merge #20120: net, rpc, test, bugfix: update GetNetworkName, GetNetworksInfo, regression tests

7b5bd3102e test: add getnetworkinfo network name regression tests (Jon Atack)
9a75e1e569 rpc: update GetNetworksInfo() to not return unsupported networks (Jon Atack)
ba8997fb2e 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 7b5bd3102e
  vasild:
    ACK 7b5bd3102

Tree-SHA512: 8f12363eb430e6f45e59e3b1d69c2f2eb5ead254ce7a67547d116c3b70138d763157335a3c85b51f684a3b3b502c6aace4f6fa60ac3b988cf7a9475a7423c4d7
pull/764/head
Wladimir J. van der Laan 4 years ago
commit 79a3b59cc7
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D

@ -52,14 +52,20 @@ enum Network ParseNetwork(const std::string& net_in) {
return NET_UNROUTABLE; return NET_UNROUTABLE;
} }
std::string GetNetworkName(enum Network net) { std::string GetNetworkName(enum Network net)
switch(net) {
{ switch (net) {
case NET_UNROUTABLE: return "unroutable";
case NET_IPV4: return "ipv4"; case NET_IPV4: return "ipv4";
case NET_IPV6: return "ipv6"; case NET_IPV6: return "ipv6";
case NET_ONION: return "onion"; case NET_ONION: return "onion";
default: return ""; case NET_I2P: return "i2p";
} case NET_CJDNS: return "cjdns";
case NET_INTERNAL: return "internal";
case NET_MAX: assert(false);
} // no default case, so the compiler can warn about missing cases
assert(false);
} }
bool static LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup) bool static LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)

@ -497,11 +497,9 @@ static RPCHelpMan getnettotals()
static UniValue GetNetworksInfo() static UniValue GetNetworksInfo()
{ {
UniValue networks(UniValue::VARR); UniValue networks(UniValue::VARR);
for(int n=0; n<NET_MAX; ++n) for (int n = 0; n < NET_MAX; ++n) {
{
enum Network network = static_cast<enum Network>(n); enum Network network = static_cast<enum Network>(n);
if(network == NET_UNROUTABLE || network == NET_INTERNAL) if (network == NET_UNROUTABLE || network == NET_I2P || network == NET_CJDNS || network == NET_INTERNAL) continue;
continue;
proxyType proxy; proxyType proxy;
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
GetProxy(network, proxy); GetProxy(network, proxy);

@ -26,6 +26,8 @@ addnode connect to IPv4
addnode connect to IPv6 addnode connect to IPv6
addnode connect to onion addnode connect to onion
addnode connect to generic DNS name addnode connect to generic DNS name
- Test getnetworkinfo for each node
""" """
import socket import socket
@ -41,12 +43,16 @@ from test_framework.util import (
from test_framework.netutil import test_ipv6_local from test_framework.netutil import test_ipv6_local
RANGE_BEGIN = PORT_MIN + 2 * PORT_RANGE # Start after p2p and rpc ports RANGE_BEGIN = PORT_MIN + 2 * PORT_RANGE # Start after p2p and rpc ports
# From GetNetworkName() in netbase.cpp:
NET_UNROUTABLE = "" # Networks returned by RPC getpeerinfo, defined in src/netbase.cpp::GetNetworkName()
NET_UNROUTABLE = "unroutable"
NET_IPV4 = "ipv4" NET_IPV4 = "ipv4"
NET_IPV6 = "ipv6" NET_IPV6 = "ipv6"
NET_ONION = "onion" NET_ONION = "onion"
# Networks returned by RPC getnetworkinfo, defined in src/rpc/net.cpp::GetNetworksInfo()
NETWORKS = frozenset({NET_IPV4, NET_IPV6, NET_ONION})
class ProxyTest(BitcoinTestFramework): class ProxyTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
@ -84,14 +90,14 @@ class ProxyTest(BitcoinTestFramework):
self.serv3 = Socks5Server(self.conf3) self.serv3 = Socks5Server(self.conf3)
self.serv3.start() self.serv3.start()
# Note: proxies are not used to connect to local nodes # Note: proxies are not used to connect to local nodes. This is because the proxy to
# this is because the proxy to use is based on CService.GetNetwork(), which return NET_UNROUTABLE for localhost # use is based on CService.GetNetwork(), which returns NET_UNROUTABLE for localhost.
args = [ args = [
['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-proxyrandomize=1'], ['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-proxyrandomize=1'],
['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-onion=%s:%i' % (self.conf2.addr),'-proxyrandomize=0'], ['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-onion=%s:%i' % (self.conf2.addr),'-proxyrandomize=0'],
['-listen', '-proxy=%s:%i' % (self.conf2.addr),'-proxyrandomize=1'], ['-listen', '-proxy=%s:%i' % (self.conf2.addr),'-proxyrandomize=1'],
[] []
] ]
if self.have_ipv6: if self.have_ipv6:
args[3] = ['-listen', '-proxy=[%s]:%i' % (self.conf3.addr),'-proxyrandomize=0', '-noonion'] args[3] = ['-listen', '-proxy=[%s]:%i' % (self.conf3.addr),'-proxyrandomize=0', '-noonion']
self.add_nodes(self.num_nodes, extra_args=args) self.add_nodes(self.num_nodes, extra_args=args)
@ -189,15 +195,17 @@ class ProxyTest(BitcoinTestFramework):
r[x['name']] = x r[x['name']] = x
return r return r
# test RPC getnetworkinfo self.log.info("Test RPC getnetworkinfo")
n0 = networks_dict(self.nodes[0].getnetworkinfo()) n0 = networks_dict(self.nodes[0].getnetworkinfo())
for net in ['ipv4','ipv6','onion']: assert_equal(NETWORKS, n0.keys())
for net in NETWORKS:
assert_equal(n0[net]['proxy'], '%s:%i' % (self.conf1.addr)) assert_equal(n0[net]['proxy'], '%s:%i' % (self.conf1.addr))
assert_equal(n0[net]['proxy_randomize_credentials'], True) assert_equal(n0[net]['proxy_randomize_credentials'], True)
assert_equal(n0['onion']['reachable'], True) assert_equal(n0['onion']['reachable'], True)
n1 = networks_dict(self.nodes[1].getnetworkinfo()) n1 = networks_dict(self.nodes[1].getnetworkinfo())
for net in ['ipv4','ipv6']: assert_equal(NETWORKS, n1.keys())
for net in ['ipv4', 'ipv6']:
assert_equal(n1[net]['proxy'], '%s:%i' % (self.conf1.addr)) assert_equal(n1[net]['proxy'], '%s:%i' % (self.conf1.addr))
assert_equal(n1[net]['proxy_randomize_credentials'], False) assert_equal(n1[net]['proxy_randomize_credentials'], False)
assert_equal(n1['onion']['proxy'], '%s:%i' % (self.conf2.addr)) assert_equal(n1['onion']['proxy'], '%s:%i' % (self.conf2.addr))
@ -205,14 +213,16 @@ class ProxyTest(BitcoinTestFramework):
assert_equal(n1['onion']['reachable'], True) assert_equal(n1['onion']['reachable'], True)
n2 = networks_dict(self.nodes[2].getnetworkinfo()) n2 = networks_dict(self.nodes[2].getnetworkinfo())
for net in ['ipv4','ipv6','onion']: assert_equal(NETWORKS, n2.keys())
for net in NETWORKS:
assert_equal(n2[net]['proxy'], '%s:%i' % (self.conf2.addr)) assert_equal(n2[net]['proxy'], '%s:%i' % (self.conf2.addr))
assert_equal(n2[net]['proxy_randomize_credentials'], True) assert_equal(n2[net]['proxy_randomize_credentials'], True)
assert_equal(n2['onion']['reachable'], True) assert_equal(n2['onion']['reachable'], True)
if self.have_ipv6: if self.have_ipv6:
n3 = networks_dict(self.nodes[3].getnetworkinfo()) n3 = networks_dict(self.nodes[3].getnetworkinfo())
for net in ['ipv4','ipv6']: assert_equal(NETWORKS, n3.keys())
for net in NETWORKS:
assert_equal(n3[net]['proxy'], '[%s]:%i' % (self.conf3.addr)) assert_equal(n3[net]['proxy'], '[%s]:%i' % (self.conf3.addr))
assert_equal(n3[net]['proxy_randomize_credentials'], False) assert_equal(n3[net]['proxy_randomize_credentials'], False)
assert_equal(n3['onion']['reachable'], False) assert_equal(n3['onion']['reachable'], False)

Loading…
Cancel
Save