Skip to content

Commit 5730a43

Browse files
test: Add functional test for AddrFetch connections
Co-authored-by: Amiti Uttarwar <[email protected]>
1 parent c34ad33 commit 5730a43

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

test/functional/p2p_addrfetch.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2021 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""
6+
Test p2p addr-fetch connections
7+
"""
8+
9+
import time
10+
11+
from test_framework.messages import msg_addr, CAddress, NODE_NETWORK, NODE_WITNESS
12+
from test_framework.p2p import P2PInterface, p2p_lock
13+
from test_framework.test_framework import BitcoinTestFramework
14+
from test_framework.util import assert_equal
15+
16+
ADDR = CAddress()
17+
ADDR.time = int(time.time())
18+
ADDR.nServices = NODE_NETWORK | NODE_WITNESS
19+
ADDR.ip = "192.0.0.8"
20+
ADDR.port = 18444
21+
22+
23+
class P2PAddrFetch(BitcoinTestFramework):
24+
25+
def set_test_params(self):
26+
self.setup_clean_chain = True
27+
self.num_nodes = 1
28+
29+
def run_test(self):
30+
node = self.nodes[0]
31+
self.log.info("Connect to an addr-fetch peer")
32+
peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, connection_type="addr-fetch")
33+
info = node.getpeerinfo()
34+
assert_equal(len(info), 1)
35+
assert_equal(info[0]['connection_type'], 'addr-fetch')
36+
37+
self.log.info("Check that we send getaddr but don't try to sync headers with the addr-fetch peer")
38+
peer.sync_send_with_ping()
39+
with p2p_lock:
40+
assert peer.message_count['getaddr'] == 1
41+
assert peer.message_count['getheaders'] == 0
42+
43+
self.log.info("Check that answering the getaddr with a single address does not lead to disconnect")
44+
# This prevents disconnecting on self-announcements
45+
msg = msg_addr()
46+
msg.addrs = [ADDR]
47+
peer.send_and_ping(msg)
48+
assert_equal(len(node.getpeerinfo()), 1)
49+
50+
self.log.info("Check that answering with larger addr messages leads to disconnect")
51+
msg.addrs = [ADDR] * 2
52+
peer.send_message(msg)
53+
peer.wait_for_disconnect(timeout=5)
54+
55+
self.log.info("Check timeout for addr-fetch peer that does not send addrs")
56+
peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, connection_type="addr-fetch")
57+
node.setmocktime(int(time.time()) + 301) # Timeout: 5 minutes
58+
peer.wait_for_disconnect(timeout=5)
59+
60+
61+
if __name__ == '__main__':
62+
P2PAddrFetch().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
'p2p_addr_relay.py',
183183
'p2p_getaddr_caching.py',
184184
'p2p_getdata.py',
185+
'p2p_addrfetch.py',
185186
'rpc_net.py',
186187
'wallet_keypool.py --legacy-wallet',
187188
'wallet_keypool.py --descriptors',

0 commit comments

Comments
 (0)