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""" 6from collections import defaultdict 7 8from test_framework.messages import ( 9 CInv, 10 msg_getdata, 11) 12from test_framework.p2p import P2PInterface 13from test_framework.test_framework import BitcoinTestFramework 14 15 16class P2PStoreBlock(P2PInterface): 17 def __init__(self): 18 super().__init__() 19 self.blocks = defaultdict(int) 20 21 def on_block(self, message): 22 message.block.calc_sha256() 23 self.blocks[message.block.sha256] += 1 24 25 26class GetdataTest(BitcoinTestFramework): 27 def set_test_params(self): 28 self.num_nodes = 1 29 30 def run_test(self): 31 p2p_block_store = self.nodes[0].add_p2p_connection(P2PStoreBlock()) 32 33 self.log.info("test that an invalid GETDATA doesn't prevent processing of future messages") 34 35 # Send invalid message and verify that node responds to later ping 36 invalid_getdata = msg_getdata() 37 invalid_getdata.inv.append(CInv(t=0, h=0)) # INV type 0 is invalid. 38 p2p_block_store.send_and_ping(invalid_getdata) 39 40 # Check getdata still works by fetching tip block 41 best_block = int(self.nodes[0].getbestblockhash(), 16) 42 good_getdata = msg_getdata() 43 good_getdata.inv.append(CInv(t=2, h=best_block)) 44 p2p_block_store.send_and_ping(good_getdata) 45 p2p_block_store.wait_until(lambda: p2p_block_store.blocks[best_block] == 1) 46 47 48if __name__ == '__main__': 49 GetdataTest().main() 50