Skip to content

Commit

Permalink
scripted-diff: Rename cuckoo tests' local rand context
Browse files Browse the repository at this point in the history
-BEGIN VERIFY SCRIPT-
sed -i 's/insecure_rand/local_rand_ctx/g' src/test/cuckoocache_tests.cpp
-END VERIFY SCRIPT-

Cherry-picked from: 90620d6
  • Loading branch information
sipa authored and xanimo committed Mar 26, 2024
1 parent 4361d11 commit a400df2
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/test/cuckoocache_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
* using BOOST_CHECK_CLOSE to fail.
*
*/
FastRandomContext insecure_rand(true);
FastRandomContext local_rand_ctx(true);

BOOST_AUTO_TEST_SUITE(cuckoocache_tests);


/** insecure_GetRandHash fills in a uint256 from insecure_rand
/** insecure_GetRandHash fills in a uint256 from local_rand_ctx
*/
void insecure_GetRandHash(uint256& t)
{
uint32_t* ptr = (uint32_t*)t.begin();
for (uint8_t j = 0; j < 8; ++j)
*(ptr++) = insecure_rand.rand32();
*(ptr++) = local_rand_ctx.rand32();
}

/** Definition copied from /src/script/sigcache.cpp
Expand All @@ -60,9 +60,10 @@ class uint256Hasher
*/
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
{
insecure_rand = FastRandomContext(true);
local_rand_ctx = FastRandomContext(true);
CuckooCache::cache<uint256, uint256Hasher> cc{};
cc.setup_bytes(32 << 20);
size_t megabytes = 4;
cc.setup_bytes(megabytes << 20);
uint256 v;
for (int x = 0; x < 100000; ++x) {
insecure_GetRandHash(v);
Expand All @@ -80,7 +81,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
template <typename Cache>
double test_cache(size_t megabytes, double load)
{
insecure_rand = FastRandomContext(true);
local_rand_ctx = FastRandomContext(true);
std::vector<uint256> hashes;
Cache set{};
size_t bytes = megabytes * (1 << 20);
Expand All @@ -90,7 +91,7 @@ double test_cache(size_t megabytes, double load)
for (uint32_t i = 0; i < n_insert; ++i) {
uint32_t* ptr = (uint32_t*)hashes[i].begin();
for (uint8_t j = 0; j < 8; ++j)
*(ptr++) = insecure_rand.rand32();
*(ptr++) = local_rand_ctx.rand32();
}
/** We make a copy of the hashes because future optimizations of the
* cuckoocache may overwrite the inserted element, so the test is
Expand Down Expand Up @@ -151,7 +152,7 @@ template <typename Cache>
void test_cache_erase(size_t megabytes)
{
double load = 1;
insecure_rand = FastRandomContext(true);
local_rand_ctx = FastRandomContext(true);
std::vector<uint256> hashes;
Cache set{};
size_t bytes = megabytes * (1 << 20);
Expand All @@ -161,7 +162,7 @@ void test_cache_erase(size_t megabytes)
for (uint32_t i = 0; i < n_insert; ++i) {
uint32_t* ptr = (uint32_t*)hashes[i].begin();
for (uint8_t j = 0; j < 8; ++j)
*(ptr++) = insecure_rand.rand32();
*(ptr++) = local_rand_ctx.rand32();
}
/** We make a copy of the hashes because future optimizations of the
* cuckoocache may overwrite the inserted element, so the test is
Expand Down Expand Up @@ -214,7 +215,7 @@ template <typename Cache>
void test_cache_erase_parallel(size_t megabytes)
{
double load = 1;
insecure_rand = FastRandomContext(true);
local_rand_ctx = FastRandomContext(true);
std::vector<uint256> hashes;
Cache set{};
size_t bytes = megabytes * (1 << 20);
Expand All @@ -224,7 +225,7 @@ void test_cache_erase_parallel(size_t megabytes)
for (uint32_t i = 0; i < n_insert; ++i) {
uint32_t* ptr = (uint32_t*)hashes[i].begin();
for (uint8_t j = 0; j < 8; ++j)
*(ptr++) = insecure_rand.rand32();
*(ptr++) = local_rand_ctx.rand32();
}
/** We make a copy of the hashes because future optimizations of the
* cuckoocache may overwrite the inserted element, so the test is
Expand Down Expand Up @@ -316,7 +317,7 @@ void test_cache_generations()
// iterations with non-deterministic values, so it isn't "overfit" to the
// specific entropy in FastRandomContext(true) and implementation of the
// cache.
insecure_rand = FastRandomContext(true);
local_rand_ctx = FastRandomContext(true);

// block_activity models a chunk of network activity. n_insert elements are
// adde to the cache. The first and last n/4 are stored for removal later
Expand All @@ -333,7 +334,7 @@ void test_cache_generations()
for (uint32_t i = 0; i < n_insert; ++i) {
uint32_t* ptr = (uint32_t*)inserts[i].begin();
for (uint8_t j = 0; j < 8; ++j)
*(ptr++) = insecure_rand.rand32();
*(ptr++) = local_rand_ctx.rand32();
}
for (uint32_t i = 0; i < n_insert / 4; ++i)
reads.push_back(inserts[i]);
Expand Down

0 comments on commit a400df2

Please sign in to comment.