From 2904e301c5b5bae917f5c199dbabee415915debb Mon Sep 17 00:00:00 2001 From: John Newbery Date: Mon, 16 Oct 2017 22:31:18 -0400 Subject: [PATCH 1/4] [tests] Remove dead code from mininode.py Removes the dead deliver_sleep_time and EarlyDisconnectError code --- test/functional/test_framework/mininode.py | 37 +++------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/test/functional/test_framework/mininode.py b/test/functional/test_framework/mininode.py index 3e751f0f3..49dc644ac 100755 --- a/test/functional/test_framework/mininode.py +++ b/test/functional/test_framework/mininode.py @@ -1458,9 +1458,7 @@ class NodeConnCB(): """Callback and helper functions for P2P connection to a bitcoind node. Individual testcases should subclass this and override the on_* methods - if they want to alter message handling behaviour. - """ - + if they want to alter message handling behaviour.""" def __init__(self): # Track whether we have a P2P connection open to the node self.connected = False @@ -1474,25 +1472,13 @@ class NodeConnCB(): # A count of the number of ping messages we've sent to the node self.ping_counter = 1 - # deliver_sleep_time is helpful for debugging race conditions in p2p - # tests; it causes message delivery to sleep for the specified time - # before acquiring the global lock and delivering the next message. - self.deliver_sleep_time = None - # Message receiving methods def deliver(self, conn, message): """Receive message and dispatch message to appropriate callback. We keep a count of how many of each message type has been received - and the most recent message of each type. - - Optionally waits for deliver_sleep_time before dispatching message. - """ - - deliver_sleep = self.get_deliver_sleep_time() - if deliver_sleep is not None: - time.sleep(deliver_sleep) + and the most recent message of each type.""" with mininode_lock: try: command = message.command.decode('ascii') @@ -1504,10 +1490,6 @@ class NodeConnCB(): sys.exc_info()[0])) raise - def get_deliver_sleep_time(self): - with mininode_lock: - return self.deliver_sleep_time - # Callback methods. Can be overridden by subclasses in individual test # cases to provide custom message handling behaviour. @@ -1616,9 +1598,10 @@ class NodeConnCB(): wait_until(test_function, timeout=timeout, lock=mininode_lock) self.ping_counter += 1 -# The actual NodeConn class -# This class provides an interface for a p2p connection to a specified node class NodeConn(asyncore.dispatcher): + """The actual NodeConn class + + This class provides an interface for a p2p connection to a specified node.""" messagemap = { b"version": msg_version, b"verack": msg_verack, @@ -1838,13 +1821,3 @@ class NetworkThread(Thread): [ obj.handle_close() for obj in disconnected ] asyncore.loop(0.1, use_poll=True, map=mininode_socket_map, count=1) logger.debug("Network thread closing") - - -# An exception we can raise if we detect a potential disconnect -# (p2p or rpc) before the test is complete -class EarlyDisconnectError(Exception): - def __init__(self, value): - self.value = value - - def __str__(self): - return repr(self.value) From c0b1274705f89de86db90bb31d6eb2164639dded Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 17 Oct 2017 07:57:37 -0400 Subject: [PATCH 2/4] [tests] Remove support for bre-BIP31 ping messages BIP31 support was added to Bitcoin Core in version 0.6.1. Our test framework is incompatible with Bitcoin Core versions that old, so remove all special logic for handling pre-BIP31 pings. --- test/functional/test_framework/mininode.py | 23 +--------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/test/functional/test_framework/mininode.py b/test/functional/test_framework/mininode.py index 49dc644ac..0028f8117 100755 --- a/test/functional/test_framework/mininode.py +++ b/test/functional/test_framework/mininode.py @@ -37,7 +37,6 @@ from threading import RLock, Thread from test_framework.siphash import siphash256 from test_framework.util import hex_str_to_bytes, bytes_to_hex_str, wait_until -BIP0031_VERSION = 60000 MY_VERSION = 70014 # past bip-31 for ping/pong MY_SUBVERSION = b"/python-mininode-tester:0.0.3/" MY_RELAY = 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37) @@ -1195,22 +1194,6 @@ class msg_getaddr(): return "msg_getaddr()" -class msg_ping_prebip31(): - command = b"ping" - - def __init__(self): - pass - - def deserialize(self, f): - pass - - def serialize(self): - return b"" - - def __repr__(self): - return "msg_ping() (pre-bip31)" - - class msg_ping(): command = b"ping" @@ -1528,8 +1511,7 @@ class NodeConnCB(): conn.send_message(want) def on_ping(self, conn, message): - if conn.ver_send > BIP0031_VERSION: - conn.send_message(msg_pong(message.nonce)) + conn.send_message(msg_pong(message.nonce)) def on_verack(self, conn, message): conn.ver_recv = conn.ver_send @@ -1786,9 +1768,6 @@ class NodeConn(asyncore.dispatcher): self.last_sent = time.time() def got_message(self, message): - if message.command == b"version": - if message.nVersion <= BIP0031_VERSION: - self.messagemap[b'ping'] = msg_ping_prebip31 if self.last_sent + 30 * 60 < time.time(): self.send_message(self.messagemap[b'ping']()) self._log_message("receive", message) From 3858aabbd084b2cc4534c40a178bbcc22fb82968 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 17 Oct 2017 11:01:37 -0400 Subject: [PATCH 3/4] [tests] Remove support for p2p alert messages Alert messages were removed in p2p version 70013 (Bitcoin Core V0.13.0) --- test/functional/p2p-leaktests.py | 1 - test/functional/test_framework/mininode.py | 96 ---------------------- 2 files changed, 97 deletions(-) diff --git a/test/functional/p2p-leaktests.py b/test/functional/p2p-leaktests.py index 2499624f2..a6e47b5df 100755 --- a/test/functional/p2p-leaktests.py +++ b/test/functional/p2p-leaktests.py @@ -39,7 +39,6 @@ class CLazyNode(NodeConnCB): def on_reject(self, conn, message): self.bad_message(message) def on_inv(self, conn, message): self.bad_message(message) def on_addr(self, conn, message): self.bad_message(message) - def on_alert(self, conn, message): self.bad_message(message) def on_getdata(self, conn, message): self.bad_message(message) def on_getblocks(self, conn, message): self.bad_message(message) def on_tx(self, conn, message): self.bad_message(message) diff --git a/test/functional/test_framework/mininode.py b/test/functional/test_framework/mininode.py index 0028f8117..805d08861 100755 --- a/test/functional/test_framework/mininode.py +++ b/test/functional/test_framework/mininode.py @@ -665,81 +665,6 @@ class CBlock(CBlockHeader): time.ctime(self.nTime), self.nBits, self.nNonce, repr(self.vtx)) -class CUnsignedAlert(): - def __init__(self): - self.nVersion = 1 - self.nRelayUntil = 0 - self.nExpiration = 0 - self.nID = 0 - self.nCancel = 0 - self.setCancel = [] - self.nMinVer = 0 - self.nMaxVer = 0 - self.setSubVer = [] - self.nPriority = 0 - self.strComment = b"" - self.strStatusBar = b"" - self.strReserved = b"" - - def deserialize(self, f): - self.nVersion = struct.unpack(" Date: Tue, 17 Oct 2017 10:59:20 -0400 Subject: [PATCH 4/4] [tests] Explicitly disallow support for p2p versions below 60001 The mininode module includes code to support p2p versions below 60001. However, the test_framework does not support versions of Bitcoin Core before V0.13.0. Remove code supporting p2p versions before 60001 (which has never been run). --- test/functional/test_framework/mininode.py | 68 +++++++++------------- 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/test/functional/test_framework/mininode.py b/test/functional/test_framework/mininode.py index 805d08861..8fbc63fba 100755 --- a/test/functional/test_framework/mininode.py +++ b/test/functional/test_framework/mininode.py @@ -37,6 +37,7 @@ from threading import RLock, Thread from test_framework.siphash import siphash256 from test_framework.util import hex_str_to_bytes, bytes_to_hex_str, wait_until +MIN_VERSION_SUPPORTED = 60001 MY_VERSION = 70014 # past bip-31 for ping/pong MY_SUBVERSION = b"/python-mininode-tester:0.0.3/" MY_RELAY = 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37) @@ -1423,11 +1424,8 @@ class NodeConnCB(): self.verack_received = True def on_version(self, conn, message): - if message.nVersion >= 209: - conn.send_message(msg_verack()) - conn.ver_send = min(MY_VERSION, message.nVersion) - if message.nVersion < 209: - conn.ver_recv = conn.ver_send + assert message.nVersion >= MIN_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_VERSION_SUPPORTED) + conn.send_message(msg_verack()) conn.nServices = message.nServices # Connection helper methods @@ -1609,40 +1607,27 @@ class NodeConn(asyncore.dispatcher): return if self.recvbuf[:4] != self.MAGIC_BYTES[self.network]: raise ValueError("got garbage %s" % repr(self.recvbuf)) - if self.ver_recv < 209: - if len(self.recvbuf) < 4 + 12 + 4: - return - command = self.recvbuf[4:4+12].split(b"\x00", 1)[0] - msglen = struct.unpack("= 209: - th = sha256(data) - h = sha256(th) - tmsg += h[:4] + th = sha256(data) + h = sha256(th) + tmsg += h[:4] tmsg += data with mininode_lock: if (len(self.sendbuf) == 0 and not pushbuf):