Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/mininode.py
Show First 20 Lines • Show All 1,369 Lines • ▼ Show 20 Lines | class NodeConnCB(): | ||||
# Message sending helper functions | # Message sending helper functions | ||||
def send_message(self, message): | def send_message(self, message): | ||||
if self.connection: | if self.connection: | ||||
self.connection.send_message(message) | self.connection.send_message(message) | ||||
else: | else: | ||||
logger.error("Cannot send message. No connection to node!") | logger.error("Cannot send message. No connection to node!") | ||||
def send_raw_message(self, message): | |||||
if self.connection: | |||||
self.connection.send_raw_message(message) | |||||
else: | |||||
logger.error("Cannot send raw message. No connection to node!") | |||||
def send_and_ping(self, message): | def send_and_ping(self, message): | ||||
self.send_message(message) | self.send_message(message) | ||||
self.sync_with_ping() | self.sync_with_ping() | ||||
# 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)) | ||||
▲ Show 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | def got_data(self): | ||||
f = BytesIO(msg) | f = BytesIO(msg) | ||||
m = self.messagemap[command]() | m = self.messagemap[command]() | ||||
m.deserialize(f) | m.deserialize(f) | ||||
return m | return m | ||||
except Exception as e: | except Exception as e: | ||||
logger.exception('Error reading message:', repr(e)) | logger.exception('Error reading message:', repr(e)) | ||||
raise | raise | ||||
def send_message(self, message, pushbuf=False): | def format_message(self, message): | ||||
if self.state != "connected" and not pushbuf: | |||||
raise IOError('Not connected, no pushbuf') | |||||
self._log_message("send", message) | |||||
command = message.command | command = message.command | ||||
data = message.serialize() | data = message.serialize() | ||||
tmsg = self.MAGIC_BYTES[self.network] | tmsg = self.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) | ||||
h = sha256(th) | h = sha256(th) | ||||
tmsg += h[:4] | tmsg += h[:4] | ||||
tmsg += data | tmsg += data | ||||
return tmsg | |||||
def send_message(self, message, pushbuf=False): | |||||
self._log_message("send", message) | |||||
tmsg = self.format_message(message) | |||||
self.send_raw_message(tmsg, pushbuf) | |||||
def send_raw_message(self, tmsg, pushbuf=False): | |||||
if self.state != "connected" and not pushbuf: | |||||
raise IOError('Not connected, no pushbuf') | |||||
with mininode_lock: | with mininode_lock: | ||||
if (len(self.sendbuf) == 0 and not pushbuf): | if (len(self.sendbuf) == 0 and not pushbuf): | ||||
try: | try: | ||||
sent = self.send(tmsg) | sent = self.send(tmsg) | ||||
self.sendbuf = tmsg[sent:] | self.sendbuf = tmsg[sent:] | ||||
except BlockingIOError: | except BlockingIOError: | ||||
self.sendbuf = tmsg | self.sendbuf = tmsg | ||||
else: | else: | ||||
Show All 38 Lines |