@@ -28,6 +28,10 @@ def __init__(self):
28
28
self .last_getblocktxn = None
29
29
self .last_block = None
30
30
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 ()
31
35
32
36
def on_sendcmpct (self , conn , message ):
33
37
self .last_sendcmpct = message
@@ -38,14 +42,22 @@ def on_block(self, conn, message):
38
42
def on_cmpctblock (self , conn , message ):
39
43
self .last_cmpctblock = message
40
44
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 )
41
47
42
48
def on_headers (self , conn , message ):
43
49
self .last_headers = message
44
50
self .block_announced = True
51
+ for x in self .last_headers .headers :
52
+ x .calc_sha256 ()
53
+ self .set_announced_blockhashes .add (x .sha256 )
45
54
46
55
def on_inv (self , conn , message ):
47
56
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 )
49
61
50
62
def on_getdata (self , conn , message ):
51
63
self .last_getdata = message
@@ -85,6 +97,12 @@ def request_headers_and_sync(self, locator, hashstop=0):
85
97
assert (self .received_block_announcement ())
86
98
self .clear_block_announcement ()
87
99
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 )
88
106
89
107
class CompactBlocksTest (BitcoinTestFramework ):
90
108
def __init__ (self ):
@@ -237,7 +255,9 @@ def test_compactblock_construction(self):
237
255
for i in range (num_transactions ):
238
256
self .nodes [0 ].sendtoaddress (address , 0.1 )
239
257
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 ))
241
261
242
262
# Now mine a block, and look at the resulting compact block.
243
263
self .test_node .clear_block_announcement ()
0 commit comments