21
21
* using BOOST_CHECK_CLOSE to fail.
22
22
*
23
23
*/
24
- FastRandomContext local_rand_ctx (true );
25
-
26
24
BOOST_AUTO_TEST_SUITE (cuckoocache_tests);
27
25
28
-
29
- /* * insecure_GetRandHash fills in a uint256 from local_rand_ctx
30
- */
31
- static void insecure_GetRandHash (uint256& t)
32
- {
33
- uint32_t * ptr = (uint32_t *)t.begin ();
34
- for (uint8_t j = 0 ; j < 8 ; ++j)
35
- *(ptr++) = local_rand_ctx.rand32 ();
36
- }
37
-
38
-
39
-
40
26
/* Test that no values not inserted into the cache are read out of it.
41
27
*
42
28
* There are no repeats in the first 200000 insecure_GetRandHash calls
43
29
*/
44
30
BOOST_AUTO_TEST_CASE (test_cuckoocache_no_fakes)
45
31
{
46
- local_rand_ctx = FastRandomContext (true );
32
+ SeedInsecureRand (true );
47
33
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
48
34
size_t megabytes = 4 ;
49
35
cc.setup_bytes (megabytes << 20 );
50
- uint256 v;
51
36
for (int x = 0 ; x < 100000 ; ++x) {
52
- insecure_GetRandHash (v);
53
- cc.insert (v);
37
+ cc.insert (InsecureRand256 ());
54
38
}
55
39
for (int x = 0 ; x < 100000 ; ++x) {
56
- insecure_GetRandHash (v);
57
- BOOST_CHECK (!cc.contains (v, false ));
40
+ BOOST_CHECK (!cc.contains (InsecureRand256 (), false ));
58
41
}
59
42
};
60
43
@@ -64,7 +47,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
64
47
template <typename Cache>
65
48
static double test_cache (size_t megabytes, double load)
66
49
{
67
- local_rand_ctx = FastRandomContext (true );
50
+ SeedInsecureRand (true );
68
51
std::vector<uint256> hashes;
69
52
Cache set{};
70
53
size_t bytes = megabytes * (1 << 20 );
@@ -74,7 +57,7 @@ static double test_cache(size_t megabytes, double load)
74
57
for (uint32_t i = 0 ; i < n_insert; ++i) {
75
58
uint32_t * ptr = (uint32_t *)hashes[i].begin ();
76
59
for (uint8_t j = 0 ; j < 8 ; ++j)
77
- *(ptr++) = local_rand_ctx. rand32 ();
60
+ *(ptr++) = InsecureRand32 ();
78
61
}
79
62
/* * We make a copy of the hashes because future optimizations of the
80
63
* cuckoocache may overwrite the inserted element, so the test is
@@ -135,7 +118,7 @@ template <typename Cache>
135
118
static void test_cache_erase (size_t megabytes)
136
119
{
137
120
double load = 1 ;
138
- local_rand_ctx = FastRandomContext (true );
121
+ SeedInsecureRand (true );
139
122
std::vector<uint256> hashes;
140
123
Cache set{};
141
124
size_t bytes = megabytes * (1 << 20 );
@@ -145,7 +128,7 @@ static void test_cache_erase(size_t megabytes)
145
128
for (uint32_t i = 0 ; i < n_insert; ++i) {
146
129
uint32_t * ptr = (uint32_t *)hashes[i].begin ();
147
130
for (uint8_t j = 0 ; j < 8 ; ++j)
148
- *(ptr++) = local_rand_ctx. rand32 ();
131
+ *(ptr++) = InsecureRand32 ();
149
132
}
150
133
/* * We make a copy of the hashes because future optimizations of the
151
134
* cuckoocache may overwrite the inserted element, so the test is
@@ -198,7 +181,7 @@ template <typename Cache>
198
181
static void test_cache_erase_parallel (size_t megabytes)
199
182
{
200
183
double load = 1 ;
201
- local_rand_ctx = FastRandomContext (true );
184
+ SeedInsecureRand (true );
202
185
std::vector<uint256> hashes;
203
186
Cache set{};
204
187
size_t bytes = megabytes * (1 << 20 );
@@ -208,7 +191,7 @@ static void test_cache_erase_parallel(size_t megabytes)
208
191
for (uint32_t i = 0 ; i < n_insert; ++i) {
209
192
uint32_t * ptr = (uint32_t *)hashes[i].begin ();
210
193
for (uint8_t j = 0 ; j < 8 ; ++j)
211
- *(ptr++) = local_rand_ctx. rand32 ();
194
+ *(ptr++) = InsecureRand32 ();
212
195
}
213
196
/* * We make a copy of the hashes because future optimizations of the
214
197
* cuckoocache may overwrite the inserted element, so the test is
@@ -300,7 +283,7 @@ static void test_cache_generations()
300
283
// iterations with non-deterministic values, so it isn't "overfit" to the
301
284
// specific entropy in FastRandomContext(true) and implementation of the
302
285
// cache.
303
- local_rand_ctx = FastRandomContext (true );
286
+ SeedInsecureRand (true );
304
287
305
288
// block_activity models a chunk of network activity. n_insert elements are
306
289
// added to the cache. The first and last n/4 are stored for removal later
@@ -317,7 +300,7 @@ static void test_cache_generations()
317
300
for (uint32_t i = 0 ; i < n_insert; ++i) {
318
301
uint32_t * ptr = (uint32_t *)inserts[i].begin ();
319
302
for (uint8_t j = 0 ; j < 8 ; ++j)
320
- *(ptr++) = local_rand_ctx. rand32 ();
303
+ *(ptr++) = InsecureRand32 ();
321
304
}
322
305
for (uint32_t i = 0 ; i < n_insert / 4 ; ++i)
323
306
reads.push_back (inserts[i]);
0 commit comments