Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/p2p.py
Show First 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | from test_framework.messages import ( | ||||
msg_tx, | msg_tx, | ||||
MSG_TX, | MSG_TX, | ||||
MSG_TYPE_MASK, | MSG_TYPE_MASK, | ||||
msg_verack, | msg_verack, | ||||
msg_version, | msg_version, | ||||
NODE_NETWORK, | NODE_NETWORK, | ||||
sha256, | sha256, | ||||
) | ) | ||||
from test_framework.util import wait_until | from test_framework.util import wait_until_helper | ||||
logger = logging.getLogger("TestFramework.p2p") | logger = logging.getLogger("TestFramework.p2p") | ||||
MESSAGEMAP = { | MESSAGEMAP = { | ||||
b"addr": msg_addr, | b"addr": msg_addr, | ||||
b"addrv2": msg_addrv2, | b"addrv2": msg_addrv2, | ||||
b"avapoll": msg_avapoll, | b"avapoll": msg_avapoll, | ||||
b"avaproof": msg_avaproof, | b"avaproof": msg_avaproof, | ||||
▲ Show 20 Lines • Show All 245 Lines • ▼ Show 20 Lines | def __init__(self, support_addrv2=False): | ||||
super().__init__() | super().__init__() | ||||
# Track number of messages of each type received. | # Track number of messages of each type received. | ||||
# Should be read-only in a test. | # Should be read-only in a test. | ||||
self.message_count = defaultdict(int) | self.message_count = defaultdict(int) | ||||
# Track the most recent message of each type. | # Track the most recent message of each type. | ||||
# To wait for a message to be received, pop that message from | # To wait for a message to be received, pop that message from | ||||
# this and use wait_until. | # this and use self.wait_until. | ||||
self.last_message = {} | self.last_message = {} | ||||
# A count of the number of ping messages we've sent to the node | # A count of the number of ping messages we've sent to the node | ||||
self.ping_counter = 1 | self.ping_counter = 1 | ||||
# The network services received from the peer | # The network services received from the peer | ||||
self.nServices = 0 | self.nServices = 0 | ||||
▲ Show 20 Lines • Show All 130 Lines • ▼ Show 20 Lines | class P2PInterface(P2PConnection): | ||||
def wait_until(self, test_function_in, *, timeout=60, | def wait_until(self, test_function_in, *, timeout=60, | ||||
check_connected=True): | check_connected=True): | ||||
def test_function(): | def test_function(): | ||||
if check_connected: | if check_connected: | ||||
assert self.is_connected | assert self.is_connected | ||||
return test_function_in() | return test_function_in() | ||||
wait_until(test_function, timeout=timeout, lock=p2p_lock, | wait_until_helper(test_function, timeout=timeout, lock=p2p_lock, | ||||
timeout_factor=self.timeout_factor) | timeout_factor=self.timeout_factor) | ||||
def wait_for_disconnect(self, timeout=60): | def wait_for_disconnect(self, timeout=60): | ||||
def test_function(): return not self.is_connected | def test_function(): return not self.is_connected | ||||
self.wait_until(test_function, timeout=timeout, check_connected=False) | self.wait_until(test_function, timeout=timeout, check_connected=False) | ||||
# Message receiving helper methods | # Message receiving helper methods | ||||
def wait_for_tx(self, txid, timeout=60): | def wait_for_tx(self, txid, timeout=60): | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | class NetworkThread(threading.Thread): | ||||
def run(self): | def run(self): | ||||
"""Start the network thread.""" | """Start the network thread.""" | ||||
self.network_event_loop.run_forever() | self.network_event_loop.run_forever() | ||||
def close(self, timeout=10): | def close(self, timeout=10): | ||||
"""Close the connections and network event loop.""" | """Close the connections and network event loop.""" | ||||
self.network_event_loop.call_soon_threadsafe( | self.network_event_loop.call_soon_threadsafe( | ||||
self.network_event_loop.stop) | self.network_event_loop.stop) | ||||
wait_until(lambda: not self.network_event_loop.is_running(), | wait_until_helper(lambda: not self.network_event_loop.is_running(), | ||||
timeout=timeout) | timeout=timeout) | ||||
self.network_event_loop.close() | self.network_event_loop.close() | ||||
self.join(timeout) | self.join(timeout) | ||||
# Safe to remove event loop. | # Safe to remove event loop. | ||||
NetworkThread.network_event_loop = None | NetworkThread.network_event_loop = None | ||||
class P2PDataStore(P2PInterface): | class P2PDataStore(P2PInterface): | ||||
"""A P2P data store class. | """A P2P data store class. | ||||
▲ Show 20 Lines • Show All 177 Lines • Show Last 20 Lines |