[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:
1 parent
90ad8ad61a
commit
640476eb0e
1 file changed
+16
-20
+16
-20
@@ -74,22 +74,9 @@ CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& f
|
|||||||
return addr;
|
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.
|
// Add some of the addresses directly to the "tried" table.
|
||||||
|
|
||||||
// 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
|
// 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
|
||||||
@@ -108,19 +95,28 @@ public:
|
|||||||
for (size_t j = 0; j < num_addresses; ++j) {
|
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 addr = CAddress{CService{RandAddr(fuzzed_data_provider, fast_random_context), 8333}, NODE_NETWORK};
|
||||||
const auto time_penalty = fast_random_context.randrange(100000001);
|
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) {
|
if (n > 0 && addrman.size() % n == 0) {
|
||||||
m_impl->Good_(addr, false, GetTime());
|
addrman.Good(addr, GetTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add 10% of the addresses from more than one source.
|
// Add 10% of the addresses from more than one source.
|
||||||
if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) {
|
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;
|
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);
|
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);
|
||||||
|
|||||||
Reference in new issue
Block a user