7
7
#include < addrman_impl.h>
8
8
#include < chainparams.h>
9
9
#include < merkleblock.h>
10
+ #include < random.h>
10
11
#include < test/fuzz/FuzzedDataProvider.h>
11
12
#include < test/fuzz/fuzz.h>
12
13
#include < test/fuzz/util.h>
@@ -50,7 +51,7 @@ class AddrManDeterministic : public AddrMan
50
51
/* *
51
52
* Generate a random address. Always returns a valid address.
52
53
*/
53
- CNetAddr RandAddr () EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs)
54
+ CNetAddr RandAddr (FastRandomContext& fast_random_context ) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs)
54
55
{
55
56
CNetAddr addr;
56
57
if (m_fuzzed_data_provider.remaining_bytes () > 1 && m_fuzzed_data_provider.ConsumeBool ()) {
@@ -62,15 +63,15 @@ class AddrManDeterministic : public AddrMan
62
63
{4 , ADDR_TORV3_SIZE},
63
64
{5 , ADDR_I2P_SIZE},
64
65
{6 , ADDR_CJDNS_SIZE}};
65
- uint8_t net = m_impl-> insecure_rand .randrange (5 ) + 1 ; // [1..5]
66
+ uint8_t net = fast_random_context .randrange (5 ) + 1 ; // [1..5]
66
67
if (net == 3 ) {
67
68
net = 6 ;
68
69
}
69
70
70
71
CDataStream s (SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT);
71
72
72
73
s << net;
73
- s << m_impl-> insecure_rand .randbytes (net_len_map.at (net));
74
+ s << fast_random_context .randbytes (net_len_map.at (net));
74
75
75
76
s >> addr;
76
77
}
@@ -99,24 +100,26 @@ class AddrManDeterministic : public AddrMan
99
100
100
101
const size_t num_sources = m_fuzzed_data_provider.ConsumeIntegralInRange <size_t >(1 , 50 );
101
102
CNetAddr prev_source;
102
- // Use insecure_rand inside the loops instead of m_fuzzed_data_provider because when
103
- // the latter is exhausted it just returns 0.
103
+ // Generate a FastRandomContext seed to use inside the loops instead of
104
+ // m_fuzzed_data_provider. When m_fuzzed_data_provider is exhausted it
105
+ // just returns 0.
106
+ FastRandomContext fast_random_context{ConsumeUInt256 (m_fuzzed_data_provider)};
104
107
for (size_t i = 0 ; i < num_sources; ++i) {
105
- const auto source = RandAddr ();
106
- const size_t num_addresses = m_impl-> insecure_rand .randrange (500 ) + 1 ; // [1..500]
108
+ const auto source = RandAddr (fast_random_context );
109
+ const size_t num_addresses = fast_random_context .randrange (500 ) + 1 ; // [1..500]
107
110
108
111
for (size_t j = 0 ; j < num_addresses; ++j) {
109
- const auto addr = CAddress{CService{RandAddr (), 8333 }, NODE_NETWORK};
110
- const auto time_penalty = m_impl-> insecure_rand .randrange (100000001 );
112
+ const auto addr = CAddress{CService{RandAddr (fast_random_context ), 8333 }, NODE_NETWORK};
113
+ const auto time_penalty = fast_random_context .randrange (100000001 );
111
114
m_impl->Add_ (addr, source, time_penalty);
112
115
113
116
if (n > 0 && m_impl->mapInfo .size () % n == 0 ) {
114
117
m_impl->Good_ (addr, false , GetTime ());
115
118
}
116
119
117
120
// Add 10% of the addresses from more than one source.
118
- if (m_impl-> insecure_rand .randrange (10 ) == 0 && prev_source.IsValid ()) {
119
- m_impl->Add_ ({ addr} , prev_source, time_penalty);
121
+ if (fast_random_context .randrange (10 ) == 0 && prev_source.IsValid ()) {
122
+ m_impl->Add_ (addr, prev_source, time_penalty);
120
123
}
121
124
}
122
125
prev_source = source;
0 commit comments