Skip to content

Commit 8188b77

Browse files
[test] Add tests for getaddr behavior
Co-authored-by: Amiti Uttarwar <[email protected]>
1 parent d2dbfe6 commit 8188b77

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

test/functional/p2p_addr_relay.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
NODE_NETWORK,
1212
NODE_WITNESS,
1313
msg_addr,
14+
msg_getaddr
1415
)
1516
from test_framework.p2p import P2PInterface
1617
from test_framework.test_framework import BitcoinTestFramework
@@ -32,6 +33,21 @@ def on_addr(self, message):
3233
self.num_ipv4_received += 1
3334

3435

36+
class GetAddrStore(P2PInterface):
37+
getaddr_received = False
38+
num_ipv4_received = 0
39+
40+
def on_getaddr(self, message):
41+
self.getaddr_received = True
42+
43+
def on_addr(self, message):
44+
for addr in message.addrs:
45+
self.num_ipv4_received += 1
46+
47+
def addr_received(self):
48+
return self.num_ipv4_received != 0
49+
50+
3551
class AddrTest(BitcoinTestFramework):
3652
counter = 0
3753
mocktime = int(time.time())
@@ -42,6 +58,7 @@ def set_test_params(self):
4258
def run_test(self):
4359
self.oversized_addr_test()
4460
self.relay_tests()
61+
self.getaddr_tests()
4562

4663
def setup_addr_msg(self, num):
4764
addrs = []
@@ -105,6 +122,42 @@ def relay_tests(self):
105122

106123
self.nodes[0].disconnect_p2ps()
107124

125+
def getaddr_tests(self):
126+
self.log.info('Test getaddr behavior')
127+
self.log.info('Check that we send a getaddr message upon connecting to an outbound-full-relay peer')
128+
full_outbound_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=0, connection_type="outbound-full-relay")
129+
full_outbound_peer.sync_with_ping()
130+
assert full_outbound_peer.getaddr_received
131+
132+
self.log.info('Check that we do not send a getaddr message upon connecting to a block-relay-only peer')
133+
block_relay_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=1, connection_type="block-relay-only")
134+
block_relay_peer.sync_with_ping()
135+
assert_equal(block_relay_peer.getaddr_received, False)
136+
137+
self.log.info('Check that we answer getaddr messages only from inbound peers')
138+
inbound_peer = self.nodes[0].add_p2p_connection(GetAddrStore())
139+
inbound_peer.sync_with_ping()
140+
# Add some addresses to addrman
141+
for i in range(1000):
142+
first_octet = i >> 8
143+
second_octet = i % 256
144+
a = f"{first_octet}.{second_octet}.1.1"
145+
self.nodes[0].addpeeraddress(a, 8333)
146+
147+
full_outbound_peer.send_and_ping(msg_getaddr())
148+
block_relay_peer.send_and_ping(msg_getaddr())
149+
inbound_peer.send_and_ping(msg_getaddr())
150+
151+
self.mocktime += 5 * 60
152+
self.nodes[0].setmocktime(self.mocktime)
153+
inbound_peer.wait_until(inbound_peer.addr_received)
154+
155+
assert_equal(full_outbound_peer.num_ipv4_received, 0)
156+
assert_equal(block_relay_peer.num_ipv4_received, 0)
157+
assert inbound_peer.num_ipv4_received > 100
158+
159+
self.nodes[0].disconnect_p2ps()
160+
108161

109162
if __name__ == '__main__':
110163
AddrTest().main()

0 commit comments

Comments
 (0)