diff options
author | John Newbery <john@johnnewbery.com> | 2021-09-21 10:48:32 +0100 |
---|---|---|
committer | John Newbery <john@johnnewbery.com> | 2021-10-05 16:59:09 +0100 |
commit | 640476eb0e17fd4c64d4840ceab229642f1d79d9 (patch) | |
tree | 3b47c5b4d959d9d89b5aa372e6d5f7d017293bf4 /src/test | |
parent | 90ad8ad61a38dbb1f247a5f3d5f649a856d9938a (diff) |
[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.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/fuzz/addrman.cpp | 78 |
1 files changed, 37 insertions, 41 deletions
diff --git a/src/test/fuzz/addrman.cpp b/src/test/fuzz/addrman.cpp index 7b88e9b6c2..297656ac3d 100644 --- a/src/test/fuzz/addrman.cpp +++ b/src/test/fuzz/addrman.cpp @@ -74,6 +74,42 @@ CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& f 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 { public: @@ -84,46 +120,6 @@ public: } /** - * 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. * This compares: * - the values in `mapInfo` (the keys aka ids are ignored) @@ -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); |