Skip to content

Commit 085120d

Browse files
MarcoFalkeknst
authored andcommitted
Merge bitcoin#20993: test: store subversion (user agent) as string in msg_version
de85af5 test: store subversion (user agent) as string in msg_version (Sebastian Falbesoner) Pull request description: It seems more natural to treat the "subversion" field (=user agent string, see [BIP 14](https://github.com/bitcoin/bips/blob/master/bip-0014.mediawiki#Proposal)) of a node as pure string rather than a bytestring within the test framework. This is also suggested with the naming prefix in `msg_version.strSubVer`: one probably wouldn't expect a field starting with "str" to be a bytestring that needs further decoding to be useful. This PR moves the encoding/decoding parts to the serialization/deserialization routines so that the user doesn't have to bother with that anymore. Note that currently, in the master branch the `msg_version.strSubVer` is never read (only in `msg_version.__repr__`); However, one issue that is solved by this PR came up while testing bitcoin#19509 (not merged yet): A decoding script for binary message capture files takes use of the functional test framework convert it into JSON format. Bytestrings will be convered to hexstrings, while pure strings will (surprise surprise) end up without modification in the file. So without this patch, we get: ``` $ jq . out.json | grep -m5 strSubVer "strSubVer": "2f5361746f7368693a32312e39392e302f" "strSubVer": "2f5361746f7368693a302e32302e312f" "strSubVer": "2f5361746f7368693a32312e39392e302f" "strSubVer": "2f5361746f7368693a302e32302e312f" "strSubVer": "2f5361746f7368693a32312e39392e302f" ``` After this patch: ``` $ jq . out2.json | grep -m5 strSubVer "strSubVer": "/Satoshi:21.99.0/" "strSubVer": "/Satoshi:0.20.1/" "strSubVer": "/Satoshi:21.99.0/" "strSubVer": "/Satoshi:0.20.1/" "strSubVer": "/Satoshi:21.99.0/" ``` ACKs for top commit: jnewbery: utACK de85af5 Tree-SHA512: ff23642705c858e8387a625537dfec82e6b8a15da6d99b8d12152560e52d243ba17431b602b26f60996d897e00e3f37dcf8dc8a303ffb1d544df29a5937080f9
1 parent e866b43 commit 085120d

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

test/functional/p2p_quorum_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_id():
7878
for p2p in node.p2ps:
7979
if uacomment is not None and p2p.uacomment != uacomment:
8080
continue
81-
if p["subver"] == p2p.strSubVer.decode():
81+
if p["subver"] == p2p.strSubVer:
8282
return p["id"]
8383
return None
8484
wait_until_helper(lambda: get_id() is not None, timeout=10)

test/functional/test_framework/messages.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
MIN_VERSION_SUPPORTED = 60001
3535
MY_VERSION = 70231 # NO_LEGACY_ISLOCK_PROTO_VERSION
36-
MY_SUBVERSION = b"/python-p2p-tester:0.0.3%s/"
36+
MY_SUBVERSION = "/python-p2p-tester:0.0.3%s/"
3737
MY_RELAY = 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37)
3838

3939
MAX_LOCATOR_SZ = 101
@@ -1521,7 +1521,7 @@ def __init__(self):
15211521
self.addrTo = CAddress()
15221522
self.addrFrom = CAddress()
15231523
self.nNonce = random.getrandbits(64)
1524-
self.strSubVer = MY_SUBVERSION % b""
1524+
self.strSubVer = MY_SUBVERSION % ""
15251525
self.nStartingHeight = -1
15261526
self.nRelay = MY_RELAY
15271527

@@ -1535,7 +1535,7 @@ def deserialize(self, f):
15351535
self.addrFrom = CAddress()
15361536
self.addrFrom.deserialize(f, with_time=False)
15371537
self.nNonce = struct.unpack("<Q", f.read(8))[0]
1538-
self.strSubVer = deser_string(f)
1538+
self.strSubVer = deser_string(f).decode('utf-8')
15391539

15401540
self.nStartingHeight = struct.unpack("<i", f.read(4))[0]
15411541

@@ -1554,7 +1554,7 @@ def serialize(self):
15541554
r += self.addrTo.serialize(with_time=False)
15551555
r += self.addrFrom.serialize(with_time=False)
15561556
r += struct.pack("<Q", self.nNonce)
1557-
r += ser_string(self.strSubVer)
1557+
r += ser_string(self.strSubVer.encode('utf-8'))
15581558
r += struct.pack("<i", self.nStartingHeight)
15591559
r += struct.pack("<b", self.nRelay)
15601560
return r

test/functional/test_framework/p2p.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ def peer_connect_helper(self, dstaddr, dstport, net, timeout_factor, uacomment):
186186
if net == "devnet":
187187
devnet_name = "devnet1" # see initialize_datadir()
188188
if self.uacomment is None:
189-
self.strSubVer = MY_SUBVERSION % ("(devnet.devnet-%s)" % devnet_name).encode()
189+
self.strSubVer = MY_SUBVERSION % ("(devnet.devnet-%s)" % devnet_name)
190190
else:
191-
self.strSubVer = MY_SUBVERSION % ("(devnet.devnet-%s,%s)" % (devnet_name, self.uacomment)).encode()
191+
self.strSubVer = MY_SUBVERSION % ("(devnet.devnet-%s,%s)" % (devnet_name, self.uacomment))
192192
elif self.uacomment is not None:
193-
self.strSubVer = MY_SUBVERSION % ("(%s)" % self.uacomment).encode()
193+
self.strSubVer = MY_SUBVERSION % ("(%s)" % self.uacomment)
194194
else:
195-
self.strSubVer = MY_SUBVERSION % b""
195+
self.strSubVer = MY_SUBVERSION % ""
196196

197197
def peer_connect(self, dstaddr, dstport, *, net, timeout_factor, uacomment=None):
198198
self.peer_connect_helper(dstaddr, dstport, net, timeout_factor, uacomment)

test/functional/test_framework/test_node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ def addconnection_callback(address, port):
592592

593593
def num_test_p2p_connections(self):
594594
"""Return number of test framework p2p connections to the node."""
595-
return len([peer for peer in self.getpeerinfo() if peer['subver'] == MY_SUBVERSION.decode("utf-8")])
595+
return len([peer for peer in self.getpeerinfo() if peer['subver'] == MY_SUBVERSION])
596596

597597
def disconnect_p2ps(self):
598598
"""Close all p2p connections to the node."""
@@ -603,7 +603,7 @@ def disconnect_p2ps(self):
603603
def check_peers():
604604
for p in self.getpeerinfo():
605605
for p2p in self.p2ps:
606-
if p['subver'] == p2p.strSubVer.decode():
606+
if p['subver'] == p2p.strSubVer:
607607
return False
608608
return True
609609
wait_until_helper(check_peers, timeout=5)

0 commit comments

Comments
 (0)