Skip to content

Commit 7e158a6

Browse files
committed
[test] Move MY_VERSION to p2p.py
The messages.py module should contain code and helpers for [de]serializing p2p messages. Specific usage of those messages should be in p2p.py. Therefore move MY_VERSION to p2p.py. Also rename to P2P_VERSION to distinguish it from other versioning used in Bitcoin/Bitcoin Core. Also always set the nVersion field in CBlockLocator to 0 and ignore the field in deserialized messages. The field is not currently used for anything in Bitcoin Core.
1 parent 6523111 commit 7e158a6

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

test/functional/p2p_filter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
msg_mempool,
2020
msg_version,
2121
)
22-
from test_framework.p2p import P2PInterface, p2p_lock
22+
from test_framework.p2p import (
23+
P2PInterface,
24+
P2P_VERSION,
25+
p2p_lock,
26+
)
2327
from test_framework.script import MAX_SCRIPT_ELEMENT_SIZE
2428
from test_framework.test_framework import BitcoinTestFramework
2529

@@ -218,6 +222,7 @@ def run_test(self):
218222
filter_peer_without_nrelay = self.nodes[0].add_p2p_connection(P2PBloomFilter(), send_version=False, wait_for_verack=False)
219223
# Send version with fRelay=False
220224
version_without_fRelay = msg_version()
225+
version_without_fRelay.nVersion = P2P_VERSION
221226
version_without_fRelay.nRelay = 0
222227
filter_peer_without_nrelay.send_message(version_without_fRelay)
223228
filter_peer_without_nrelay.wait_for_verack()

test/functional/test_framework/messages.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
from test_framework.siphash import siphash256
3232
from test_framework.util import hex_str_to_bytes, assert_equal
3333

34-
MY_VERSION = 70016 # past wtxid relay
3534
MY_SUBVERSION = "/python-p2p-tester:0.0.3/"
3635
MY_RELAY = 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37)
3736

@@ -325,22 +324,20 @@ class CBlockLocator:
325324
__slots__ = ("nVersion", "vHave")
326325

327326
def __init__(self):
328-
self.nVersion = MY_VERSION
329327
self.vHave = []
330328

331329
def deserialize(self, f):
332-
self.nVersion = struct.unpack("<i", f.read(4))[0]
330+
struct.unpack("<i", f.read(4))[0] # Ignore version field.
333331
self.vHave = deser_uint256_vector(f)
334332

335333
def serialize(self):
336334
r = b""
337-
r += struct.pack("<i", self.nVersion)
335+
r += struct.pack("<i", 0) # Bitcoin Core ignores version field. Set it to 0.
338336
r += ser_uint256_vector(self.vHave)
339337
return r
340338

341339
def __repr__(self):
342-
return "CBlockLocator(nVersion=%i vHave=%s)" \
343-
% (self.nVersion, repr(self.vHave))
340+
return "CBlockLocator(vHave=%s)" % (repr(self.vHave))
344341

345342

346343
class COutPoint:
@@ -1027,7 +1024,7 @@ class msg_version:
10271024
msgtype = b"version"
10281025

10291026
def __init__(self):
1030-
self.nVersion = MY_VERSION
1027+
self.nVersion = 0
10311028
self.nServices = NODE_NETWORK | NODE_WITNESS
10321029
self.nTime = int(time.time())
10331030
self.addrTo = CAddress()

test/functional/test_framework/p2p.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080

8181
# The minimum P2P version that this test framework supports
8282
MIN_P2P_VERSION_SUPPORTED = 60001
83+
# The P2P version that this test framework implements and sends in its `version` message
84+
# Version 70016 supports wtxid relay
85+
P2P_VERSION = 70016
8386

8487
MESSAGEMAP = {
8588
b"addr": msg_addr,
@@ -329,6 +332,7 @@ def __init__(self, support_addrv2=False, wtxidrelay=True):
329332
def peer_connect_send_version(self, services):
330333
# Send a version msg
331334
vt = msg_version()
335+
vt.nVersion = P2P_VERSION
332336
vt.nServices = services
333337
vt.addrTo.ip = self.dstaddr
334338
vt.addrTo.port = self.dstport

0 commit comments

Comments
 (0)