Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
addrman.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-present 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 <common/args.h>
10#include <merkleblock.h>
11#include <random.h>
13#include <test/fuzz/fuzz.h>
14#include <test/fuzz/util.h>
15#include <test/fuzz/util/net.h>
17#include <util/asmap.h>
18#include <util/chaintype.h>
19
20#include <cassert>
21#include <cstdint>
22#include <ctime>
23#include <optional>
24#include <string>
25#include <vector>
26
27namespace {
29
31{
32 return std::clamp<int32_t>(g_setup->m_node.args->GetIntArg("-checkaddrman", 0), 0, 1000000);
33}
34} // namespace
35
37{
38 static const auto testing_setup = MakeNoLogFileContext<>(ChainType::REGTEST);
39 g_setup = testing_setup.get();
40}
41
54
59{
60 CNetAddr addr;
61 assert(!addr.IsValid());
62 for (size_t i = 0; i < 8 && !addr.IsValid(); ++i) {
65 } else {
67 }
68 }
69
70 // Return a dummy IPv4 5.5.5.5 if we generated an invalid address.
71 if (!addr.IsValid()) {
72 in_addr v4_addr = {};
73 v4_addr.s_addr = 0x05050505;
74 addr = CNetAddr{v4_addr};
75 }
76
77 return addr;
78}
79
82{
83 // Add a fraction of the addresses to the "tried" table.
84 // 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
85 const size_t n = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
86
87 const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
89 // Generate a FastRandomContext seed to use inside the loops instead of
90 // fuzzed_data_provider. When fuzzed_data_provider is exhausted it
91 // just returns 0.
93 for (size_t i = 0; i < num_sources; ++i) {
95 const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
96
97 for (size_t j = 0; j < num_addresses; ++j) {
99 const std::chrono::seconds time_penalty{fast_random_context.randrange(100000001)};
100 addrman.Add({addr}, source, time_penalty);
101
102 if (n > 0 && addrman.Size() % n == 0) {
103 addrman.Good(addr, Now<NodeSeconds>());
104 }
105
106 // Add 10% of the addresses from more than one source.
107 if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) {
108 addrman.Add({addr}, prev_source, time_penalty);
109 }
110 }
112 }
113}
114
116{
118 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
121 auto addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
125 try {
126 ds >> *addr_man_ptr;
127 } catch (const std::ios_base::failure&) {
128 addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
129 }
130 }
133 CallOneOf(
135 [&] {
136 addr_man.ResolveCollisions();
137 },
138 [&] {
139 (void)addr_man.SelectTriedCollision();
140 },
141 [&] {
142 std::vector<CAddress> addresses;
145 }
147 auto time_penalty = ConsumeDuration(fuzzed_data_provider, /*min=*/0s, /*max=*/100000000s);
149 },
150 [&] {
153 addr_man.Good(addr, time);
154 },
155 [&] {
159 addr_man.Attempt(addr, count_failure, time);
160 },
161 [&] {
164 addr_man.Connected(addr, time);
165 },
166 [&] {
169 addr_man.SetServices(addr, n_services);
170 });
171 }
173 std::optional<Network> network;
176 }
180 (void)const_addr_man.GetAddr(max_addresses, max_pct, network, filtered);
181
182 std::unordered_set<Network> nets;
183 for (const auto& net : ALL_NETWORKS) {
185 nets.insert(net);
186 }
187 }
189
190 std::optional<bool> in_new;
193 }
194 (void)const_addr_man.Size(network, in_new);
197}
198
199// Check that serialize followed by unserialize produces the same addrman.
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
Definition addrdb.cpp:191
static int32_t GetCheckRatio(const NodeContext &node_ctx)
const TestingSetup * g_setup
Stochastic address manager.
Definition addrman.h:89
size_t Size(std::optional< Network > net=std::nullopt, std::optional< bool > in_new=std::nullopt) const
Return size information about addrman.
Definition addrman.cpp:1297
bool Good(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
Mark an address record as accessible and attempt to move it to addrman's tried table.
Definition addrman.cpp:1307
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty=0s)
Attempt to add one or more addresses to addrman's new table.
Definition addrman.cpp:1302
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Definition args.h:306
A CService with information about it as peer.
Definition protocol.h:367
Network address.
Definition netaddress.h:113
bool IsValid() const
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:530
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:133
Fast randomness source.
Definition random.h:386
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
Netgroup manager.
Definition netgroup.h:17
#define FUZZ_TARGET(...)
Definition fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
@ NODE_NETWORK
Definition protocol.h:315
const char * source
Basic testing setup.
node::NodeContext m_node
ArgsManager * args
Definition context.h:74
void FillAddrman(AddrMan &addrman, FuzzedDataProvider &fuzzed_data_provider)
Fill addrman with lots of addresses from lots of sources.
Definition addrman.cpp:81
CNetAddr RandAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext &fast_random_context)
Generate a random address.
Definition addrman.cpp:58
void initialize_addrman()
Definition addrman.cpp:36
CAddress ConsumeAddress(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition net.cpp:88
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
Definition net.cpp:29
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition net.h:250
NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition net.h:236
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition util.cpp:34
std::chrono::seconds ConsumeDuration(FuzzedDataProvider &fuzzed_data_provider, std::chrono::seconds min, std::chrono::seconds max) noexcept
Definition util.cpp:42
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition util.h:128
DataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:68
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition util.h:167
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition util.h:35
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:57
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
Definition net.h:120
constexpr auto ALL_NETWORKS
Definition net.h:153
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition time.cpp:44
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:38