Skip to content

Commit 4f1bb46

Browse files
committed
test: Add test for multiplicity in addrman new tables
1 parent e880bb7 commit 4f1bb46

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/test/addrman_tests.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,38 @@ BOOST_AUTO_TEST_CASE(addrman_new_collisions)
218218
BOOST_CHECK_EQUAL(addrman->size(), num_addrs - collisions);
219219
}
220220

221+
BOOST_AUTO_TEST_CASE(addrman_new_multiplicity)
222+
{
223+
auto addrman = TestAddrMan();
224+
CAddress addr{CAddress(ResolveService("253.3.3.3", 8333), NODE_NONE)};
225+
int64_t start_time{GetAdjustedTime()};
226+
addr.nTime = start_time;
227+
228+
// test that multiplicity stays at 1 if nTime doesn't increase
229+
for (unsigned int i = 1; i < 20; ++i) {
230+
std::string addr_ip{ToString(i % 256) + "." + ToString(i >> 8 % 256) + ".1.1"};
231+
CNetAddr source{ResolveIP(addr_ip)};
232+
addrman->Add({addr}, source);
233+
}
234+
AddressPosition addr_pos = addrman->FindAddressEntry(addr).value();
235+
BOOST_CHECK_EQUAL(addr_pos.multiplicity, 1U);
236+
BOOST_CHECK_EQUAL(addrman->size(), 1U);
237+
238+
// if nTime increases, an addr can occur in up to 8 buckets
239+
// The acceptance probability decreases exponentially with existing multiplicity -
240+
// choose number of iterations such that it gets to 8 with deterministic addrman.
241+
for (unsigned int i = 1; i < 400; ++i) {
242+
std::string addr_ip{ToString(i % 256) + "." + ToString(i >> 8 % 256) + ".1.1"};
243+
CNetAddr source{ResolveIP(addr_ip)};
244+
addr.nTime = start_time + i;
245+
addrman->Add({addr}, source);
246+
}
247+
AddressPosition addr_pos_multi = addrman->FindAddressEntry(addr).value();
248+
BOOST_CHECK_EQUAL(addr_pos_multi.multiplicity, 8U);
249+
// multiplicity doesn't affect size
250+
BOOST_CHECK_EQUAL(addrman->size(), 1U);
251+
}
252+
221253
BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
222254
{
223255
auto addrman = TestAddrMan();

0 commit comments

Comments
 (0)