Skip to content

Commit

Permalink
test: more coverage of at tip stalling behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Oct 11, 2024
1 parent 32d8faf commit cfca7d4
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions test/functional/p2p_compactblocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test compact blocks (BIP 152)."""
import random
import time

from test_framework.blocktools import (
COINBASE_MATURITY,
Expand Down Expand Up @@ -109,6 +110,10 @@ def clear_getblocktxn(self):
with p2p_lock:
self.last_message.pop("getblocktxn", None)

def clear_getdata(self):
with p2p_lock:
self.last_message.pop("getdata", None)

def get_headers(self, locator, hashstop):
msg = msg_getheaders()
msg.locator.vHave = locator
Expand Down Expand Up @@ -834,6 +839,9 @@ def test_compactblock_reconstruction_parallel_reconstruction(self, stalling_peer
node = self.nodes[0]
assert len(self.utxos)

self.mocktime = int(time.time())
node.setmocktime(self.mocktime)

def announce_cmpct_block(node, peer, txn_count):
utxo = self.utxos.pop(0)
block = self.build_block_with_transactions(node, utxo, txn_count)
Expand All @@ -854,7 +862,10 @@ def announce_cmpct_block(node, peer, txn_count):
msg.block_transactions.transactions = block.vtx[1:]
peer.send_and_ping(msg)
assert_equal(int(node.getbestblockhash(), 16), block.sha256)

for peer in self.all_peers:
peer.clear_getblocktxn()
peer.clear_getdata()

# Test the simple parallel download case...
for num_missing in [1, 5, 20]:
Expand All @@ -864,12 +875,15 @@ def announce_cmpct_block(node, peer, txn_count):

block, cmpct_block = announce_cmpct_block(node, stalling_peer, num_missing)

non_fetched_peer = None

inbound_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
with p2p_lock:
# The inbound peer to announce should *not* get a getblocktxn
# unless first peer was already outbound
if staller_inbound:
assert "getblocktxn" not in inbound_peer.last_message
non_fetched_peer = inbound_peer
else:
assert "getblocktxn" in inbound_peer.last_message
assert int(node.getbestblockhash(), 16) != block.sha256
Expand All @@ -882,8 +896,19 @@ def announce_cmpct_block(node, peer, txn_count):
assert "getblocktxn" in outbound_peer.last_message
else:
assert "getblocktxn" not in outbound_peer.last_message
non_fetched_peer = outbound_peer
assert int(node.getbestblockhash(), 16) != block.sha256

# If we wait 30 seconds, getdata for full block should be emitted
# to a peer not already selected for download

# No blocks have been directly fetched yet
assert_equal(sum("getdata" in peer.last_message for peer in self.all_peers), 0)
self.mocktime += 31
node.setmocktime(self.mocktime)
self.wait_until(lambda: "getdata" in non_fetched_peer.last_message)
self.wait_until(lambda: sum("getdata" in peer.last_message for peer in self.all_peers) == 1)

# Non staller peer that was requested completes the compact block first
msg = msg_blocktxn()
msg.block_transactions.blockhash = block.sha256
Expand All @@ -898,9 +923,9 @@ def announce_cmpct_block(node, peer, txn_count):
stalling_peer.send_and_ping(msg)
self.utxos.append([block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue])

inbound_peer.clear_getblocktxn()
outbound_peer.clear_getblocktxn()

for peer in self.all_peers:
peer.clear_getblocktxn()
peer.clear_getdata()

def run_test(self):
self.wallet = MiniWallet(self.nodes[0])
Expand All @@ -911,6 +936,7 @@ def run_test(self):
self.onemore_inbound_node = self.nodes[0].add_p2p_connection(TestP2PConn())
self.outbound_node = self.nodes[0].add_outbound_p2p_connection(TestP2PConn(), p2p_idx=3, connection_type="outbound-full-relay")
self.additional_outbound_node = self.nodes[0].add_outbound_p2p_connection(TestP2PConn(), p2p_idx=4, connection_type="outbound-full-relay")
self.all_peers = [self.segwit_node, self.additional_segwit_node, self.onemore_inbound_node, self.outbound_node, self.additional_outbound_node]

# We will need UTXOs to construct transactions in later tests.
self.make_utxos()
Expand Down

0 comments on commit cfca7d4

Please sign in to comment.