Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p-compactblocks.py
Show All 24 Lines | def __init__(self): | ||||
super().__init__() | super().__init__() | ||||
self.last_sendcmpct = [] | self.last_sendcmpct = [] | ||||
self.block_announced = False | self.block_announced = False | ||||
# Store the hashes of blocks we've seen announced. | # Store the hashes of blocks we've seen announced. | ||||
# This is for synchronizing the p2p message traffic, | # This is for synchronizing the p2p message traffic, | ||||
# so we can eg wait until a particular block is announced. | # so we can eg wait until a particular block is announced. | ||||
self.announced_blockhashes = set() | self.announced_blockhashes = set() | ||||
def on_sendcmpct(self, conn, message): | def on_sendcmpct(self, message): | ||||
self.last_sendcmpct.append(message) | self.last_sendcmpct.append(message) | ||||
def on_cmpctblock(self, conn, message): | def on_cmpctblock(self, message): | ||||
self.block_announced = True | self.block_announced = True | ||||
self.last_message["cmpctblock"].header_and_shortids.header.calc_sha256() | self.last_message["cmpctblock"].header_and_shortids.header.calc_sha256() | ||||
self.announced_blockhashes.add( | self.announced_blockhashes.add( | ||||
self.last_message["cmpctblock"].header_and_shortids.header.sha256) | self.last_message["cmpctblock"].header_and_shortids.header.sha256) | ||||
def on_headers(self, conn, message): | def on_headers(self, message): | ||||
self.block_announced = True | self.block_announced = True | ||||
for x in self.last_message["headers"].headers: | for x in self.last_message["headers"].headers: | ||||
x.calc_sha256() | x.calc_sha256() | ||||
self.announced_blockhashes.add(x.sha256) | self.announced_blockhashes.add(x.sha256) | ||||
def on_inv(self, conn, message): | def on_inv(self, message): | ||||
for x in self.last_message["inv"].inv: | for x in self.last_message["inv"].inv: | ||||
if x.type == 2: | if x.type == 2: | ||||
self.block_announced = True | self.block_announced = True | ||||
self.announced_blockhashes.add(x.hash) | self.announced_blockhashes.add(x.hash) | ||||
# Requires caller to hold mininode_lock | # Requires caller to hold mininode_lock | ||||
def received_block_announcement(self): | def received_block_announcement(self): | ||||
return self.block_announced | return self.block_announced | ||||
def clear_block_announcement(self): | def clear_block_announcement(self): | ||||
with mininode_lock: | with mininode_lock: | ||||
self.block_announced = False | self.block_announced = False | ||||
self.last_message.pop("inv", None) | self.last_message.pop("inv", None) | ||||
self.last_message.pop("headers", None) | self.last_message.pop("headers", None) | ||||
self.last_message.pop("cmpctblock", None) | self.last_message.pop("cmpctblock", None) | ||||
def get_headers(self, locator, hashstop): | def get_headers(self, locator, hashstop): | ||||
msg = msg_getheaders() | msg = msg_getheaders() | ||||
msg.locator.vHave = locator | msg.locator.vHave = locator | ||||
msg.hashstop = hashstop | msg.hashstop = hashstop | ||||
self.connection.send_message(msg) | self.send_message(msg) | ||||
def send_header_for_blocks(self, new_blocks): | def send_header_for_blocks(self, new_blocks): | ||||
headers_message = msg_headers() | headers_message = msg_headers() | ||||
headers_message.headers = [CBlockHeader(b) for b in new_blocks] | headers_message.headers = [CBlockHeader(b) for b in new_blocks] | ||||
self.send_message(headers_message) | self.send_message(headers_message) | ||||
def request_headers_and_sync(self, locator, hashstop=0): | def request_headers_and_sync(self, locator, hashstop=0): | ||||
self.clear_block_announcement() | self.clear_block_announcement() | ||||
Show All 10 Lines | def wait_for_block_announcement(self, block_hash, timeout=30): | ||||
wait_until(received_hash, timeout=timeout, lock=mininode_lock) | wait_until(received_hash, timeout=timeout, lock=mininode_lock) | ||||
def send_await_disconnect(self, message, timeout=30): | def send_await_disconnect(self, message, timeout=30): | ||||
"""Sends a message to the node and wait for disconnect. | """Sends a message to the node and wait for disconnect. | ||||
This is used when we want to send a message into the node that we expect | This is used when we want to send a message into the node that we expect | ||||
will get us disconnected, eg an invalid block.""" | will get us disconnected, eg an invalid block.""" | ||||
self.send_message(message) | self.send_message(message) | ||||
wait_until(lambda: not self.connected, | wait_until(lambda: self.state != "connected", | ||||
timeout=timeout, lock=mininode_lock) | timeout=timeout, lock=mininode_lock) | ||||
class CompactBlocksTest(BitcoinTestFramework): | class CompactBlocksTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 2 | self.num_nodes = 2 | ||||
self.extra_args = [[], ["-txindex"]] | self.extra_args = [[], ["-txindex"]] | ||||
▲ Show 20 Lines • Show All 795 Lines • Show Last 20 Lines |