Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/mininode.py
Show First 20 Lines • Show All 228 Lines • ▼ Show 20 Lines | class P2PConnection(asyncio.Protocol): | ||||
def send_message(self, message): | def send_message(self, message): | ||||
"""Send a P2P message over the socket. | """Send a P2P message over the socket. | ||||
This method takes a P2P payload, builds the P2P header and adds | This method takes a P2P payload, builds the P2P header and adds | ||||
the message to the send buffer to be sent over the socket.""" | the message to the send buffer to be sent over the socket.""" | ||||
if not self.is_connected: | if not self.is_connected: | ||||
raise IOError('Not connected') | raise IOError('Not connected') | ||||
tmsg = self.build_message(message) | |||||
self._log_message("send", message) | self._log_message("send", message) | ||||
tmsg = self._build_message(message) | return self.send_raw_message(tmsg) | ||||
self.send_raw_message(tmsg) | |||||
def send_raw_message(self, tmsg): | def send_raw_message(self, raw_message_bytes): | ||||
"""Send any raw message over the socket. | """Send any raw message over the socket. | ||||
This method adds a raw message to the send buffer to be sent over the | This method adds a raw message to the send buffer to be sent over the | ||||
socket.""" | socket.""" | ||||
if not self.is_connected: | if not self.is_connected: | ||||
raise IOError('Not connected') | raise IOError('Not connected') | ||||
def maybe_write(): | def maybe_write(): | ||||
if not self._transport: | if not self._transport: | ||||
return | return | ||||
if self._transport.is_closing(): | if self._transport.is_closing(): | ||||
return | return | ||||
self._transport.write(tmsg) | self._transport.write(raw_message_bytes) | ||||
NetworkThread.network_event_loop.call_soon_threadsafe(maybe_write) | NetworkThread.network_event_loop.call_soon_threadsafe(maybe_write) | ||||
# Class utility methods | # Class utility methods | ||||
def _build_message(self, message): | def build_message(self, message): | ||||
"""Build a serialized P2P message""" | """Build a serialized P2P message""" | ||||
command = message.command | command = message.command | ||||
data = message.serialize() | data = message.serialize() | ||||
tmsg = MAGIC_BYTES[self.network] | tmsg = MAGIC_BYTES[self.network] | ||||
tmsg += command | tmsg += command | ||||
tmsg += b"\x00" * (12 - len(command)) | tmsg += b"\x00" * (12 - len(command)) | ||||
tmsg += struct.pack("<I", len(data)) | tmsg += struct.pack("<I", len(data)) | ||||
th = sha256(data) | th = sha256(data) | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | def wait_for_inv(self, expected_inv, timeout=60): | ||||
wait_until(test_function, timeout=timeout, lock=mininode_lock) | wait_until(test_function, timeout=timeout, lock=mininode_lock) | ||||
def wait_for_verack(self, timeout=60): | def wait_for_verack(self, timeout=60): | ||||
def test_function(): return self.message_count["verack"] | def test_function(): return self.message_count["verack"] | ||||
wait_until(test_function, timeout=timeout, lock=mininode_lock) | wait_until(test_function, timeout=timeout, lock=mininode_lock) | ||||
# Message sending helper functions | # Message sending helper functions | ||||
def send_and_ping(self, message): | def send_and_ping(self, message, timeout=60): | ||||
self.send_message(message) | self.send_message(message) | ||||
self.sync_with_ping() | self.sync_with_ping(timeout=timeout) | ||||
# Sync up with the node | # Sync up with the node | ||||
def sync_with_ping(self, timeout=60): | def sync_with_ping(self, timeout=60): | ||||
self.send_message(msg_ping(nonce=self.ping_counter)) | self.send_message(msg_ping(nonce=self.ping_counter)) | ||||
def test_function(): | def test_function(): | ||||
if not self.last_message.get("pong"): | if not self.last_message.get("pong"): | ||||
return False | return False | ||||
▲ Show 20 Lines • Show All 180 Lines • Show Last 20 Lines |