Random generation utilities for tests

This commit is contained in:
Piotr Wójcik 2024-05-16 19:08:33 +02:00
commit 49ed0cff7e
10 changed files with 70 additions and 30 deletions

View file

@ -1,6 +1,7 @@
#include <nano/lib/blocks.hpp>
#include <nano/node/transport/fake.hpp>
#include <nano/test_common/chains.hpp>
#include <nano/test_common/random.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

View file

@ -1,4 +1,5 @@
#include <nano/node/fork_cache.hpp>
#include <nano/test_common/random.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

View file

@ -4,6 +4,7 @@
#include <nano/node/endpoint.hpp>
#include <nano/node/network.hpp>
#include <nano/secure/vote.hpp>
#include <nano/test_common/random.hpp>
#include <nano/test_common/testutil.hpp>
#include <gtest/gtest.h>

View file

@ -1,6 +1,7 @@
#include <nano/lib/blocks.hpp>
#include <nano/node/transport/message_deserializer.hpp>
#include <nano/secure/vote.hpp>
#include <nano/test_common/random.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

View file

@ -1,5 +1,6 @@
#include <nano/node/election.hpp>
#include <nano/node/vote_cache.hpp>
#include <nano/test_common/random.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

View file

@ -8,6 +8,8 @@ add_library(
make_store.cpp
network.hpp
network.cpp
random.hpp
random.cpp
rate_observer.cpp
rate_observer.hpp
system.hpp

View file

@ -0,0 +1,46 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/test_common/random.hpp>
nano::hash_or_account nano::test::random_hash_or_account ()
{
nano::hash_or_account random_hash;
nano::random_pool::generate_block (random_hash.bytes.data (), random_hash.bytes.size ());
return random_hash;
}
nano::block_hash nano::test::random_hash ()
{
return nano::test::random_hash_or_account ().as_block_hash ();
}
nano::account nano::test::random_account ()
{
return nano::test::random_hash_or_account ().as_account ();
}
nano::qualified_root nano::test::random_qualified_root ()
{
return { nano::test::random_hash (), nano::test::random_hash () };
}
nano::amount nano::test::random_amount ()
{
nano::amount result;
nano::random_pool::generate_block (result.bytes.data (), result.bytes.size ());
return result;
}
std::shared_ptr<nano::block> nano::test::random_block ()
{
nano::keypair key;
auto block = std::make_shared<nano::state_block> (
nano::test::random_account (),
nano::test::random_hash (),
nano::test::random_account (),
nano::test::random_amount (),
nano::test::random_hash (),
key.prv,
key.pub,
0);
return block;
}

View file

@ -0,0 +1,17 @@
#pragma once
#include <nano/lib/blocks.hpp>
#include <nano/secure/common.hpp>
namespace nano::test
{
/*
* Random generators
*/
nano::hash_or_account random_hash_or_account ();
nano::block_hash random_hash ();
nano::account random_account ();
nano::qualified_root random_qualified_root ();
nano::amount random_amount ();
std::shared_ptr<nano::block> random_block ();
}

View file

@ -50,23 +50,6 @@ void nano::test::wait_peer_connections (nano::test::system & system_a)
wait_peer_count (false);
}
nano::hash_or_account nano::test::random_hash_or_account ()
{
nano::hash_or_account random_hash;
nano::random_pool::generate_block (random_hash.bytes.data (), random_hash.bytes.size ());
return random_hash;
}
nano::block_hash nano::test::random_hash ()
{
return nano::test::random_hash_or_account ().as_block_hash ();
}
nano::account nano::test::random_account ()
{
return nano::test::random_hash_or_account ().as_account ();
}
bool nano::test::process (nano::node & node, std::vector<std::shared_ptr<nano::block>> blocks)
{
auto const transaction = node.ledger.tx_begin_write ();

View file

@ -265,19 +265,6 @@ namespace test
void wait_peer_connections (nano::test::system &);
/**
* Generate a random block hash
*/
nano::hash_or_account random_hash_or_account ();
/**
* Generate a random block hash
*/
nano::block_hash random_hash ();
/**
* Generate a random block hash
*/
nano::account random_account ();
/**
Convenience function to call `node::process` function for multiple blocks at once.
@return true if all blocks were successfully processed and inserted into ledger