|
3 | 3 | # Distributed under the MIT software license, see the accompanying
|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Test node responses to invalid network messages."""
|
| 6 | +import asyncio |
6 | 7 | import os
|
7 | 8 | import struct
|
8 | 9 |
|
9 | 10 | from test_framework import messages
|
10 |
| -from test_framework.mininode import P2PDataStore |
| 11 | +from test_framework.mininode import P2PDataStore, NetworkThread |
11 | 12 | from test_framework.test_framework import BitcoinTestFramework
|
12 | 13 |
|
13 | 14 |
|
@@ -143,8 +144,15 @@ def run_test(self):
|
143 | 144 |
|
144 | 145 | def test_magic_bytes(self):
|
145 | 146 | conn = self.nodes[0].add_p2p_connection(P2PDataStore())
|
146 |
| - conn._on_data = lambda: None # Need to ignore all incoming messages from now, since they come with "invalid" magic bytes |
147 |
| - conn.magic_bytes = b'\x00\x11\x22\x32' |
| 147 | + |
| 148 | + def swap_magic_bytes(): |
| 149 | + conn._on_data = lambda: None # Need to ignore all incoming messages from now, since they come with "invalid" magic bytes |
| 150 | + conn.magic_bytes = b'\x00\x11\x22\x32' |
| 151 | + |
| 152 | + # Call .result() to block until the atomic swap is complete, otherwise |
| 153 | + # we might run into races later on |
| 154 | + asyncio.run_coroutine_threadsafe(asyncio.coroutine(swap_magic_bytes)(), NetworkThread.network_event_loop).result() |
| 155 | + |
148 | 156 | with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART ping']):
|
149 | 157 | conn.send_message(messages.msg_ping(nonce=0xff))
|
150 | 158 | conn.wait_for_disconnect(timeout=1)
|
|
0 commit comments