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