|
5 | 5 | """
|
6 | 6 | Test v2 transport
|
7 | 7 | """
|
| 8 | +import socket |
8 | 9 |
|
9 | 10 | from test_framework.messages import NODE_P2P_V2
|
| 11 | +from test_framework.p2p import MAGIC_BYTES |
10 | 12 | from test_framework.test_framework import BitcoinTestFramework
|
11 |
| -from test_framework.util import assert_equal |
| 13 | +from test_framework.util import ( |
| 14 | + assert_equal, |
| 15 | + p2p_port, |
| 16 | +) |
| 17 | + |
12 | 18 |
|
13 | 19 | class V2TransportTest(BitcoinTestFramework):
|
14 | 20 | def set_test_params(self):
|
@@ -123,5 +129,25 @@ def run_test(self):
|
123 | 129 | self.sync_all()
|
124 | 130 | assert_equal(self.nodes[4].getblockcount(), 11)
|
125 | 131 |
|
| 132 | + # Check v1 prefix detection |
| 133 | + V1_PREFIX = MAGIC_BYTES["regtest"] + b"version\x00\x00\x00\x00\x00" |
| 134 | + assert_equal(len(V1_PREFIX), 16) |
| 135 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 136 | + num_peers = len(self.nodes[0].getpeerinfo()) |
| 137 | + s.connect(("127.0.0.1", p2p_port(0))) |
| 138 | + self.wait_until(lambda: len(self.nodes[0].getpeerinfo()) == num_peers + 1) |
| 139 | + s.sendall(V1_PREFIX[:-1]) |
| 140 | + assert_equal(self.nodes[0].getpeerinfo()[-1]["transport_protocol_type"], "detecting") |
| 141 | + s.sendall(bytes([V1_PREFIX[-1]])) # send out last prefix byte |
| 142 | + self.wait_until(lambda: self.nodes[0].getpeerinfo()[-1]["transport_protocol_type"] == "v1") |
| 143 | + |
| 144 | + # Check wrong network prefix detection (hits if the next 12 bytes correspond to a v1 version message) |
| 145 | + wrong_network_magic_prefix = MAGIC_BYTES["signet"] + V1_PREFIX[4:] |
| 146 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 147 | + s.connect(("127.0.0.1", p2p_port(0))) |
| 148 | + with self.nodes[0].assert_debug_log("V2 transport error: V1 peer with wrong MessageStart"): |
| 149 | + s.sendall(wrong_network_magic_prefix + b"somepayload") |
| 150 | + |
| 151 | + |
126 | 152 | if __name__ == '__main__':
|
127 | 153 | V2TransportTest().main()
|
0 commit comments