Skip to content

Commit

Permalink
Extract common test chain setup code
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 25, 2023
1 parent d98f2bb commit 54a7655
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 255 deletions.
99 changes: 6 additions & 93 deletions nano/core_test/active_transactions.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <nano/lib/jsonconfig.hpp>
#include <nano/node/election.hpp>
#include <nano/node/transport/inproc.hpp>
#include <nano/test_common/chains.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

Expand Down Expand Up @@ -1407,94 +1408,6 @@ TEST (active_transactions, fifo)
ASSERT_TIMELY (1s, node.active.election (receive2->qualified_root ()) != nullptr);
}

namespace
{
/*
* Sends `amount` raw from genesis chain into a new account and makes it a representative
*/
nano::keypair setup_rep (nano::test::system & system, nano::node & node, nano::uint128_t const amount)
{
auto latest = node.latest (nano::dev::genesis_key.pub);
auto balance = node.balance (nano::dev::genesis_key.pub);

nano::keypair key;
nano::block_builder builder;

auto send = builder
.send ()
.previous (latest)
.destination (key.pub)
.balance (balance - amount)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (latest))
.build_shared ();

auto open = builder
.open ()
.source (send->hash ())
.representative (key.pub)
.account (key.pub)
.sign (key.prv, key.pub)
.work (*system.work.generate (key.pub))
.build_shared ();

EXPECT_TRUE (nano::test::process (node, { send, open }));
EXPECT_TIMELY (5s, nano::test::confirm (node, { send, open }));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { send, open }));

return key;
}

/*
* Creates `count` 1 raw sends from genesis to unique accounts and corresponding open blocks.
* The genesis chain is then confirmed, but leaves open blocks unconfirmed.
*/
std::vector<std::shared_ptr<nano::block>> setup_independent_blocks (nano::test::system & system, nano::node & node, int count)
{
std::vector<std::shared_ptr<nano::block>> blocks;

auto latest = node.latest (nano::dev::genesis_key.pub);
auto balance = node.balance (nano::dev::genesis_key.pub);

for (int n = 0; n < count; ++n)
{
nano::keypair key;
nano::block_builder builder;

balance -= 1;
auto send = builder
.send ()
.previous (latest)
.destination (key.pub)
.balance (balance)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (latest))
.build_shared ();
latest = send->hash ();

auto open = builder
.open ()
.source (send->hash ())
.representative (key.pub)
.account (key.pub)
.sign (key.prv, key.pub)
.work (*system.work.generate (key.pub))
.build_shared ();

EXPECT_TRUE (nano::test::process (node, { send, open }));
EXPECT_TIMELY (5s, nano::test::exists (node, { send, open })); // Ensure blocks are in the ledger

blocks.push_back (open);
}

// Confirm whole genesis chain at once
EXPECT_TIMELY (5s, nano::test::confirm (node, { latest }));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { latest }));

return blocks;
}
}

/*
* Ensures we limit the number of vote hinted elections in AEC
*/
Expand All @@ -1511,10 +1424,10 @@ TEST (active_transactions, limit_vote_hinted_elections)
// Setup representatives
// Enough weight to trigger election hinting but not enough to confirm block on its own
const auto amount = ((node.online_reps.trended () / 100) * node.config.election_hint_weight_percent) + 1000 * nano::Gxrb_ratio;
nano::keypair rep1 = setup_rep (system, node, amount / 2);
nano::keypair rep2 = setup_rep (system, node, amount / 2);
nano::keypair rep1 = nano::test::setup_rep (system, node, amount / 2);
nano::keypair rep2 = nano::test::setup_rep (system, node, amount / 2);

auto blocks = setup_independent_blocks (system, node, 2);
auto blocks = nano::test::setup_independent_blocks (system, node, 2);
auto open0 = blocks[0];
auto open1 = blocks[1];

Expand Down Expand Up @@ -1573,7 +1486,7 @@ TEST (active_transactions, allow_limited_overflow)
config.active_elections_hinted_limit_percentage = 20; // Should give us a limit of 4 hinted elections
auto & node = *system.add_node (config);

auto blocks = setup_independent_blocks (system, node, aec_limit * 4);
auto blocks = nano::test::setup_independent_blocks (system, node, aec_limit * 4);

// Split blocks in two halves
std::vector<std::shared_ptr<nano::block>> blocks1 (blocks.begin (), blocks.begin () + blocks.size () / 2);
Expand Down Expand Up @@ -1622,7 +1535,7 @@ TEST (active_transactions, allow_limited_overflow_adapt)
config.active_elections_hinted_limit_percentage = 20; // Should give us a limit of 4 hinted elections
auto & node = *system.add_node (config);

auto blocks = setup_independent_blocks (system, node, aec_limit * 4);
auto blocks = nano::test::setup_independent_blocks (system, node, aec_limit * 4);

// Split blocks in two halves
std::vector<std::shared_ptr<nano::block>> blocks1 (blocks.begin (), blocks.begin () + blocks.size () / 2);
Expand Down
63 changes: 3 additions & 60 deletions nano/core_test/backlog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <nano/node/active_transactions.hpp>
#include <nano/test_common/chains.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

Expand All @@ -8,72 +9,14 @@

using namespace std::chrono_literals;

namespace
{
using block_list_t = std::vector<std::shared_ptr<nano::block>>;

/*
* Creates `count` 1 raw sends from genesis to unique accounts and corresponding open blocks.
* The genesis chain is then confirmed, but leaves open blocks unconfirmed
* The list of unconfirmed open blocks is returned.
*/
block_list_t setup_independent_blocks (nano::test::system & system, nano::node & node, int count)
{
std::vector<std::shared_ptr<nano::block>> blocks;

auto latest = node.latest (nano::dev::genesis_key.pub);
auto balance = node.balance (nano::dev::genesis_key.pub);

for (int n = 0; n < count; ++n)
{
nano::keypair key;
nano::block_builder builder;

balance -= 1;
auto send = builder
.state ()
.account (nano::dev::genesis_key.pub)
.previous (latest)
.representative (nano::dev::genesis_key.pub)
.balance (balance)
.link (key.pub)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (latest))
.build_shared ();
latest = send->hash ();

auto open = builder
.state ()
.account (key.pub)
.previous (0)
.representative (key.pub)
.balance (1)
.link (send->hash ())
.sign (key.prv, key.pub)
.work (*system.work.generate (key.pub))
.build_shared ();

EXPECT_TRUE (nano::test::process (node, { send, open }));
EXPECT_TIMELY (5s, nano::test::exists (node, { send, open })); // Ensure blocks are in the ledger

blocks.push_back (open);
}

// Confirm whole genesis chain at once
EXPECT_TIMELY (5s, nano::test::confirm (node, { latest }));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { latest }));

return blocks;
}
}

/*
* Ensures all not confirmed accounts get activated by backlog scan periodically
*/
TEST (backlog, population)
{
nano::mutex mutex;
std::unordered_set<nano::account> activated;

nano::test::system system{};
auto & node = *system.add_node ();

Expand All @@ -83,7 +26,7 @@ TEST (backlog, population)
activated.insert (account);
});

auto blocks = setup_independent_blocks (system, node, 256);
auto blocks = nano::test::setup_independent_blocks (system, node, 256);

// Checks if `activated` set contains all accounts we previously set up
auto all_activated = [&] () {
Expand Down
Loading

0 comments on commit 54a7655

Please sign in to comment.