2020-05-12 02:34:46 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2020 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
"""Test GETDATA processing behavior"""
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
from test_framework.messages import (
|
|
|
|
CInv,
|
|
|
|
msg_getdata,
|
|
|
|
)
|
2024-01-15 20:35:29 +01:00
|
|
|
from test_framework.p2p import P2PInterface
|
2020-05-12 02:34:46 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
|
|
|
|
|
2020-05-26 18:27:21 +02:00
|
|
|
class P2PStoreBlock(P2PInterface):
|
2020-05-12 02:34:46 +02:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.blocks = defaultdict(int)
|
|
|
|
|
|
|
|
def on_block(self, message):
|
|
|
|
message.block.calc_sha256()
|
|
|
|
self.blocks[message.block.sha256] += 1
|
|
|
|
|
2020-05-26 18:27:21 +02:00
|
|
|
|
2020-05-12 02:34:46 +02:00
|
|
|
class GetdataTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.num_nodes = 1
|
|
|
|
|
|
|
|
def run_test(self):
|
2020-05-26 18:27:21 +02:00
|
|
|
p2p_block_store = self.nodes[0].add_p2p_connection(P2PStoreBlock())
|
2020-05-12 02:34:46 +02:00
|
|
|
|
|
|
|
self.log.info("test that an invalid GETDATA doesn't prevent processing of future messages")
|
|
|
|
|
|
|
|
# Send invalid message and verify that node responds to later ping
|
|
|
|
invalid_getdata = msg_getdata()
|
|
|
|
invalid_getdata.inv.append(CInv(t=0, h=0)) # INV type 0 is invalid.
|
2020-05-26 18:27:21 +02:00
|
|
|
p2p_block_store.send_and_ping(invalid_getdata)
|
2020-05-12 02:34:46 +02:00
|
|
|
|
|
|
|
# Check getdata still works by fetching tip block
|
|
|
|
best_block = int(self.nodes[0].getbestblockhash(), 16)
|
|
|
|
good_getdata = msg_getdata()
|
|
|
|
good_getdata.inv.append(CInv(t=2, h=best_block))
|
2020-05-26 18:27:21 +02:00
|
|
|
p2p_block_store.send_and_ping(good_getdata)
|
2020-09-26 12:55:16 +02:00
|
|
|
p2p_block_store.wait_until(lambda: p2p_block_store.blocks[best_block] == 1)
|
2020-05-26 18:27:21 +02:00
|
|
|
|
2020-05-12 02:34:46 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
GetdataTest().main()
|