Skip to content

Commit c2f5a3b

Browse files
committed
[test] Change P2P_SUBVERSION to be a string
This forces the user to convert to bytes to encode it in a version message, which would result in an easy-to-detect type error if omitted. Also add a check that new connections from the test framework to the node have the correct user agent string. Again, this makes bugs easier to detect if the user agent string ever changes.
1 parent e808800 commit c2f5a3b

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

test/functional/p2p_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def run_test(self):
227227
version_without_fRelay = msg_version()
228228
version_without_fRelay.nVersion = P2P_VERSION
229229
version_without_fRelay.nServices = P2P_SERVICES
230-
version_without_fRelay.strSubVer = P2P_SUBVERSION
230+
version_without_fRelay.strSubVer = P2P_SUBVERSION.encode('utf-8')
231231
version_without_fRelay.relay = 0
232232
filter_peer_without_nrelay.send_message(version_without_fRelay)
233233
filter_peer_without_nrelay.wait_for_verack()

test/functional/p2p_leak.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def run_test(self):
154154
old_version_msg = msg_version()
155155
old_version_msg.nVersion = 31799
156156
old_version_msg.nServices = P2P_SERVICES
157-
old_version_msg.strSubVer = P2P_SUBVERSION
157+
old_version_msg.strSubVer = P2P_SUBVERSION.encode('utf-8')
158158
old_version_msg.relay = P2P_VERSION_RELAY
159159
with self.nodes[0].assert_debug_log(['peer=4 using obsolete version 31799; disconnecting']):
160160
p2p_old_peer.send_message(old_version_msg)

test/functional/test_framework/p2p.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
# The services that this test framework offers in its `version` message
8383
P2P_SERVICES = NODE_NETWORK | NODE_WITNESS
8484
# The P2P user agent string that this test framework sends in its `version` message
85-
P2P_SUBVERSION = b"/python-p2p-tester:0.0.3/"
85+
P2P_SUBVERSION = "/python-p2p-tester:0.0.3/"
8686
# Value for relay that this test framework sends in its `version` message
8787
P2P_VERSION_RELAY = 1
8888

@@ -328,7 +328,7 @@ def peer_connect(self, *args, services=P2P_SERVICES, send_version=True, **kwargs
328328
# Send a version msg
329329
vt = msg_version()
330330
vt.nVersion = P2P_VERSION
331-
vt.strSubVer = P2P_SUBVERSION
331+
vt.strSubVer = P2P_SUBVERSION.encode('utf-8')
332332
vt.relay = P2P_VERSION_RELAY
333333
vt.nServices = services
334334
vt.addrTo.ip = self.dstaddr

test/functional/test_framework/test_node.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .p2p import P2P_SUBVERSION
2727
from .util import (
2828
MAX_NODES,
29+
assert_equal,
2930
append_config,
3031
delete_cookie_file,
3132
get_auth_cookie,
@@ -540,11 +541,16 @@ def add_p2p_connection(self, p2p_conn, *, wait_for_verack=True, **kwargs):
540541
# in comparison to the upside of making tests less fragile and unexpected intermittent errors less likely.
541542
p2p_conn.sync_with_ping()
542543

544+
# Consistency check that the Bitcoin Core has received our user agent string. This checks the
545+
# node's newest peer. It could be racy if another Bitcoin Core node has connected since we opened
546+
# our connection, but we don't expect that to happen.
547+
assert_equal(self.getpeerinfo()[-1]['subver'], P2P_SUBVERSION)
548+
543549
return p2p_conn
544550

545551
def num_test_p2p_connections(self):
546552
"""Return number of test framework p2p connections to the node."""
547-
return len([peer for peer in self.getpeerinfo() if peer['subver'] == P2P_SUBVERSION.decode("utf-8")])
553+
return len([peer for peer in self.getpeerinfo() if peer['subver'] == P2P_SUBVERSION])
548554

549555
def disconnect_p2ps(self):
550556
"""Close all p2p connections to the node."""

0 commit comments

Comments
 (0)