This PR improves the following fragile construct for detection of a new connection to the node under test in p2p_v2_transport.py
:
https://github.com/bitcoin/bitcoin/blob/6d5790956f45e3de5c6c4ee6fda21878b0d1287b/test/functional/p2p_v2_transport.py#L154-L156
Only relying on the number of peers for that suffers from race conditions, as unrelated previous peers could disconnect at anytime in-between. In the test run in #29002, the following happens:
getpeerinfo()
is called the first time -> assigned tonum_peers
- previous peer disconnects, the node’s peer count is now
num_peers - 1
(in most test runs, this happens before the first getpeerinfo call) - new peer connects, the node’s peer count is now
num_peers
- the condition that the node’s peer count is
num_peers + 1
is never true, test fails
Use the more robust approach of watching for an increased highest peer id instead (again using the getpeerinfo
RPC call), with a newly introduced context manager method TestNode.wait_for_new_peer()
. Note that for the opposite case of a disconnect, no new method is introduced; this is currently used only once in the test and is also simpler.
Still happy to take suggestions for alternative solutions.
Fixes #29002.