|  | 
| 4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | 
| 5 | 5 | """Test node responses to invalid network messages.""" | 
| 6 | 6 | 
 | 
|  | 7 | +import random | 
|  | 8 | + | 
| 7 | 9 | 
 | 
| 8 | 10 | from test_framework.messages import ( | 
| 9 | 11 |     CBlockHeader, | 
| @@ -61,7 +63,7 @@ def run_test(self): | 
| 61 | 63 |         self.test_oversized_inv_msg() | 
| 62 | 64 |         self.test_oversized_getdata_msg() | 
| 63 | 65 |         self.test_oversized_headers_msg() | 
| 64 |  | -        self.test_resource_exhaustion() | 
|  | 66 | +        self.test_noncontinuous_headers_msg() | 
| 65 | 67 | 
 | 
| 66 | 68 |     def test_buffer(self): | 
| 67 | 69 |         self.log.info("Test message with header split across two buffers is received") | 
| @@ -186,6 +188,25 @@ def test_oversized_headers2_msg(self): | 
| 186 | 188 |         size = MAX_HEADERS_COMPRESSED_RESULT + 1 | 
| 187 | 189 |         self.test_oversized_msg(msg_headers2([CBlockHeader()] * size), size) | 
| 188 | 190 | 
 | 
|  | 191 | +    def test_noncontinuous_headers_msg(self): | 
|  | 192 | +        self.log.info("Test headers message with non-continuous headers sequence is logged as misbehaving") | 
|  | 193 | +        block_hashes = self.generate(self.nodes[0], 10) | 
|  | 194 | +        block_headers = [] | 
|  | 195 | +        for block_hash in block_hashes: | 
|  | 196 | +            block_headers.append(from_hex(CBlockHeader(), self.nodes[0].getblockheader(block_hash, False))) | 
|  | 197 | + | 
|  | 198 | +        # continuous headers sequence should be fine | 
|  | 199 | +        MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS = ['Misbehaving', 'non-continuous headers sequence'] | 
|  | 200 | +        peer = self.nodes[0].add_p2p_connection(P2PInterface()) | 
|  | 201 | +        with self.nodes[0].assert_debug_log([], unexpected_msgs=MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS): | 
|  | 202 | +            peer.send_and_ping(msg_headers(block_headers)) | 
|  | 203 | + | 
|  | 204 | +        # delete arbitrary block header somewhere in the middle to break link | 
|  | 205 | +        del block_headers[random.randrange(1, len(block_headers)-1)] | 
|  | 206 | +        with self.nodes[0].assert_debug_log(expected_msgs=MISBEHAVING_NONCONTINUOUS_HEADERS_MSGS): | 
|  | 207 | +            peer.send_and_ping(msg_headers(block_headers)) | 
|  | 208 | +        self.nodes[0].disconnect_p2ps() | 
|  | 209 | + | 
| 189 | 210 |     def test_resource_exhaustion(self): | 
| 190 | 211 |         self.log.info("Test node stays up despite many large junk messages") | 
| 191 | 212 |         # Don't use v2 here - the non-optimised encryption would take too long to encrypt | 
|  | 
0 commit comments