Skip to content

Commit 3bd67ba

Browse files
committed
Test addr response caching
1 parent cf1569e commit 3bd67ba

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 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+
"""Test addr response caching"""
6+
7+
import time
8+
from test_framework.messages import (
9+
CAddress,
10+
NODE_NETWORK,
11+
NODE_WITNESS,
12+
msg_addr,
13+
msg_getaddr,
14+
)
15+
from test_framework.mininode import (
16+
P2PInterface,
17+
mininode_lock
18+
)
19+
from test_framework.test_framework import BitcoinTestFramework
20+
from test_framework.util import (
21+
assert_equal,
22+
)
23+
24+
MAX_ADDR_TO_SEND = 1000
25+
26+
def gen_addrs(n):
27+
addrs = []
28+
for i in range(n):
29+
addr = CAddress()
30+
addr.time = int(time.time())
31+
addr.nServices = NODE_NETWORK | NODE_WITNESS
32+
# Use first octets to occupy different AddrMan buckets
33+
first_octet = i >> 8
34+
second_octet = i % 256
35+
addr.ip = "{}.{}.1.1".format(first_octet, second_octet)
36+
addr.port = 8333
37+
addrs.append(addr)
38+
return addrs
39+
40+
class AddrReceiver(P2PInterface):
41+
42+
def __init__(self):
43+
super().__init__()
44+
self.received_addrs = None
45+
46+
def get_received_addrs(self):
47+
with mininode_lock:
48+
return self.received_addrs
49+
50+
def on_addr(self, message):
51+
self.received_addrs = []
52+
for addr in message.addrs:
53+
self.received_addrs.append(addr.ip)
54+
55+
def addr_received(self):
56+
return self.received_addrs is not None
57+
58+
59+
class AddrTest(BitcoinTestFramework):
60+
def set_test_params(self):
61+
self.setup_clean_chain = False
62+
self.num_nodes = 1
63+
64+
def run_test(self):
65+
self.log.info('Create connection that sends and requests addr messages')
66+
addr_source = self.nodes[0].add_p2p_connection(P2PInterface())
67+
68+
msg_send_addrs = msg_addr()
69+
self.log.info('Fill peer AddrMan with a lot of records')
70+
# Since these addrs are sent from the same source, not all of them will be stored,
71+
# because we allocate a limited number of AddrMan buckets per addr source.
72+
total_addrs = 10000
73+
addrs = gen_addrs(total_addrs)
74+
for i in range(int(total_addrs/MAX_ADDR_TO_SEND)):
75+
msg_send_addrs.addrs = addrs[i * MAX_ADDR_TO_SEND:(i + 1) * MAX_ADDR_TO_SEND]
76+
addr_source.send_and_ping(msg_send_addrs)
77+
78+
responses = []
79+
self.log.info('Send many addr requests within short time to receive same response')
80+
N = 5
81+
cur_mock_time = int(time.time())
82+
for i in range(N):
83+
addr_receiver = self.nodes[0].add_p2p_connection(AddrReceiver())
84+
addr_receiver.send_and_ping(msg_getaddr())
85+
# Trigger response
86+
cur_mock_time += 5 * 60
87+
self.nodes[0].setmocktime(cur_mock_time)
88+
addr_receiver.wait_until(addr_receiver.addr_received)
89+
responses.append(addr_receiver.get_received_addrs())
90+
for response in responses[1:]:
91+
assert_equal(response, responses[0])
92+
assert(len(response) < MAX_ADDR_TO_SEND)
93+
94+
cur_mock_time += 3 * 24 * 60 * 60
95+
self.nodes[0].setmocktime(cur_mock_time)
96+
97+
self.log.info('After time passed, see a new response to addr request')
98+
last_addr_receiver = self.nodes[0].add_p2p_connection(AddrReceiver())
99+
last_addr_receiver.send_and_ping(msg_getaddr())
100+
# Trigger response
101+
cur_mock_time += 5 * 60
102+
self.nodes[0].setmocktime(cur_mock_time)
103+
last_addr_receiver.wait_until(last_addr_receiver.addr_received)
104+
# new response is different
105+
assert(set(responses[0]) != set(last_addr_receiver.get_received_addrs()))
106+
107+
108+
if __name__ == '__main__':
109+
AddrTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
'rpc_deprecated.py',
160160
'wallet_disable.py',
161161
'p2p_addr_relay.py',
162+
'p2p_getaddr_caching.py',
162163
'p2p_getdata.py',
163164
'rpc_net.py',
164165
'wallet_keypool.py',

0 commit comments

Comments
 (0)