Bitcoin Core 28.99.0
P2P Digital Currency
addrman.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-2022 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 <time.h>
18#include <util/asmap.h>
19#include <util/chaintype.h>
20
21#include <cassert>
22#include <cstdint>
23#include <optional>
24#include <string>
25#include <vector>
26
27namespace {
28const BasicTestingSetup* g_setup;
29
30int32_t GetCheckRatio()
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
42FUZZ_TARGET(data_stream_addr_man, .init = initialize_addrman)
43{
45 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
46 DataStream data_stream = ConsumeDataStream(fuzzed_data_provider);
47 NetGroupManager netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
48 AddrMan addr_man(netgroupman, /*deterministic=*/false, GetCheckRatio());
49 try {
50 ReadFromStream(addr_man, data_stream);
51 } catch (const std::exception&) {
52 }
53}
54
58CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& fast_random_context)
59{
60 CNetAddr addr;
61 assert(!addr.IsValid());
62 for (size_t i = 0; i < 8 && !addr.IsValid(); ++i) {
63 if (fuzzed_data_provider.remaining_bytes() > 1 && fuzzed_data_provider.ConsumeBool()) {
64 addr = ConsumeNetAddr(fuzzed_data_provider);
65 } else {
66 addr = ConsumeNetAddr(fuzzed_data_provider, &fast_random_context);
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
81void FillAddrman(AddrMan& addrman, FuzzedDataProvider& fuzzed_data_provider)
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);
88 CNetAddr prev_source;
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.
92 FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
93 for (size_t i = 0; i < num_sources; ++i) {
94 const auto source = RandAddr(fuzzed_data_provider, fast_random_context);
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) {
98 const auto addr = CAddress{CService{RandAddr(fuzzed_data_provider, fast_random_context), 8333}, NODE_NETWORK};
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 }
111 prev_source = source;
112 }
113}
114
116{
118 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
119 SetMockTime(ConsumeTime(fuzzed_data_provider));
120 NetGroupManager netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
121 auto addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
122 if (fuzzed_data_provider.ConsumeBool()) {
123 const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
124 DataStream ds{serialized_data};
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 }
131 AddrManDeterministic& addr_man = *addr_man_ptr;
132 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
133 CallOneOf(
134 fuzzed_data_provider,
135 [&] {
136 addr_man.ResolveCollisions();
137 },
138 [&] {
139 (void)addr_man.SelectTriedCollision();
140 },
141 [&] {
142 std::vector<CAddress> addresses;
143 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
144 addresses.push_back(ConsumeAddress(fuzzed_data_provider));
145 }
146 auto net_addr = ConsumeNetAddr(fuzzed_data_provider);
147 auto time_penalty = std::chrono::seconds{ConsumeTime(fuzzed_data_provider, 0, 100000000)};
148 addr_man.Add(addresses, net_addr, time_penalty);
149 },
150 [&] {
151 auto addr = ConsumeService(fuzzed_data_provider);
152 auto time = NodeSeconds{std::chrono::seconds{ConsumeTime(fuzzed_data_provider)}};
153 addr_man.Good(addr, time);
154 },
155 [&] {
156 auto addr = ConsumeService(fuzzed_data_provider);
157 auto count_failure = fuzzed_data_provider.ConsumeBool();
158 auto time = NodeSeconds{std::chrono::seconds{ConsumeTime(fuzzed_data_provider)}};
159 addr_man.Attempt(addr, count_failure, time);
160 },
161 [&] {
162 auto addr = ConsumeService(fuzzed_data_provider);
163 auto time = NodeSeconds{std::chrono::seconds{ConsumeTime(fuzzed_data_provider)}};
164 addr_man.Connected(addr, time);
165 },
166 [&] {
167 auto addr = ConsumeService(fuzzed_data_provider);
168 auto n_services = ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS);
169 addr_man.SetServices(addr, n_services);
170 });
171 }
172 const AddrMan& const_addr_man{addr_man};
173 std::optional<Network> network;
174 if (fuzzed_data_provider.ConsumeBool()) {
175 network = fuzzed_data_provider.PickValueInArray(ALL_NETWORKS);
176 }
177 auto max_addresses = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096);
178 auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
179 auto filtered = fuzzed_data_provider.ConsumeBool();
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) {
184 if (fuzzed_data_provider.ConsumeBool()) {
185 nets.insert(net);
186 }
187 }
188 (void)const_addr_man.Select(fuzzed_data_provider.ConsumeBool(), nets);
189
190 std::optional<bool> in_new;
191 if (fuzzed_data_provider.ConsumeBool()) {
192 in_new = fuzzed_data_provider.ConsumeBool();
193 }
194 (void)const_addr_man.Size(network, in_new);
195 DataStream data_stream{};
196 data_stream << const_addr_man;
197}
198
199// Check that serialize followed by unserialize produces the same addrman.
201{
203 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
204 SetMockTime(ConsumeTime(fuzzed_data_provider));
205
206 NetGroupManager netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
207 AddrManDeterministic addr_man1{netgroupman, fuzzed_data_provider, GetCheckRatio()};
208 AddrManDeterministic addr_man2{netgroupman, fuzzed_data_provider, GetCheckRatio()};
209
210 DataStream data_stream{};
211
212 FillAddrman(addr_man1, fuzzed_data_provider);
213 data_stream << addr_man1;
214 data_stream >> addr_man2;
215 assert(addr_man1 == addr_man2);
216}
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
Definition: addrdb.cpp:186
static int32_t GetCheckRatio(const NodeContext &node_ctx)
Stochastic address manager.
Definition: addrman.h:89
void Connected(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
We have successfully connected to this peer.
Definition: addrman.cpp:1342
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=Now< NodeSeconds >())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1312
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
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1317
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
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1322
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
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1347
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:482
A CService with information about it as peer.
Definition: protocol.h:367
Network address.
Definition: netaddress.h:112
bool IsValid() const
Definition: netaddress.cpp:428
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
Fast randomness source.
Definition: random.h:377
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
Netgroup manager.
Definition: netgroup.h:16
#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
Definition: rpcconsole.cpp:62
Basic testing setup.
Definition: setup_common.h:63
node::NodeContext m_node
Definition: setup_common.h:65
ArgsManager * args
Definition: context.h:74
FUZZ_TARGET(data_stream_addr_man,.init=initialize_addrman)
Definition: addrman.cpp:42
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:87
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
Definition: net.cpp:28
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:213
NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:201
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:133
DataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:73
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:171
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:94
constexpr auto ALL_NETWORKS
Definition: net.h:126
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:35
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
assert(!tx.IsCoinBase())