mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-28 13:20:55 +02:00
Make unit tests use the insecure_rand_ctx exclusively
This commit is contained in:
parent
8d98d42611
commit
fd3e7973ff
@ -21,40 +21,23 @@
|
|||||||
* using BOOST_CHECK_CLOSE to fail.
|
* using BOOST_CHECK_CLOSE to fail.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
FastRandomContext local_rand_ctx(true);
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
|
BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
|
||||||
|
|
||||||
|
|
||||||
/** insecure_GetRandHash fills in a uint256 from local_rand_ctx
|
|
||||||
*/
|
|
||||||
static void insecure_GetRandHash(uint256& t)
|
|
||||||
{
|
|
||||||
uint32_t* ptr = (uint32_t*)t.begin();
|
|
||||||
for (uint8_t j = 0; j < 8; ++j)
|
|
||||||
*(ptr++) = local_rand_ctx.rand32();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Test that no values not inserted into the cache are read out of it.
|
/* Test that no values not inserted into the cache are read out of it.
|
||||||
*
|
*
|
||||||
* There are no repeats in the first 200000 insecure_GetRandHash calls
|
* There are no repeats in the first 200000 insecure_GetRandHash calls
|
||||||
*/
|
*/
|
||||||
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
||||||
{
|
{
|
||||||
local_rand_ctx = FastRandomContext(true);
|
SeedInsecureRand(true);
|
||||||
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
|
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
|
||||||
size_t megabytes = 4;
|
size_t megabytes = 4;
|
||||||
cc.setup_bytes(megabytes << 20);
|
cc.setup_bytes(megabytes << 20);
|
||||||
uint256 v;
|
|
||||||
for (int x = 0; x < 100000; ++x) {
|
for (int x = 0; x < 100000; ++x) {
|
||||||
insecure_GetRandHash(v);
|
cc.insert(InsecureRand256());
|
||||||
cc.insert(v);
|
|
||||||
}
|
}
|
||||||
for (int x = 0; x < 100000; ++x) {
|
for (int x = 0; x < 100000; ++x) {
|
||||||
insecure_GetRandHash(v);
|
BOOST_CHECK(!cc.contains(InsecureRand256(), false));
|
||||||
BOOST_CHECK(!cc.contains(v, false));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,7 +47,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
|||||||
template <typename Cache>
|
template <typename Cache>
|
||||||
static double test_cache(size_t megabytes, double load)
|
static double test_cache(size_t megabytes, double load)
|
||||||
{
|
{
|
||||||
local_rand_ctx = FastRandomContext(true);
|
SeedInsecureRand(true);
|
||||||
std::vector<uint256> hashes;
|
std::vector<uint256> hashes;
|
||||||
Cache set{};
|
Cache set{};
|
||||||
size_t bytes = megabytes * (1 << 20);
|
size_t bytes = megabytes * (1 << 20);
|
||||||
@ -74,7 +57,7 @@ static double test_cache(size_t megabytes, double load)
|
|||||||
for (uint32_t i = 0; i < n_insert; ++i) {
|
for (uint32_t i = 0; i < n_insert; ++i) {
|
||||||
uint32_t* ptr = (uint32_t*)hashes[i].begin();
|
uint32_t* ptr = (uint32_t*)hashes[i].begin();
|
||||||
for (uint8_t j = 0; j < 8; ++j)
|
for (uint8_t j = 0; j < 8; ++j)
|
||||||
*(ptr++) = local_rand_ctx.rand32();
|
*(ptr++) = InsecureRand32();
|
||||||
}
|
}
|
||||||
/** We make a copy of the hashes because future optimizations of the
|
/** We make a copy of the hashes because future optimizations of the
|
||||||
* cuckoocache may overwrite the inserted element, so the test is
|
* cuckoocache may overwrite the inserted element, so the test is
|
||||||
@ -135,7 +118,7 @@ template <typename Cache>
|
|||||||
static void test_cache_erase(size_t megabytes)
|
static void test_cache_erase(size_t megabytes)
|
||||||
{
|
{
|
||||||
double load = 1;
|
double load = 1;
|
||||||
local_rand_ctx = FastRandomContext(true);
|
SeedInsecureRand(true);
|
||||||
std::vector<uint256> hashes;
|
std::vector<uint256> hashes;
|
||||||
Cache set{};
|
Cache set{};
|
||||||
size_t bytes = megabytes * (1 << 20);
|
size_t bytes = megabytes * (1 << 20);
|
||||||
@ -145,7 +128,7 @@ static void test_cache_erase(size_t megabytes)
|
|||||||
for (uint32_t i = 0; i < n_insert; ++i) {
|
for (uint32_t i = 0; i < n_insert; ++i) {
|
||||||
uint32_t* ptr = (uint32_t*)hashes[i].begin();
|
uint32_t* ptr = (uint32_t*)hashes[i].begin();
|
||||||
for (uint8_t j = 0; j < 8; ++j)
|
for (uint8_t j = 0; j < 8; ++j)
|
||||||
*(ptr++) = local_rand_ctx.rand32();
|
*(ptr++) = InsecureRand32();
|
||||||
}
|
}
|
||||||
/** We make a copy of the hashes because future optimizations of the
|
/** We make a copy of the hashes because future optimizations of the
|
||||||
* cuckoocache may overwrite the inserted element, so the test is
|
* cuckoocache may overwrite the inserted element, so the test is
|
||||||
@ -198,7 +181,7 @@ template <typename Cache>
|
|||||||
static void test_cache_erase_parallel(size_t megabytes)
|
static void test_cache_erase_parallel(size_t megabytes)
|
||||||
{
|
{
|
||||||
double load = 1;
|
double load = 1;
|
||||||
local_rand_ctx = FastRandomContext(true);
|
SeedInsecureRand(true);
|
||||||
std::vector<uint256> hashes;
|
std::vector<uint256> hashes;
|
||||||
Cache set{};
|
Cache set{};
|
||||||
size_t bytes = megabytes * (1 << 20);
|
size_t bytes = megabytes * (1 << 20);
|
||||||
@ -208,7 +191,7 @@ static void test_cache_erase_parallel(size_t megabytes)
|
|||||||
for (uint32_t i = 0; i < n_insert; ++i) {
|
for (uint32_t i = 0; i < n_insert; ++i) {
|
||||||
uint32_t* ptr = (uint32_t*)hashes[i].begin();
|
uint32_t* ptr = (uint32_t*)hashes[i].begin();
|
||||||
for (uint8_t j = 0; j < 8; ++j)
|
for (uint8_t j = 0; j < 8; ++j)
|
||||||
*(ptr++) = local_rand_ctx.rand32();
|
*(ptr++) = InsecureRand32();
|
||||||
}
|
}
|
||||||
/** We make a copy of the hashes because future optimizations of the
|
/** We make a copy of the hashes because future optimizations of the
|
||||||
* cuckoocache may overwrite the inserted element, so the test is
|
* cuckoocache may overwrite the inserted element, so the test is
|
||||||
@ -300,7 +283,7 @@ static void test_cache_generations()
|
|||||||
// iterations with non-deterministic values, so it isn't "overfit" to the
|
// iterations with non-deterministic values, so it isn't "overfit" to the
|
||||||
// specific entropy in FastRandomContext(true) and implementation of the
|
// specific entropy in FastRandomContext(true) and implementation of the
|
||||||
// cache.
|
// cache.
|
||||||
local_rand_ctx = FastRandomContext(true);
|
SeedInsecureRand(true);
|
||||||
|
|
||||||
// block_activity models a chunk of network activity. n_insert elements are
|
// block_activity models a chunk of network activity. n_insert elements are
|
||||||
// added to the cache. The first and last n/4 are stored for removal later
|
// added to the cache. The first and last n/4 are stored for removal later
|
||||||
@ -317,7 +300,7 @@ static void test_cache_generations()
|
|||||||
for (uint32_t i = 0; i < n_insert; ++i) {
|
for (uint32_t i = 0; i < n_insert; ++i) {
|
||||||
uint32_t* ptr = (uint32_t*)inserts[i].begin();
|
uint32_t* ptr = (uint32_t*)inserts[i].begin();
|
||||||
for (uint8_t j = 0; j < 8; ++j)
|
for (uint8_t j = 0; j < 8; ++j)
|
||||||
*(ptr++) = local_rand_ctx.rand32();
|
*(ptr++) = InsecureRand32();
|
||||||
}
|
}
|
||||||
for (uint32_t i = 0; i < n_insert / 4; ++i)
|
for (uint32_t i = 0; i < n_insert / 4; ++i)
|
||||||
reads.push_back(inserts[i]);
|
reads.push_back(inserts[i]);
|
||||||
|
@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
|
|||||||
|
|
||||||
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic)
|
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic)
|
||||||
{
|
{
|
||||||
CAddress addr(ip(GetRandInt(0xffffffff)), NODE_NONE);
|
CAddress addr(ip(insecure_rand_ctx.randbits(32)), NODE_NONE);
|
||||||
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", /*fInboundIn=*/ false));
|
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", /*fInboundIn=*/ false));
|
||||||
CNode &node = *vNodes.back();
|
CNode &node = *vNodes.back();
|
||||||
node.SetSendVersion(PROTOCOL_VERSION);
|
node.SetSendVersion(PROTOCOL_VERSION);
|
||||||
|
@ -104,8 +104,8 @@ void BuildChain(const uint256& root, int height, const unsigned int invalid_rate
|
|||||||
{
|
{
|
||||||
if (height <= 0 || blocks.size() >= max_size) return;
|
if (height <= 0 || blocks.size() >= max_size) return;
|
||||||
|
|
||||||
bool gen_invalid = GetRand(100) < invalid_rate;
|
bool gen_invalid = InsecureRandRange(100) < invalid_rate;
|
||||||
bool gen_fork = GetRand(100) < branch_rate;
|
bool gen_fork = InsecureRandRange(100) < branch_rate;
|
||||||
|
|
||||||
const std::shared_ptr<const CBlock> pblock = gen_invalid ? BadBlock(root) : GoodBlock(root);
|
const std::shared_ptr<const CBlock> pblock = gen_invalid ? BadBlock(root) : GoodBlock(root);
|
||||||
blocks.push_back(pblock);
|
blocks.push_back(pblock);
|
||||||
@ -157,7 +157,7 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
|
|||||||
threads.create_thread([&blocks]() {
|
threads.create_thread([&blocks]() {
|
||||||
bool ignored;
|
bool ignored;
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
auto block = blocks[GetRand(blocks.size() - 1)];
|
auto block = blocks[InsecureRandRange(blocks.size() - 1)];
|
||||||
ProcessNewBlock(Params(), block, true, &ignored);
|
ProcessNewBlock(Params(), block, true, &ignored);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user