Skip to content

Commit 5e3f5e4

Browse files
author
MarcoFalke
committed
Merge #11638: [tests] Dead mininode code
fb00c45 [tests] Explicitly disallow support for p2p versions below 60001 (John Newbery) 3858aab [tests] Remove support for p2p alert messages (John Newbery) c0b1274 [tests] Remove support for bre-BIP31 ping messages (John Newbery) 2904e30 [tests] Remove dead code from mininode.py (John Newbery) Pull request description: This is the first part of #11518. It removes a ~150 lines of unused code from the mininode module: - remove unused `deliver_sleep_time` and `EarlyDisconnectError` code - remove support for pre-BIP31 ping messages - remove support for alert message - explicitly don't support p2p versions lower than 60001 Should be an easy ACK for reviewers. If all extended tests pass, then this code really was dead :) Tree-SHA512: 508e612ceb0b094250d18e75522d51e6b14cd069443050ba4af34d6f890c58721cb5653e8bc000b60635b9474d035b0dcd9c509c0dcdb3a7501df17b787f83b0
2 parents 045a809 + fb00c45 commit 5e3f5e4

File tree

2 files changed

+32
-193
lines changed

2 files changed

+32
-193
lines changed

test/functional/p2p-leaktests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def on_verack(self, conn, message): self.bad_message(message)
3939
def on_reject(self, conn, message): self.bad_message(message)
4040
def on_inv(self, conn, message): self.bad_message(message)
4141
def on_addr(self, conn, message): self.bad_message(message)
42-
def on_alert(self, conn, message): self.bad_message(message)
4342
def on_getdata(self, conn, message): self.bad_message(message)
4443
def on_getblocks(self, conn, message): self.bad_message(message)
4544
def on_tx(self, conn, message): self.bad_message(message)

test/functional/test_framework/mininode.py

Lines changed: 32 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from test_framework.siphash import siphash256
3838
from test_framework.util import hex_str_to_bytes, bytes_to_hex_str, wait_until
3939

40-
BIP0031_VERSION = 60000
40+
MIN_VERSION_SUPPORTED = 60001
4141
MY_VERSION = 70014 # past bip-31 for ping/pong
4242
MY_SUBVERSION = b"/python-mininode-tester:0.0.3/"
4343
MY_RELAY = 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37)
@@ -666,81 +666,6 @@ def __repr__(self):
666666
time.ctime(self.nTime), self.nBits, self.nNonce, repr(self.vtx))
667667

668668

669-
class CUnsignedAlert():
670-
def __init__(self):
671-
self.nVersion = 1
672-
self.nRelayUntil = 0
673-
self.nExpiration = 0
674-
self.nID = 0
675-
self.nCancel = 0
676-
self.setCancel = []
677-
self.nMinVer = 0
678-
self.nMaxVer = 0
679-
self.setSubVer = []
680-
self.nPriority = 0
681-
self.strComment = b""
682-
self.strStatusBar = b""
683-
self.strReserved = b""
684-
685-
def deserialize(self, f):
686-
self.nVersion = struct.unpack("<i", f.read(4))[0]
687-
self.nRelayUntil = struct.unpack("<q", f.read(8))[0]
688-
self.nExpiration = struct.unpack("<q", f.read(8))[0]
689-
self.nID = struct.unpack("<i", f.read(4))[0]
690-
self.nCancel = struct.unpack("<i", f.read(4))[0]
691-
self.setCancel = deser_int_vector(f)
692-
self.nMinVer = struct.unpack("<i", f.read(4))[0]
693-
self.nMaxVer = struct.unpack("<i", f.read(4))[0]
694-
self.setSubVer = deser_string_vector(f)
695-
self.nPriority = struct.unpack("<i", f.read(4))[0]
696-
self.strComment = deser_string(f)
697-
self.strStatusBar = deser_string(f)
698-
self.strReserved = deser_string(f)
699-
700-
def serialize(self):
701-
r = b""
702-
r += struct.pack("<i", self.nVersion)
703-
r += struct.pack("<q", self.nRelayUntil)
704-
r += struct.pack("<q", self.nExpiration)
705-
r += struct.pack("<i", self.nID)
706-
r += struct.pack("<i", self.nCancel)
707-
r += ser_int_vector(self.setCancel)
708-
r += struct.pack("<i", self.nMinVer)
709-
r += struct.pack("<i", self.nMaxVer)
710-
r += ser_string_vector(self.setSubVer)
711-
r += struct.pack("<i", self.nPriority)
712-
r += ser_string(self.strComment)
713-
r += ser_string(self.strStatusBar)
714-
r += ser_string(self.strReserved)
715-
return r
716-
717-
def __repr__(self):
718-
return "CUnsignedAlert(nVersion %d, nRelayUntil %d, nExpiration %d, nID %d, nCancel %d, nMinVer %d, nMaxVer %d, nPriority %d, strComment %s, strStatusBar %s, strReserved %s)" \
719-
% (self.nVersion, self.nRelayUntil, self.nExpiration, self.nID,
720-
self.nCancel, self.nMinVer, self.nMaxVer, self.nPriority,
721-
self.strComment, self.strStatusBar, self.strReserved)
722-
723-
724-
class CAlert():
725-
def __init__(self):
726-
self.vchMsg = b""
727-
self.vchSig = b""
728-
729-
def deserialize(self, f):
730-
self.vchMsg = deser_string(f)
731-
self.vchSig = deser_string(f)
732-
733-
def serialize(self):
734-
r = b""
735-
r += ser_string(self.vchMsg)
736-
r += ser_string(self.vchSig)
737-
return r
738-
739-
def __repr__(self):
740-
return "CAlert(vchMsg.sz %d, vchSig.sz %d)" \
741-
% (len(self.vchMsg), len(self.vchSig))
742-
743-
744669
class PrefilledTransaction():
745670
def __init__(self, index=0, tx = None):
746671
self.index = index
@@ -1044,25 +969,6 @@ def __repr__(self):
1044969
return "msg_addr(addrs=%s)" % (repr(self.addrs))
1045970

1046971

1047-
class msg_alert():
1048-
command = b"alert"
1049-
1050-
def __init__(self):
1051-
self.alert = CAlert()
1052-
1053-
def deserialize(self, f):
1054-
self.alert = CAlert()
1055-
self.alert.deserialize(f)
1056-
1057-
def serialize(self):
1058-
r = b""
1059-
r += self.alert.serialize()
1060-
return r
1061-
1062-
def __repr__(self):
1063-
return "msg_alert(alert=%s)" % (repr(self.alert), )
1064-
1065-
1066972
class msg_inv():
1067973
command = b"inv"
1068974

@@ -1195,22 +1101,6 @@ def __repr__(self):
11951101
return "msg_getaddr()"
11961102

11971103

1198-
class msg_ping_prebip31():
1199-
command = b"ping"
1200-
1201-
def __init__(self):
1202-
pass
1203-
1204-
def deserialize(self, f):
1205-
pass
1206-
1207-
def serialize(self):
1208-
return b""
1209-
1210-
def __repr__(self):
1211-
return "msg_ping() (pre-bip31)"
1212-
1213-
12141104
class msg_ping():
12151105
command = b"ping"
12161106

@@ -1458,9 +1348,7 @@ class NodeConnCB():
14581348
"""Callback and helper functions for P2P connection to a bitcoind node.
14591349
14601350
Individual testcases should subclass this and override the on_* methods
1461-
if they want to alter message handling behaviour.
1462-
"""
1463-
1351+
if they want to alter message handling behaviour."""
14641352
def __init__(self):
14651353
# Track whether we have a P2P connection open to the node
14661354
self.connected = False
@@ -1474,25 +1362,13 @@ def __init__(self):
14741362
# A count of the number of ping messages we've sent to the node
14751363
self.ping_counter = 1
14761364

1477-
# deliver_sleep_time is helpful for debugging race conditions in p2p
1478-
# tests; it causes message delivery to sleep for the specified time
1479-
# before acquiring the global lock and delivering the next message.
1480-
self.deliver_sleep_time = None
1481-
14821365
# Message receiving methods
14831366

14841367
def deliver(self, conn, message):
14851368
"""Receive message and dispatch message to appropriate callback.
14861369
14871370
We keep a count of how many of each message type has been received
1488-
and the most recent message of each type.
1489-
1490-
Optionally waits for deliver_sleep_time before dispatching message.
1491-
"""
1492-
1493-
deliver_sleep = self.get_deliver_sleep_time()
1494-
if deliver_sleep is not None:
1495-
time.sleep(deliver_sleep)
1371+
and the most recent message of each type."""
14961372
with mininode_lock:
14971373
try:
14981374
command = message.command.decode('ascii')
@@ -1504,10 +1380,6 @@ def deliver(self, conn, message):
15041380
sys.exc_info()[0]))
15051381
raise
15061382

1507-
def get_deliver_sleep_time(self):
1508-
with mininode_lock:
1509-
return self.deliver_sleep_time
1510-
15111383
# Callback methods. Can be overridden by subclasses in individual test
15121384
# cases to provide custom message handling behaviour.
15131385

@@ -1519,7 +1391,6 @@ def on_close(self, conn):
15191391
self.connection = None
15201392

15211393
def on_addr(self, conn, message): pass
1522-
def on_alert(self, conn, message): pass
15231394
def on_block(self, conn, message): pass
15241395
def on_blocktxn(self, conn, message): pass
15251396
def on_cmpctblock(self, conn, message): pass
@@ -1546,19 +1417,15 @@ def on_inv(self, conn, message):
15461417
conn.send_message(want)
15471418

15481419
def on_ping(self, conn, message):
1549-
if conn.ver_send > BIP0031_VERSION:
1550-
conn.send_message(msg_pong(message.nonce))
1420+
conn.send_message(msg_pong(message.nonce))
15511421

15521422
def on_verack(self, conn, message):
15531423
conn.ver_recv = conn.ver_send
15541424
self.verack_received = True
15551425

15561426
def on_version(self, conn, message):
1557-
if message.nVersion >= 209:
1558-
conn.send_message(msg_verack())
1559-
conn.ver_send = min(MY_VERSION, message.nVersion)
1560-
if message.nVersion < 209:
1561-
conn.ver_recv = conn.ver_send
1427+
assert message.nVersion >= MIN_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_VERSION_SUPPORTED)
1428+
conn.send_message(msg_verack())
15621429
conn.nServices = message.nServices
15631430

15641431
# Connection helper methods
@@ -1616,14 +1483,14 @@ def sync_with_ping(self, timeout=60):
16161483
wait_until(test_function, timeout=timeout, lock=mininode_lock)
16171484
self.ping_counter += 1
16181485

1619-
# The actual NodeConn class
1620-
# This class provides an interface for a p2p connection to a specified node
16211486
class NodeConn(asyncore.dispatcher):
1487+
"""The actual NodeConn class
1488+
1489+
This class provides an interface for a p2p connection to a specified node."""
16221490
messagemap = {
16231491
b"version": msg_version,
16241492
b"verack": msg_verack,
16251493
b"addr": msg_addr,
1626-
b"alert": msg_alert,
16271494
b"inv": msg_inv,
16281495
b"getdata": msg_getdata,
16291496
b"getblocks": msg_getblocks,
@@ -1740,40 +1607,27 @@ def got_data(self):
17401607
return
17411608
if self.recvbuf[:4] != self.MAGIC_BYTES[self.network]:
17421609
raise ValueError("got garbage %s" % repr(self.recvbuf))
1743-
if self.ver_recv < 209:
1744-
if len(self.recvbuf) < 4 + 12 + 4:
1745-
return
1746-
command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
1747-
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
1748-
checksum = None
1749-
if len(self.recvbuf) < 4 + 12 + 4 + msglen:
1750-
return
1751-
msg = self.recvbuf[4+12+4:4+12+4+msglen]
1752-
self.recvbuf = self.recvbuf[4+12+4+msglen:]
1753-
else:
1754-
if len(self.recvbuf) < 4 + 12 + 4 + 4:
1755-
return
1756-
command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
1757-
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
1758-
checksum = self.recvbuf[4+12+4:4+12+4+4]
1759-
if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen:
1760-
return
1761-
msg = self.recvbuf[4+12+4+4:4+12+4+4+msglen]
1762-
th = sha256(msg)
1763-
h = sha256(th)
1764-
if checksum != h[:4]:
1765-
raise ValueError("got bad checksum " + repr(self.recvbuf))
1766-
self.recvbuf = self.recvbuf[4+12+4+4+msglen:]
1767-
if command in self.messagemap:
1768-
f = BytesIO(msg)
1769-
t = self.messagemap[command]()
1770-
t.deserialize(f)
1771-
self.got_message(t)
1772-
else:
1773-
logger.warning("Received unknown command from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, command, repr(msg)))
1774-
raise ValueError("Unknown command: '%s'" % (command))
1610+
if len(self.recvbuf) < 4 + 12 + 4 + 4:
1611+
return
1612+
command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
1613+
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
1614+
checksum = self.recvbuf[4+12+4:4+12+4+4]
1615+
if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen:
1616+
return
1617+
msg = self.recvbuf[4+12+4+4:4+12+4+4+msglen]
1618+
th = sha256(msg)
1619+
h = sha256(th)
1620+
if checksum != h[:4]:
1621+
raise ValueError("got bad checksum " + repr(self.recvbuf))
1622+
self.recvbuf = self.recvbuf[4+12+4+4+msglen:]
1623+
if command not in self.messagemap:
1624+
raise ValueError("Received unknown command from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, command, repr(msg)))
1625+
f = BytesIO(msg)
1626+
t = self.messagemap[command]()
1627+
t.deserialize(f)
1628+
self.got_message(t)
17751629
except Exception as e:
1776-
logger.exception('got_data:', repr(e))
1630+
logger.exception('Error reading message:', repr(e))
17771631
raise
17781632

17791633
def send_message(self, message, pushbuf=False):
@@ -1786,10 +1640,9 @@ def send_message(self, message, pushbuf=False):
17861640
tmsg += command
17871641
tmsg += b"\x00" * (12 - len(command))
17881642
tmsg += struct.pack("<I", len(data))
1789-
if self.ver_send >= 209:
1790-
th = sha256(data)
1791-
h = sha256(th)
1792-
tmsg += h[:4]
1643+
th = sha256(data)
1644+
h = sha256(th)
1645+
tmsg += h[:4]
17931646
tmsg += data
17941647
with mininode_lock:
17951648
if (len(self.sendbuf) == 0 and not pushbuf):
@@ -1803,9 +1656,6 @@ def send_message(self, message, pushbuf=False):
18031656
self.last_sent = time.time()
18041657

18051658
def got_message(self, message):
1806-
if message.command == b"version":
1807-
if message.nVersion <= BIP0031_VERSION:
1808-
self.messagemap[b'ping'] = msg_ping_prebip31
18091659
if self.last_sent + 30 * 60 < time.time():
18101660
self.send_message(self.messagemap[b'ping']())
18111661
self._log_message("receive", message)
@@ -1838,13 +1688,3 @@ def run(self):
18381688
[ obj.handle_close() for obj in disconnected ]
18391689
asyncore.loop(0.1, use_poll=True, map=mininode_socket_map, count=1)
18401690
logger.debug("Network thread closing")
1841-
1842-
1843-
# An exception we can raise if we detect a potential disconnect
1844-
# (p2p or rpc) before the test is complete
1845-
class EarlyDisconnectError(Exception):
1846-
def __init__(self, value):
1847-
self.value = value
1848-
1849-
def __str__(self):
1850-
return repr(self.value)

0 commit comments

Comments
 (0)