4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test message sending before handshake completion.
6
6
7
- A node should never send anything other than VERSION/VERACK until it's
8
- received a VERACK .
7
+ Before receiving a VERACK, a node should not send anything but VERSION/VERACK
8
+ and feature negotiation messages (WTXIDRELAY, SENDADDRV2) .
9
9
10
10
This test connects to a node and sends it a few messages, trying to entice it
11
11
into sending us something it shouldn't."""
@@ -35,10 +35,12 @@ def __init__(self):
35
35
super ().__init__ ()
36
36
self .unexpected_msg = False
37
37
self .ever_connected = False
38
+ self .got_wtxidrelay = False
39
+ self .got_sendaddrv2 = False
38
40
39
41
def bad_message (self , message ):
40
42
self .unexpected_msg = True
41
- self . log . info ("should not have received message: %s" % message .msgtype )
43
+ print ("should not have received message: %s" % message .msgtype )
42
44
43
45
def on_open (self ):
44
46
self .ever_connected = True
@@ -64,6 +66,8 @@ def on_sendcmpct(self, message): self.bad_message(message)
64
66
def on_cmpctblock (self , message ): self .bad_message (message )
65
67
def on_getblocktxn (self , message ): self .bad_message (message )
66
68
def on_blocktxn (self , message ): self .bad_message (message )
69
+ def on_wtxidrelay (self , message ): self .got_wtxidrelay = True
70
+ def on_sendaddrv2 (self , message ): self .got_sendaddrv2 = True
67
71
68
72
69
73
# Peer that sends a version but not a verack.
@@ -94,32 +98,61 @@ def on_version(self, msg):
94
98
class P2PLeakTest (BitcoinTestFramework ):
95
99
def set_test_params (self ):
96
100
self .num_nodes = 1
101
+ self .extra_args = [['-peertimeout=4' ]]
102
+
103
+ def create_old_version (self , nversion ):
104
+ old_version_msg = msg_version ()
105
+ old_version_msg .nVersion = nversion
106
+ old_version_msg .strSubVer = P2P_SUBVERSION
107
+ old_version_msg .nServices = P2P_SERVICES
108
+ old_version_msg .relay = P2P_VERSION_RELAY
109
+ return old_version_msg
97
110
98
111
def run_test (self ):
99
- # Another peer that never sends a version, nor any other messages. It shouldn't receive anything from the node.
112
+ self .log .info ('Check that the node doesn\' t send unexpected messages before handshake completion' )
113
+ # Peer that never sends a version, nor any other messages. It shouldn't receive anything from the node.
100
114
no_version_idle_peer = self .nodes [0 ].add_p2p_connection (LazyPeer (), send_version = False , wait_for_verack = False )
101
115
102
116
# Peer that sends a version but not a verack.
103
117
no_verack_idle_peer = self .nodes [0 ].add_p2p_connection (NoVerackIdlePeer (), wait_for_verack = False )
104
118
105
- # Wait until we got the verack in response to the version. Though, don't wait for the node to receive the
106
- # verack, since we never sent one
119
+ # Pre-wtxidRelay peer that sends a version but not a verack and does not support feature negotiation
120
+ # messages which start at nVersion == 70016
121
+ pre_wtxidrelay_peer = self .nodes [0 ].add_p2p_connection (NoVerackIdlePeer (), send_version = False , wait_for_verack = False )
122
+ pre_wtxidrelay_peer .send_message (self .create_old_version (70015 ))
123
+
124
+ # Wait until the peer gets the verack in response to the version. Though, don't wait for the node to receive the
125
+ # verack, since the peer never sent one
107
126
no_verack_idle_peer .wait_for_verack ()
127
+ pre_wtxidrelay_peer .wait_for_verack ()
108
128
109
129
no_version_idle_peer .wait_until (lambda : no_version_idle_peer .ever_connected )
110
130
no_verack_idle_peer .wait_until (lambda : no_verack_idle_peer .version_received )
131
+ pre_wtxidrelay_peer .wait_until (lambda : pre_wtxidrelay_peer .version_received )
111
132
112
133
# Mine a block and make sure that it's not sent to the connected peers
113
134
self .nodes [0 ].generate (nblocks = 1 )
114
135
115
- #Give the node enough time to possibly leak out a message
136
+ # Give the node enough time to possibly leak out a message
116
137
time .sleep (5 )
117
138
118
- self .nodes [0 ].disconnect_p2ps ()
139
+ # Make sure only expected messages came in
140
+ assert not no_version_idle_peer .unexpected_msg
141
+ assert not no_version_idle_peer .got_wtxidrelay
142
+ assert not no_version_idle_peer .got_sendaddrv2
119
143
120
- # Make sure no unexpected messages came in
121
- assert no_version_idle_peer .unexpected_msg == False
122
- assert no_verack_idle_peer .unexpected_msg == False
144
+ assert not no_verack_idle_peer .unexpected_msg
145
+ assert no_verack_idle_peer .got_wtxidrelay
146
+ assert no_verack_idle_peer .got_sendaddrv2
147
+
148
+ assert not pre_wtxidrelay_peer .unexpected_msg
149
+ assert not pre_wtxidrelay_peer .got_wtxidrelay
150
+ assert not pre_wtxidrelay_peer .got_sendaddrv2
151
+
152
+ # Expect peers to be disconnected due to timeout
153
+ assert not no_version_idle_peer .is_connected
154
+ assert not no_verack_idle_peer .is_connected
155
+ assert not pre_wtxidrelay_peer .is_connected
123
156
124
157
self .log .info ('Check that the version message does not leak the local address of the node' )
125
158
p2p_version_store = self .nodes [0 ].add_p2p_connection (P2PVersionStore ())
@@ -134,13 +167,8 @@ def run_test(self):
134
167
135
168
self .log .info ('Check that old peers are disconnected' )
136
169
p2p_old_peer = self .nodes [0 ].add_p2p_connection (P2PInterface (), send_version = False , wait_for_verack = False )
137
- old_version_msg = msg_version ()
138
- old_version_msg .nVersion = 31799
139
- old_version_msg .strSubVer = P2P_SUBVERSION
140
- old_version_msg .nServices = P2P_SERVICES
141
- old_version_msg .relay = P2P_VERSION_RELAY
142
- with self .nodes [0 ].assert_debug_log (['peer=3 using obsolete version 31799; disconnecting' ]):
143
- p2p_old_peer .send_message (old_version_msg )
170
+ with self .nodes [0 ].assert_debug_log (['peer=4 using obsolete version 31799; disconnecting' ]):
171
+ p2p_old_peer .send_message (self .create_old_version (31799 ))
144
172
p2p_old_peer .wait_for_disconnect ()
145
173
146
174
0 commit comments