Repository: bitcoin/bitcoin
File: test/functional/rpc_net.py (Python)

Code before:
        addrman_info = node.getaddrmaninfo()
        assert_equal(addrman_info["all_networks"]["tried"], 1)
        assert_equal(addrman_info["all_networks"]["new"], 2)

        self.log.debug("Test that adding an another address to the new table succeeds")
        assert_equal(node.addpeeraddress(address="2.0.0.0", port=8333), {"success": True})
        addrman_info = node.getaddrmaninfo()
        assert_equal(addrman_info["all_networks"]["tried"], 1)
        assert_equal(addrman_info["all_networks"]["new"], 3)
        node.getnodeaddresses(count=0)  # getnodeaddresses re-runs the addrman checks

    def test_sendmsgtopeer(self):
        node = self.nodes[0]

        self.restart_node(0)
        # we want to use a p2p v1 connection here in order to ensure
        # a peer id of zero (a downgrade from v2 to v1 would lead
        # to an increase of the peer id)
        self.connect_nodes(0, 1, peer_advertises_v2=False)

        self.log.info("Test sendmsgtopeer")
        self.log.debug("Send a valid message")
        msg_bytes_before = self.nodes[1].getpeerinfo()[-1]["bytesrecv_per_msg"].get("addr", 0)
        with self.nodes[1].assert_debug_log(expected_msgs=["received: addr"]):
            node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="FFFFFF")
            self.wait_until(lambda: self.nodes[1].getpeerinfo()[-1]["bytesrecv_per_msg"].get("addr", 0) > msg_bytes_before)

        self.log.debug("Test error for sending to non-existing peer")
        assert_raises_rpc_error(-1, "Error: Could not send message to peer", node.sendmsgtopeer, peer_id=100, msg_type="addr", msg="FF")

        self.log.debug("Test that zero-length msg_type is allowed")
        node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="")

        self.log.debug("Test error for msg_type that is too long")
        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")

        self.log.debug("Test that unknown msg_type is allowed")
        node.sendmsgtopeer(peer_id=0, msg_type="unknown", msg="FF")

        self.log.debug("Test that empty msg is allowed")
        node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="FF")

        self.log.debug("Test that oversized messages are allowed, but get us disconnected")
        zero_byte_string = b'\x00' * 4000001
        node.sendmsgtopeer(peer_id=0, msg_type="addr", msg=zero_byte_string.hex())
        self.wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 0, timeout=10)

    def test_getaddrmaninfo(self):
        self.log.info("Test getaddrmaninfo")
        self.restart_node(1, extra_args=["-cjdnsreachable", "-test=addrman"], clear_addrman=True)
        node = self.nodes[1]

Review comment (bug):
Nit: Using `-1` as the index gives the impression that there are multiple entries and the last one is used for some reason that the reader doesn't know about. The `0` index doesn't carry that assumption usually.

```diff
--- a/test/functional/rpc_net.py
+++ b/test/functional/rpc_net.py
@@ -418,10 +418,11 @@ class NetTest(BitcoinTestFramework):
 
         self.log.info("Test sendmsgtopeer")
         self.log.debug("Send a valid message")
-        msg_bytes_before = self.nodes[1].getpeerinfo()[-1]["bytesrecv_per_msg"].get("addr", 0)
+        msg_bytes_before = self.nodes[1].getpeerinfo()[0]["bytesrecv_per_msg"].get("addr", 0)
         with self.nodes[1].assert_debug_log(expected_msgs=["received: addr"]):
             node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="FFFFFF")
-            self.wait_until(lambda: self.nodes[1].getpeerinfo()[-1]["bytesrecv_per_msg"].get("addr", 0) > msg_bytes_before)
+            self.wait_until(lambda: self.nodes[1].getpeerinfo()[0]["bytesrecv_per_msg"].get("addr", 0) > msg_bytes_before)
```

Code after:
        addrman_info = node.getaddrmaninfo()
        assert_equal(addrman_info["all_networks"]["tried"], 1)
        assert_equal(addrman_info["all_networks"]["new"], 2)

        self.log.debug("Test that adding an another address to the new table succeeds")
        assert_equal(node.addpeeraddress(address="2.0.0.0", port=8333), {"success": True})
        addrman_info = node.getaddrmaninfo()
        assert_equal(addrman_info["all_networks"]["tried"], 1)
        assert_equal(addrman_info["all_networks"]["new"], 3)
        node.getnodeaddresses(count=0)  # getnodeaddresses re-runs the addrman checks

    def test_sendmsgtopeer(self):
        node = self.nodes[0]

        self.restart_node(0)
        # we want to use a p2p v1 connection here in order to ensure
        # a peer id of zero (a downgrade from v2 to v1 would lead
        # to an increase of the peer id)
        self.connect_nodes(0, 1, peer_advertises_v2=False)

        self.log.info("Test sendmsgtopeer")
        self.log.debug("Send a valid message")
        msg_bytes_before = node.getpeerinfo()[0]["bytesrecv_per_msg"]["pong"]

        with self.nodes[1].assert_debug_log(expected_msgs=["received: addr"]):
            node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="FFFFFF")
            node.ping()
            self.wait_until(lambda: node.getpeerinfo()[0]["bytesrecv_per_msg"]["pong"] > msg_bytes_before)

        self.log.debug("Test error for sending to non-existing peer")
        assert_raises_rpc_error(-1, "Error: Could not send message to peer", node.sendmsgtopeer, peer_id=100, msg_type="addr", msg="FF")

        self.log.debug("Test that zero-length msg_type is allowed")
        node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="")

        self.log.debug("Test error for msg_type that is too long")
        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")

        self.log.debug("Test that unknown msg_type is allowed")
        node.sendmsgtopeer(peer_id=0, msg_type="unknown", msg="FF")

        self.log.debug("Test that empty msg is allowed")
        node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="FF")

        self.log.debug("Test that oversized messages are allowed, but get us disconnected")
        zero_byte_string = b'\x00' * 4000001
        node.sendmsgtopeer(peer_id=0, msg_type="addr", msg=zero_byte_string.hex())
        self.wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 0, timeout=10)

    def test_getaddrmaninfo(self):
        self.log.info("Test getaddrmaninfo")