p2p_v2_transport.py raw
1 #!/usr/bin/env python3
2 # Copyright (c) 2021-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 """
6 Test v2 transport
7 """
8 import socket
9
10 from test_framework.messages import MAGIC_BYTES, NODE_P2P_V2
11 from test_framework.test_framework import LimenkaTestFramework
12 from test_framework.util import (
13 assert_equal,
14 p2p_port,
15 assert_raises_rpc_error
16 )
17
18
19 class V2TransportTest(LimenkaTestFramework):
20 def set_test_params(self):
21 self.setup_clean_chain = True
22 self.num_nodes = 5
23 self.extra_args = [["-v2transport=1"], ["-v2transport=1"], ["-v2transport=0"], ["-v2transport=0"], ["-v2transport=0"]]
24
25 def run_test(self):
26 sending_handshake = "start sending v2 handshake to peer"
27 downgrading_to_v1 = "retrying with v1 transport protocol for peer"
28 self.disconnect_nodes(0, 1)
29 self.disconnect_nodes(1, 2)
30 self.disconnect_nodes(2, 3)
31 self.disconnect_nodes(3, 4)
32
33 # verify local services
34 network_info = self.nodes[2].getnetworkinfo()
35 assert_equal(int(network_info["localservices"], 16) & NODE_P2P_V2, 0)
36 assert "P2P_V2" not in network_info["localservicesnames"]
37 network_info = self.nodes[1].getnetworkinfo()
38 assert_equal(int(network_info["localservices"], 16) & NODE_P2P_V2, NODE_P2P_V2)
39 assert "P2P_V2" in network_info["localservicesnames"]
40
41 # V2 nodes can sync with V2 nodes
42 assert_equal(self.nodes[0].getblockcount(), 0)
43 assert_equal(self.nodes[1].getblockcount(), 0)
44 with self.nodes[0].assert_debug_log(expected_msgs=[sending_handshake],
45 unexpected_msgs=[downgrading_to_v1]):
46 self.connect_nodes(0, 1, peer_advertises_v2=True)
47 self.generate(self.nodes[0], 5, sync_fun=lambda: self.sync_all(self.nodes[0:2]))
48 assert_equal(self.nodes[1].getblockcount(), 5)
49 # verify there is a v2 connection between node 0 and 1
50 node_0_info = self.nodes[0].getpeerinfo()
51 node_1_info = self.nodes[1].getpeerinfo()
52 assert_equal(len(node_0_info), 1)
53 assert_equal(len(node_1_info), 1)
54 assert_equal(node_0_info[0]["transport_protocol_type"], "v2")
55 assert_equal(node_1_info[0]["transport_protocol_type"], "v2")
56 assert_equal(len(node_0_info[0]["session_id"]), 64)
57 assert_equal(len(node_1_info[0]["session_id"]), 64)
58 assert_equal(node_0_info[0]["session_id"], node_1_info[0]["session_id"])
59
60 # V1 nodes can sync with each other
61 assert_equal(self.nodes[2].getblockcount(), 0)
62 assert_equal(self.nodes[3].getblockcount(), 0)
63
64 # addnode rpc error when v2transport requested but not enabled
65 ip_port = "127.0.0.1:{}".format(p2p_port(3))
66 assert_raises_rpc_error(-8, "Error: v2transport requested but not enabled (see -v2transport)", self.nodes[2].addnode, node=ip_port, command='add', v2transport=True)
67
68 with self.nodes[2].assert_debug_log(expected_msgs=[],
69 unexpected_msgs=[sending_handshake, downgrading_to_v1]):
70 self.connect_nodes(2, 3, peer_advertises_v2=False)
71 self.generate(self.nodes[2], 8, sync_fun=lambda: self.sync_all(self.nodes[2:4]))
72 assert_equal(self.nodes[3].getblockcount(), 8)
73 assert self.nodes[0].getbestblockhash() != self.nodes[2].getbestblockhash()
74 # verify there is a v1 connection between node 2 and 3
75 node_2_info = self.nodes[2].getpeerinfo()
76 node_3_info = self.nodes[3].getpeerinfo()
77 assert_equal(len(node_2_info), 1)
78 assert_equal(len(node_3_info), 1)
79 assert_equal(node_2_info[0]["transport_protocol_type"], "v1")
80 assert_equal(node_3_info[0]["transport_protocol_type"], "v1")
81 assert_equal(len(node_2_info[0]["session_id"]), 0)
82 assert_equal(len(node_3_info[0]["session_id"]), 0)
83
84 # V1 nodes can sync with V2 nodes
85 self.disconnect_nodes(0, 1)
86 self.disconnect_nodes(2, 3)
87 with self.nodes[2].assert_debug_log(expected_msgs=[],
88 unexpected_msgs=[sending_handshake, downgrading_to_v1]):
89 self.connect_nodes(2, 1, peer_advertises_v2=False) # cannot enable v2 on v1 node
90 self.sync_all(self.nodes[1:3])
91 assert_equal(self.nodes[1].getblockcount(), 8)
92 assert self.nodes[0].getbestblockhash() != self.nodes[1].getbestblockhash()
93 # verify there is a v1 connection between node 1 and 2
94 node_1_info = self.nodes[1].getpeerinfo()
95 node_2_info = self.nodes[2].getpeerinfo()
96 assert_equal(len(node_1_info), 1)
97 assert_equal(len(node_2_info), 1)
98 assert_equal(node_1_info[0]["transport_protocol_type"], "v1")
99 assert_equal(node_2_info[0]["transport_protocol_type"], "v1")
100 assert_equal(len(node_1_info[0]["session_id"]), 0)
101 assert_equal(len(node_2_info[0]["session_id"]), 0)
102
103 # V2 nodes can sync with V1 nodes
104 self.disconnect_nodes(1, 2)
105 with self.nodes[0].assert_debug_log(expected_msgs=[],
106 unexpected_msgs=[sending_handshake, downgrading_to_v1]):
107 self.connect_nodes(0, 3, peer_advertises_v2=False)
108 self.sync_all([self.nodes[0], self.nodes[3]])
109 assert_equal(self.nodes[0].getblockcount(), 8)
110 # verify there is a v1 connection between node 0 and 3
111 node_0_info = self.nodes[0].getpeerinfo()
112 node_3_info = self.nodes[3].getpeerinfo()
113 assert_equal(len(node_0_info), 1)
114 assert_equal(len(node_3_info), 1)
115 assert_equal(node_0_info[0]["transport_protocol_type"], "v1")
116 assert_equal(node_3_info[0]["transport_protocol_type"], "v1")
117 assert_equal(len(node_0_info[0]["session_id"]), 0)
118 assert_equal(len(node_3_info[0]["session_id"]), 0)
119
120 # V2 node mines another block and everyone gets it
121 self.connect_nodes(0, 1, peer_advertises_v2=True)
122 self.connect_nodes(1, 2, peer_advertises_v2=False)
123 self.generate(self.nodes[1], 1, sync_fun=lambda: self.sync_all(self.nodes[0:4]))
124 assert_equal(self.nodes[0].getblockcount(), 9) # sync_all() verifies tip hashes match
125
126 # V1 node mines another block and everyone gets it
127 self.generate(self.nodes[3], 2, sync_fun=lambda: self.sync_all(self.nodes[0:4]))
128 assert_equal(self.nodes[2].getblockcount(), 11) # sync_all() verifies tip hashes match
129
130 assert_equal(self.nodes[4].getblockcount(), 0)
131 # Peer 4 is v1 p2p, but is falsely advertised as v2.
132 with self.nodes[1].assert_debug_log(expected_msgs=[sending_handshake, downgrading_to_v1]):
133 self.connect_nodes(1, 4, peer_advertises_v2=True)
134 self.sync_all()
135 assert_equal(self.nodes[4].getblockcount(), 11)
136
137 # Check v1 prefix detection
138 V1_PREFIX = MAGIC_BYTES["regtest"] + b"version\x00\x00\x00\x00\x00"
139 assert_equal(len(V1_PREFIX), 16)
140 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
141 with self.nodes[0].wait_for_new_peer():
142 s.connect(("127.0.0.1", p2p_port(0)))
143 s.sendall(V1_PREFIX[:-1])
144 assert_equal(self.nodes[0].getpeerinfo()[-1]["transport_protocol_type"], "detecting")
145 s.sendall(bytes([V1_PREFIX[-1]])) # send out last prefix byte
146 self.wait_until(lambda: self.nodes[0].getpeerinfo()[-1]["transport_protocol_type"] == "v1")
147
148 # Check wrong network prefix detection (hits if the next 12 bytes correspond to a v1 version message)
149 wrong_network_magic_prefix = MAGIC_BYTES["signet"] + V1_PREFIX[4:]
150 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
151 with self.nodes[0].wait_for_new_peer():
152 s.connect(("127.0.0.1", p2p_port(0)))
153 with self.nodes[0].assert_debug_log(["V2 transport error: V1 peer with wrong MessageStart"]):
154 s.sendall(wrong_network_magic_prefix + b"somepayload")
155
156 # Check detection of missing garbage terminator (hits after fixed amount of data if terminator never matches garbage)
157 MAX_KEY_GARB_AND_GARBTERM_LEN = 64 + 4095 + 16
158 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
159 with self.nodes[0].wait_for_new_peer():
160 s.connect(("127.0.0.1", p2p_port(0)))
161 s.sendall(b'\x00' * (MAX_KEY_GARB_AND_GARBTERM_LEN - 1))
162 self.wait_until(lambda: self.nodes[0].getpeerinfo()[-1]["bytesrecv"] == MAX_KEY_GARB_AND_GARBTERM_LEN - 1)
163 with self.nodes[0].assert_debug_log(["V2 transport error: missing garbage terminator"]):
164 peer_id = self.nodes[0].getpeerinfo()[-1]["id"]
165 s.sendall(b'\x00') # send out last byte
166 # should disconnect immediately
167 self.wait_until(lambda: not peer_id in [p["id"] for p in self.nodes[0].getpeerinfo()])
168
169
170 if __name__ == '__main__':
171 V2TransportTest(__file__).main()
172