mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -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:
parent
90ad8ad61a
commit
640476eb0e
1 changed files with 37 additions and 41 deletions
|
@ -74,6 +74,42 @@ CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& f
|
||||||
return addr;
|
return addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Fill addrman with lots of addresses from lots of sources. */
|
||||||
|
void FillAddrman(AddrMan& addrman, FuzzedDataProvider& fuzzed_data_provider)
|
||||||
|
{
|
||||||
|
// Add some of the addresses directly to the "tried" table.
|
||||||
|
|
||||||
|
// 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
|
||||||
|
const size_t n = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
|
||||||
|
|
||||||
|
const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
|
||||||
|
CNetAddr prev_source;
|
||||||
|
// Generate a FastRandomContext seed to use inside the loops instead of
|
||||||
|
// fuzzed_data_provider. When fuzzed_data_provider is exhausted it
|
||||||
|
// just returns 0.
|
||||||
|
FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
|
||||||
|
for (size_t i = 0; i < num_sources; ++i) {
|
||||||
|
const auto source = RandAddr(fuzzed_data_provider, fast_random_context);
|
||||||
|
const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
|
||||||
|
|
||||||
|
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);
|
||||||
|
addrman.Add({addr}, source, time_penalty);
|
||||||
|
|
||||||
|
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()) {
|
||||||
|
addrman.Add({addr}, prev_source, time_penalty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prev_source = source;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class AddrManDeterministic : public AddrMan
|
class AddrManDeterministic : public AddrMan
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -83,46 +119,6 @@ public:
|
||||||
WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
|
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%
|
|
||||||
const size_t n = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
|
|
||||||
|
|
||||||
const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
|
|
||||||
CNetAddr prev_source;
|
|
||||||
// Generate a FastRandomContext seed to use inside the loops instead of
|
|
||||||
// fuzzed_data_provider. When fuzzed_data_provider is exhausted it
|
|
||||||
// just returns 0.
|
|
||||||
FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
|
|
||||||
for (size_t i = 0; i < num_sources; ++i) {
|
|
||||||
const auto source = RandAddr(fuzzed_data_provider, fast_random_context);
|
|
||||||
const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (n > 0 && m_impl->mapInfo.size() % n == 0) {
|
|
||||||
m_impl->Good_(addr, false, 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
prev_source = source;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare with another AddrMan.
|
* Compare with another AddrMan.
|
||||||
* This compares:
|
* This compares:
|
||||||
|
@ -307,7 +303,7 @@ FUZZ_TARGET_INIT(addrman_serdeser, initialize_addrman)
|
||||||
|
|
||||||
CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION);
|
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_man1;
|
||||||
data_stream >> addr_man2;
|
data_stream >> addr_man2;
|
||||||
assert(addr_man1 == addr_man2);
|
assert(addr_man1 == addr_man2);
|
||||||
|
|
Loading…
Add table
Reference in a new issue