Browse Source

[qa] Another attempt to fix race condition in p2p-compactblocks.py

sync_with_ping() only guarantees that the node has processed messages
it's received from the peer, not that block announcements from the node have
made it back to the peer.  Replace sync_with_ping() with an explicit check that
the node's tip has been announced.

Github-Pull: #8882
Rebased-From: 6976db2f46
0.13
Suhas Daftuar 8 years ago committed by MarcoFalke
parent
commit
b73f0653f3
  1. 22
      qa/rpc-tests/p2p-compactblocks.py

22
qa/rpc-tests/p2p-compactblocks.py

@ -28,6 +28,10 @@ class TestNode(SingleNodeConnCB):
self.last_getblocktxn = None self.last_getblocktxn = None
self.last_block = None self.last_block = None
self.last_blocktxn = None self.last_blocktxn = None
# Store the hashes of blocks we've seen announced.
# This is for synchronizing the p2p message traffic,
# so we can eg wait until a particular block is announced.
self.set_announced_blockhashes = set()
def on_sendcmpct(self, conn, message): def on_sendcmpct(self, conn, message):
self.last_sendcmpct = message self.last_sendcmpct = message
@ -38,14 +42,22 @@ class TestNode(SingleNodeConnCB):
def on_cmpctblock(self, conn, message): def on_cmpctblock(self, conn, message):
self.last_cmpctblock = message self.last_cmpctblock = message
self.block_announced = True self.block_announced = True
self.last_cmpctblock.header_and_shortids.header.calc_sha256()
self.set_announced_blockhashes.add(self.last_cmpctblock.header_and_shortids.header.sha256)
def on_headers(self, conn, message): def on_headers(self, conn, message):
self.last_headers = message self.last_headers = message
self.block_announced = True self.block_announced = True
for x in self.last_headers.headers:
x.calc_sha256()
self.set_announced_blockhashes.add(x.sha256)
def on_inv(self, conn, message): def on_inv(self, conn, message):
self.last_inv = message self.last_inv = message
for x in self.last_inv.inv:
if x.type == 2:
self.block_announced = True self.block_announced = True
self.set_announced_blockhashes.add(x.hash)
def on_getdata(self, conn, message): def on_getdata(self, conn, message):
self.last_getdata = message self.last_getdata = message
@ -85,6 +97,12 @@ class TestNode(SingleNodeConnCB):
assert(self.received_block_announcement()) assert(self.received_block_announcement())
self.clear_block_announcement() self.clear_block_announcement()
# Block until a block announcement for a particular block hash is
# received.
def wait_for_block_announcement(self, block_hash, timeout=30):
def received_hash():
return (block_hash in self.set_announced_blockhashes)
return wait_until(received_hash, timeout=timeout)
class CompactBlocksTest(BitcoinTestFramework): class CompactBlocksTest(BitcoinTestFramework):
def __init__(self): def __init__(self):
@ -237,7 +255,9 @@ class CompactBlocksTest(BitcoinTestFramework):
for i in range(num_transactions): for i in range(num_transactions):
self.nodes[0].sendtoaddress(address, 0.1) self.nodes[0].sendtoaddress(address, 0.1)
self.test_node.sync_with_ping() # Wait until we've seen the block announcement for the resulting tip
tip = int(self.nodes[0].getbestblockhash(), 16)
assert(self.test_node.wait_for_block_announcement(tip))
# Now mine a block, and look at the resulting compact block. # Now mine a block, and look at the resulting compact block.
self.test_node.clear_block_announcement() self.test_node.clear_block_announcement()

Loading…
Cancel
Save