rpc_net.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2017-present The Bitcoin Core developers
   3  # Distributed under the MIT software license, see the accompanying
   4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  """Test RPC calls related to net.
   6  
   7  Tests correspond to code in rpc/net.cpp.
   8  """
   9  
  10  from decimal import Decimal
  11  from itertools import product
  12  import platform
  13  import time
  14  
  15  import test_framework.messages
  16  from test_framework.p2p import (
  17      P2PInterface,
  18      P2P_SERVICES,
  19  )
  20  from test_framework.test_framework import BitcoinTestFramework
  21  from test_framework.util import (
  22      assert_approx,
  23      assert_equal,
  24      assert_greater_than,
  25      assert_raises_rpc_error,
  26      p2p_port,
  27  )
  28  from test_framework.wallet import MiniWallet
  29  
  30  
  31  def assert_net_servicesnames(servicesflag, servicenames):
  32      """Utility that checks if all flags are correctly decoded in
  33      `getpeerinfo` and `getnetworkinfo`.
  34  
  35      :param servicesflag: The services as an integer.
  36      :param servicenames: The list of decoded services names, as strings.
  37      """
  38      servicesflag_generated = 0
  39      for servicename in servicenames:
  40          servicesflag_generated |= getattr(test_framework.messages, 'NODE_' + servicename)
  41      assert_equal(servicesflag_generated, servicesflag)
  42  
  43  
  44  def seed_addrman(node):
  45      """ Populate the addrman with addresses from different networks.
  46      Here 2 ipv4, 2 ipv6, 1 cjdns, 2 onion and 1 i2p addresses are added.
  47      """
  48      # These addresses currently don't collide with a deterministic addrman.
  49      # If the addrman positioning/bucketing is changed, these might collide
  50      # and adding them fails.
  51      success = { "success": True }
  52      assert_equal(node.addpeeraddress(address="1.2.3.4", tried=True, port=8333), success)
  53      assert_equal(node.addpeeraddress(address="2.0.0.0", port=8333), success)
  54      assert_equal(node.addpeeraddress(address="1233:3432:2434:2343:3234:2345:6546:4534", tried=True, port=8333), success)
  55      assert_equal(node.addpeeraddress(address="2803:0:1234:abcd::1", port=45324), success)
  56      assert_equal(node.addpeeraddress(address="fc00:1:2:3:4:5:6:7", port=8333), success)
  57      assert_equal(node.addpeeraddress(address="pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion", tried=True, port=8333), success)
  58      assert_equal(node.addpeeraddress(address="nrfj6inpyf73gpkyool35hcmne5zwfmse3jl3aw23vk7chdemalyaqad.onion", port=45324, tried=True), success)
  59      assert_equal(node.addpeeraddress(address="c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p", port=8333), success)
  60  
  61  
  62  class NetTest(BitcoinTestFramework):
  63      def set_test_params(self):
  64          self.num_nodes = 2
  65          self.extra_args = [
  66              ["-minrelaytxfee=0.00001000"],
  67              ["-minrelaytxfee=0.00000500"],
  68          ]
  69          # Specify a non-working proxy to make sure no actual connections to public IPs are attempted
  70          for args in self.extra_args:
  71              args.append("-proxy=127.0.0.1:1")
  72          self.supports_cli = False
  73  
  74      def run_test(self):
  75          # We need miniwallet to make a transaction
  76          self.wallet = MiniWallet(self.nodes[0])
  77  
  78          # By default, the test framework sets up an addnode connection from
  79          # node 1 --> node0. By connecting node0 --> node 1, we're left with
  80          # the two nodes being connected both ways.
  81          # Topology will look like: node0 <--> node1
  82          self.connect_nodes(0, 1)
  83          self.sync_all()
  84  
  85          self.test_connection_count()
  86          self.test_getpeerinfo()
  87          self.test_getnettotals()
  88          self.test_getnetworkinfo()
  89          self.test_addnode_getaddednodeinfo()
  90          self.test_service_flags()
  91          self.test_getnodeaddresses()
  92          self.test_addpeeraddress()
  93          self.test_sendmsgtopeer()
  94          self.test_getaddrmaninfo()
  95          self.test_getrawaddrman()
  96  
  97      def test_connection_count(self):
  98          self.log.info("Test getconnectioncount")
  99          # After using `connect_nodes` to connect nodes 0 and 1 to each other.
 100          assert_equal(self.nodes[0].getconnectioncount(), 2)
 101  
 102      def test_getpeerinfo(self):
 103          self.log.info("Test getpeerinfo")
 104          # Create a few getpeerinfo last_block/last_transaction values.
 105          self.wallet.send_self_transfer(from_node=self.nodes[0]) # Make a transaction so we can see it in the getpeerinfo results
 106          self.generate(self.nodes[1], 1)
 107          time_now = int(time.time())
 108          peer_info = [x.getpeerinfo() for x in self.nodes]
 109          # Verify last_block and last_transaction keys/values.
 110          for node, peer, field in product(range(self.num_nodes), range(2), ['last_block', 'last_transaction']):
 111              assert field in peer_info[node][peer].keys()
 112              if peer_info[node][peer][field] != 0:
 113                  assert_approx(peer_info[node][peer][field], time_now, vspan=60)
 114          # check both sides of bidirectional connection between nodes
 115          # the address bound to on one side will be the source address for the other node
 116          assert_equal(peer_info[0][0]['addrbind'], peer_info[1][0]['addr'])
 117          assert_equal(peer_info[1][0]['addrbind'], peer_info[0][0]['addr'])
 118          assert_equal(peer_info[0][0]['minfeefilter'], Decimal("0.00000500"))
 119          assert_equal(peer_info[1][0]['minfeefilter'], Decimal("0.00001000"))
 120          # check the `servicesnames` field
 121          for info in peer_info:
 122              assert_net_servicesnames(int(info[0]["services"], 0x10), info[0]["servicesnames"])
 123  
 124          assert_equal(peer_info[0][0]['connection_type'], 'inbound')
 125          assert_equal(peer_info[0][1]['connection_type'], 'manual')
 126  
 127          assert_equal(peer_info[1][0]['connection_type'], 'manual')
 128          assert_equal(peer_info[1][1]['connection_type'], 'inbound')
 129  
 130          # Check dynamically generated networks list in getpeerinfo help output.
 131          assert "(ipv4, ipv6, onion, i2p, cjdns, not_publicly_routable)" in self.nodes[0].help("getpeerinfo")
 132  
 133          self.log.info("Check getpeerinfo output before a version message was sent")
 134          no_version_peer_id = 2
 135          no_version_peer_conntime = int(time.time())
 136          self.nodes[0].setmocktime(no_version_peer_conntime)
 137          with self.nodes[0].wait_for_new_peer():
 138              no_version_peer = self.nodes[0].add_p2p_connection(P2PInterface(), send_version=False, wait_for_verack=False)
 139          if self.options.v2transport:
 140              self.wait_until(lambda: self.nodes[0].getpeerinfo()[no_version_peer_id]["transport_protocol_type"] == "v2")
 141          self.nodes[0].setmocktime(0)
 142          peer_info = self.nodes[0].getpeerinfo()[no_version_peer_id]
 143          peer_info.pop("addr")
 144          peer_info.pop("addrbind")
 145          # The next two fields will vary for v2 connections because we send a rng-based number of decoy messages
 146          peer_info.pop("bytesrecv")
 147          peer_info.pop("bytessent")
 148          assert_equal(
 149              peer_info,
 150              {
 151                  "addr_processed": 0,
 152                  "addr_rate_limited": 0,
 153                  "addr_relay_enabled": False,
 154                  "bip152_hb_from": False,
 155                  "bip152_hb_to": False,
 156                  "bytesrecv_per_msg": {},
 157                  "bytessent_per_msg": {},
 158                  "connection_type": "inbound",
 159                  "conntime": no_version_peer_conntime,
 160                  "id": no_version_peer_id,
 161                  "inbound": True,
 162                  "inflight": [],
 163                  "last_block": 0,
 164                  "last_transaction": 0,
 165                  "lastrecv": 0 if not self.options.v2transport else no_version_peer_conntime,
 166                  "lastsend": 0 if not self.options.v2transport else no_version_peer_conntime,
 167                  "minfeefilter": Decimal("0E-8"),
 168                  "network": "not_publicly_routable",
 169                  "permissions": [],
 170                  "presynced_headers": -1,
 171                  "relaytxes": False,
 172                  "inv_to_send": 0,
 173                  "last_inv_sequence": 0,
 174                  "services": "0000000000000000",
 175                  "servicesnames": [],
 176                  "session_id": "" if not self.options.v2transport else no_version_peer.v2_state.peer['session_id'].hex(),
 177                  "subver": "",
 178                  "synced_blocks": -1,
 179                  "synced_headers": -1,
 180                  "timeoffset": 0,
 181                  "transport_protocol_type": "v1" if not self.options.v2transport else "v2",
 182                  "version": 0,
 183              },
 184          )
 185          no_version_peer.peer_disconnect()
 186          self.wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 2)
 187  
 188      def test_getnettotals(self):
 189          self.log.info("Test getnettotals")
 190          # Test getnettotals and getpeerinfo by doing a ping. The bytes
 191          # sent/received should increase by at least the size of one ping
 192          # and one pong. Both have a payload size of 8 bytes, but the total
 193          # size depends on the used p2p version:
 194          #   - p2p v1: 24 bytes (header) + 8 bytes (payload) = 32 bytes
 195          #   - p2p v2: 21 bytes (header/tag with short-id) + 8 bytes (payload) = 29 bytes
 196          ping_size = 32 if not self.options.v2transport else 29
 197          net_totals_before = self.nodes[0].getnettotals()
 198          peer_info_before = self.nodes[0].getpeerinfo()
 199  
 200          self.nodes[0].ping()
 201          self.wait_until(lambda: (self.nodes[0].getnettotals()['totalbytessent'] >= net_totals_before['totalbytessent'] + ping_size * 2), timeout=1)
 202          self.wait_until(lambda: (self.nodes[0].getnettotals()['totalbytesrecv'] >= net_totals_before['totalbytesrecv'] + ping_size * 2), timeout=1)
 203  
 204          for peer_before in peer_info_before:
 205              peer_after = lambda: next(p for p in self.nodes[0].getpeerinfo() if p['id'] == peer_before['id'])
 206              self.wait_until(lambda: peer_after()['bytesrecv_per_msg'].get('pong', 0) >= peer_before['bytesrecv_per_msg'].get('pong', 0) + ping_size, timeout=1)
 207              self.wait_until(lambda: peer_after()['bytessent_per_msg'].get('ping', 0) >= peer_before['bytessent_per_msg'].get('ping', 0) + ping_size, timeout=1)
 208  
 209      def test_getnetworkinfo(self):
 210          self.log.info("Test getnetworkinfo")
 211          info = self.nodes[0].getnetworkinfo()
 212          assert_equal(info['networkactive'], True)
 213          assert_equal(info['connections'], 2)
 214          assert_equal(info['connections_in'], 1)
 215          assert_equal(info['connections_out'], 1)
 216  
 217          with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']):
 218              self.nodes[0].setnetworkactive(state=False)
 219          assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
 220          # Wait a bit for all sockets to close
 221          for n in self.nodes:
 222              self.wait_until(lambda: n.getnetworkinfo()['connections'] == 0, timeout=3)
 223  
 224          with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: true\n']):
 225              self.nodes[0].setnetworkactive(state=True)
 226          # Connect nodes both ways.
 227          self.connect_nodes(0, 1)
 228          self.connect_nodes(1, 0)
 229  
 230          info = self.nodes[0].getnetworkinfo()
 231          assert_equal(info['networkactive'], True)
 232          assert_equal(info['connections'], 2)
 233          assert_equal(info['connections_in'], 1)
 234          assert_equal(info['connections_out'], 1)
 235  
 236          # check the `servicesnames` field
 237          network_info = [node.getnetworkinfo() for node in self.nodes]
 238          for info in network_info:
 239              assert_net_servicesnames(int(info["localservices"], 0x10), info["localservicesnames"])
 240  
 241          # Check dynamically generated networks list in getnetworkinfo help output.
 242          assert "(ipv4, ipv6, onion, i2p, cjdns)" in self.nodes[0].help("getnetworkinfo")
 243  
 244      def test_addnode_getaddednodeinfo(self):
 245          self.log.info("Test addnode and getaddednodeinfo")
 246          assert_equal(self.nodes[0].getaddednodeinfo(), [])
 247          self.log.info("Add a node (node2) to node0")
 248          ip_port = "127.0.0.1:{}".format(p2p_port(2))
 249          self.nodes[0].addnode(node=ip_port, command='add')
 250          self.log.info("Try to add an equivalent ip and check it fails")
 251          self.log.debug("(note that OpenBSD doesn't support the IPv4 shorthand notation with omitted zero-bytes)")
 252          if platform.system() != "OpenBSD":
 253              ip_port2 = "127.1:{}".format(p2p_port(2))
 254              assert_raises_rpc_error(-23, "Node already added", self.nodes[0].addnode, node=ip_port2, command='add')
 255          self.log.info("Check that the node has indeed been added")
 256          added_nodes = self.nodes[0].getaddednodeinfo()
 257          assert_equal(len(added_nodes), 1)
 258          assert_equal(added_nodes[0]['addednode'], ip_port)
 259          self.log.info("Check that filtering by node works")
 260          self.nodes[0].addnode(node="11.22.33.44", command='add')
 261          first_added_node = self.nodes[0].getaddednodeinfo(node=ip_port)
 262          assert_equal(added_nodes, first_added_node)
 263          assert_equal(len(self.nodes[0].getaddednodeinfo()), 2)
 264          self.log.info("Check that node cannot be added again")
 265          assert_raises_rpc_error(-23, "Node already added", self.nodes[0].addnode, node=ip_port, command='add')
 266          self.log.info("Check that node can be removed")
 267          self.nodes[0].addnode(node=ip_port, command='remove')
 268          added_nodes = self.nodes[0].getaddednodeinfo()
 269          assert_equal(len(added_nodes), 1)
 270          assert_equal(added_nodes[0]['addednode'], "11.22.33.44")
 271          self.log.info("Check that an invalid command returns an error")
 272          assert_raises_rpc_error(-1, 'addnode "node" "command"', self.nodes[0].addnode, node=ip_port, command='abc')
 273          self.log.info("Check that trying to remove the node again returns an error")
 274          assert_raises_rpc_error(-24, "Node could not be removed", self.nodes[0].addnode, node=ip_port, command='remove')
 275          self.log.info("Check that a non-existent node returns an error")
 276          assert_raises_rpc_error(-24, "Node has not been added", self.nodes[0].getaddednodeinfo, '1.1.1.1')
 277  
 278      def test_service_flags(self):
 279          self.log.info("Test service flags")
 280          self.nodes[0].add_p2p_connection(P2PInterface(), services=(1 << 4) | (1 << 63))
 281          if self.options.v2transport:
 282              assert_equal(['UNKNOWN[2^4]', 'P2P_V2', 'UNKNOWN[2^63]'], self.nodes[0].getpeerinfo()[-1]['servicesnames'])
 283          else:
 284              assert_equal(['UNKNOWN[2^4]', 'UNKNOWN[2^63]'], self.nodes[0].getpeerinfo()[-1]['servicesnames'])
 285          self.nodes[0].disconnect_p2ps()
 286  
 287      def test_getnodeaddresses(self):
 288          self.log.info("Test getnodeaddresses")
 289          self.nodes[0].add_p2p_connection(P2PInterface())
 290  
 291          # Add an IPv6 address to the address manager.
 292          ipv6_addr = "1233:3432:2434:2343:3234:2345:6546:4534"
 293          self.nodes[0].addpeeraddress(address=ipv6_addr, port=8333)
 294  
 295          # Add 10,000 IPv4 addresses to the address manager. Due to the way bucket
 296          # and bucket positions are calculated, some of these addresses will collide.
 297          imported_addrs = []
 298          for i in range(10000):
 299              first_octet = i >> 8
 300              second_octet = i % 256
 301              a = f"{first_octet}.{second_octet}.1.1"
 302              imported_addrs.append(a)
 303              self.nodes[0].addpeeraddress(a, 8333)
 304  
 305          # Fetch the addresses via the RPC and test the results.
 306          assert_equal(len(self.nodes[0].getnodeaddresses()), 1)  # default count is 1
 307          assert_equal(len(self.nodes[0].getnodeaddresses(count=2)), 2)
 308          assert_equal(len(self.nodes[0].getnodeaddresses(network="ipv4", count=8)), 8)
 309  
 310          # Maximum possible addresses in AddrMan is 10000. The actual number will
 311          # usually be less due to bucket and bucket position collisions.
 312          node_addresses = self.nodes[0].getnodeaddresses(0, "ipv4")
 313          assert_greater_than(len(node_addresses), 5000)
 314          assert_greater_than(10000, len(node_addresses))
 315          for a in node_addresses:
 316              assert_greater_than(a["time"], 1527811200)  # 1st June 2018
 317              assert_equal(a["services"], P2P_SERVICES)
 318              assert a["address"] in imported_addrs
 319              assert_equal(a["port"], 8333)
 320              assert_equal(a["network"], "ipv4")
 321  
 322          # Test the IPv6 address.
 323          res = self.nodes[0].getnodeaddresses(0, "ipv6")
 324          assert_equal(len(res), 1)
 325          assert_equal(res[0]["address"], ipv6_addr)
 326          assert_equal(res[0]["network"], "ipv6")
 327          assert_equal(res[0]["port"], 8333)
 328          assert_equal(res[0]["services"], P2P_SERVICES)
 329  
 330          # Test for the absence of onion, I2P and CJDNS addresses.
 331          for network in ["onion", "i2p", "cjdns"]:
 332              assert_equal(self.nodes[0].getnodeaddresses(0, network), [])
 333  
 334          # Test invalid arguments.
 335          assert_raises_rpc_error(-8, "Address count out of range", self.nodes[0].getnodeaddresses, -1)
 336          assert_raises_rpc_error(-8, "Network not recognized: Foo", self.nodes[0].getnodeaddresses, 1, "Foo")
 337  
 338      def test_addpeeraddress(self):
 339          self.log.info("Test addpeeraddress")
 340          # The node has an existing, non-deterministic addrman from a previous test.
 341          # Clear it to have a deterministic addrman.
 342          self.restart_node(1, ["-checkaddrman=1", "-test=addrman"], clear_addrman=True)
 343          node = self.nodes[1]
 344  
 345          self.log.debug("Test that addpeeraddress is a hidden RPC")
 346          # It is hidden from general help, but its detailed help may be called directly.
 347          assert "addpeeraddress" not in node.help()
 348          assert "unknown command: addpeeraddress" not in node.help("addpeeraddress")
 349  
 350          self.log.debug("Test that adding an empty address fails")
 351          assert_raises_rpc_error(-30, "Invalid IP address", node.addpeeraddress, address="", port=8333)
 352          assert_equal(node.getnodeaddresses(count=0), [])
 353  
 354          self.log.debug("Test that adding a non-IP/hostname fails (no DNS lookup allowed)")
 355          assert_raises_rpc_error(-30, "Invalid IP address", node.addpeeraddress, address="not_an_ip", port=8333)
 356  
 357          self.log.debug("Test that non-bool tried fails")
 358          assert_raises_rpc_error(-3, "JSON value of type string is not of expected type bool", self.nodes[0].addpeeraddress, address="1.2.3.4", tried="True", port=1234)
 359  
 360          self.log.debug("Test that adding an address with invalid port fails")
 361          assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress, address="1.2.3.4", port=-1)
 362          assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress, address="1.2.3.4", port=65536)
 363  
 364          self.log.debug("Test that adding a valid address to the new table succeeds")
 365          assert_equal(node.addpeeraddress(address="1.0.0.0", tried=False, port=8333), {"success": True})
 366          addrman = node.getrawaddrman()
 367          assert_equal(len(addrman["tried"]), 0)
 368          new_table = list(addrman["new"].values())
 369          assert_equal(len(new_table), 1)
 370          assert_equal(new_table[0]["address"], "1.0.0.0")
 371          assert_equal(new_table[0]["port"], 8333)
 372  
 373          self.log.debug("Test that adding an already-present new address to the new and tried tables fails")
 374          for value in [True, False]:
 375              assert_equal(node.addpeeraddress(address="1.0.0.0", tried=value, port=8333), {"success": False, "error": "failed-adding-to-new"})
 376          assert_equal(len(node.getnodeaddresses(count=0)), 1)
 377  
 378          self.log.debug("Test that adding a valid address to the tried table succeeds")
 379          assert_equal(node.addpeeraddress(address="1.2.3.4", tried=True, port=8333), {"success": True})
 380          addrman = node.getrawaddrman()
 381          assert_equal(len(addrman["new"]), 1)
 382          tried_table = list(addrman["tried"].values())
 383          assert_equal(len(tried_table), 1)
 384          assert_equal(tried_table[0]["address"], "1.2.3.4")
 385          assert_equal(tried_table[0]["port"], 8333)
 386          node.getnodeaddresses(count=0)  # getnodeaddresses re-runs the addrman checks
 387  
 388          self.log.debug("Test that adding an already-present tried address to the new and tried tables fails")
 389          for value in [True, False]:
 390              assert_equal(node.addpeeraddress(address="1.2.3.4", tried=value, port=8333), {"success": False, "error": "failed-adding-to-new"})
 391          assert_equal(len(node.getnodeaddresses(count=0)), 2)
 392  
 393          self.log.debug("Test that adding an address, which collides with the address in tried table, fails")
 394          colliding_address = "1.2.5.45"  # grinded address that produces a tried-table collision
 395          assert_equal(node.addpeeraddress(address=colliding_address, tried=True, port=8333), {"success": False, "error": "failed-adding-to-tried"})
 396          # When adding an address to the tried table, it's first added to the new table.
 397          # As we fail to move it to the tried table, it remains in the new table.
 398          addrman_info = node.getaddrmaninfo()
 399          assert_equal(addrman_info["all_networks"]["tried"], 1)
 400          assert_equal(addrman_info["all_networks"]["new"], 2)
 401  
 402          self.log.debug("Test that adding an another address to the new table succeeds")
 403          assert_equal(node.addpeeraddress(address="2.0.0.0", port=8333), {"success": True})
 404          addrman_info = node.getaddrmaninfo()
 405          assert_equal(addrman_info["all_networks"]["tried"], 1)
 406          assert_equal(addrman_info["all_networks"]["new"], 3)
 407          node.getnodeaddresses(count=0)  # getnodeaddresses re-runs the addrman checks
 408  
 409      def test_sendmsgtopeer(self):
 410          node = self.nodes[0]
 411  
 412          self.restart_node(0)
 413          # we want to use a p2p v1 connection here in order to ensure
 414          # a peer id of zero (a downgrade from v2 to v1 would lead
 415          # to an increase of the peer id)
 416          self.connect_nodes(0, 1, peer_advertises_v2=False)
 417  
 418          self.log.info("Test sendmsgtopeer")
 419          self.log.debug("Send a valid message")
 420          msg_bytes_before = node.getpeerinfo()[0]["bytesrecv_per_msg"]["pong"]
 421  
 422          with self.nodes[1].assert_debug_log(expected_msgs=["received: addr"]):
 423              node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="FFFFFF")
 424              node.ping()
 425              self.wait_until(lambda: node.getpeerinfo()[0]["bytesrecv_per_msg"]["pong"] > msg_bytes_before)
 426  
 427          self.log.debug("Test error for sending to non-existing peer")
 428          assert_raises_rpc_error(-1, "Error: Could not send message to peer", node.sendmsgtopeer, peer_id=100, msg_type="addr", msg="FF")
 429  
 430          self.log.debug("Test that zero-length msg_type is allowed")
 431          node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="")
 432  
 433          self.log.debug("Test error for msg_type that is too long")
 434          assert_raises_rpc_error(-8, "Error: msg_type too long, max length is 12", node.sendmsgtopeer, peer_id=0, msg_type="long_msg_type", msg="FF")
 435  
 436          self.log.debug("Test that unknown msg_type is allowed")
 437          node.sendmsgtopeer(peer_id=0, msg_type="unknown", msg="FF")
 438  
 439          self.log.debug("Test that empty msg is allowed")
 440          node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="FF")
 441  
 442          self.log.debug("Test that oversized messages are allowed, but get us disconnected")
 443          zero_byte_string = b'\x00' * 4000001
 444          node.sendmsgtopeer(peer_id=0, msg_type="addr", msg=zero_byte_string.hex())
 445          self.wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 0, timeout=10)
 446  
 447      def test_getaddrmaninfo(self):
 448          self.log.info("Test getaddrmaninfo")
 449          self.restart_node(1, extra_args=["-cjdnsreachable", "-test=addrman"], clear_addrman=True)
 450          node = self.nodes[1]
 451          seed_addrman(node)
 452  
 453          expected_network_count = {
 454              'all_networks': {'new': 4, 'tried': 4, 'total': 8},
 455              'ipv4': {'new': 1, 'tried': 1, 'total': 2},
 456              'ipv6': {'new': 1, 'tried': 1, 'total': 2},
 457              'onion': {'new': 0, 'tried': 2, 'total': 2},
 458              'i2p': {'new': 1, 'tried': 0, 'total': 1},
 459              'cjdns': {'new': 1, 'tried': 0, 'total': 1},
 460          }
 461  
 462          self.log.debug("Test that count of addresses in addrman match expected values")
 463          res = node.getaddrmaninfo()
 464          for network, count in expected_network_count.items():
 465              assert_equal(res[network]['new'], count['new'])
 466              assert_equal(res[network]['tried'], count['tried'])
 467              assert_equal(res[network]['total'], count['total'])
 468  
 469      def test_getrawaddrman(self):
 470          self.log.info("Test getrawaddrman")
 471          self.restart_node(1, extra_args=["-cjdnsreachable", "-test=addrman"], clear_addrman=True)
 472          node = self.nodes[1]
 473          self.addr_time = int(time.time())
 474          node.setmocktime(self.addr_time)
 475          seed_addrman(node)
 476  
 477          self.log.debug("Test that getrawaddrman is a hidden RPC")
 478          # It is hidden from general help, but its detailed help may be called directly.
 479          assert "getrawaddrman" not in node.help()
 480          assert "unknown command: getrawaddrman" not in node.help("getrawaddrman")
 481  
 482          def check_addr_information(result, expected):
 483              """Utility to compare a getrawaddrman result entry with an expected entry"""
 484              assert_equal(result["address"], expected["address"])
 485              assert_equal(result["port"], expected["port"])
 486              assert_equal(result["services"], expected["services"])
 487              assert_equal(result["network"], expected["network"])
 488              assert_equal(result["source"], expected["source"])
 489              assert_equal(result["source_network"], expected["source_network"])
 490              assert_equal(result["time"], self.addr_time)
 491  
 492          def check_getrawaddrman_entries(expected):
 493              """Utility to compare a getrawaddrman result with expected addrman contents"""
 494              getrawaddrman = node.getrawaddrman()
 495              getaddrmaninfo = node.getaddrmaninfo()
 496              for (table_name, table_info) in expected.items():
 497                  assert_equal(len(getrawaddrman[table_name]), len(table_info))
 498                  assert_equal(len(getrawaddrman[table_name]), getaddrmaninfo["all_networks"][table_name])
 499  
 500                  for bucket_position in getrawaddrman[table_name].keys():
 501                      entry = getrawaddrman[table_name][bucket_position]
 502                      expected_entry = list(filter(lambda e: e["address"] == entry["address"], table_info))[0]
 503                      assert_equal(bucket_position, expected_entry["bucket_position"])
 504                      check_addr_information(entry, expected_entry)
 505  
 506          # we expect 4 new and 4 tried table entries in the addrman which were added using seed_addrman()
 507          expected = {
 508              "new": [
 509                      {
 510                          "bucket_position": "82/8",
 511                          "address": "2.0.0.0",
 512                          "port": 8333,
 513                          "services": 9,
 514                          "network": "ipv4",
 515                          "source": "2.0.0.0",
 516                          "source_network": "ipv4",
 517                      },
 518                      {
 519                          "bucket_position": "336/24",
 520                          "address": "fc00:1:2:3:4:5:6:7",
 521                          "port": 8333,
 522                          "services": 9,
 523                          "network": "cjdns",
 524                          "source": "fc00:1:2:3:4:5:6:7",
 525                          "source_network": "cjdns",
 526                      },
 527                      {
 528                          "bucket_position": "963/46",
 529                          "address": "c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p",
 530                          "port": 8333,
 531                          "services": 9,
 532                          "network": "i2p",
 533                          "source": "c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p",
 534                          "source_network": "i2p",
 535                      },
 536                      {
 537                          "bucket_position": "613/6",
 538                          "address": "2803:0:1234:abcd::1",
 539                          "services": 9,
 540                          "network": "ipv6",
 541                          "source": "2803:0:1234:abcd::1",
 542                          "source_network": "ipv6",
 543                          "port": 45324,
 544                      }
 545              ],
 546              "tried": [
 547                      {
 548                          "bucket_position": "6/33",
 549                          "address": "1.2.3.4",
 550                          "port": 8333,
 551                          "services": 9,
 552                          "network": "ipv4",
 553                          "source": "1.2.3.4",
 554                          "source_network": "ipv4",
 555                      },
 556                      {
 557                          "bucket_position": "197/34",
 558                          "address": "1233:3432:2434:2343:3234:2345:6546:4534",
 559                          "port": 8333,
 560                          "services": 9,
 561                          "network": "ipv6",
 562                          "source": "1233:3432:2434:2343:3234:2345:6546:4534",
 563                          "source_network": "ipv6",
 564                      },
 565                      {
 566                          "bucket_position": "72/61",
 567                          "address": "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion",
 568                          "port": 8333,
 569                          "services": 9,
 570                          "network": "onion",
 571                          "source": "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion",
 572                          "source_network": "onion"
 573                      },
 574                      {
 575                          "bucket_position": "139/46",
 576                          "address": "nrfj6inpyf73gpkyool35hcmne5zwfmse3jl3aw23vk7chdemalyaqad.onion",
 577                          "services": 9,
 578                          "network": "onion",
 579                          "source": "nrfj6inpyf73gpkyool35hcmne5zwfmse3jl3aw23vk7chdemalyaqad.onion",
 580                          "source_network": "onion",
 581                          "port": 45324,
 582                      }
 583              ]
 584          }
 585  
 586          self.log.debug("Test that getrawaddrman contains information about newly added addresses in each addrman table")
 587          check_getrawaddrman_entries(expected)
 588  
 589  
 590  if __name__ == '__main__':
 591      NetTest(__file__).main()
 592