Skip to content

Commit 14ae71f

Browse files
test: make notfound_on_unannounced more reliable
By using mocktime, we will always hit both the notfound branch and the tx sent branch. The previous version didn't achieve that due to timing issues. Co-authored-by: Martin Zumsande <[email protected]>
1 parent 99bc552 commit 14ae71f

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

test/functional/p2p_leak_tx.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,31 @@ def test_notfound_on_unannounced_tx(self):
102102
self.gen_node.disconnect_p2ps()
103103
inbound_peer = self.gen_node.add_p2p_connection(P2PNode()) # An "attacking" inbound peer
104104

105-
MAX_REPEATS = 100
106-
self.log.info("Running test up to {} times.".format(MAX_REPEATS))
107-
for i in range(MAX_REPEATS):
108-
self.log.info('Run repeat {}'.format(i + 1))
109-
wtxid = self.miniwallet.send_self_transfer(from_node=self.gen_node)["wtxid"]
110-
111-
want_tx = msg_getdata()
112-
want_tx.inv.append(CInv(t=MSG_WTX, h=int(wtxid, 16)))
113-
with p2p_lock:
114-
inbound_peer.last_message.pop('notfound', None)
115-
inbound_peer.send_and_ping(want_tx)
116-
if inbound_peer.last_message.get('notfound'):
117-
self.log.debug('tx {} was not yet announced to us.'.format(wtxid))
118-
self.log.debug("node has responded with a notfound message. End test.")
119-
assert_equal(inbound_peer.last_message['notfound'].vec[0].hash, int(wtxid, 16))
120-
with p2p_lock:
121-
inbound_peer.last_message.pop('notfound')
122-
break
123-
else:
124-
self.log.debug('tx {} was already announced to us. Try test again.'.format(wtxid))
125-
assert int(wtxid, 16) in [inv.hash for inv in inbound_peer.last_message['inv'].inv]
105+
# Set a mock time so that time does not pass, and gen_node never announces the transaction
106+
self.gen_node.setmocktime(self.mocktime)
107+
wtxid = int(self.miniwallet.send_self_transfer(from_node=self.gen_node)["wtxid"], 16)
108+
109+
want_tx = msg_getdata()
110+
want_tx.inv.append(CInv(t=MSG_WTX, h=wtxid))
111+
with p2p_lock:
112+
inbound_peer.last_message.pop('notfound', None)
113+
inbound_peer.send_and_ping(want_tx)
114+
inbound_peer.wait_until(lambda: "notfound" in inbound_peer.last_message)
115+
with p2p_lock:
116+
assert_equal(inbound_peer.last_message.get("notfound").vec[0].hash, wtxid)
117+
inbound_peer.last_message.pop('notfound')
118+
119+
# Move mocktime forward and wait for the announcement.
120+
inbound_peer.last_message.pop('inv', None)
121+
self.mocktime += 120
122+
self.gen_node.setmocktime(self.mocktime)
123+
inbound_peer.wait_for_inv([CInv(t=MSG_WTX, h=wtxid)], timeout=120)
124+
125+
# Send the getdata again, this time the node should send us a TX message.
126+
inbound_peer.last_message.pop('tx', None)
127+
inbound_peer.send_and_ping(want_tx)
128+
self.wait_until(lambda: "tx" in inbound_peer.last_message)
129+
assert_equal(wtxid, int(inbound_peer.last_message["tx"].tx.wtxid_hex, 16))
126130

127131

128132
if __name__ == '__main__':

0 commit comments

Comments
 (0)