Skip to content

Commit 6426716

Browse files
committed
Add send_await_disconnect() method to p2p-compactblocks.py
p2p-compactblocks was incorrectly using sync_with_ping() when sending in invalid block. The node would disconnect us and never respond to the ping, so the sync_with_ping would just time out after 30 seconds and continue with the test. This commit adds a send_await_disconnect() method that sends the message, and then waits for the node to disconnect us. In this commit I've added the method to p2p-compactblocks.py, but a future commit could move it to mininode since it could be useful more generally. This commit reduces the p2p-compactblock runtime by 30 seconds.
1 parent 8ac8041 commit 6426716

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

test/functional/p2p-compactblocks.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ def __init__(self):
3232
# This is for synchronizing the p2p message traffic,
3333
# so we can eg wait until a particular block is announced.
3434
self.set_announced_blockhashes = set()
35+
self.connected = False
36+
37+
def on_open(self, conn):
38+
self.connected = True
39+
40+
def on_close(self, conn):
41+
self.connected = False
3542

3643
def on_sendcmpct(self, conn, message):
3744
self.last_sendcmpct.append(message)
@@ -107,6 +114,18 @@ def received_hash():
107114
return (block_hash in self.set_announced_blockhashes)
108115
return wait_until(received_hash, timeout=timeout)
109116

117+
def send_await_disconnect(self, message, timeout=30):
118+
"""Sends a message to the node and wait for disconnect.
119+
120+
This is used when we want to send a message into the node that we expect
121+
will get us disconnected, eg an invalid block."""
122+
self.send_message(message)
123+
success = wait_until(lambda: not self.connected, timeout=timeout)
124+
if not success:
125+
logger.error("send_await_disconnect failed!")
126+
raise AssertionError("send_await_disconnect failed!")
127+
return success
128+
110129
class CompactBlocksTest(BitcoinTestFramework):
111130
def __init__(self):
112131
super().__init__()
@@ -274,8 +293,8 @@ def test_invalid_cmpctblock_message(self):
274293
# This index will be too high
275294
prefilled_txn = PrefilledTransaction(1, block.vtx[0])
276295
cmpct_block.prefilled_txn = [prefilled_txn]
277-
self.test_node.send_and_ping(msg_cmpctblock(cmpct_block))
278-
assert(int(self.nodes[0].getbestblockhash(), 16) == block.hashPrevBlock)
296+
self.test_node.send_await_disconnect(msg_cmpctblock(cmpct_block))
297+
assert_equal(int(self.nodes[0].getbestblockhash(), 16), block.hashPrevBlock)
279298

280299
# Compare the generated shortids to what we expect based on BIP 152, given
281300
# bitcoind's choice of nonce.

0 commit comments

Comments
 (0)