|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2020 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test GETDATA processing behavior""" |
| 6 | +from collections import defaultdict |
| 7 | + |
| 8 | +from test_framework.messages import ( |
| 9 | + CInv, |
| 10 | + msg_getdata, |
| 11 | +) |
| 12 | +from test_framework.mininode import ( |
| 13 | + mininode_lock, |
| 14 | + P2PInterface, |
| 15 | +) |
| 16 | +from test_framework.test_framework import BitcoinTestFramework |
| 17 | +from test_framework.util import wait_until |
| 18 | + |
| 19 | +class P2PStoreBlock(P2PInterface): |
| 20 | + |
| 21 | + def __init__(self): |
| 22 | + super().__init__() |
| 23 | + self.blocks = defaultdict(int) |
| 24 | + |
| 25 | + def on_block(self, message): |
| 26 | + message.block.calc_sha256() |
| 27 | + self.blocks[message.block.sha256] += 1 |
| 28 | + |
| 29 | +class GetdataTest(BitcoinTestFramework): |
| 30 | + def set_test_params(self): |
| 31 | + self.num_nodes = 1 |
| 32 | + |
| 33 | + def run_test(self): |
| 34 | + self.nodes[0].add_p2p_connection(P2PStoreBlock()) |
| 35 | + |
| 36 | + self.log.info("test that an invalid GETDATA doesn't prevent processing of future messages") |
| 37 | + |
| 38 | + # Send invalid message and verify that node responds to later ping |
| 39 | + invalid_getdata = msg_getdata() |
| 40 | + invalid_getdata.inv.append(CInv(t=0, h=0)) # INV type 0 is invalid. |
| 41 | + self.nodes[0].p2ps[0].send_and_ping(invalid_getdata) |
| 42 | + |
| 43 | + # Check getdata still works by fetching tip block |
| 44 | + best_block = int(self.nodes[0].getbestblockhash(), 16) |
| 45 | + good_getdata = msg_getdata() |
| 46 | + good_getdata.inv.append(CInv(t=2, h=best_block)) |
| 47 | + self.nodes[0].p2ps[0].send_and_ping(good_getdata) |
| 48 | + wait_until(lambda: self.nodes[0].p2ps[0].blocks[best_block] == 1, timeout=30, lock=mininode_lock) |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + GetdataTest().main() |
0 commit comments