8#include <chainparams.h>
13#include <test/data/asmap.raw.h>
18#include <boost/test/unit_test.hpp>
23using namespace std::literals;
32 return std::clamp<int32_t>(node_ctx.
args->
GetIntArg(
"-checkaddrman", 100), 0, 1000000);
37 const std::optional<CNetAddr> addr{
LookupHost(
ip,
false)};
38 BOOST_CHECK_MESSAGE(addr.has_value(),
strprintf(
"failed to resolve: %s",
ip));
44 const std::optional<CService> serv{
Lookup(
ip, port,
false)};
45 BOOST_CHECK_MESSAGE(serv.has_value(),
strprintf(
"failed to resolve: %s:%i",
ip, port));
52 int vector_size(
source.size() * 8);
53 std::vector<bool> result(vector_size);
54 for (
int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
55 uint8_t cur_byte{std::to_integer<uint8_t>(
source[byte_i])};
56 for (
int bit_i = 0; bit_i < 8; ++bit_i) {
57 result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
73 auto addr_null = addrman->Select().first;
80 auto addr_ret1 = addrman->Select().first;
102 std::vector<CAddress> vAddr;
125 auto addr_ret2 = addrman->Select().first;
126 BOOST_CHECK(addr_ret2.ToStringAddrPort() ==
"250.1.1.1:8333" || addr_ret2.ToStringAddrPort() ==
"250.1.1.1:8334");
132 bool new_only =
true;
133 auto addr_ret3 = addrman->Select(new_only).first;
140 BOOST_CHECK(!addrman->Select(
false).first.IsValid());
141 BOOST_CHECK(!addrman->Select(
true).first.IsValid());
157 BOOST_CHECK(!addrman->Select(
true).first.IsValid());
178 BOOST_CHECK(addrman->Add({CAddress(addr5, NODE_NONE)}, addr3));
180 BOOST_CHECK(addrman->Add({CAddress(addr6, NODE_NONE)}, addr3));
189 std::set<uint16_t> ports;
190 for (
int i = 0; i < 20; ++i) {
191 ports.insert(addrman->Select().first.GetPort());
199 BOOST_CHECK(!addrman->Select(
true, {NET_IPV4}).first.IsValid());
200 BOOST_CHECK(!addrman->Select(
false, {NET_IPV4}).first.IsValid());
209 BOOST_CHECK(!addrman->Select(
false, {NET_IPV6}).first.IsValid());
210 BOOST_CHECK(!addrman->Select(
false, {NET_ONION}).first.IsValid());
211 BOOST_CHECK(!addrman->Select(
false, {NET_I2P}).first.IsValid());
212 BOOST_CHECK(!addrman->Select(
false, {NET_CJDNS}).first.IsValid());
213 BOOST_CHECK(!addrman->Select(
true, {NET_CJDNS}).first.IsValid());
218 i2p_addr.
SetSpecial(
"udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p");
221 BOOST_CHECK(addrman->Select(
true, {NET_I2P}).first == i2p_addr);
222 BOOST_CHECK(addrman->Select(
false, {NET_I2P}).first == i2p_addr);
225 BOOST_CHECK(addrman->Select(
false, nets_with_entries).first.IsValid());
226 BOOST_CHECK(!addrman->Select(
false, {NET_IPV6}).first.IsValid());
227 BOOST_CHECK(!addrman->Select(
false, {NET_ONION}).first.IsValid());
228 BOOST_CHECK(!addrman->Select(
false, {NET_CJDNS}).first.IsValid());
230 BOOST_CHECK(!addrman->Select(
false, nets_without_entries).first.IsValid());
235 BOOST_CHECK(!addrman->Select(
true, {NET_I2P}).first.IsValid());
236 BOOST_CHECK(addrman->Select(
false, {NET_I2P}).first == i2p_addr);
240 i2p_addr2.
SetSpecial(
"c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p");
243 BOOST_CHECK(addrman->Select(
true, {NET_I2P}).first == i2p_addr2);
246 bool new_selected{
false};
247 bool tried_selected{
false};
250 while (--counter > 0 && (!new_selected || !tried_selected)) {
252 BOOST_REQUIRE(selected == i2p_addr || selected == i2p_addr2);
253 if (selected == i2p_addr) {
254 tried_selected =
true;
273 i2p_addr.
SetSpecial(
"udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p");
293 uint32_t num_addrs{0};
297 while (num_addrs < 22) {
307 uint32_t collisions{1};
320 const auto start_time{Now<NodeSeconds>()};
321 addr.
nTime = start_time;
324 for (
unsigned int i = 1; i < 20; ++i) {
325 std::string addr_ip{
ToString(i % 256) +
"." +
ToString(i >> 8 % 256) +
".1.1"};
327 addrman->Add({addr},
source);
336 for (
unsigned int i = 1; i < 400; ++i) {
337 std::string addr_ip{
ToString(i % 256) +
"." +
ToString(i >> 8 % 256) +
".1.1"};
339 addr.nTime = start_time + std::chrono::seconds{i};
340 addrman->Add({addr},
source);
342 AddressPosition addr_pos_multi = addrman->FindAddressEntry(addr).value();
354 uint32_t num_addrs{0};
358 while (num_addrs < 35) {
386 std::vector<CAddress> vAddr1 = addrman->GetAddr(0, 0, std::nullopt);
390 addr1.nTime = Now<NodeSeconds>();
392 addr2.nTime = Now<NodeSeconds>();
394 addr3.
nTime = Now<NodeSeconds>();
396 addr4.
nTime = Now<NodeSeconds>();
398 addr5.
nTime = Now<NodeSeconds>();
403 BOOST_CHECK(addrman->Add({addr1, addr3, addr5}, source1));
404 BOOST_CHECK(addrman->Add({addr2, addr4}, source2));
417 for (
unsigned int i = 1; i < (8 * 256); i++) {
418 int octet1 = i % 256;
419 int octet2 = i >> 8 % 256;
424 addr.
nTime = Now<NodeSeconds>();
425 addrman->Add({addr},
ResolveIP(strAddr));
429 std::vector<CAddress> vAddr = addrman->GetAddr(2500, 23, std::nullopt);
431 size_t percent23 = (addrman->Size() * 23) / 100;
444 addr1.nTime = Now<NodeSeconds>();
453 addr3.
nTime = Now<NodeSeconds>();
454 addrman->Good(addr3, Now<NodeSeconds>());
458 for (
size_t i = 0; i < 3; ++i) {
459 addrman->Attempt(addr3,
true, Now<NodeSeconds>() - 61
s);
494 std::set<int> buckets;
495 for (
int i = 0; i < 255; i++) {
500 buckets.insert(bucket);
507 for (
int j = 0; j < 255; j++) {
512 buckets.insert(bucket);
544 std::set<int> buckets;
545 for (
int i = 0; i < 255; i++) {
550 buckets.insert(bucket);
557 for (
int j = 0; j < 4 * 255; j++) {
563 buckets.insert(bucket);
570 for (
int p = 0; p < 255; p++) {
575 buckets.insert(bucket);
595 std::vector<bool> asmap =
FromBytes(test::data::asmap);
622 std::set<int> buckets;
623 for (
int j = 0; j < 255; j++) {
628 buckets.insert(bucket);
635 for (
int j = 0; j < 255; j++) {
640 buckets.insert(bucket);
649 std::vector<bool> asmap =
FromBytes(test::data::asmap);
675 std::set<int> buckets;
676 for (
int i = 0; i < 255; i++) {
681 buckets.insert(bucket);
688 for (
int j = 0; j < 4 * 255; j++) {
694 buckets.insert(bucket);
701 for (
int p = 0; p < 255; p++) {
706 buckets.insert(bucket);
713 for (
int p = 0; p < 255; p++) {
718 buckets.insert(bucket);
727 std::vector<bool> asmap1 =
FromBytes(test::data::asmap);
731 auto addrman_asmap1 = std::make_unique<AddrMan>(netgroupman,
DETERMINISTIC, ratio);
732 auto addrman_asmap1_dup = std::make_unique<AddrMan>(netgroupman,
DETERMINISTIC, ratio);
740 addrman_asmap1->Add({addr}, default_source);
742 stream << *addrman_asmap1;
744 stream >> *addrman_asmap1_dup;
746 AddressPosition addr_pos1 = addrman_asmap1->FindAddressEntry(addr).value();
747 AddressPosition addr_pos2 = addrman_asmap1_dup->FindAddressEntry(addr).value();
754 stream << *addrman_asmap1;
755 stream >> *addrman_noasmap;
756 AddressPosition addr_pos3 = addrman_noasmap->FindAddressEntry(addr).value();
762 addrman_asmap1 = std::make_unique<AddrMan>(netgroupman,
DETERMINISTIC, ratio);
764 addrman_noasmap->Add({addr}, default_source);
765 stream << *addrman_noasmap;
766 stream >> *addrman_asmap1;
768 AddressPosition addr_pos4 = addrman_asmap1->FindAddressEntry(addr).value();
774 addrman_asmap1 = std::make_unique<AddrMan>(netgroupman,
DETERMINISTIC, ratio);
778 addrman_noasmap->Add({addr,
addr2}, default_source);
782 stream << *addrman_noasmap;
783 stream >> *addrman_asmap1;
802 addrman->Add({new1, tried1, new2, tried2},
CNetAddr{});
803 addrman->Good(tried1);
804 addrman->Good(tried2);
805 BOOST_REQUIRE_EQUAL(addrman->Size(), 4);
809 const std::string str{stream.str()};
812 const char new2_raw[]{6, 6, 6, 6};
813 const uint8_t new2_raw_replacement[]{0, 0, 0, 0};
814 pos = str.find(new2_raw, 0,
sizeof(new2_raw));
815 BOOST_REQUIRE(pos != std::string::npos);
816 BOOST_REQUIRE(pos +
sizeof(new2_raw_replacement) <= stream.size());
817 memcpy(stream.data() + pos, new2_raw_replacement,
sizeof(new2_raw_replacement));
819 const char tried2_raw[]{8, 8, 8, 8};
820 const uint8_t tried2_raw_replacement[]{255, 255, 255, 255};
821 pos = str.find(tried2_raw, 0,
sizeof(tried2_raw));
822 BOOST_REQUIRE(pos != std::string::npos);
823 BOOST_REQUIRE(pos +
sizeof(tried2_raw_replacement) <= stream.size());
824 memcpy(stream.data() + pos, tried2_raw_replacement,
sizeof(tried2_raw_replacement));
838 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
842 for (
unsigned int i = 1; i < 23; i++) {
848 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
853 for (
unsigned int i = 1; i < 23; i++) {
860 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
870 for (
unsigned int i = 1; i < 36; i++) {
882 BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(),
"250.1.1.19:0");
887 addrman->ResolveCollisions();
888 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
891 for (
unsigned int i = 37; i < 59; i++) {
902 BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(),
"250.1.1.10:0");
909 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() !=
"[::]:0");
912 addrman->ResolveCollisions();
913 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
923 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
927 for (
unsigned int i = 1; i < 36; i++) {
940 auto info = addrman->SelectTriedCollision().first;
946 addrman->Attempt(info,
false, Now<NodeSeconds>() - 61
s);
949 addrman->ResolveCollisions();
950 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
959 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
965 BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(),
"250.1.1.36:0");
969 addrman->ResolveCollisions();
970 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
982 ssPeersIn << addrman;
990 std::optional<CService>
addr1,
addr2, addr3, addr4;
995 addr3 =
Lookup(
"250.7.3.3", 9999,
false);
997 addr3 =
Lookup(
"250.7.3.3"s, 9999,
false);
999 addr4 =
Lookup(
"250.7.3.3\0example.com"s, 9999,
false);
1003 const std::optional<CService>
source{
Lookup(
"252.5.1.1", 8333,
false)};
1011 bool exceptionThrown =
false;
1016 unsigned char pchMsgTmp[4];
1017 ssPeers1 >> pchMsgTmp;
1018 ssPeers1 >> addrman1;
1019 }
catch (
const std::exception&) {
1020 exceptionThrown =
true;
1041 unsigned char nVersion = 1;
1043 s << ((
unsigned char)32);
1051 const std::optional<CService> serv{
Lookup(
"252.1.1.1", 7777,
false)};
1052 BOOST_REQUIRE(serv.has_value());
1054 std::optional<CNetAddr> resolved{
LookupHost(
"252.2.2.2",
false)};
1055 BOOST_REQUIRE(resolved.has_value());
1066 bool exceptionThrown =
false;
1070 unsigned char pchMsgTmp[4];
1071 ssPeers1 >> pchMsgTmp;
1072 ssPeers1 >> addrman1;
1073 }
catch (
const std::exception&) {
1074 exceptionThrown =
true;
1093 const auto start_time{Now<NodeSeconds>() - 10000
s};
1094 addr.nTime = start_time;
1100 addr_diff_port.
nTime = start_time;
1101 addrman->Connected(addr_diff_port);
1103 std::vector<CAddress> vAddr1{addrman->GetAddr(0, 0, std::nullopt)};
1109 addrman->Connected(addr);
1111 std::vector<CAddress> vAddr2 = addrman->GetAddr(0, 0, std::nullopt);
1113 BOOST_CHECK(vAddr2.at(0).nTime >= start_time + 10000
s);
1118 addr_v2.
nTime = start_time;
1120 std::vector<CAddress> vAddr3{addrman->GetAddr(0, 0, std::nullopt)};
1126 std::vector<CAddress> vAddr4{addrman->GetAddr(0, 0, std::nullopt)};
1132 std::vector<CAddress> vAddr5{addrman->GetAddr(0, 0, std::nullopt)};
1138 std::vector<CAddress> vAddr6{addrman->GetAddr(0, 0, std::nullopt)};
1170 i2p_addr.
SetSpecial(
"UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P");
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
static NetGroupManager EMPTY_NETGROUPMAN
static CService ResolveService(const std::string &ip, uint16_t port=0)
static std::vector< bool > FromBytes(std::span< const std::byte > source)
static int32_t GetCheckRatio(const NodeContext &node_ctx)
static auto AddrmanToStream(const AddrMan &addrman)
static const bool DETERMINISTIC
BOOST_AUTO_TEST_CASE(addrman_simple)
static auto MakeCorruptPeersDat()
static CNetAddr ResolveIP(const std::string &ip)
const CChainParams & Params()
Return the currently selected parameters.
Extended statistics about a CAddress.
int GetNewBucket(const uint256 &nKey, const CNetAddr &src, const NetGroupManager &netgroupman) const
Calculate in which "new" bucket this entry belongs, given a certain source.
int GetTriedBucket(const uint256 &nKey, const NetGroupManager &netgroupman) const
Calculate in which "tried" bucket this entry belongs.
Stochastic address manager.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
A CService with information about it as peer.
NodeSeconds nTime
Always included in serialization. The behavior is unspecified if the value is not representable as ui...
static constexpr SerParams V1_DISK
const MessageStartChars & MessageStart() const
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
A combination of a network address (CNetAddr) and a (TCP) port.
std::vector< unsigned char > GetKey() const
Double ended buffer combining vector and stream-like interfaces.
A writer stream (for serialization) that computes a 256-bit hash.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
static CService ip(uint32_t i)
static const std::string addr1
static const std::string addr2
std::string ToString(const T &t)
Locale-independent version of std::to_string.
@ NET_ONION
TOR (v2 or v3)
std::vector< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
std::vector< CService > Lookup(const std::string &name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
Resolve a service string to its corresponding service.
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
Location information for an address in AddrMan.
NodeContext struct containing references to chain state and connection state.
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds