@@ -28,6 +28,10 @@ def __init__(self):
2828 self .last_getblocktxn = None
2929 self .last_block = None
3030 self .last_blocktxn = None
31+ # Store the hashes of blocks we've seen announced.
32+ # This is for synchronizing the p2p message traffic,
33+ # so we can eg wait until a particular block is announced.
34+ self .set_announced_blockhashes = set ()
3135
3236 def on_sendcmpct (self , conn , message ):
3337 self .last_sendcmpct = message
@@ -38,14 +42,22 @@ def on_block(self, conn, message):
3842 def on_cmpctblock (self , conn , message ):
3943 self .last_cmpctblock = message
4044 self .block_announced = True
45+ self .last_cmpctblock .header_and_shortids .header .calc_sha256 ()
46+ self .set_announced_blockhashes .add (self .last_cmpctblock .header_and_shortids .header .sha256 )
4147
4248 def on_headers (self , conn , message ):
4349 self .last_headers = message
4450 self .block_announced = True
51+ for x in self .last_headers .headers :
52+ x .calc_sha256 ()
53+ self .set_announced_blockhashes .add (x .sha256 )
4554
4655 def on_inv (self , conn , message ):
4756 self .last_inv = message
48- self .block_announced = True
57+ for x in self .last_inv .inv :
58+ if x .type == 2 :
59+ self .block_announced = True
60+ self .set_announced_blockhashes .add (x .hash )
4961
5062 def on_getdata (self , conn , message ):
5163 self .last_getdata = message
@@ -85,6 +97,12 @@ def request_headers_and_sync(self, locator, hashstop=0):
8597 assert (self .received_block_announcement ())
8698 self .clear_block_announcement ()
8799
100+ # Block until a block announcement for a particular block hash is
101+ # received.
102+ def wait_for_block_announcement (self , block_hash , timeout = 30 ):
103+ def received_hash ():
104+ return (block_hash in self .set_announced_blockhashes )
105+ return wait_until (received_hash , timeout = timeout )
88106
89107class CompactBlocksTest (BitcoinTestFramework ):
90108 def __init__ (self ):
@@ -237,7 +255,9 @@ def test_compactblock_construction(self):
237255 for i in range (num_transactions ):
238256 self .nodes [0 ].sendtoaddress (address , 0.1 )
239257
240- self .test_node .sync_with_ping ()
258+ # Wait until we've seen the block announcement for the resulting tip
259+ tip = int (self .nodes [0 ].getbestblockhash (), 16 )
260+ assert (self .test_node .wait_for_block_announcement (tip ))
241261
242262 # Now mine a block, and look at the resulting compact block.
243263 self .test_node .clear_block_announcement ()
0 commit comments