partial bitcoin#20524: Move MIN_VERSION_SUPPORTED to p2p.py

excludes:
- 9f21ed4037758f407b536c0dd129f8da83173c79
This commit is contained in:
Kittywhiskers Van Gogh 2024-08-27 18:53:33 +00:00
parent b1fadfb14f
commit cbff29a630
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
5 changed files with 58 additions and 35 deletions

View File

@ -19,7 +19,13 @@ from test_framework.messages import (
msg_mempool, msg_mempool,
msg_version, msg_version,
) )
from test_framework.p2p import P2PInterface, p2p_lock from test_framework.p2p import (
P2PInterface,
P2P_SERVICES,
P2P_SUBVERSION,
P2P_VERSION,
p2p_lock,
)
from test_framework.script import MAX_SCRIPT_ELEMENT_SIZE from test_framework.script import MAX_SCRIPT_ELEMENT_SIZE
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
@ -215,9 +221,12 @@ class FilterTest(BitcoinTestFramework):
self.log.info('Test BIP 37 for a node with fRelay = False') self.log.info('Test BIP 37 for a node with fRelay = False')
# Add peer but do not send version yet # Add peer but do not send version yet
filter_peer_without_nrelay = self.nodes[0].add_p2p_connection(P2PBloomFilter(), send_version=False, wait_for_verack=False) filter_peer_without_nrelay = self.nodes[0].add_p2p_connection(P2PBloomFilter(), send_version=False, wait_for_verack=False)
# Send version with fRelay=False # Send version with relay=False
version_without_fRelay = msg_version() version_without_fRelay = msg_version()
version_without_fRelay.nRelay = 0 version_without_fRelay.nVersion = P2P_VERSION
version_without_fRelay.strSubVer = P2P_SUBVERSION
version_without_fRelay.nServices = P2P_SERVICES
version_without_fRelay.relay = 0
filter_peer_without_nrelay.send_message(version_without_fRelay) filter_peer_without_nrelay.send_message(version_without_fRelay)
filter_peer_without_nrelay.wait_for_verack() filter_peer_without_nrelay.wait_for_verack()
assert not self.nodes[0].getpeerinfo()[0]['relaytxes'] assert not self.nodes[0].getpeerinfo()[0]['relaytxes']

View File

@ -18,7 +18,12 @@ from test_framework.messages import (
msg_ping, msg_ping,
msg_version, msg_version,
) )
from test_framework.p2p import P2PInterface from test_framework.p2p import (
P2PInterface,
P2P_SUBVERSION,
P2P_SERVICES,
P2P_VERSION_RELAY,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
@ -126,12 +131,15 @@ class P2PLeakTest(BitcoinTestFramework):
assert_equal(ver.addrFrom.port, 0) assert_equal(ver.addrFrom.port, 0)
assert_equal(ver.addrFrom.ip, '0.0.0.0') assert_equal(ver.addrFrom.ip, '0.0.0.0')
assert_equal(ver.nStartingHeight, 201) assert_equal(ver.nStartingHeight, 201)
assert_equal(ver.nRelay, 1) assert_equal(ver.relay, 1)
self.log.info('Check that old peers are disconnected') self.log.info('Check that old peers are disconnected')
p2p_old_peer = self.nodes[0].add_p2p_connection(P2PInterface(), send_version=False, wait_for_verack=False) p2p_old_peer = self.nodes[0].add_p2p_connection(P2PInterface(), send_version=False, wait_for_verack=False)
old_version_msg = msg_version() old_version_msg = msg_version()
old_version_msg.nVersion = 31799 old_version_msg.nVersion = 31799
old_version_msg.strSubVer = P2P_SUBVERSION
old_version_msg.nServices = P2P_SERVICES
old_version_msg.relay = P2P_VERSION_RELAY
with self.nodes[0].assert_debug_log(['peer=3 using obsolete version 31799; disconnecting']): with self.nodes[0].assert_debug_log(['peer=3 using obsolete version 31799; disconnecting']):
p2p_old_peer.send_message(old_version_msg) p2p_old_peer.send_message(old_version_msg)
p2p_old_peer.wait_for_disconnect() p2p_old_peer.wait_for_disconnect()

View File

@ -31,11 +31,6 @@ from test_framework.util import assert_equal
import dash_hash import dash_hash
MIN_VERSION_SUPPORTED = 60001
MY_VERSION = 70231 # NO_LEGACY_ISLOCK_PROTO_VERSION
MY_SUBVERSION = "/python-p2p-tester:0.0.3%s/"
MY_RELAY = 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37)
MAX_LOCATOR_SZ = 101 MAX_LOCATOR_SZ = 101
MAX_BLOCK_SIZE = 2000000 MAX_BLOCK_SIZE = 2000000
MAX_BLOOM_FILTER_SIZE = 36000 MAX_BLOOM_FILTER_SIZE = 36000
@ -383,22 +378,20 @@ class CBlockLocator:
__slots__ = ("nVersion", "vHave") __slots__ = ("nVersion", "vHave")
def __init__(self): def __init__(self):
self.nVersion = MY_VERSION
self.vHave = [] self.vHave = []
def deserialize(self, f): def deserialize(self, f):
self.nVersion = struct.unpack("<i", f.read(4))[0] struct.unpack("<i", f.read(4))[0] # Ignore version field.
self.vHave = deser_uint256_vector(f) self.vHave = deser_uint256_vector(f)
def serialize(self): def serialize(self):
r = b"" r = b""
r += struct.pack("<i", self.nVersion) r += struct.pack("<i", 0) # Bitcoin Core ignores version field. Set it to 0.
r += ser_uint256_vector(self.vHave) r += ser_uint256_vector(self.vHave)
return r return r
def __repr__(self): def __repr__(self):
return "CBlockLocator(nVersion=%i vHave=%s)" \ return "CBlockLocator(vHave=%s)" % (repr(self.vHave))
% (self.nVersion, repr(self.vHave))
class COutPoint: class COutPoint:
@ -1534,20 +1527,20 @@ class CBLSIESEncryptedSecretKey:
# Objects that correspond to messages on the wire # Objects that correspond to messages on the wire
class msg_version: class msg_version:
__slots__ = ("addrFrom", "addrTo", "nNonce", "nRelay", "nServices", __slots__ = ("addrFrom", "addrTo", "nNonce", "relay", "nServices",
"nStartingHeight", "nTime", "nVersion", "strSubVer") "nStartingHeight", "nTime", "nVersion", "strSubVer")
msgtype = b"version" msgtype = b"version"
def __init__(self): def __init__(self):
self.nVersion = MY_VERSION self.nVersion = 0
self.nServices = 1 self.nServices = 0
self.nTime = int(time.time()) self.nTime = int(time.time())
self.addrTo = CAddress() self.addrTo = CAddress()
self.addrFrom = CAddress() self.addrFrom = CAddress()
self.nNonce = random.getrandbits(64) self.nNonce = random.getrandbits(64)
self.strSubVer = MY_SUBVERSION % "" self.strSubVer = ''
self.nStartingHeight = -1 self.nStartingHeight = -1
self.nRelay = MY_RELAY self.relay = 0
def deserialize(self, f): def deserialize(self, f):
self.nVersion = struct.unpack("<i", f.read(4))[0] self.nVersion = struct.unpack("<i", f.read(4))[0]
@ -1566,9 +1559,9 @@ class msg_version:
# Relay field is optional for version 70001 onwards # Relay field is optional for version 70001 onwards
# But, unconditionally check it to match behaviour in bitcoind # But, unconditionally check it to match behaviour in bitcoind
try: try:
self.nRelay = struct.unpack("<b", f.read(1))[0] self.relay = struct.unpack("<b", f.read(1))[0]
except struct.error: except struct.error:
self.nRelay = 0 self.relay = 0
def serialize(self): def serialize(self):
r = b"" r = b""
@ -1580,14 +1573,14 @@ class msg_version:
r += struct.pack("<Q", self.nNonce) r += struct.pack("<Q", self.nNonce)
r += ser_string(self.strSubVer.encode('utf-8')) r += ser_string(self.strSubVer.encode('utf-8'))
r += struct.pack("<i", self.nStartingHeight) r += struct.pack("<i", self.nStartingHeight)
r += struct.pack("<b", self.nRelay) r += struct.pack("<b", self.relay)
return r return r
def __repr__(self): def __repr__(self):
return 'msg_version(nVersion=%i nServices=%i nTime=%s addrTo=%s addrFrom=%s nNonce=0x%016X strSubVer=%s nStartingHeight=%i nRelay=%i)' \ return 'msg_version(nVersion=%i nServices=%i nTime=%s addrTo=%s addrFrom=%s nNonce=0x%016X strSubVer=%s nStartingHeight=%i relay=%i)' \
% (self.nVersion, self.nServices, time.ctime(self.nTime), % (self.nVersion, self.nServices, time.ctime(self.nTime),
repr(self.addrTo), repr(self.addrFrom), self.nNonce, repr(self.addrTo), repr(self.addrFrom), self.nNonce,
self.strSubVer, self.nStartingHeight, self.nRelay) self.strSubVer, self.nStartingHeight, self.relay)
class msg_verack: class msg_verack:

View File

@ -32,7 +32,6 @@ from test_framework.messages import (
CBlockHeader, CBlockHeader,
CompressibleBlockHeader, CompressibleBlockHeader,
MAX_HEADERS_RESULTS, MAX_HEADERS_RESULTS,
MIN_VERSION_SUPPORTED,
NODE_HEADERS_COMPRESSED, NODE_HEADERS_COMPRESSED,
msg_addr, msg_addr,
msg_addrv2, msg_addrv2,
@ -75,7 +74,6 @@ from test_framework.messages import (
msg_tx, msg_tx,
msg_verack, msg_verack,
msg_version, msg_version,
MY_SUBVERSION,
MSG_BLOCK, MSG_BLOCK,
MSG_TX, MSG_TX,
MSG_TYPE_MASK, MSG_TYPE_MASK,
@ -90,6 +88,18 @@ from test_framework.util import (
logger = logging.getLogger("TestFramework.p2p") logger = logging.getLogger("TestFramework.p2p")
# The minimum P2P version that this test framework supports
MIN_P2P_VERSION_SUPPORTED = 60001
# The P2P version that this test framework implements and sends in its `version` message
# Version 70231 drops supports for legacy InstantSend locks
P2P_VERSION = 70231
# The services that this test framework offers in its `version` message
P2P_SERVICES = NODE_NETWORK | NODE_HEADERS_COMPRESSED
# The P2P user agent string that this test framework sends in its `version` message
P2P_SUBVERSION = "/python-p2p-tester:0.0.3%s/"
# Value for relay that this test framework sends in its `version` message
P2P_VERSION_RELAY = 1
MESSAGEMAP = { MESSAGEMAP = {
b"addr": msg_addr, b"addr": msg_addr,
b"addrv2": msg_addrv2, b"addrv2": msg_addrv2,
@ -186,13 +196,13 @@ class P2PConnection(asyncio.Protocol):
if net == "devnet": if net == "devnet":
devnet_name = "devnet1" # see initialize_datadir() devnet_name = "devnet1" # see initialize_datadir()
if self.uacomment is None: if self.uacomment is None:
self.strSubVer = MY_SUBVERSION % ("(devnet.devnet-%s)" % devnet_name) self.strSubVer = P2P_SUBVERSION % ("(devnet.devnet-%s)" % devnet_name)
else: else:
self.strSubVer = MY_SUBVERSION % ("(devnet.devnet-%s,%s)" % (devnet_name, self.uacomment)) self.strSubVer = P2P_SUBVERSION % ("(devnet.devnet-%s,%s)" % (devnet_name, self.uacomment))
elif self.uacomment is not None: elif self.uacomment is not None:
self.strSubVer = MY_SUBVERSION % ("(%s)" % self.uacomment) self.strSubVer = P2P_SUBVERSION % ("(%s)" % self.uacomment)
else: else:
self.strSubVer = MY_SUBVERSION % "" self.strSubVer = P2P_SUBVERSION % ""
def peer_connect(self, dstaddr, dstport, *, net, timeout_factor, uacomment=None): def peer_connect(self, dstaddr, dstport, *, net, timeout_factor, uacomment=None):
self.peer_connect_helper(dstaddr, dstport, net, timeout_factor, uacomment) self.peer_connect_helper(dstaddr, dstport, net, timeout_factor, uacomment)
@ -368,6 +378,9 @@ class P2PInterface(P2PConnection):
def peer_connect_send_version(self, services): def peer_connect_send_version(self, services):
# Send a version msg # Send a version msg
vt = msg_version() vt = msg_version()
vt.nVersion = P2P_VERSION
vt.strSubVer = P2P_SUBVERSION
vt.relay = P2P_VERSION_RELAY
vt.nServices = services vt.nServices = services
vt.addrTo.ip = self.dstaddr vt.addrTo.ip = self.dstaddr
vt.addrTo.port = self.dstport vt.addrTo.port = self.dstport
@ -376,7 +389,7 @@ class P2PInterface(P2PConnection):
vt.strSubVer = self.strSubVer vt.strSubVer = self.strSubVer
self.on_connection_send_msg = vt # Will be sent soon after connection_made self.on_connection_send_msg = vt # Will be sent soon after connection_made
def peer_connect(self, *args, services=NODE_NETWORK | NODE_HEADERS_COMPRESSED, send_version=True, **kwargs): def peer_connect(self, *args, services=P2P_SERVICES, send_version=True, **kwargs):
create_conn = super().peer_connect(*args, **kwargs) create_conn = super().peer_connect(*args, **kwargs)
if send_version: if send_version:
@ -469,7 +482,7 @@ class P2PInterface(P2PConnection):
def on_verack(self, message): pass def on_verack(self, message): pass
def on_version(self, message): def on_version(self, message):
assert message.nVersion >= MIN_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_VERSION_SUPPORTED) assert message.nVersion >= MIN_P2P_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_P2P_VERSION_SUPPORTED)
if self.support_addrv2: if self.support_addrv2:
self.send_message(msg_sendaddrv2()) self.send_message(msg_sendaddrv2())
self.send_message(msg_verack()) self.send_message(msg_verack())

View File

@ -23,7 +23,7 @@ import collections
from .authproxy import JSONRPCException from .authproxy import JSONRPCException
from .descriptors import descsum_create from .descriptors import descsum_create
from .messages import MY_SUBVERSION from .p2p import P2P_SUBVERSION
from .util import ( from .util import (
MAX_NODES, MAX_NODES,
append_config, append_config,
@ -596,7 +596,7 @@ class TestNode():
def num_test_p2p_connections(self): def num_test_p2p_connections(self):
"""Return number of test framework p2p connections to the node.""" """Return number of test framework p2p connections to the node."""
return len([peer for peer in self.getpeerinfo() if peer['subver'] == MY_SUBVERSION]) return len([peer for peer in self.getpeerinfo() if P2P_SUBVERSION % "" in peer['subver']])
def disconnect_p2ps(self): def disconnect_p2ps(self):
"""Close all p2p connections to the node.""" """Close all p2p connections to the node."""