0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

test: Wait until is_connected in add_p2p_connection

This commit is contained in:
MarcoFalke 2020-08-04 12:55:35 +02:00
parent 3c93623be2
commit fa4dfd215f
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
4 changed files with 11 additions and 14 deletions

View file

@ -218,11 +218,6 @@ class FilterTest(BitcoinTestFramework):
# 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 fRelay=False
filter_peer_without_nrelay.wait_until(
lambda: filter_peer_without_nrelay.is_connected,
timeout=10,
check_connected=False,
)
version_without_fRelay = msg_version() version_without_fRelay = msg_version()
version_without_fRelay.nRelay = 0 version_without_fRelay.nRelay = 0
filter_peer_without_nrelay.send_message(version_without_fRelay) filter_peer_without_nrelay.send_message(version_without_fRelay)

View file

@ -63,16 +63,12 @@ class CLazyNode(P2PInterface):
def on_getblocktxn(self, message): self.bad_message(message) def on_getblocktxn(self, message): self.bad_message(message)
def on_blocktxn(self, message): self.bad_message(message) def on_blocktxn(self, message): self.bad_message(message)
# Node that never sends a version. We'll use this to send a bunch of messages # Node that never sends a version. We'll use this to send a bunch of messages
# anyway, and eventually get disconnected. # anyway, and eventually get disconnected.
class CNodeNoVersionMisbehavior(CLazyNode): class CNodeNoVersionMisbehavior(CLazyNode):
# Send enough veracks without a message to reach the peer discouragement pass
# threshold. This should get us disconnected. NOTE: implementation-specific
# test; update if our discouragement policy for peer misbehavior changes.
def on_open(self):
super().on_open()
for _ in range(DISCOURAGEMENT_THRESHOLD):
self.send_message(msg_verack())
# Node that never sends a version. This one just sits idle and hopes to receive # Node that never sends a version. This one just sits idle and hopes to receive
# any message (it shouldn't!) # any message (it shouldn't!)
@ -80,6 +76,7 @@ class CNodeNoVersionIdle(CLazyNode):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
# Node that sends a version but not a verack. # Node that sends a version but not a verack.
class CNodeNoVerackIdle(CLazyNode): class CNodeNoVerackIdle(CLazyNode):
def __init__(self): def __init__(self):
@ -114,6 +111,11 @@ class P2PLeakTest(BitcoinTestFramework):
no_version_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVersionIdle(), send_version=False, wait_for_verack=False) no_version_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVersionIdle(), send_version=False, wait_for_verack=False)
no_verack_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVerackIdle(), wait_for_verack=False) no_verack_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVerackIdle(), wait_for_verack=False)
# Send enough veracks without a message to reach the peer discouragement
# threshold. This should get us disconnected.
for _ in range(DISCOURAGEMENT_THRESHOLD):
no_version_disconnect_node.send_message(msg_verack())
# Wait until we got the verack in response to the version. Though, don't wait for the other node to receive the # Wait until we got the verack in response to the version. Though, don't wait for the other node to receive the
# verack, since we never sent one # verack, since we never sent one
no_verack_idlenode.wait_for_verack() no_verack_idlenode.wait_for_verack()
@ -153,7 +155,6 @@ class P2PLeakTest(BitcoinTestFramework):
p2p_old_node = self.nodes[0].add_p2p_connection(P2PInterface(), send_version=False, wait_for_verack=False) p2p_old_node = 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
wait_until(lambda: p2p_old_node.is_connected)
with self.nodes[0].assert_debug_log(['peer=4 using obsolete version 31799; disconnecting']): with self.nodes[0].assert_debug_log(['peer=4 using obsolete version 31799; disconnecting']):
p2p_old_node.send_message(old_version_msg) p2p_old_node.send_message(old_version_msg)
p2p_old_node.wait_for_disconnect() p2p_old_node.wait_for_disconnect()

View file

@ -474,7 +474,7 @@ class P2PInterface(P2PConnection):
def test_function(): def test_function():
return "verack" in self.last_message return "verack" in self.last_message
self.wait_until(test_function, timeout=timeout, check_connected=False) self.wait_until(test_function, timeout=timeout)
# Message sending helper functions # Message sending helper functions

View file

@ -524,6 +524,7 @@ class TestNode():
p2p_conn.peer_connect(**kwargs, net=self.chain, timeout_factor=self.timeout_factor)() p2p_conn.peer_connect(**kwargs, net=self.chain, timeout_factor=self.timeout_factor)()
self.p2ps.append(p2p_conn) self.p2ps.append(p2p_conn)
p2p_conn.wait_until(lambda: p2p_conn.is_connected, check_connected=False)
if wait_for_verack: if wait_for_verack:
# Wait for the node to send us the version and verack # Wait for the node to send us the version and verack
p2p_conn.wait_for_verack() p2p_conn.wait_for_verack()