rpc_net.py raw

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