Branch data Line data Source code
1 : : // Copyright (c) 2012-2022 The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <addrdb.h>
6 : : #include <addrman.h>
7 : : #include <addrman_impl.h>
8 : : #include <chainparams.h>
9 : : #include <clientversion.h>
10 : : #include <hash.h>
11 : : #include <netbase.h>
12 : : #include <random.h>
13 : : #include <test/data/asmap.raw.h>
14 : : #include <test/util/setup_common.h>
15 : : #include <util/asmap.h>
16 : : #include <util/string.h>
17 : :
18 : : #include <boost/test/unit_test.hpp>
19 : :
20 : : #include <optional>
21 : : #include <string>
22 : :
23 : : using namespace std::literals;
24 : : using node::NodeContext;
25 : : using util::ToString;
26 : :
27 : : static NetGroupManager EMPTY_NETGROUPMAN{std::vector<bool>()};
28 : : static const bool DETERMINISTIC{true};
29 : :
30 : 24 : static int32_t GetCheckRatio(const NodeContext& node_ctx)
31 : : {
32 [ + - + - ]: 48 : return std::clamp<int32_t>(node_ctx.args->GetIntArg("-checkaddrman", 100), 0, 1000000);
33 : : }
34 : :
35 : 6819 : static CNetAddr ResolveIP(const std::string& ip)
36 : : {
37 [ + - ]: 6819 : const std::optional<CNetAddr> addr{LookupHost(ip, false)};
38 [ + - + - : 13638 : BOOST_CHECK_MESSAGE(addr.has_value(), strprintf("failed to resolve: %s", ip));
+ - ]
39 [ + - ]: 6819 : return addr.value_or(CNetAddr{});
40 : 6819 : }
41 : :
42 : 6629 : static CService ResolveService(const std::string& ip, uint16_t port = 0)
43 : : {
44 [ + - ]: 6629 : const std::optional<CService> serv{Lookup(ip, port, false)};
45 [ + - + - : 13258 : BOOST_CHECK_MESSAGE(serv.has_value(), strprintf("failed to resolve: %s:%i", ip, port));
+ - ]
46 [ + - ]: 6629 : return serv.value_or(CService{});
47 : 6629 : }
48 : :
49 : :
50 : 3 : static std::vector<bool> FromBytes(std::span<const std::byte> source)
51 : : {
52 : 3 : int vector_size(source.size() * 8);
53 : 3 : std::vector<bool> result(vector_size);
54 [ + + ]: 180 : for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
55 : 177 : uint8_t cur_byte{std::to_integer<uint8_t>(source[byte_i])};
56 [ + + ]: 1593 : for (int bit_i = 0; bit_i < 8; ++bit_i) {
57 : 1416 : result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
58 : : }
59 : : }
60 : 3 : return result;
61 : : }
62 : :
63 : : BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
64 : :
65 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_simple)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
66 : : {
67 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
68 : :
69 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
70 : :
71 : : // Test: Does Addrman respond correctly when empty.
72 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 0U);
+ - ]
73 [ + - ]: 1 : auto addr_null = addrman->Select().first;
74 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr_null.ToStringAddrPort(), "[::]:0");
+ - ]
75 : :
76 : : // Test: Does Addrman::Add work as expected.
77 [ + - + - ]: 1 : CService addr1 = ResolveService("250.1.1.1", 8333);
78 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
79 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 1U);
+ - ]
80 [ + - ]: 1 : auto addr_ret1 = addrman->Select().first;
81 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr_ret1.ToStringAddrPort(), "250.1.1.1:8333");
+ - ]
82 : :
83 : : // Test: Does IP address deduplication work correctly.
84 : : // Expected dup IP should not be added.
85 [ + - + - ]: 1 : CService addr1_dup = ResolveService("250.1.1.1", 8333);
86 [ + - + - : 5 : BOOST_CHECK(!addrman->Add({CAddress(addr1_dup, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
87 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 1U);
+ - ]
88 : :
89 : :
90 : : // Test: New table has one addr and we add a diff addr we should
91 : : // have at least one addr.
92 : : // Note that addrman's size cannot be tested reliably after insertion, as
93 : : // hash collisions may occur. But we can always be sure of at least one
94 : : // success.
95 : :
96 [ + - + - ]: 1 : CService addr2 = ResolveService("250.1.1.2", 8333);
97 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr2, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
98 [ + - + - : 2 : BOOST_CHECK(addrman->Size() >= 1);
+ - + - ]
99 : :
100 : : // Test: reset addrman and test AddrMan::Add multiple addresses works as expected
101 [ + - + - ]: 2 : addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
102 : 1 : std::vector<CAddress> vAddr;
103 [ + - + - : 2 : vAddr.emplace_back(ResolveService("250.1.1.3", 8333), NODE_NONE);
+ - ]
104 [ + - + - : 2 : vAddr.emplace_back(ResolveService("250.1.1.4", 8333), NODE_NONE);
+ - ]
105 [ + - + - : 2 : BOOST_CHECK(addrman->Add(vAddr, source));
+ - + - ]
106 [ + - + - : 2 : BOOST_CHECK(addrman->Size() >= 1);
+ - ]
107 : 4 : }
108 : :
109 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_ports)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
110 : : {
111 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
112 : :
113 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
114 : :
115 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 0U);
+ - ]
116 : :
117 : : // Test 7; Addr with same IP but diff port does not replace existing addr.
118 [ + - + - ]: 1 : CService addr1 = ResolveService("250.1.1.1", 8333);
119 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
120 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 1U);
+ - ]
121 : :
122 [ + - + - ]: 1 : CService addr1_port = ResolveService("250.1.1.1", 8334);
123 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr1_port, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
124 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 2U);
+ - ]
125 [ + - ]: 1 : auto addr_ret2 = addrman->Select().first;
126 [ + - + - : 3 : BOOST_CHECK(addr_ret2.ToStringAddrPort() == "250.1.1.1:8333" || addr_ret2.ToStringAddrPort() == "250.1.1.1:8334");
- + - - -
- + - - +
- - - - ]
127 : :
128 : : // Test: Add same IP but diff port to tried table; this converts the entry with
129 : : // the specified port to tried, but not the other.
130 [ + - ]: 2 : addrman->Good(CAddress(addr1_port, NODE_NONE));
131 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 2U);
+ - ]
132 : 1 : bool new_only = true;
133 [ + - ]: 1 : auto addr_ret3 = addrman->Select(new_only).first;
134 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr_ret3.ToStringAddrPort(), "250.1.1.1:8333");
+ - ]
135 : 3 : }
136 : :
137 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_select)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
138 : : {
139 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
140 [ + - + - : 2 : BOOST_CHECK(!addrman->Select(false).first.IsValid());
+ - + - +
- ]
141 [ + - + - : 2 : BOOST_CHECK(!addrman->Select(true).first.IsValid());
+ - + - +
- ]
142 : :
143 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
144 : :
145 : : // Add 1 address to the new table
146 [ + - + - ]: 1 : CService addr1 = ResolveService("250.1.1.1", 8333);
147 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
148 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 1U);
+ - ]
149 : :
150 [ + - + - : 2 : BOOST_CHECK(addrman->Select(/*new_only=*/true).first == addr1);
+ - + - +
- ]
151 [ + - + - : 2 : BOOST_CHECK(addrman->Select(/*new_only=*/false).first == addr1);
+ - + - +
- ]
152 : :
153 : : // Move address to the tried table
154 [ + - + - : 2 : BOOST_CHECK(addrman->Good(CAddress(addr1, NODE_NONE)));
+ - + - ]
155 : :
156 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 1U);
+ - ]
157 [ + - + - : 2 : BOOST_CHECK(!addrman->Select(/*new_only=*/true).first.IsValid());
+ - + - +
- ]
158 [ + - + - : 2 : BOOST_CHECK(addrman->Select().first == addr1);
+ - + - +
- ]
159 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 1U);
+ - ]
160 : :
161 : : // Add one address to the new table
162 [ + - + - ]: 1 : CService addr2 = ResolveService("250.3.1.1", 8333);
163 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr2, NODE_NONE)}, addr2));
+ - + - +
+ + - -
- ]
164 [ + - + - : 2 : BOOST_CHECK(addrman->Select(/*new_only=*/true).first == addr2);
+ - + - +
- ]
165 : :
166 : : // Add two more addresses to the new table
167 [ + - + - ]: 1 : CService addr3 = ResolveService("250.3.2.2", 9999);
168 [ + - + - ]: 1 : CService addr4 = ResolveService("250.3.3.3", 9999);
169 : :
170 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr3, NODE_NONE)}, addr2));
+ - + - +
+ + - -
- ]
171 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr4, NODE_NONE)}, ResolveService("250.4.1.1", 8333)));
+ - + - +
- + - + +
+ - - - ]
172 : :
173 : : // Add three addresses to tried table.
174 [ + - + - ]: 1 : CService addr5 = ResolveService("250.4.4.4", 8333);
175 [ + - + - ]: 1 : CService addr6 = ResolveService("250.4.5.5", 7777);
176 [ + - + - ]: 1 : CService addr7 = ResolveService("250.4.6.6", 8333);
177 : :
178 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr5, NODE_NONE)}, addr3));
+ - + - +
+ + - -
- ]
179 [ + - + - : 2 : BOOST_CHECK(addrman->Good(CAddress(addr5, NODE_NONE)));
+ - + - ]
180 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr6, NODE_NONE)}, addr3));
+ - + - +
+ + - -
- ]
181 [ + - + - : 2 : BOOST_CHECK(addrman->Good(CAddress(addr6, NODE_NONE)));
+ - + - ]
182 [ + - + - : 6 : BOOST_CHECK(addrman->Add({CAddress(addr7, NODE_NONE)}, ResolveService("250.1.1.3", 8333)));
+ - + - +
- + - + +
- - ]
183 [ + - + - : 2 : BOOST_CHECK(addrman->Good(CAddress(addr7, NODE_NONE)));
+ - + - ]
184 : :
185 : : // 6 addrs + 1 addr from last test = 7.
186 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 7U);
+ - ]
187 : :
188 : : // Select pulls from new and tried regardless of port number.
189 : 1 : std::set<uint16_t> ports;
190 [ + + ]: 21 : for (int i = 0; i < 20; ++i) {
191 [ + - + - : 40 : ports.insert(addrman->Select().first.GetPort());
+ - ]
192 : : }
193 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ports.size(), 3U);
194 : 8 : }
195 : :
196 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_select_by_network)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
197 : : {
198 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
199 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/true, {NET_IPV4}).first.IsValid());
+ - + - +
- + - ]
200 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/false, {NET_IPV4}).first.IsValid());
+ - + - +
- + - ]
201 : :
202 : : // add ipv4 address to the new table
203 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
204 [ + - + - ]: 1 : CService addr1 = ResolveService("250.1.1.1", 8333);
205 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
206 : :
207 [ + - + - : 3 : BOOST_CHECK(addrman->Select(/*new_only=*/true, {NET_IPV4}).first == addr1);
+ - + - +
- + - ]
208 [ + - + - : 3 : BOOST_CHECK(addrman->Select(/*new_only=*/false, {NET_IPV4}).first == addr1);
+ - + - +
- + - ]
209 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/false, {NET_IPV6}).first.IsValid());
+ - + - +
- + - ]
210 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/false, {NET_ONION}).first.IsValid());
+ - + - +
- + - ]
211 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/false, {NET_I2P}).first.IsValid());
+ - + - +
- + - ]
212 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/false, {NET_CJDNS}).first.IsValid());
+ - + - +
- + - ]
213 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/true, {NET_CJDNS}).first.IsValid());
+ - + - +
- + - ]
214 [ + - + - : 2 : BOOST_CHECK(addrman->Select(/*new_only=*/false).first == addr1);
+ - + - +
- ]
215 : :
216 : : // add I2P address to the new table
217 [ + - ]: 1 : CAddress i2p_addr;
218 [ + - + - ]: 1 : i2p_addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p");
219 [ + - + - : 3 : BOOST_CHECK(addrman->Add({i2p_addr}, source));
+ - + - +
+ - - ]
220 : :
221 [ + - + - : 3 : BOOST_CHECK(addrman->Select(/*new_only=*/true, {NET_I2P}).first == i2p_addr);
+ - + - +
- + - ]
222 [ + - + - : 3 : BOOST_CHECK(addrman->Select(/*new_only=*/false, {NET_I2P}).first == i2p_addr);
+ - + - +
- + - ]
223 [ + - + - : 3 : BOOST_CHECK(addrman->Select(/*new_only=*/false, {NET_IPV4}).first == addr1);
+ - + - +
- + - ]
224 [ + - ]: 1 : std::unordered_set<Network> nets_with_entries = {NET_IPV4, NET_I2P};
225 [ + - + - : 2 : BOOST_CHECK(addrman->Select(/*new_only=*/false, nets_with_entries).first.IsValid());
+ - + - +
- ]
226 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/false, {NET_IPV6}).first.IsValid());
+ - + - +
- + - ]
227 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/false, {NET_ONION}).first.IsValid());
+ - + - +
- + - ]
228 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/false, {NET_CJDNS}).first.IsValid());
+ - + - +
- + - ]
229 [ + - ]: 1 : std::unordered_set<Network> nets_without_entries = {NET_IPV6, NET_ONION, NET_CJDNS};
230 [ + - + - : 2 : BOOST_CHECK(!addrman->Select(/*new_only=*/false, nets_without_entries).first.IsValid());
+ - + - +
- ]
231 : :
232 : : // bump I2P address to tried table
233 [ + - + - : 2 : BOOST_CHECK(addrman->Good(i2p_addr));
+ - + - ]
234 : :
235 [ + - + - : 3 : BOOST_CHECK(!addrman->Select(/*new_only=*/true, {NET_I2P}).first.IsValid());
+ - + - +
- + - ]
236 [ + - + - : 3 : BOOST_CHECK(addrman->Select(/*new_only=*/false, {NET_I2P}).first == i2p_addr);
+ - + - +
- + - ]
237 : :
238 : : // add another I2P address to the new table
239 [ + - ]: 1 : CAddress i2p_addr2;
240 [ + - + - ]: 1 : i2p_addr2.SetSpecial("c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p");
241 [ + - + - : 3 : BOOST_CHECK(addrman->Add({i2p_addr2}, source));
+ - + - +
+ - - ]
242 : :
243 [ + - + - : 3 : BOOST_CHECK(addrman->Select(/*new_only=*/true, {NET_I2P}).first == i2p_addr2);
+ - + - +
- ]
244 : :
245 : : // ensure that both new and tried table are selected from
246 : 1 : bool new_selected{false};
247 : 1 : bool tried_selected{false};
248 : 1 : int counter = 256;
249 : :
250 [ + - + + ]: 3 : while (--counter > 0 && (!new_selected || !tried_selected)) {
251 [ + - + - : 4 : const CAddress selected{addrman->Select(/*new_only=*/false, {NET_I2P}).first};
+ - ]
252 [ + - + - : 5 : BOOST_REQUIRE(selected == i2p_addr || selected == i2p_addr2);
+ + + - +
- + - +
- ]
253 [ + - + + ]: 2 : if (selected == i2p_addr) {
254 : : tried_selected = true;
255 : : } else {
256 : 1 : new_selected = true;
257 : : }
258 : 2 : }
259 : :
260 [ + - + - : 2 : BOOST_CHECK(new_selected);
+ - ]
261 [ + - + - ]: 2 : BOOST_CHECK(tried_selected);
262 : 4 : }
263 : :
264 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_select_special)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
265 : : {
266 : : // use a non-deterministic addrman to ensure a passing test isn't due to setup
267 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, /*deterministic=*/false, GetCheckRatio(m_node));
268 : :
269 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
270 : :
271 : : // add I2P address to the tried table
272 [ + - ]: 1 : CAddress i2p_addr;
273 [ + - + - ]: 1 : i2p_addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p");
274 [ + - + - : 3 : BOOST_CHECK(addrman->Add({i2p_addr}, source));
+ - + - +
+ - - ]
275 [ + - + - : 2 : BOOST_CHECK(addrman->Good(i2p_addr));
+ - + - ]
276 : :
277 : : // add ipv4 address to the new table
278 [ + - + - ]: 1 : CService addr1 = ResolveService("250.1.1.3", 8333);
279 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
280 : :
281 : : // since the only ipv4 address is on the new table, ensure that the new
282 : : // table gets selected even if new_only is false. if the table was being
283 : : // selected at random, this test will sporadically fail
284 [ + - + - : 5 : BOOST_CHECK(addrman->Select(/*new_only=*/false, {NET_IPV4}).first == addr1);
+ - + - +
- ]
285 : 3 : }
286 : :
287 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_new_collisions)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
288 : : {
289 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
290 : :
291 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
292 : :
293 : 1 : uint32_t num_addrs{0};
294 : :
295 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), num_addrs);
+ - ]
296 : :
297 [ + + ]: 23 : while (num_addrs < 22) { // Magic number! 250.1.1.1 - 250.1.1.22 do not collide with deterministic key = 1
298 [ + - + - : 44 : CService addr = ResolveService("250.1.1." + ToString(++num_addrs));
+ - ]
299 [ + - + - : 110 : BOOST_CHECK(addrman->Add({CAddress(addr, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
300 : :
301 : : // Test: No collision in new table yet.
302 [ + - + - : 22 : BOOST_CHECK_EQUAL(addrman->Size(), num_addrs);
+ - ]
303 : 22 : }
304 : :
305 : : // Test: new table collision!
306 [ + - + - : 2 : CService addr1 = ResolveService("250.1.1." + ToString(++num_addrs));
+ - ]
307 : 1 : uint32_t collisions{1};
308 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
309 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), num_addrs - collisions);
+ - ]
310 : :
311 [ + - + - : 2 : CService addr2 = ResolveService("250.1.1." + ToString(++num_addrs));
+ - ]
312 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr2, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
313 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), num_addrs - collisions);
+ - ]
314 : 25 : }
315 : :
316 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_new_multiplicity)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
317 : : {
318 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
319 [ + - + - ]: 1 : CAddress addr{CAddress(ResolveService("253.3.3.3", 8333), NODE_NONE)};
320 : 1 : const auto start_time{Now<NodeSeconds>()};
321 : 1 : addr.nTime = start_time;
322 : :
323 : : // test that multiplicity stays at 1 if nTime doesn't increase
324 [ + + ]: 20 : for (unsigned int i = 1; i < 20; ++i) {
325 [ + - + - : 57 : std::string addr_ip{ToString(i % 256) + "." + ToString(i >> 8 % 256) + ".1.1"};
+ - ]
326 [ + - ]: 19 : CNetAddr source{ResolveIP(addr_ip)};
327 [ + - + - : 38 : addrman->Add({addr}, source);
+ + - - ]
328 : 19 : }
329 [ + - ]: 1 : AddressPosition addr_pos = addrman->FindAddressEntry(addr).value();
330 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(addr_pos.multiplicity, 1U);
331 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 1U);
+ - ]
332 : :
333 : : // if nTime increases, an addr can occur in up to 8 buckets
334 : : // The acceptance probability decreases exponentially with existing multiplicity -
335 : : // choose number of iterations such that it gets to 8 with deterministic addrman.
336 [ + + ]: 400 : for (unsigned int i = 1; i < 400; ++i) {
337 [ + - + - : 1197 : std::string addr_ip{ToString(i % 256) + "." + ToString(i >> 8 % 256) + ".1.1"};
+ - ]
338 [ + - ]: 399 : CNetAddr source{ResolveIP(addr_ip)};
339 : 399 : addr.nTime = start_time + std::chrono::seconds{i};
340 [ + - + - : 798 : addrman->Add({addr}, source);
+ + - - ]
341 : 399 : }
342 [ + - ]: 1 : AddressPosition addr_pos_multi = addrman->FindAddressEntry(addr).value();
343 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(addr_pos_multi.multiplicity, 8U);
344 : : // multiplicity doesn't affect size
345 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 1U);
+ - ]
346 : 419 : }
347 : :
348 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
349 : : {
350 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
351 : :
352 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
353 : :
354 : 1 : uint32_t num_addrs{0};
355 : :
356 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), num_addrs);
+ - ]
357 : :
358 [ + + ]: 36 : while (num_addrs < 35) { // Magic number! 250.1.1.1 - 250.1.1.35 do not collide in tried with deterministic key = 1
359 [ + - + - : 70 : CService addr = ResolveService("250.1.1." + ToString(++num_addrs));
+ - ]
360 [ + - + - : 175 : BOOST_CHECK(addrman->Add({CAddress(addr, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
361 : :
362 : : // Test: Add to tried without collision
363 [ + - + - : 70 : BOOST_CHECK(addrman->Good(CAddress(addr, NODE_NONE)));
+ - ]
364 : :
365 : 35 : }
366 : :
367 : : // Test: Unable to add to tried table due to collision!
368 [ + - + - : 2 : CService addr1 = ResolveService("250.1.1." + ToString(++num_addrs));
+ - ]
369 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
370 [ + - + - : 2 : BOOST_CHECK(!addrman->Good(CAddress(addr1, NODE_NONE)));
+ - + - ]
371 : :
372 : : // Test: Add the next address to tried without collision
373 [ + - + - : 2 : CService addr2 = ResolveService("250.1.1." + ToString(++num_addrs));
+ - ]
374 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr2, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
375 [ + - + - : 2 : BOOST_CHECK(addrman->Good(CAddress(addr2, NODE_NONE)));
+ - ]
376 : 38 : }
377 : :
378 : :
379 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_getaddr)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
380 : : {
381 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
382 : :
383 : : // Test: Sanity check, GetAddr should never return anything if addrman
384 : : // is empty.
385 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 0U);
+ - ]
386 [ + - ]: 1 : std::vector<CAddress> vAddr1 = addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt);
387 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(vAddr1.size(), 0U);
388 : :
389 [ + - + - ]: 1 : CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE);
390 : 1 : addr1.nTime = Now<NodeSeconds>(); // Set time so isTerrible = false
391 [ + - + - ]: 1 : CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE);
392 : 1 : addr2.nTime = Now<NodeSeconds>();
393 [ + - + - ]: 1 : CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE);
394 : 1 : addr3.nTime = Now<NodeSeconds>();
395 [ + - + - ]: 1 : CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE);
396 : 1 : addr4.nTime = Now<NodeSeconds>();
397 [ + - + - ]: 1 : CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE);
398 : 1 : addr5.nTime = Now<NodeSeconds>();
399 [ + - + - ]: 1 : CNetAddr source1 = ResolveIP("250.1.2.1");
400 [ + - + - ]: 1 : CNetAddr source2 = ResolveIP("250.2.3.3");
401 : :
402 : : // Test: Ensure GetAddr works with new addresses.
403 [ + - + - : 6 : BOOST_CHECK(addrman->Add({addr1, addr3, addr5}, source1));
+ - + - +
+ - - ]
404 [ + - + - : 5 : BOOST_CHECK(addrman->Add({addr2, addr4}, source2));
+ - + - +
+ - - ]
405 : :
406 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt).size(), 5U);
+ - ]
407 : : // Net processing asks for 23% of addresses. 23% of 5 is 1 rounded down.
408 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->GetAddr(/*max_addresses=*/2500, /*max_pct=*/23, /*network=*/std::nullopt).size(), 1U);
+ - ]
409 : :
410 : : // Test: Ensure GetAddr works with new and tried addresses.
411 [ + - + - : 2 : BOOST_CHECK(addrman->Good(CAddress(addr1, NODE_NONE)));
+ - + - ]
412 [ + - + - : 2 : BOOST_CHECK(addrman->Good(CAddress(addr2, NODE_NONE)));
+ - + - ]
413 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt).size(), 5U);
+ - ]
414 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->GetAddr(/*max_addresses=*/2500, /*max_pct=*/23, /*network=*/std::nullopt).size(), 1U);
+ - ]
415 : :
416 : : // Test: Ensure GetAddr still returns 23% when addrman has many addrs.
417 [ + + ]: 2048 : for (unsigned int i = 1; i < (8 * 256); i++) {
418 : 2047 : int octet1 = i % 256;
419 : 2047 : int octet2 = i >> 8 % 256;
420 [ + - + - : 6141 : std::string strAddr = ToString(octet1) + "." + ToString(octet2) + ".1.23";
+ - ]
421 [ + - ]: 2047 : CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE);
422 : :
423 : : // Ensure that for all addrs in addrman, isTerrible == false.
424 : 2047 : addr.nTime = Now<NodeSeconds>();
425 [ + - + - : 6141 : addrman->Add({addr}, ResolveIP(strAddr));
+ - + + -
- ]
426 [ + + ]: 2047 : if (i % 8 == 0)
427 [ + - ]: 255 : addrman->Good(addr);
428 : 2047 : }
429 [ + - ]: 1 : std::vector<CAddress> vAddr = addrman->GetAddr(/*max_addresses=*/2500, /*max_pct=*/23, /*network=*/std::nullopt);
430 : :
431 [ + - ]: 1 : size_t percent23 = (addrman->Size() * 23) / 100;
432 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(vAddr.size(), percent23);
433 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(vAddr.size(), 461U);
434 : : // (addrman.Size() < number of addresses added) due to address collisions.
435 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 2006U);
+ - ]
436 : 2050 : }
437 : :
438 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(getaddr_unfiltered)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
439 : : {
440 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
441 : :
442 : : // Set time on this addr so isTerrible = false
443 [ + - + - ]: 1 : CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE);
444 : 1 : addr1.nTime = Now<NodeSeconds>();
445 : : // Not setting time so this addr should be isTerrible = true
446 [ + - + - ]: 1 : CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE);
447 : :
448 [ + - + - ]: 1 : CNetAddr source = ResolveIP("250.1.2.1");
449 [ + - + - : 5 : BOOST_CHECK(addrman->Add({addr1, addr2}, source));
+ - + - +
+ - - ]
450 : :
451 : : // Filtered GetAddr should only return addr1
452 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt).size(), 1U);
+ - ]
453 : : // Unfiltered GetAddr should return addr1 and addr2
454 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt, /*filtered=*/false).size(), 2U);
+ - ]
455 : 2 : }
456 : :
457 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket_legacy)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
458 : : {
459 [ + - ]: 1 : CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
460 [ + - + - ]: 1 : CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
461 : :
462 [ + - + - ]: 1 : CNetAddr source1 = ResolveIP("250.1.1.1");
463 : :
464 : :
465 : 1 : AddrInfo info1 = AddrInfo(addr1, source1);
466 : :
467 [ + - + - : 1 : uint256 nKey1 = (HashWriter{} << 1).GetHash();
+ - ]
468 [ + - + - : 1 : uint256 nKey2 = (HashWriter{} << 2).GetHash();
+ - ]
469 : :
470 [ + - + - : 1 : BOOST_CHECK_EQUAL(info1.GetTriedBucket(nKey1, EMPTY_NETGROUPMAN), 40);
+ - ]
471 : :
472 : : // Test: Make sure key actually randomizes bucket placement. A fail on
473 : : // this test could be a security issue.
474 [ + - + - : 2 : BOOST_CHECK(info1.GetTriedBucket(nKey1, EMPTY_NETGROUPMAN) != info1.GetTriedBucket(nKey2, EMPTY_NETGROUPMAN));
+ - + - ]
475 : :
476 : : // Test: Two addresses with same IP but different ports can map to
477 : : // different buckets because they have different keys.
478 : 1 : AddrInfo info2 = AddrInfo(addr2, source1);
479 : :
480 [ + - + - : 2 : BOOST_CHECK(info1.GetKey() != info2.GetKey());
+ - + - +
- ]
481 [ + - + - : 2 : BOOST_CHECK(info1.GetTriedBucket(nKey1, EMPTY_NETGROUPMAN) != info2.GetTriedBucket(nKey1, EMPTY_NETGROUPMAN));
+ - + - ]
482 : :
483 : 1 : std::set<int> buckets;
484 [ + + ]: 256 : for (int i = 0; i < 255; i++) {
485 : 255 : AddrInfo infoi = AddrInfo(
486 [ + - + - : 510 : CAddress(ResolveService("250.1.1." + ToString(i)), NODE_NONE),
+ - ]
487 [ + - + - : 765 : ResolveIP("250.1.1." + ToString(i)));
+ - ]
488 [ + - ]: 255 : int bucket = infoi.GetTriedBucket(nKey1, EMPTY_NETGROUPMAN);
489 [ + - ]: 255 : buckets.insert(bucket);
490 : 255 : }
491 : : // Test: IP addresses in the same /16 prefix should
492 : : // never get more than 8 buckets with legacy grouping
493 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(buckets.size(), 8U);
494 : :
495 : 1 : buckets.clear();
496 [ + + ]: 256 : for (int j = 0; j < 255; j++) {
497 : 255 : AddrInfo infoj = AddrInfo(
498 [ + - + - : 765 : CAddress(ResolveService("250." + ToString(j) + ".1.1"), NODE_NONE),
+ - ]
499 [ + - + - : 1020 : ResolveIP("250." + ToString(j) + ".1.1"));
+ - ]
500 [ + - ]: 255 : int bucket = infoj.GetTriedBucket(nKey1, EMPTY_NETGROUPMAN);
501 [ + - ]: 255 : buckets.insert(bucket);
502 : 255 : }
503 : : // Test: IP addresses in the different /16 prefix should map to more than
504 : : // 8 buckets with legacy grouping
505 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(buckets.size(), 160U);
506 : 3 : }
507 : :
508 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
509 : : {
510 [ + - ]: 1 : CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
511 [ + - + - ]: 1 : CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
512 : :
513 [ + - + - ]: 1 : CNetAddr source1 = ResolveIP("250.1.2.1");
514 : :
515 : 1 : AddrInfo info1 = AddrInfo(addr1, source1);
516 : :
517 [ + - + - : 1 : uint256 nKey1 = (HashWriter{} << 1).GetHash();
+ - ]
518 [ + - + - : 1 : uint256 nKey2 = (HashWriter{} << 2).GetHash();
+ - ]
519 : :
520 : : // Test: Make sure the buckets are what we expect
521 [ + - + - : 1 : BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, EMPTY_NETGROUPMAN), 786);
+ - ]
522 [ + - + - : 1 : BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, source1, EMPTY_NETGROUPMAN), 786);
+ - ]
523 : :
524 : : // Test: Make sure key actually randomizes bucket placement. A fail on
525 : : // this test could be a security issue.
526 [ + - + - : 2 : BOOST_CHECK(info1.GetNewBucket(nKey1, EMPTY_NETGROUPMAN) != info1.GetNewBucket(nKey2, EMPTY_NETGROUPMAN));
+ - + - ]
527 : :
528 : : // Test: Ports should not affect bucket placement in the addr
529 : 1 : AddrInfo info2 = AddrInfo(addr2, source1);
530 [ + - + - : 2 : BOOST_CHECK(info1.GetKey() != info2.GetKey());
+ - + - +
- ]
531 [ + - + - : 1 : BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, EMPTY_NETGROUPMAN), info2.GetNewBucket(nKey1, EMPTY_NETGROUPMAN));
+ - + - ]
532 : :
533 : 1 : std::set<int> buckets;
534 [ + + ]: 256 : for (int i = 0; i < 255; i++) {
535 : 255 : AddrInfo infoi = AddrInfo(
536 [ + - + - : 510 : CAddress(ResolveService("250.1.1." + ToString(i)), NODE_NONE),
+ - ]
537 [ + - + - : 765 : ResolveIP("250.1.1." + ToString(i)));
+ - ]
538 [ + - ]: 255 : int bucket = infoi.GetNewBucket(nKey1, EMPTY_NETGROUPMAN);
539 [ + - ]: 255 : buckets.insert(bucket);
540 : 255 : }
541 : : // Test: IP addresses in the same group (\16 prefix for IPv4) should
542 : : // always map to the same bucket.
543 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(buckets.size(), 1U);
544 : :
545 : 1 : buckets.clear();
546 [ + + ]: 1021 : for (int j = 0; j < 4 * 255; j++) {
547 [ + - ]: 2040 : AddrInfo infoj = AddrInfo(CAddress(
548 : 1020 : ResolveService(
549 [ + - + - : 4080 : ToString(250 + (j / 255)) + "." + ToString(j % 256) + ".1.1"), NODE_NONE),
+ - + - ]
550 [ + - + - ]: 3060 : ResolveIP("251.4.1.1"));
551 [ + - ]: 1020 : int bucket = infoj.GetNewBucket(nKey1, EMPTY_NETGROUPMAN);
552 [ + - ]: 1020 : buckets.insert(bucket);
553 : 1020 : }
554 : : // Test: IP addresses in the same source groups should map to NO MORE
555 : : // than 64 buckets.
556 [ + - + - ]: 2 : BOOST_CHECK(buckets.size() <= 64);
557 : :
558 : 1 : buckets.clear();
559 [ + + ]: 256 : for (int p = 0; p < 255; p++) {
560 : 255 : AddrInfo infoj = AddrInfo(
561 [ + - + - ]: 510 : CAddress(ResolveService("250.1.1.1"), NODE_NONE),
562 [ + - + - : 1020 : ResolveIP("250." + ToString(p) + ".1.1"));
+ - ]
563 [ + - ]: 255 : int bucket = infoj.GetNewBucket(nKey1, EMPTY_NETGROUPMAN);
564 [ + - ]: 255 : buckets.insert(bucket);
565 : 255 : }
566 : : // Test: IP addresses in the different source groups should map to MORE
567 : : // than 64 buckets.
568 [ + - + - ]: 2 : BOOST_CHECK(buckets.size() > 64);
569 : 3 : }
570 : :
571 : : // The following three test cases use asmap.raw
572 : : // We use an artificial minimal mock mapping
573 : : // 250.0.0.0/8 AS1000
574 : : // 101.1.0.0/16 AS1
575 : : // 101.2.0.0/16 AS2
576 : : // 101.3.0.0/16 AS3
577 : : // 101.4.0.0/16 AS4
578 : : // 101.5.0.0/16 AS5
579 : : // 101.6.0.0/16 AS6
580 : : // 101.7.0.0/16 AS7
581 : : // 101.8.0.0/16 AS8
582 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
583 : : {
584 : 1 : std::vector<bool> asmap = FromBytes(test::data::asmap);
585 [ + - ]: 2 : NetGroupManager ngm_asmap{asmap};
586 : :
587 [ + - + - ]: 1 : CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
588 [ + - + - ]: 1 : CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
589 : :
590 [ + - + - ]: 1 : CNetAddr source1 = ResolveIP("250.1.1.1");
591 : :
592 : :
593 : 1 : AddrInfo info1 = AddrInfo(addr1, source1);
594 : :
595 [ + - + - : 1 : uint256 nKey1 = (HashWriter{} << 1).GetHash();
+ - ]
596 [ + - + - : 1 : uint256 nKey2 = (HashWriter{} << 2).GetHash();
+ - ]
597 : :
598 [ + - + - : 1 : BOOST_CHECK_EQUAL(info1.GetTriedBucket(nKey1, ngm_asmap), 236);
+ - ]
599 : :
600 : : // Test: Make sure key actually randomizes bucket placement. A fail on
601 : : // this test could be a security issue.
602 [ + - + - : 2 : BOOST_CHECK(info1.GetTriedBucket(nKey1, ngm_asmap) != info1.GetTriedBucket(nKey2, ngm_asmap));
+ - + - ]
603 : :
604 : : // Test: Two addresses with same IP but different ports can map to
605 : : // different buckets because they have different keys.
606 : 1 : AddrInfo info2 = AddrInfo(addr2, source1);
607 : :
608 [ + - + - : 2 : BOOST_CHECK(info1.GetKey() != info2.GetKey());
+ - + - +
- ]
609 [ + - + - : 2 : BOOST_CHECK(info1.GetTriedBucket(nKey1, ngm_asmap) != info2.GetTriedBucket(nKey1, ngm_asmap));
+ - + - ]
610 : :
611 : 1 : std::set<int> buckets;
612 [ + + ]: 256 : for (int j = 0; j < 255; j++) {
613 : 255 : AddrInfo infoj = AddrInfo(
614 [ + - + - : 765 : CAddress(ResolveService("101." + ToString(j) + ".1.1"), NODE_NONE),
+ - ]
615 [ + - + - : 1020 : ResolveIP("101." + ToString(j) + ".1.1"));
+ - ]
616 [ + - ]: 255 : int bucket = infoj.GetTriedBucket(nKey1, ngm_asmap);
617 [ + - ]: 255 : buckets.insert(bucket);
618 : 255 : }
619 : : // Test: IP addresses in the different /16 prefix MAY map to more than
620 : : // 8 buckets.
621 [ + - + - ]: 2 : BOOST_CHECK(buckets.size() > 8);
622 : :
623 : 1 : buckets.clear();
624 [ + + ]: 256 : for (int j = 0; j < 255; j++) {
625 : 255 : AddrInfo infoj = AddrInfo(
626 [ + - + - : 765 : CAddress(ResolveService("250." + ToString(j) + ".1.1"), NODE_NONE),
+ - ]
627 [ + - + - : 1020 : ResolveIP("250." + ToString(j) + ".1.1"));
+ - ]
628 [ + - ]: 255 : int bucket = infoj.GetTriedBucket(nKey1, ngm_asmap);
629 [ + - ]: 255 : buckets.insert(bucket);
630 : 255 : }
631 : : // Test: IP addresses in the different /16 prefix MAY NOT map to more than
632 : : // 8 buckets.
633 [ + - + - ]: 2 : BOOST_CHECK(buckets.size() == 8);
634 : 3 : }
635 : :
636 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
637 : : {
638 : 1 : std::vector<bool> asmap = FromBytes(test::data::asmap);
639 [ + - ]: 2 : NetGroupManager ngm_asmap{asmap};
640 : :
641 [ + - + - ]: 1 : CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
642 [ + - + - ]: 1 : CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
643 : :
644 [ + - + - ]: 1 : CNetAddr source1 = ResolveIP("250.1.2.1");
645 : :
646 : 1 : AddrInfo info1 = AddrInfo(addr1, source1);
647 : :
648 [ + - + - : 1 : uint256 nKey1 = (HashWriter{} << 1).GetHash();
+ - ]
649 [ + - + - : 1 : uint256 nKey2 = (HashWriter{} << 2).GetHash();
+ - ]
650 : :
651 : : // Test: Make sure the buckets are what we expect
652 [ + - + - : 1 : BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, ngm_asmap), 795);
+ - ]
653 [ + - + - : 1 : BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, source1, ngm_asmap), 795);
+ - ]
654 : :
655 : : // Test: Make sure key actually randomizes bucket placement. A fail on
656 : : // this test could be a security issue.
657 [ + - + - : 2 : BOOST_CHECK(info1.GetNewBucket(nKey1, ngm_asmap) != info1.GetNewBucket(nKey2, ngm_asmap));
+ - + - ]
658 : :
659 : : // Test: Ports should not affect bucket placement in the addr
660 : 1 : AddrInfo info2 = AddrInfo(addr2, source1);
661 [ + - + - : 2 : BOOST_CHECK(info1.GetKey() != info2.GetKey());
+ - + - +
- ]
662 [ + - + - : 1 : BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, ngm_asmap), info2.GetNewBucket(nKey1, ngm_asmap));
+ - + - ]
663 : :
664 : 1 : std::set<int> buckets;
665 [ + + ]: 256 : for (int i = 0; i < 255; i++) {
666 : 255 : AddrInfo infoi = AddrInfo(
667 [ + - + - : 510 : CAddress(ResolveService("250.1.1." + ToString(i)), NODE_NONE),
+ - ]
668 [ + - + - : 765 : ResolveIP("250.1.1." + ToString(i)));
+ - ]
669 [ + - ]: 255 : int bucket = infoi.GetNewBucket(nKey1, ngm_asmap);
670 [ + - ]: 255 : buckets.insert(bucket);
671 : 255 : }
672 : : // Test: IP addresses in the same /16 prefix
673 : : // usually map to the same bucket.
674 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(buckets.size(), 1U);
675 : :
676 : 1 : buckets.clear();
677 [ + + ]: 1021 : for (int j = 0; j < 4 * 255; j++) {
678 [ + - ]: 2040 : AddrInfo infoj = AddrInfo(CAddress(
679 : 1020 : ResolveService(
680 [ + - + - : 4080 : ToString(250 + (j / 255)) + "." + ToString(j % 256) + ".1.1"), NODE_NONE),
+ - + - ]
681 [ + - + - ]: 3060 : ResolveIP("251.4.1.1"));
682 [ + - ]: 1020 : int bucket = infoj.GetNewBucket(nKey1, ngm_asmap);
683 [ + - ]: 1020 : buckets.insert(bucket);
684 : 1020 : }
685 : : // Test: IP addresses in the same source /16 prefix should not map to more
686 : : // than 64 buckets.
687 [ + - + - ]: 2 : BOOST_CHECK(buckets.size() <= 64);
688 : :
689 : 1 : buckets.clear();
690 [ + + ]: 256 : for (int p = 0; p < 255; p++) {
691 : 255 : AddrInfo infoj = AddrInfo(
692 [ + - + - ]: 510 : CAddress(ResolveService("250.1.1.1"), NODE_NONE),
693 [ + - + - : 1020 : ResolveIP("101." + ToString(p) + ".1.1"));
+ - ]
694 [ + - ]: 255 : int bucket = infoj.GetNewBucket(nKey1, ngm_asmap);
695 [ + - ]: 255 : buckets.insert(bucket);
696 : 255 : }
697 : : // Test: IP addresses in the different source /16 prefixes usually map to MORE
698 : : // than 1 bucket.
699 [ + - + - ]: 2 : BOOST_CHECK(buckets.size() > 1);
700 : :
701 : 1 : buckets.clear();
702 [ + + ]: 256 : for (int p = 0; p < 255; p++) {
703 : 255 : AddrInfo infoj = AddrInfo(
704 [ + - + - ]: 510 : CAddress(ResolveService("250.1.1.1"), NODE_NONE),
705 [ + - + - : 1020 : ResolveIP("250." + ToString(p) + ".1.1"));
+ - ]
706 [ + - ]: 255 : int bucket = infoj.GetNewBucket(nKey1, ngm_asmap);
707 [ + - ]: 255 : buckets.insert(bucket);
708 : 255 : }
709 : : // Test: IP addresses in the different source /16 prefixes sometimes map to NO MORE
710 : : // than 1 bucket.
711 [ + - + - ]: 2 : BOOST_CHECK(buckets.size() == 1);
712 : 3 : }
713 : :
714 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_serialization)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
715 : : {
716 : 1 : std::vector<bool> asmap1 = FromBytes(test::data::asmap);
717 [ + - ]: 2 : NetGroupManager netgroupman{asmap1};
718 : :
719 [ + - ]: 1 : const auto ratio = GetCheckRatio(m_node);
720 [ + - ]: 1 : auto addrman_asmap1 = std::make_unique<AddrMan>(netgroupman, DETERMINISTIC, ratio);
721 [ + - ]: 1 : auto addrman_asmap1_dup = std::make_unique<AddrMan>(netgroupman, DETERMINISTIC, ratio);
722 [ + - ]: 1 : auto addrman_noasmap = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, ratio);
723 : :
724 : 1 : DataStream stream{};
725 : :
726 [ + - + - ]: 1 : CAddress addr = CAddress(ResolveService("250.1.1.1"), NODE_NONE);
727 [ + - ]: 1 : CNetAddr default_source;
728 : :
729 [ + - + - : 2 : addrman_asmap1->Add({addr}, default_source);
+ + - - ]
730 : :
731 [ + - ]: 1 : stream << *addrman_asmap1;
732 : : // serizalizing/deserializing addrman with the same asmap
733 [ + - ]: 1 : stream >> *addrman_asmap1_dup;
734 : :
735 [ + - ]: 1 : AddressPosition addr_pos1 = addrman_asmap1->FindAddressEntry(addr).value();
736 [ + - ]: 1 : AddressPosition addr_pos2 = addrman_asmap1_dup->FindAddressEntry(addr).value();
737 [ + - + - : 2 : BOOST_CHECK(addr_pos1.multiplicity != 0);
+ - ]
738 [ + - + - : 2 : BOOST_CHECK(addr_pos2.multiplicity != 0);
+ - ]
739 : :
740 [ + - + - : 2 : BOOST_CHECK(addr_pos1 == addr_pos2);
+ - ]
741 : :
742 : : // deserializing asmaped peers.dat to non-asmaped addrman
743 [ + - ]: 1 : stream << *addrman_asmap1;
744 [ + - ]: 1 : stream >> *addrman_noasmap;
745 [ + - ]: 1 : AddressPosition addr_pos3 = addrman_noasmap->FindAddressEntry(addr).value();
746 [ + - + - : 2 : BOOST_CHECK(addr_pos3.multiplicity != 0);
+ - ]
747 [ + - + - : 2 : BOOST_CHECK(addr_pos1.bucket != addr_pos3.bucket);
+ - ]
748 [ + - + - : 2 : BOOST_CHECK(addr_pos1.position != addr_pos3.position);
+ - ]
749 : :
750 : : // deserializing non-asmaped peers.dat to asmaped addrman
751 [ + - ]: 2 : addrman_asmap1 = std::make_unique<AddrMan>(netgroupman, DETERMINISTIC, ratio);
752 [ + - ]: 2 : addrman_noasmap = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, ratio);
753 [ + - + - : 2 : addrman_noasmap->Add({addr}, default_source);
+ + - - ]
754 [ + - ]: 1 : stream << *addrman_noasmap;
755 [ + - ]: 1 : stream >> *addrman_asmap1;
756 : :
757 [ + - ]: 1 : AddressPosition addr_pos4 = addrman_asmap1->FindAddressEntry(addr).value();
758 [ + - + - : 2 : BOOST_CHECK(addr_pos4.multiplicity != 0);
+ - ]
759 [ + - + - : 2 : BOOST_CHECK(addr_pos4.bucket != addr_pos3.bucket);
+ - ]
760 [ + - + - : 2 : BOOST_CHECK(addr_pos4 == addr_pos2);
+ - ]
761 : :
762 : : // used to map to different buckets, now maps to the same bucket.
763 [ + - ]: 2 : addrman_asmap1 = std::make_unique<AddrMan>(netgroupman, DETERMINISTIC, ratio);
764 [ + - ]: 2 : addrman_noasmap = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, ratio);
765 [ + - + - ]: 1 : CAddress addr1 = CAddress(ResolveService("250.1.1.1"), NODE_NONE);
766 [ + - + - ]: 1 : CAddress addr2 = CAddress(ResolveService("250.2.1.1"), NODE_NONE);
767 [ + - + - : 4 : addrman_noasmap->Add({addr, addr2}, default_source);
+ + - - ]
768 [ + - ]: 1 : AddressPosition addr_pos5 = addrman_noasmap->FindAddressEntry(addr1).value();
769 [ + - ]: 1 : AddressPosition addr_pos6 = addrman_noasmap->FindAddressEntry(addr2).value();
770 [ + - + - : 2 : BOOST_CHECK(addr_pos5.bucket != addr_pos6.bucket);
+ - ]
771 [ + - ]: 1 : stream << *addrman_noasmap;
772 [ + - ]: 1 : stream >> *addrman_asmap1;
773 [ + - ]: 1 : AddressPosition addr_pos7 = addrman_asmap1->FindAddressEntry(addr1).value();
774 [ + - ]: 1 : AddressPosition addr_pos8 = addrman_asmap1->FindAddressEntry(addr2).value();
775 [ + - + - : 2 : BOOST_CHECK(addr_pos7.bucket == addr_pos8.bucket);
+ - ]
776 [ + - + - ]: 2 : BOOST_CHECK(addr_pos7.position != addr_pos8.position);
777 : 4 : }
778 : :
779 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(remove_invalid)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
780 : : {
781 : : // Confirm that invalid addresses are ignored in unserialization.
782 : :
783 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
784 : 1 : DataStream stream{};
785 : :
786 [ + - + - ]: 1 : const CAddress new1{ResolveService("5.5.5.5"), NODE_NONE};
787 [ + - + - ]: 1 : const CAddress new2{ResolveService("6.6.6.6"), NODE_NONE};
788 [ + - + - ]: 1 : const CAddress tried1{ResolveService("7.7.7.7"), NODE_NONE};
789 [ + - + - ]: 1 : const CAddress tried2{ResolveService("8.8.8.8"), NODE_NONE};
790 : :
791 [ + - + - : 6 : addrman->Add({new1, tried1, new2, tried2}, CNetAddr{});
+ - + + -
- ]
792 [ + - ]: 1 : addrman->Good(tried1);
793 [ + - ]: 1 : addrman->Good(tried2);
794 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(addrman->Size(), 4);
+ - ]
795 : :
796 [ + - ]: 1 : stream << *addrman;
797 : :
798 [ + - ]: 1 : const std::string str{stream.str()};
799 : 1 : size_t pos;
800 : :
801 : 1 : const char new2_raw[]{6, 6, 6, 6};
802 : 1 : const uint8_t new2_raw_replacement[]{0, 0, 0, 0}; // 0.0.0.0 is !IsValid()
803 : 1 : pos = str.find(new2_raw, 0, sizeof(new2_raw));
804 [ + - + - : 2 : BOOST_REQUIRE(pos != std::string::npos);
+ - ]
805 [ + - + - ]: 2 : BOOST_REQUIRE(pos + sizeof(new2_raw_replacement) <= stream.size());
806 : 1 : memcpy(stream.data() + pos, new2_raw_replacement, sizeof(new2_raw_replacement));
807 : :
808 : 1 : const char tried2_raw[]{8, 8, 8, 8};
809 : 1 : const uint8_t tried2_raw_replacement[]{255, 255, 255, 255}; // 255.255.255.255 is !IsValid()
810 : 1 : pos = str.find(tried2_raw, 0, sizeof(tried2_raw));
811 [ + - + - : 2 : BOOST_REQUIRE(pos != std::string::npos);
+ - ]
812 [ + - + - : 2 : BOOST_REQUIRE(pos + sizeof(tried2_raw_replacement) <= stream.size());
+ - ]
813 [ + - ]: 1 : memcpy(stream.data() + pos, tried2_raw_replacement, sizeof(tried2_raw_replacement));
814 : :
815 [ + - + - ]: 2 : addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
816 [ + - ]: 1 : stream >> *addrman;
817 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 2);
+ - ]
818 : 2 : }
819 : :
820 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_selecttriedcollision)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
821 : : {
822 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
823 : :
824 [ + - + - : 2 : BOOST_CHECK(addrman->Size() == 0);
+ - + - ]
825 : :
826 : : // Empty addrman should return blank addrman info.
827 [ + - + - : 2 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() == "[::]:0");
+ - + - +
- ]
828 : :
829 : : // Add twenty two addresses.
830 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
831 [ + + ]: 23 : for (unsigned int i = 1; i < 23; i++) {
832 [ + - + - : 44 : CService addr = ResolveService("250.1.1." + ToString(i));
+ - ]
833 [ + - + - : 110 : BOOST_CHECK(addrman->Add({CAddress(addr, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
834 : :
835 : : // No collisions in tried.
836 [ + - + - : 44 : BOOST_CHECK(addrman->Good(addr));
+ - + - ]
837 [ + - + - : 44 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() == "[::]:0");
+ - + - ]
838 : 22 : }
839 : :
840 : : // Ensure Good handles duplicates well.
841 : : // If an address is a duplicate, Good will return false but will not count it as a collision.
842 [ + + ]: 23 : for (unsigned int i = 1; i < 23; i++) {
843 [ + - + - : 44 : CService addr = ResolveService("250.1.1." + ToString(i));
+ - ]
844 : :
845 : : // Unable to add duplicate address to tried table.
846 [ + - + - : 44 : BOOST_CHECK(!addrman->Good(addr));
+ - + - ]
847 : :
848 : : // Verify duplicate address not marked as a collision.
849 [ + - + - : 44 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() == "[::]:0");
+ - + - ]
850 : 22 : }
851 : 23 : }
852 : :
853 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_noevict)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
854 : : {
855 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
856 : :
857 : : // Add 35 addresses.
858 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
859 [ + + ]: 36 : for (unsigned int i = 1; i < 36; i++) {
860 [ + - + - : 70 : CService addr = ResolveService("250.1.1." + ToString(i));
+ - ]
861 [ + - + - : 175 : BOOST_CHECK(addrman->Add({CAddress(addr, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
862 : :
863 : : // No collision yet.
864 [ + - + - : 70 : BOOST_CHECK(addrman->Good(addr));
+ - ]
865 : 35 : }
866 : :
867 : : // Collision in tried table between 36 and 19.
868 [ + - + - ]: 1 : CService addr36 = ResolveService("250.1.1.36");
869 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr36, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
870 [ + - + - : 2 : BOOST_CHECK(!addrman->Good(addr36));
+ - + - ]
871 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(), "250.1.1.19:0");
+ - + - ]
872 : :
873 : : // 36 should be discarded and 19 not evicted.
874 : : // This means we keep 19 in the tried table and
875 : : // 36 stays in the new table.
876 [ + - ]: 1 : addrman->ResolveCollisions();
877 [ + - + - : 2 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() == "[::]:0");
+ - + - ]
878 : :
879 : : // Lets create two collisions.
880 [ + + ]: 23 : for (unsigned int i = 37; i < 59; i++) {
881 [ + - + - : 44 : CService addr = ResolveService("250.1.1." + ToString(i));
+ - ]
882 [ + - + - : 110 : BOOST_CHECK(addrman->Add({CAddress(addr, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
883 [ + - + - : 44 : BOOST_CHECK(addrman->Good(addr));
+ - ]
884 : 22 : }
885 : :
886 : : // Cause a collision in the tried table.
887 [ + - + - ]: 1 : CService addr59 = ResolveService("250.1.1.59");
888 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr59, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
889 [ + - + - : 2 : BOOST_CHECK(!addrman->Good(addr59));
+ - + - ]
890 : :
891 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(), "250.1.1.10:0");
+ - + - ]
892 : :
893 : : // Cause a second collision in the new table.
894 [ + - + - : 5 : BOOST_CHECK(!addrman->Add({CAddress(addr36, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
895 : :
896 : : // 36 still cannot be moved from new to tried due to colliding with 19
897 [ + - + - : 2 : BOOST_CHECK(!addrman->Good(addr36));
+ - + - ]
898 [ + - + - : 2 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() != "[::]:0");
+ - + - +
- ]
899 : :
900 : : // Resolve all collisions.
901 [ + - ]: 1 : addrman->ResolveCollisions();
902 [ + - + - : 2 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() == "[::]:0");
+ - + - ]
903 : 61 : }
904 : :
905 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_evictionworks)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
906 : : {
907 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
908 : :
909 [ + - + - : 2 : BOOST_CHECK(addrman->Size() == 0);
+ - + - ]
910 : :
911 : : // Empty addrman should return blank addrman info.
912 [ + - + - : 2 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() == "[::]:0");
+ - + - +
- ]
913 : :
914 : : // Add 35 addresses
915 [ + - + - ]: 1 : CNetAddr source = ResolveIP("252.2.2.2");
916 [ + + ]: 36 : for (unsigned int i = 1; i < 36; i++) {
917 [ + - + - : 70 : CService addr = ResolveService("250.1.1." + ToString(i));
+ - ]
918 [ + - + - : 175 : BOOST_CHECK(addrman->Add({CAddress(addr, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
919 : :
920 : : // No collision yet.
921 [ + - + - : 70 : BOOST_CHECK(addrman->Good(addr));
+ - ]
922 : 35 : }
923 : :
924 : : // Collision between 36 and 19.
925 [ + - + - ]: 1 : CService addr = ResolveService("250.1.1.36");
926 [ + - + - : 5 : BOOST_CHECK(addrman->Add({CAddress(addr, NODE_NONE)}, source));
+ - + - +
+ + - -
- ]
927 [ + - + - : 2 : BOOST_CHECK(!addrman->Good(addr));
+ - + - ]
928 : :
929 [ + - ]: 1 : auto info = addrman->SelectTriedCollision().first;
930 [ + - + - : 1 : BOOST_CHECK_EQUAL(info.ToStringAddrPort(), "250.1.1.19:0");
+ - ]
931 : :
932 : : // Ensure test of address fails, so that it is evicted.
933 : : // Update entry in tried by setting last good connection in the deep past.
934 [ + - + - : 2 : BOOST_CHECK(!addrman->Good(info, NodeSeconds{1s}));
+ - ]
935 [ + - ]: 1 : addrman->Attempt(info, /*fCountFailure=*/false, Now<NodeSeconds>() - 61s);
936 : :
937 : : // Should swap 36 for 19.
938 [ + - ]: 1 : addrman->ResolveCollisions();
939 [ + - + - : 2 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() == "[::]:0");
+ - + - ]
940 [ + - ]: 2 : AddressPosition addr_pos{addrman->FindAddressEntry(CAddress(addr, NODE_NONE)).value()};
941 [ + - + - : 2 : BOOST_CHECK(addr_pos.tried);
+ - ]
942 : :
943 : : // If 36 was swapped for 19, then adding 36 to tried should fail because we
944 : : // are attempting to add a duplicate.
945 : : // We check this by verifying Good() returns false and also verifying that
946 : : // we have no collisions.
947 [ + - + - : 2 : BOOST_CHECK(!addrman->Good(addr));
+ - + - ]
948 [ + - + - : 2 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() == "[::]:0");
+ - + - +
- ]
949 : :
950 : : // 19 should fail as a collision (not a duplicate) if we now attempt to move
951 : : // it to the tried table.
952 [ + - + - ]: 1 : CService addr19 = ResolveService("250.1.1.19");
953 [ + - + - : 2 : BOOST_CHECK(!addrman->Good(addr19));
+ - + - ]
954 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(), "250.1.1.36:0");
+ - + - ]
955 : :
956 : : // Eviction is also successful if too much time has passed since last try
957 [ + - + - ]: 1 : SetMockTime(GetTime() + 4 * 60 *60);
958 [ + - ]: 1 : addrman->ResolveCollisions();
959 [ + - + - : 2 : BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() == "[::]:0");
+ - + - ]
960 : : //Now 19 is in tried again, and 36 back to new
961 [ + - ]: 2 : AddressPosition addr_pos19{addrman->FindAddressEntry(CAddress(addr19, NODE_NONE)).value()};
962 [ + - + - ]: 2 : BOOST_CHECK(addr_pos19.tried);
963 [ + - ]: 2 : AddressPosition addr_pos36{addrman->FindAddressEntry(CAddress(addr, NODE_NONE)).value()};
964 [ + - + - ]: 2 : BOOST_CHECK(!addr_pos36.tried);
965 : 37 : }
966 : :
967 : 2 : static auto AddrmanToStream(const AddrMan& addrman)
968 : : {
969 : 2 : DataStream ssPeersIn{};
970 [ + - + - ]: 2 : ssPeersIn << Params().MessageStart();
971 [ + - ]: 2 : ssPeersIn << addrman;
972 : 2 : return ssPeersIn;
973 : 0 : }
974 : :
975 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(load_addrman)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
976 : : {
977 : 1 : AddrMan addrman{EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node)};
978 : :
979 : 1 : std::optional<CService> addr1, addr2, addr3, addr4;
980 [ + - + - : 2 : addr1 = Lookup("250.7.1.1", 8333, false);
+ - ]
981 [ + - + - : 2 : BOOST_CHECK(addr1.has_value());
+ - ]
982 [ + - + - : 2 : addr2 = Lookup("250.7.2.2", 9999, false);
+ - ]
983 [ + - + - : 2 : BOOST_CHECK(addr2.has_value());
+ - ]
984 [ + - + - : 2 : addr3 = Lookup("250.7.3.3", 9999, false);
+ - ]
985 [ + - + - : 2 : BOOST_CHECK(addr3.has_value());
+ - ]
986 [ + - + - ]: 3 : addr3 = Lookup("250.7.3.3"s, 9999, false);
987 [ + - + - : 2 : BOOST_CHECK(addr3.has_value());
+ - ]
988 [ + - + - ]: 3 : addr4 = Lookup("250.7.3.3\0example.com"s, 9999, false);
989 [ + - + - : 2 : BOOST_CHECK(!addr4.has_value());
+ - ]
990 : :
991 : : // Add three addresses to new table.
992 [ + - + - : 2 : const std::optional<CService> source{Lookup("252.5.1.1", 8333, false)};
+ - ]
993 [ + - + - : 2 : BOOST_CHECK(source.has_value());
+ - ]
994 [ - + + + : 8 : std::vector<CAddress> addresses{CAddress(addr1.value(), NODE_NONE), CAddress(addr2.value(), NODE_NONE), CAddress(addr3.value(), NODE_NONE)};
- - ]
995 [ + - + - : 2 : BOOST_CHECK(addrman.Add(addresses, source.value()));
+ - + - +
- ]
996 [ + - + - : 2 : BOOST_CHECK(addrman.Size() == 3);
+ - + - ]
997 : :
998 : : // Test that the de-serialization does not throw an exception.
999 [ + - ]: 1 : auto ssPeers1{AddrmanToStream(addrman)};
1000 : 1 : bool exceptionThrown = false;
1001 [ + - + - ]: 1 : AddrMan addrman1{EMPTY_NETGROUPMAN, !DETERMINISTIC, GetCheckRatio(m_node)};
1002 : :
1003 [ + - + - : 2 : BOOST_CHECK(addrman1.Size() == 0);
+ - + - ]
1004 : 1 : try {
1005 : 1 : unsigned char pchMsgTmp[4];
1006 [ + - ]: 1 : ssPeers1 >> pchMsgTmp;
1007 [ + - ]: 1 : ssPeers1 >> addrman1;
1008 [ - - ]: 0 : } catch (const std::exception&) {
1009 : 0 : exceptionThrown = true;
1010 : 0 : }
1011 : :
1012 [ + - + - : 2 : BOOST_CHECK(addrman1.Size() == 3);
+ - + - ]
1013 [ + - + - : 2 : BOOST_CHECK(exceptionThrown == false);
+ - ]
1014 : :
1015 : : // Test that ReadFromStream creates an addrman with the correct number of addrs.
1016 [ + - ]: 1 : DataStream ssPeers2 = AddrmanToStream(addrman);
1017 : :
1018 [ + - + - ]: 1 : AddrMan addrman2{EMPTY_NETGROUPMAN, !DETERMINISTIC, GetCheckRatio(m_node)};
1019 [ + - + - : 2 : BOOST_CHECK(addrman2.Size() == 0);
+ - + - ]
1020 [ + - ]: 1 : ReadFromStream(addrman2, ssPeers2);
1021 [ + - + - : 2 : BOOST_CHECK(addrman2.Size() == 3);
+ - ]
1022 [ + - + - : 5 : }
+ - - - ]
1023 : :
1024 : : // Produce a corrupt peers.dat that claims 20 addrs when it only has one addr.
1025 : 2 : static auto MakeCorruptPeersDat()
1026 : : {
1027 : 2 : DataStream s{};
1028 [ + - + - ]: 2 : s << ::Params().MessageStart();
1029 : :
1030 : 2 : unsigned char nVersion = 1;
1031 [ + - ]: 2 : s << nVersion;
1032 [ + - ]: 2 : s << ((unsigned char)32);
1033 [ + - ]: 2 : s << uint256::ONE;
1034 [ + - ]: 2 : s << 10; // nNew
1035 [ + - ]: 2 : s << 10; // nTried
1036 : :
1037 : 2 : int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
1038 [ + - ]: 2 : s << nUBuckets;
1039 : :
1040 [ + - + - : 4 : const std::optional<CService> serv{Lookup("252.1.1.1", 7777, false)};
+ - ]
1041 [ + - + - : 4 : BOOST_REQUIRE(serv.has_value());
+ - ]
1042 [ + - ]: 2 : CAddress addr = CAddress(serv.value(), NODE_NONE);
1043 [ + - + - : 4 : std::optional<CNetAddr> resolved{LookupHost("252.2.2.2", false)};
+ - ]
1044 [ + - + - : 4 : BOOST_REQUIRE(resolved.has_value());
+ - ]
1045 [ + - ]: 2 : AddrInfo info = AddrInfo(addr, resolved.value());
1046 [ + - ]: 2 : s << CAddress::V1_DISK(info);
1047 : :
1048 : 2 : return s;
1049 : 2 : }
1050 : :
1051 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(load_addrman_corrupted)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1052 : : {
1053 : : // Test that the de-serialization of corrupted peers.dat throws an exception.
1054 : 1 : auto ssPeers1{MakeCorruptPeersDat()};
1055 : 1 : bool exceptionThrown = false;
1056 [ + - + - ]: 1 : AddrMan addrman1{EMPTY_NETGROUPMAN, !DETERMINISTIC, GetCheckRatio(m_node)};
1057 [ + - + - : 2 : BOOST_CHECK(addrman1.Size() == 0);
+ - + - ]
1058 : 1 : try {
1059 : 1 : unsigned char pchMsgTmp[4];
1060 [ + - ]: 1 : ssPeers1 >> pchMsgTmp;
1061 [ - + ]: 1 : ssPeers1 >> addrman1;
1062 [ - + ]: 1 : } catch (const std::exception&) {
1063 : 1 : exceptionThrown = true;
1064 : 1 : }
1065 [ + - + - : 2 : BOOST_CHECK(exceptionThrown);
+ - ]
1066 : :
1067 : : // Test that ReadFromStream fails if peers.dat is corrupt
1068 [ + - ]: 1 : auto ssPeers2{MakeCorruptPeersDat()};
1069 : :
1070 [ + - + - ]: 1 : AddrMan addrman2{EMPTY_NETGROUPMAN, !DETERMINISTIC, GetCheckRatio(m_node)};
1071 [ + - + - : 2 : BOOST_CHECK(addrman2.Size() == 0);
+ - + - ]
1072 [ + - - + : 2 : BOOST_CHECK_THROW(ReadFromStream(addrman2, ssPeers2), std::ios_base::failure);
- - - - -
+ + - +
- ]
1073 : 1 : }
1074 : :
1075 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_update_address)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1076 : : {
1077 : : // Tests updating nTime via Connected() and nServices via SetServices() and Add()
1078 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
1079 [ + - + - ]: 1 : CNetAddr source{ResolveIP("252.2.2.2")};
1080 [ + - + - ]: 1 : CAddress addr{CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE)};
1081 : :
1082 : 1 : const auto start_time{Now<NodeSeconds>() - 10000s};
1083 : 1 : addr.nTime = start_time;
1084 [ + - + - : 3 : BOOST_CHECK(addrman->Add({addr}, source));
+ - + - +
+ - - ]
1085 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(), 1U);
+ - ]
1086 : :
1087 : : // Updating an addrman entry with a different port doesn't change it
1088 [ + - + - ]: 1 : CAddress addr_diff_port{CAddress(ResolveService("250.1.1.1", 8334), NODE_NONE)};
1089 : 1 : addr_diff_port.nTime = start_time;
1090 [ + - ]: 1 : addrman->Connected(addr_diff_port);
1091 [ + - ]: 1 : addrman->SetServices(addr_diff_port, NODE_NETWORK_LIMITED);
1092 [ + - ]: 1 : std::vector<CAddress> vAddr1{addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt)};
1093 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(vAddr1.size(), 1U);
1094 [ + - + - : 2 : BOOST_CHECK(vAddr1.at(0).nTime == start_time);
+ - + - ]
1095 [ + - + - : 1 : BOOST_CHECK_EQUAL(vAddr1.at(0).nServices, NODE_NONE);
+ - ]
1096 : :
1097 : : // Updating an addrman entry with the correct port is successful
1098 [ + - ]: 1 : addrman->Connected(addr);
1099 [ + - ]: 1 : addrman->SetServices(addr, NODE_NETWORK_LIMITED);
1100 [ + - ]: 1 : std::vector<CAddress> vAddr2 = addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt);
1101 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(vAddr2.size(), 1U);
1102 [ + - + - : 2 : BOOST_CHECK(vAddr2.at(0).nTime >= start_time + 10000s);
+ - + - ]
1103 [ + - + - : 1 : BOOST_CHECK_EQUAL(vAddr2.at(0).nServices, NODE_NETWORK_LIMITED);
+ - ]
1104 : :
1105 : : // Updating an existing addr through Add() (used in gossip relay) can add additional services but can't remove existing ones.
1106 [ + - + - ]: 1 : CAddress addr_v2{CAddress(ResolveService("250.1.1.1", 8333), NODE_P2P_V2)};
1107 : 1 : addr_v2.nTime = start_time;
1108 [ + - + - : 3 : BOOST_CHECK(!addrman->Add({addr_v2}, source));
+ - + - +
+ - - ]
1109 [ + - ]: 1 : std::vector<CAddress> vAddr3{addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt)};
1110 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(vAddr3.size(), 1U);
1111 [ + - + - : 1 : BOOST_CHECK_EQUAL(vAddr3.at(0).nServices, NODE_P2P_V2 | NODE_NETWORK_LIMITED);
+ - ]
1112 : :
1113 : : // SetServices() (used when we connected to them) overwrites existing service flags
1114 [ + - ]: 1 : addrman->SetServices(addr, NODE_NETWORK);
1115 [ + - ]: 1 : std::vector<CAddress> vAddr4{addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt)};
1116 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(vAddr4.size(), 1U);
1117 [ + - + - : 1 : BOOST_CHECK_EQUAL(vAddr4.at(0).nServices, NODE_NETWORK);
+ - ]
1118 : :
1119 : : // Promoting to Tried does not affect the service flags
1120 [ + - + - : 2 : BOOST_CHECK(addrman->Good(addr)); // addr has NODE_NONE
+ - + - ]
1121 [ + - ]: 1 : std::vector<CAddress> vAddr5{addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt)};
1122 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(vAddr5.size(), 1U);
1123 [ + - + - : 1 : BOOST_CHECK_EQUAL(vAddr5.at(0).nServices, NODE_NETWORK);
+ - ]
1124 : :
1125 : : // Adding service flags even works when the addr is in Tried
1126 [ + - + - : 3 : BOOST_CHECK(!addrman->Add({addr_v2}, source));
+ - + - +
+ - - ]
1127 [ + - ]: 1 : std::vector<CAddress> vAddr6{addrman->GetAddr(/*max_addresses=*/0, /*max_pct=*/0, /*network=*/std::nullopt)};
1128 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(vAddr6.size(), 1U);
1129 [ + - + - : 1 : BOOST_CHECK_EQUAL(vAddr6.at(0).nServices, NODE_NETWORK | NODE_P2P_V2);
+ - ]
1130 : 4 : }
1131 : :
1132 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(addrman_size)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1133 : : {
1134 : 1 : auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
1135 [ + - + - ]: 1 : const CNetAddr source = ResolveIP("252.2.2.2");
1136 : :
1137 : : // empty addrman
1138 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/std::nullopt, /*in_new=*/std::nullopt), 0U);
+ - ]
1139 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/NET_IPV4, /*in_new=*/std::nullopt), 0U);
+ - ]
1140 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/std::nullopt, /*in_new=*/true), 0U);
+ - ]
1141 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/NET_IPV4, /*in_new=*/false), 0U);
+ - ]
1142 : :
1143 : : // add two ipv4 addresses, one to tried and new
1144 [ + - + - ]: 1 : const CAddress addr1{ResolveService("250.1.1.1", 8333), NODE_NONE};
1145 [ + - + - : 3 : BOOST_CHECK(addrman->Add({addr1}, source));
+ - + - +
+ - - ]
1146 [ + - + - : 2 : BOOST_CHECK(addrman->Good(addr1));
+ - + - ]
1147 [ + - + - ]: 1 : const CAddress addr2{ResolveService("250.1.1.2", 8333), NODE_NONE};
1148 [ + - + - : 3 : BOOST_CHECK(addrman->Add({addr2}, source));
+ - + - +
+ - - ]
1149 : :
1150 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/std::nullopt, /*in_new=*/std::nullopt), 2U);
+ - ]
1151 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/NET_IPV4, /*in_new=*/std::nullopt), 2U);
+ - ]
1152 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/std::nullopt, /*in_new=*/true), 1U);
+ - ]
1153 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/std::nullopt, /*in_new=*/false), 1U);
+ - ]
1154 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/NET_IPV4, /*in_new=*/true), 1U);
+ - ]
1155 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/NET_IPV4, /*in_new=*/false), 1U);
+ - ]
1156 : :
1157 : : // add one i2p address to new
1158 [ + - ]: 1 : CService i2p_addr;
1159 [ + - + - ]: 1 : i2p_addr.SetSpecial("UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P");
1160 : 1 : const CAddress addr3{i2p_addr, NODE_NONE};
1161 [ + - + - : 3 : BOOST_CHECK(addrman->Add({addr3}, source));
+ - + - +
+ - - ]
1162 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/std::nullopt, /*in_new=*/std::nullopt), 3U);
+ - ]
1163 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/NET_IPV4, /*in_new=*/std::nullopt), 2U);
+ - ]
1164 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/NET_I2P, /*in_new=*/std::nullopt), 1U);
+ - ]
1165 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/NET_I2P, /*in_new=*/true), 1U);
+ - ]
1166 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/std::nullopt, /*in_new=*/true), 2U);
+ - ]
1167 [ + - + - : 1 : BOOST_CHECK_EQUAL(addrman->Size(/*net=*/std::nullopt, /*in_new=*/false), 1U);
+ - ]
1168 : 4 : }
1169 : :
1170 : : BOOST_AUTO_TEST_SUITE_END()
|