Skip to content

Commit

Permalink
Eliminate race condition in sendheaders.py test
Browse files Browse the repository at this point in the history
Clear the last block announcement before mining new blocks.
  • Loading branch information
sdaftuar committed Jan 7, 2016
1 parent 82a0ce0 commit 168915e
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions qa/rpc-tests/sendheaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,20 @@ def setup_network(self):

# mine count blocks and return the new tip
def mine_blocks(self, count):
# Clear out last block announcement from each p2p listener
[ x.clear_last_announcement() for x in self.p2p_connections ]
self.nodes[0].generate(count)
return int(self.nodes[0].getbestblockhash(), 16)

# mine a reorg that invalidates length blocks (replacing them with
# length+1 blocks).
# peers is the p2p nodes we're using; we clear their state after the
# Note: we clear the state of our p2p connections after the
# to-be-reorged-out blocks are mined, so that we don't break later tests.
# return the list of block hashes newly mined
def mine_reorg(self, length, peers):
def mine_reorg(self, length):
self.nodes[0].generate(length) # make sure all invalidated blocks are node0's
sync_blocks(self.nodes, wait=0.1)
[x.clear_last_announcement() for x in peers]
[x.clear_last_announcement() for x in self.p2p_connections]

tip_height = self.nodes[1].getblockcount()
hash_to_invalidate = self.nodes[1].getblockhash(tip_height-(length-1))
Expand All @@ -245,6 +247,8 @@ def run_test(self):
inv_node = InvNode()
test_node = TestNode()

self.p2p_connections = [inv_node, test_node]

connections = []
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], inv_node))
# Set nServices to 0 for test_node, so no block download will occur outside of
Expand Down Expand Up @@ -303,7 +307,6 @@ def run_test(self):
prev_tip = int(self.nodes[0].getbestblockhash(), 16)
test_node.get_headers(locator=[prev_tip], hashstop=0L)
test_node.sync_with_ping()
test_node.clear_last_announcement() # Clear out empty headers response

# Now that we've synced headers, headers announcements should work
tip = self.mine_blocks(1)
Expand Down Expand Up @@ -352,8 +355,6 @@ def run_test(self):
# broadcast it)
assert_equal(inv_node.last_inv, None)
assert_equal(inv_node.last_headers, None)
inv_node.clear_last_announcement()
test_node.clear_last_announcement()
tip = self.mine_blocks(1)
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
assert_equal(test_node.check_last_announcement(headers=[tip]), True)
Expand All @@ -368,15 +369,15 @@ def run_test(self):
# getheaders or inv from peer.
for j in xrange(2):
# First try mining a reorg that can propagate with header announcement
new_block_hashes = self.mine_reorg(length=7, peers=[test_node, inv_node])
new_block_hashes = self.mine_reorg(length=7)
tip = new_block_hashes[-1]
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
assert_equal(test_node.check_last_announcement(headers=new_block_hashes), True)

block_time += 8

# Mine a too-large reorg, which should be announced with a single inv
new_block_hashes = self.mine_reorg(length=8, peers=[test_node, inv_node])
new_block_hashes = self.mine_reorg(length=8)
tip = new_block_hashes[-1]
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
assert_equal(test_node.check_last_announcement(inv=[tip]), True)
Expand Down Expand Up @@ -407,7 +408,6 @@ def run_test(self):
test_node.get_headers(locator=[fork_point], hashstop=new_block_hashes[1])
test_node.get_data([tip])
test_node.wait_for_block(tip)
test_node.clear_last_announcement()
elif i == 2:
test_node.get_data([tip])
test_node.wait_for_block(tip)
Expand Down

0 comments on commit 168915e

Please sign in to comment.