8#include <chainparams.h>
13#include <test/data/asmap.raw.h>
18#include <boost/test/unit_test.hpp>
24using namespace std::literals;
33 return std::clamp<int32_t>(
node_ctx.args->GetIntArg(
"-checkaddrman", 100), 0, 1000000);
38 const std::optional<CNetAddr> addr{
LookupHost(
ip,
false)};
45 const std::optional<CService>
serv{
Lookup(
ip, port,
false)};
89 std::vector<CAddress>
vAddr;
115 std::vector<CAddress>
addresses{addrman->GetAddr(0, 0, std::nullopt)};
128 addresses = addrman->GetAddr(0, 0, std::nullopt);
151 auto addr_ret2 = addrman->Select().first;
166 BOOST_CHECK(!addrman->Select(
false).first.IsValid());
167 BOOST_CHECK(!addrman->Select(
true).first.IsValid());
183 BOOST_CHECK(!addrman->Select(
true).first.IsValid());
215 std::set<uint16_t>
ports;
216 for (
int i = 0; i < 20; ++i) {
217 ports.insert(addrman->Select().first.GetPort());
225 BOOST_CHECK(!addrman->Select(
true, {NET_IPV4}).first.IsValid());
226 BOOST_CHECK(!addrman->Select(
false, {NET_IPV4}).first.IsValid());
235 BOOST_CHECK(!addrman->Select(
false, {NET_IPV6}).first.IsValid());
236 BOOST_CHECK(!addrman->Select(
false, {NET_ONION}).first.IsValid());
237 BOOST_CHECK(!addrman->Select(
false, {NET_I2P}).first.IsValid());
238 BOOST_CHECK(!addrman->Select(
false, {NET_CJDNS}).first.IsValid());
239 BOOST_CHECK(!addrman->Select(
true, {NET_CJDNS}).first.IsValid());
252 BOOST_CHECK(!addrman->Select(
false, {NET_IPV6}).first.IsValid());
253 BOOST_CHECK(!addrman->Select(
false, {NET_ONION}).first.IsValid());
254 BOOST_CHECK(!addrman->Select(
false, {NET_CJDNS}).first.IsValid());
261 BOOST_CHECK(!addrman->Select(
true, {NET_I2P}).first.IsValid());
347 addr.
nTime = start_time;
350 for (
unsigned int i = 1; i < 20; ++i) {
351 std::string
addr_ip{ToString(i % 256) +
"." + ToString(i >> 8 % 256) +
".1.1"};
353 addrman->Add({addr},
source);
362 for (
unsigned int i = 1; i < 400; ++i) {
363 std::string
addr_ip{ToString(i % 256) +
"." + ToString(i >> 8 % 256) +
".1.1"};
365 addr.nTime = start_time + std::chrono::seconds{i};
366 addrman->Add({addr},
source);
412 std::vector<CAddress>
vAddr1 = addrman->GetAddr(0, 0, std::nullopt);
443 for (
unsigned int i = 1; i < (8 * 256); i++) {
445 int octet2 = i >> 8 % 256;
455 std::vector<CAddress>
vAddr = addrman->GetAddr(2500, 23, std::nullopt);
457 size_t percent23 = (addrman->Size() * 23) / 100;
484 for (
size_t i = 0; i < 3; ++i) {
526 std::set<int> buckets;
527 for (
int i = 0; i < 255; i++) {
532 buckets.insert(bucket);
539 for (
int j = 0;
j < 255;
j++) {
544 buckets.insert(bucket);
576 std::set<int> buckets;
577 for (
int i = 0; i < 255; i++) {
582 buckets.insert(bucket);
589 for (
int j = 0;
j < 4 * 255;
j++) {
592 ToString(250 + (
j / 255)) +
"." + ToString(
j % 256) +
".1.1"),
NODE_NONE),
595 buckets.insert(bucket);
602 for (
int p = 0;
p < 255;
p++) {
607 buckets.insert(bucket);
653 std::set<int> buckets;
654 for (
int j = 0;
j < 255;
j++) {
659 buckets.insert(bucket);
666 for (
int j = 0;
j < 255;
j++) {
671 buckets.insert(bucket);
705 std::set<int> buckets;
706 for (
int i = 0; i < 255; i++) {
711 buckets.insert(bucket);
718 for (
int j = 0;
j < 4 * 255;
j++) {
721 ToString(250 + (
j / 255)) +
"." + ToString(
j % 256) +
".1.1"),
NODE_NONE),
724 buckets.insert(bucket);
731 for (
int p = 0;
p < 255;
p++) {
736 buckets.insert(bucket);
743 for (
int p = 0;
p < 255;
p++) {
748 buckets.insert(bucket);
838 const std::string str{
stream.str()};
867 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
871 for (
unsigned int i = 1; i < 23; i++) {
877 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
882 for (
unsigned int i = 1; i < 23; i++) {
889 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
899 for (
unsigned int i = 1; i < 36; i++) {
911 BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(),
"250.1.1.19:0");
916 addrman->ResolveCollisions();
917 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
920 for (
unsigned int i = 37; i < 59; i++) {
931 BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(),
"250.1.1.10:0");
938 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() !=
"[::]:0");
941 addrman->ResolveCollisions();
942 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
952 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
956 for (
unsigned int i = 1; i < 36; i++) {
969 auto info = addrman->SelectTriedCollision().first;
978 addrman->ResolveCollisions();
979 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
988 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
994 BOOST_CHECK_EQUAL(addrman->SelectTriedCollision().first.ToStringAddrPort(),
"250.1.1.36:0");
998 addrman->ResolveCollisions();
999 BOOST_CHECK(addrman->SelectTriedCollision().first.ToStringAddrPort() ==
"[::]:0");
1028 addr4 =
Lookup(
"250.7.3.3\0example.com"s, 9999,
false);
1032 const std::optional<CService>
source{
Lookup(
"252.5.1.1", 8333,
false)};
1048 }
catch (
const std::exception&) {
1070 unsigned char nVersion = 1;
1072 s << ((
unsigned char)32);
1080 const std::optional<CService>
serv{
Lookup(
"252.1.1.1", 7777,
false)};
1102 }
catch (
const std::exception&) {
1123 addr.nTime = start_time;
1132 std::vector<CAddress>
vAddr1{addrman->GetAddr(0, 0, std::nullopt)};
1138 addrman->Connected(addr);
1140 std::vector<CAddress>
vAddr2 = addrman->GetAddr(0, 0, std::nullopt);
1149 std::vector<CAddress>
vAddr3{addrman->GetAddr(0, 0, std::nullopt)};
1155 std::vector<CAddress>
vAddr4{addrman->GetAddr(0, 0, std::nullopt)};
1161 std::vector<CAddress>
vAddr5{addrman->GetAddr(0, 0, std::nullopt)};
1167 std::vector<CAddress>
vAddr6{addrman->GetAddr(0, 0, std::nullopt)};
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
static 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)
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.
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)
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
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.