0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

[fuzz] Make Fill() a free function in fuzz/addrman.cpp

Also rename it to FillAddrman and pass an addrman reference as an
argument. Change FillAddrman to only use addrman's public interface methods.
This commit is contained in:
John Newbery 2021-09-21 10:48:32 +01:00
parent 90ad8ad61a
commit 640476eb0e

View file

@ -74,22 +74,9 @@ CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& f
return addr;
}
class AddrManDeterministic : public AddrMan
/** Fill addrman with lots of addresses from lots of sources. */
void FillAddrman(AddrMan& addrman, FuzzedDataProvider& fuzzed_data_provider)
{
public:
explicit AddrManDeterministic(std::vector<bool> asmap, FuzzedDataProvider& fuzzed_data_provider)
: AddrMan(std::move(asmap), /* deterministic */ true, /* consistency_check_ratio */ 0)
{
WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
}
/**
* Fill this addrman with lots of addresses from lots of sources.
*/
void Fill(FuzzedDataProvider& fuzzed_data_provider)
{
LOCK(m_impl->cs);
// Add some of the addresses directly to the "tried" table.
// 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
@ -108,19 +95,28 @@ public:
for (size_t j = 0; j < num_addresses; ++j) {
const auto addr = CAddress{CService{RandAddr(fuzzed_data_provider, fast_random_context), 8333}, NODE_NETWORK};
const auto time_penalty = fast_random_context.randrange(100000001);
m_impl->Add_(addr, source, time_penalty);
addrman.Add({addr}, source, time_penalty);
if (n > 0 && m_impl->mapInfo.size() % n == 0) {
m_impl->Good_(addr, false, GetTime());
if (n > 0 && addrman.size() % n == 0) {
addrman.Good(addr, GetTime());
}
// Add 10% of the addresses from more than one source.
if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) {
m_impl->Add_(addr, prev_source, time_penalty);
addrman.Add({addr}, prev_source, time_penalty);
}
}
prev_source = source;
}
}
class AddrManDeterministic : public AddrMan
{
public:
explicit AddrManDeterministic(std::vector<bool> asmap, FuzzedDataProvider& fuzzed_data_provider)
: AddrMan(std::move(asmap), /* deterministic */ true, /* consistency_check_ratio */ 0)
{
WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
}
/**
@ -307,7 +303,7 @@ FUZZ_TARGET_INIT(addrman_serdeser, initialize_addrman)
CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION);
addr_man1.Fill(fuzzed_data_provider);
FillAddrman(addr_man1, fuzzed_data_provider);
data_stream << addr_man1;
data_stream >> addr_man2;
assert(addr_man1 == addr_man2);