Skip to content

Commit 4d50598

Browse files
committed
[tests] Tidy up mininode
Add docstrings and renames some methods. Also removes the redundant NodeConn.readable() method override.
1 parent f2ae6f3 commit 4d50598

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

test/functional/test_framework/mininode.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(self):
7676

7777
# Message receiving methods
7878

79-
def deliver(self, conn, message):
79+
def on_message(self, conn, message):
8080
"""Receive message and dispatch message to appropriate callback.
8181
8282
We keep a count of how many of each message type has been received
@@ -233,12 +233,14 @@ def __init__(self, dstaddr, dstport, callback, net="regtest", services=NODE_NETW
233233
# Connection and disconnection methods
234234

235235
def handle_connect(self):
236+
"""asyncore callback when a connection is opened."""
236237
if self.state != "connected":
237238
logger.debug("Connected & Listening: %s:%d" % (self.dstaddr, self.dstport))
238239
self.state = "connected"
239240
self.cb.on_open(self)
240241

241242
def handle_close(self):
243+
"""asyncore callback when a connection is closed."""
242244
logger.debug("Closing connection to: %s:%d" % (self.dstaddr, self.dstport))
243245
self.state = "closed"
244246
self.recvbuf = b""
@@ -250,24 +252,27 @@ def handle_close(self):
250252
self.cb.on_close(self)
251253

252254
def disconnect_node(self):
253-
""" Disconnect the p2p connection.
255+
"""Disconnect the p2p connection.
254256
255257
Called by the test logic thread. Causes the p2p connection
256258
to be disconnected on the next iteration of the asyncore loop."""
257259
self.disconnect = True
258260

259261
# Socket read methods
260262

261-
def readable(self):
262-
return True
263-
264263
def handle_read(self):
264+
"""asyncore callback when data is read from the socket."""
265265
t = self.recv(8192)
266266
if len(t) > 0:
267267
self.recvbuf += t
268-
self.got_data()
268+
self._on_data()
269+
270+
def _on_data(self):
271+
"""Try to read P2P messages from the recv buffer.
269272
270-
def got_data(self):
273+
This method reads data from the buffer in a loop. It deserializes,
274+
parses and verifies the P2P header, then passes the P2P payload to
275+
the on_message callback for processing."""
271276
try:
272277
while True:
273278
if len(self.recvbuf) < 4:
@@ -292,24 +297,27 @@ def got_data(self):
292297
f = BytesIO(msg)
293298
t = MESSAGEMAP[command]()
294299
t.deserialize(f)
295-
self.got_message(t)
300+
self._log_message("receive", t)
301+
self.on_message(t)
296302
except Exception as e:
297303
logger.exception('Error reading message:', repr(e))
298304
raise
299305

300-
def got_message(self, message):
301-
self._log_message("receive", message)
302-
self.cb.deliver(self, message)
306+
def on_message(self, message):
307+
"""Callback for processing a P2P payload. Calls into NodeConnCB."""
308+
self.cb.on_message(self, message)
303309

304310
# Socket write methods
305311

306312
def writable(self):
313+
"""asyncore method to determine whether the handle_write() callback should be called on the next loop."""
307314
with mininode_lock:
308315
pre_connection = self.state == "connecting"
309316
length = len(self.sendbuf)
310317
return (length > 0 or pre_connection)
311318

312319
def handle_write(self):
320+
"""asyncore callback when data should be written to the socket."""
313321
with mininode_lock:
314322
# asyncore does not expose socket connection, only the first read/write
315323
# event, thus we must check connection manually here to know when we
@@ -327,6 +335,10 @@ def handle_write(self):
327335
self.sendbuf = self.sendbuf[sent:]
328336

329337
def send_message(self, message, pushbuf=False):
338+
"""Send a P2P message over the socket.
339+
340+
This method takes a P2P payload, builds the P2P header and adds
341+
the message to the send buffer to be sent over the socket."""
330342
if self.state != "connected" and not pushbuf:
331343
raise IOError('Not connected, no pushbuf')
332344
self._log_message("send", message)
@@ -353,6 +365,7 @@ def send_message(self, message, pushbuf=False):
353365
# Class utility methods
354366

355367
def _log_message(self, direction, msg):
368+
"""Logs a message being sent or received over the connection."""
356369
if direction == "send":
357370
log_message = "Send message to "
358371
elif direction == "receive":

0 commit comments

Comments
 (0)