8#include <chainparams.h>
14#include <test/data/asmap.raw.h>
21#include <boost/test/unit_test.hpp>
27#include <unordered_set>
31using namespace std::literals;
40 return std::clamp<int32_t>(
node_ctx.args->GetIntArg(
"-checkaddrman", 100), 0, 1000000);
45 const std::optional<CNetAddr> addr{
LookupHost(
ip,
false)};
52 const std::optional<CService>
serv{
Lookup(
ip, port,
false)};
96 std::vector<CAddress>
vAddr;
129 std::vector<CAddress>
filtered{addrman->GetAddr(0, 0, std::nullopt)};
133 std::vector<CAddress>
unfiltered{addrman->GetAddr(0, 0, std::nullopt,
false)};
157 std::vector<CAddress>
addresses{addrman->GetAddr(0, 0, std::nullopt)};
170 addresses = addrman->GetAddr(0, 0, std::nullopt);
193 auto addr_ret2 = addrman->Select().first;
208 BOOST_CHECK(!addrman->Select(
false).first.IsValid());
209 BOOST_CHECK(!addrman->Select(
true).first.IsValid());
225 BOOST_CHECK(!addrman->Select(
true).first.IsValid());
257 std::set<uint16_t>
ports;
258 for (
int i = 0; i < 20; ++i) {
259 ports.insert(addrman->Select().first.GetPort());
267 BOOST_CHECK(!addrman->Select(
true, {NET_IPV4}).first.IsValid());
268 BOOST_CHECK(!addrman->Select(
false, {NET_IPV4}).first.IsValid());
277 BOOST_CHECK(!addrman->Select(
false, {NET_IPV6}).first.IsValid());
278 BOOST_CHECK(!addrman->Select(
false, {NET_ONION}).first.IsValid());
279 BOOST_CHECK(!addrman->Select(
false, {NET_I2P}).first.IsValid());
280 BOOST_CHECK(!addrman->Select(
false, {NET_CJDNS}).first.IsValid());
281 BOOST_CHECK(!addrman->Select(
true, {NET_CJDNS}).first.IsValid());
294 BOOST_CHECK(!addrman->Select(
false, {NET_IPV6}).first.IsValid());
295 BOOST_CHECK(!addrman->Select(
false, {NET_ONION}).first.IsValid());
296 BOOST_CHECK(!addrman->Select(
false, {NET_CJDNS}).first.IsValid());
303 BOOST_CHECK(!addrman->Select(
true, {NET_I2P}).first.IsValid());
341 const std::vector<std::pair<CService, int>> entries{
347 for (
const auto& [addr, position] : entries) {
360 for (
const auto& [addr, position] : entries) {
365 for (
int i = 0; i <
SAMPLES; ++i) {
445 addr.
nTime = start_time;
448 for (
unsigned int i = 1; i < 20; ++i) {
449 std::string
addr_ip{ToString(i % 256) +
"." + ToString(i >> 8 % 256) +
".1.1"};
451 addrman->Add({addr},
source);
460 for (
unsigned int i = 1; i < 400; ++i) {
461 std::string
addr_ip{ToString(i % 256) +
"." + ToString(i >> 8 % 256) +
".1.1"};
463 addr.nTime = start_time + std::chrono::seconds{i};
464 addrman->Add({addr},
source);
510 std::vector<CAddress>
vAddr1 = addrman->GetAddr(0, 0, std::nullopt);
541 for (
unsigned int i = 1; i < (8 * 256); i++) {
543 int octet2 = i >> 8 % 256;
553 std::vector<CAddress>
vAddr = addrman->GetAddr(2500, 23, std::nullopt);
555 size_t percent23 = (addrman->Size() * 23) / 100;
582 for (
size_t i = 0; i < 3; ++i) {
624 std::set<int> buckets;
625 for (
int i = 0; i < 255; i++) {
630 buckets.insert(bucket);
637 for (
int j = 0;
j < 255;
j++) {
642 buckets.insert(bucket);
674 std::set<int> buckets;
675 for (
int i = 0; i < 255; i++) {
680 buckets.insert(bucket);
687 for (
int j = 0;
j < 4 * 255;
j++) {
690 ToString(250 + (
j / 255)) +
"." + ToString(
j % 256) +
".1.1"),
NODE_NONE),
693 buckets.insert(bucket);
700 for (
int p = 0;
p < 255;
p++) {
705 buckets.insert(bucket);
751 std::set<int> buckets;
752 for (
int j = 0;
j < 255;
j++) {
757 buckets.insert(bucket);
764 for (
int j = 0;
j < 255;
j++) {
769 buckets.insert(bucket);
803 std::set<int> buckets;
804 for (
int i = 0; i < 255; i++) {
809 buckets.insert(bucket);
816 for (
int j = 0;
j < 4 * 255;
j++) {
819 ToString(250 + (
j / 255)) +
"." + ToString(
j % 256) +
".1.1"),
NODE_NONE),
822 buckets.insert(bucket);
829 for (
int p = 0;
p < 255;
p++) {
834 buckets.insert(bucket);
841 for (
int p = 0;
p < 255;
p++) {
846 buckets.insert(bucket);
936 const std::string str{
stream.str()};
965 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
969 for (
unsigned int i = 1; i < 23; i++) {
975 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
980 for (
unsigned int i = 1; i < 23; i++) {
987 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
997 for (
unsigned int i = 1; i < 36; i++) {
1009 BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(),
"250.1.1.19:0");
1014 addrman->ResolveCollisions();
1015 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
1018 for (
unsigned int i = 37; i < 59; i++) {
1029 BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(),
"250.1.1.10:0");
1036 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() !=
"[::]:0");
1039 addrman->ResolveCollisions();
1040 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
1050 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
1054 for (
unsigned int i = 1; i < 36; i++) {
1067 auto info = addrman->SelectTriedCollision().first;
1076 addrman->ResolveCollisions();
1077 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
1086 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
1092 BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(),
"250.1.1.36:0");
1097 addrman->ResolveCollisions();
1098 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
1127 addr4 =
Lookup(
"250.7.3.3\0example.com"s, 9999,
false);
1131 const std::optional<CService>
source{
Lookup(
"252.5.1.1", 8333,
false)};
1164 unsigned char nVersion = 1;
1166 s << ((
unsigned char)32);
1174 const std::optional<CService>
serv{
Lookup(
"252.1.1.1", 7777,
false)};
1177 std::optional<CNetAddr> resolved{
LookupHost(
"252.2.2.2",
false)};
1196 }(), std::ios_base::failure,
HasReason{
"end of data"});
1214 addr.nTime = start_time;
1223 std::vector<CAddress>
vAddr1{addrman->GetAddr(0, 0, std::nullopt)};
1229 addrman->Connected(addr);
1231 std::vector<CAddress>
vAddr2 = addrman->GetAddr(0, 0, std::nullopt);
1240 std::vector<CAddress>
vAddr3{addrman->GetAddr(0, 0, std::nullopt)};
1246 std::vector<CAddress>
vAddr4{addrman->GetAddr(0, 0, std::nullopt)};
1252 std::vector<CAddress>
vAddr5{addrman->GetAddr(0, 0, std::nullopt)};
1258 std::vector<CAddress>
vAddr6{addrman->GetAddr(0, 0, std::nullopt)};
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
constexpr int32_t ADDRMAN_MAX_FAILURES
How many successive failures are allowed ...
constexpr auto ADDRMAN_MIN_FAIL
... in at least this duration
constexpr int ADDRMAN_NEW_BUCKET_COUNT
static CService ResolveService(const std::string &ip, uint16_t port=0)
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 auto EMPTY_NETGROUPMAN
static CNetAddr ResolveIP(const std::string &ip)
BOOST_CHECK_LT(ZeroL, OneL)
const CChainParams & Params()
Return the currently selected parameters.
Extended statistics about a CAddress.
Stochastic address manager.
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(std::string_view addr)
Parse a Tor or I2P address and set this object to it.
A combination of a network address (CNetAddr) and a (TCP) port.
Double ended buffer combining vector and stream-like interfaces.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
A writer stream (for serialization) that computes a 256-bit hash.
static NetGroupManager NoAsmap()
static NetGroupManager WithEmbeddedAsmap(std::span< const std::byte > asmap)
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
static CService ip(uint32_t i)
BOOST_CHECK_EXCEPTION(HTTPHeaders{}.Read(reader), std::runtime_error, HasReason{"Empty HTTP header name"})
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_NO_THROW(stmt)
#define BOOST_CHECK(expr)
Location information for an address in AddrMan.
NodeContext struct containing references to chain state and connection state.
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.