Skip to content

Commit fa90647

Browse files
author
MarcoFalke
committed
test: Wait for both veracks in add_p2p_connection
1 parent ac5c5d0 commit fa90647

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

test/functional/p2p_leak.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
banscore = 10
2121

22+
2223
class CLazyNode(P2PInterface):
2324
def __init__(self):
2425
super().__init__()
@@ -88,6 +89,7 @@ def on_version(self, message):
8889
self.send_message(msg_ping())
8990
self.send_message(msg_getaddr())
9091

92+
9193
class P2PLeakTest(BitcoinTestFramework):
9294
def set_test_params(self):
9395
self.num_nodes = 1
@@ -96,7 +98,11 @@ def set_test_params(self):
9698
def run_test(self):
9799
no_version_bannode = self.nodes[0].add_p2p_connection(CNodeNoVersionBan(), send_version=False, wait_for_verack=False)
98100
no_version_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVersionIdle(), send_version=False, wait_for_verack=False)
99-
no_verack_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVerackIdle())
101+
no_verack_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVerackIdle(), wait_for_verack=False)
102+
103+
# Wait until we got the verack in response to the version. Though, don't wait for the other node to receive the
104+
# verack, since we never sent one
105+
no_verack_idlenode.wait_for_verack()
100106

101107
wait_until(lambda: no_version_bannode.ever_connected, timeout=10, lock=mininode_lock)
102108
wait_until(lambda: no_version_idlenode.ever_connected, timeout=10, lock=mininode_lock)

test/functional/p2p_timeouts.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
from test_framework.mininode import P2PInterface
2828
from test_framework.test_framework import BitcoinTestFramework
2929

30+
3031
class TestP2PConn(P2PInterface):
3132
def on_version(self, message):
3233
# Don't send a verack in response
3334
pass
3435

36+
3537
class TimeoutsTest(BitcoinTestFramework):
3638
def set_test_params(self):
3739
self.setup_clean_chain = True
@@ -41,10 +43,14 @@ def set_test_params(self):
4143

4244
def run_test(self):
4345
# Setup the p2p connections
44-
no_verack_node = self.nodes[0].add_p2p_connection(TestP2PConn())
46+
no_verack_node = self.nodes[0].add_p2p_connection(TestP2PConn(), wait_for_verack=False)
4547
no_version_node = self.nodes[0].add_p2p_connection(TestP2PConn(), send_version=False, wait_for_verack=False)
4648
no_send_node = self.nodes[0].add_p2p_connection(TestP2PConn(), send_version=False, wait_for_verack=False)
4749

50+
# Wait until we got the verack in response to the version. Though, don't wait for the other node to receive the
51+
# verack, since we never sent one
52+
no_verack_node.wait_for_verack()
53+
4854
sleep(1)
4955

5056
assert no_verack_node.is_connected
@@ -81,5 +87,6 @@ def run_test(self):
8187
assert not no_version_node.is_connected
8288
assert not no_send_node.is_connected
8389

90+
8491
if __name__ == '__main__':
8592
TimeoutsTest().main()

test/functional/test_framework/test_node.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,19 @@ def add_p2p_connection(self, p2p_conn, *, wait_for_verack=True, **kwargs):
468468
p2p_conn.peer_connect(**kwargs, net=self.chain)()
469469
self.p2ps.append(p2p_conn)
470470
if wait_for_verack:
471+
# Wait for the node to send us the version and verack
471472
p2p_conn.wait_for_verack()
473+
# At this point we have sent our version message and received the version and verack, however the full node
474+
# has not yet received the verack from us (in reply to their version). So, the connection is not yet fully
475+
# established (fSuccessfullyConnected).
476+
#
477+
# This shouldn't lead to any issues when sending messages, since the verack will be in-flight before the
478+
# message we send. However, it might lead to races where we are expecting to receive a message. E.g. a
479+
# transaction that will be added to the mempool as soon as we return here.
480+
#
481+
# So syncing here is redundant when we only want to send a message, but the cost is low (a few milliseconds)
482+
# in comparision to the upside of making tests less fragile and unexpected intermittent errors less likely.
483+
p2p_conn.sync_with_ping()
472484

473485
return p2p_conn
474486

@@ -487,6 +499,7 @@ def disconnect_p2ps(self):
487499
p.peer_disconnect()
488500
del self.p2ps[:]
489501

502+
490503
class TestNodeCLIAttr:
491504
def __init__(self, cli, command):
492505
self.cli = cli
@@ -498,6 +511,7 @@ def __call__(self, *args, **kwargs):
498511
def get_request(self, *args, **kwargs):
499512
return lambda: self(*args, **kwargs)
500513

514+
501515
def arg_to_cli(arg):
502516
if isinstance(arg, bool):
503517
return str(arg).lower()
@@ -506,6 +520,7 @@ def arg_to_cli(arg):
506520
else:
507521
return str(arg)
508522

523+
509524
class TestNodeCLI():
510525
"""Interface to bitcoin-cli for an individual node"""
511526

0 commit comments

Comments
 (0)