Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use node reference instead of system.nodes[n] in unit tests more often #2470

Merged
merged 4 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions nano/core_test/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ using namespace std::chrono_literals;

TEST (active_transactions, confirm_one)
{
nano::system system;
nano::node_config node_config (24000, system.logging);
auto & node1 = *system.add_node (node_config);
nano::system system (1);
auto & node1 = *system.nodes[0];
// Send and vote for a block before peering with node2
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
auto send (system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::public_key (), node_config.receive_minimum.number ()));
auto send (system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::public_key (), node1.config.receive_minimum.number ()));
system.deadline_set (5s);
while (!node1.active.empty () && !node1.block_confirmed_or_being_confirmed (node1.store.tx_begin_read (), send->hash ()))
{
ASSERT_NO_ERROR (system.poll ());
}
node_config.peering_port = 24001;
auto & node2 = *system.add_node (node_config);
auto & node2 = *system.add_node (nano::node_config (nano::get_available_port (), system.logging));
system.deadline_set (5s);
// Let node2 know about the block
while (node2.active.empty ())
Expand Down Expand Up @@ -470,7 +468,7 @@ TEST (active_transactions, inactive_votes_cache_existing_vote)
ASSERT_GT (node->weight (key.pub), node->minimum_principal_weight ());
// Insert vote
auto vote1 (std::make_shared<nano::vote> (key.pub, key.prv, 1, std::vector<nano::block_hash> (1, send->hash ())));
node->vote_processor.vote (vote1, std::make_shared<nano::transport::channel_udp> (system.nodes[0]->network.udp_channels, system.nodes[0]->network.endpoint (), system.nodes[0]->network_params.protocol.protocol_version));
node->vote_processor.vote (vote1, std::make_shared<nano::transport::channel_udp> (node->network.udp_channels, node->network.endpoint (), node->network_params.protocol.protocol_version));
system.deadline_set (5s);
bool done (false);
while (!done)
Expand All @@ -480,7 +478,7 @@ TEST (active_transactions, inactive_votes_cache_existing_vote)
active_lock.unlock ();
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (1, system.nodes[0]->stats.count (nano::stat::type::election, nano::stat::detail::vote_new));
ASSERT_EQ (1, node->stats.count (nano::stat::type::election, nano::stat::detail::vote_new));
nano::lock_guard<std::mutex> active_guard (node->active.mutex);
auto last_vote1 (election->last_votes[key.pub]);
ASSERT_EQ (send->hash (), last_vote1.hash);
Expand All @@ -495,7 +493,7 @@ TEST (active_transactions, inactive_votes_cache_existing_vote)
ASSERT_EQ (last_vote1.hash, last_vote2.hash);
ASSERT_EQ (last_vote1.sequence, last_vote2.sequence);
ASSERT_EQ (last_vote1.time, last_vote2.time);
ASSERT_EQ (0, system.nodes[0]->stats.count (nano::stat::type::election, nano::stat::detail::vote_cached));
ASSERT_EQ (0, node->stats.count (nano::stat::type::election, nano::stat::detail::vote_cached));
}

TEST (active_transactions, inactive_votes_cache_multiple_votes)
Expand All @@ -504,7 +502,7 @@ TEST (active_transactions, inactive_votes_cache_multiple_votes)
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.frontiers_confirmation = nano::frontiers_confirmation_mode::disabled;
auto node = system.add_node (node_config);
nano::block_hash latest (system.nodes[0]->latest (nano::test_genesis_key.pub));
nano::block_hash latest (node->latest (nano::test_genesis_key.pub));
nano::keypair key1;
auto send1 (std::make_shared<nano::send_block> (latest, key1.pub, nano::genesis_amount - 100 * nano::Gxrb_ratio, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (latest)));
auto send2 (std::make_shared<nano::send_block> (send1->hash (), key1.pub, 100 * nano::Gxrb_ratio, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (send1->hash ()))); // Decrease genesis weight to prevent election confirmation
Expand All @@ -515,31 +513,31 @@ TEST (active_transactions, inactive_votes_cache_multiple_votes)
node->block_processor.flush ();
// Process votes
auto vote1 (std::make_shared<nano::vote> (key1.pub, key1.prv, 0, std::vector<nano::block_hash> (1, send1->hash ())));
system.nodes[0]->vote_processor.vote (vote1, std::make_shared<nano::transport::channel_udp> (system.nodes[0]->network.udp_channels, system.nodes[0]->network.endpoint (), system.nodes[0]->network_params.protocol.protocol_version));
node->vote_processor.vote (vote1, std::make_shared<nano::transport::channel_udp> (node->network.udp_channels, node->network.endpoint (), node->network_params.protocol.protocol_version));
auto vote2 (std::make_shared<nano::vote> (nano::test_genesis_key.pub, nano::test_genesis_key.prv, 0, std::vector<nano::block_hash> (1, send1->hash ())));
system.nodes[0]->vote_processor.vote (vote2, std::make_shared<nano::transport::channel_udp> (system.nodes[0]->network.udp_channels, system.nodes[0]->network.endpoint (), system.nodes[0]->network_params.protocol.protocol_version));
node->vote_processor.vote (vote2, std::make_shared<nano::transport::channel_udp> (node->network.udp_channels, node->network.endpoint (), node->network_params.protocol.protocol_version));
system.deadline_set (5s);
while (true)
{
{
nano::lock_guard<std::mutex> active_guard (system.nodes[0]->active.mutex);
if (system.nodes[0]->active.find_inactive_votes_cache (send1->hash ()).voters.size () == 2)
nano::lock_guard<std::mutex> active_guard (node->active.mutex);
if (node->active.find_inactive_votes_cache (send1->hash ()).voters.size () == 2)
{
break;
}
}
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (1, system.nodes[0]->active.inactive_votes_cache_size ());
ASSERT_EQ (1, node->active.inactive_votes_cache_size ());
// Start election
system.nodes[0]->active.start (send1);
node->active.start (send1);
{
nano::lock_guard<std::mutex> active_guard (system.nodes[0]->active.mutex);
auto it (system.nodes[0]->active.roots.begin ());
ASSERT_NE (system.nodes[0]->active.roots.end (), it);
nano::lock_guard<std::mutex> active_guard (node->active.mutex);
auto it (node->active.roots.begin ());
ASSERT_NE (node->active.roots.end (), it);
ASSERT_EQ (3, it->election->last_votes.size ()); // 2 votes and 1 default not_an_acount
}
ASSERT_EQ (2, system.nodes[0]->stats.count (nano::stat::type::election, nano::stat::detail::vote_cached));
ASSERT_EQ (2, node->stats.count (nano::stat::type::election, nano::stat::detail::vote_cached));
}

TEST (active_transactions, update_difficulty)
Expand Down
58 changes: 30 additions & 28 deletions nano/core_test/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,17 +784,17 @@ TEST (frontier_req, end)
TEST (frontier_req, count)
{
nano::system system (1);
auto & node1 (*system.nodes[0]);
auto node1 = system.nodes[0];
nano::genesis genesis;
// Public key FB93... after genesis in accounts table
nano::keypair key1 ("ED5AE0A6505B14B67435C29FD9FEEBC26F597D147BC92F6D795FFAD7AFD3D967");
nano::state_block send1 (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount - nano::Gxrb_ratio, key1.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0);
node1.work_generate_blocking (send1);
ASSERT_EQ (nano::process_result::progress, node1.process (send1).code);
node1->work_generate_blocking (send1);
ASSERT_EQ (nano::process_result::progress, node1->process (send1).code);
nano::state_block receive1 (key1.pub, 0, nano::test_genesis_key.pub, nano::Gxrb_ratio, send1.hash (), key1.prv, key1.pub, 0);
node1.work_generate_blocking (receive1);
ASSERT_EQ (nano::process_result::progress, node1.process (receive1).code);
auto connection (std::make_shared<nano::bootstrap_server> (nullptr, system.nodes[0]));
node1->work_generate_blocking (receive1);
ASSERT_EQ (nano::process_result::progress, node1->process (receive1).code);
auto connection (std::make_shared<nano::bootstrap_server> (nullptr, node1));
auto req = std::make_unique<nano::frontier_req> ();
req->start.clear ();
req->age = std::numeric_limits<decltype (req->age)>::max ();
Expand Down Expand Up @@ -857,63 +857,65 @@ TEST (bulk, genesis)
{
nano::system system (1);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::get_available_port (), nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
nano::block_hash latest1 (system.nodes[0]->latest (nano::test_genesis_key.pub));
nano::block_hash latest2 (node1->latest (nano::test_genesis_key.pub));
auto node1 = system.nodes[0];
auto node2 (std::make_shared<nano::node> (system.io_ctx, nano::get_available_port (), nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node2->init_error ());
nano::block_hash latest1 (node1->latest (nano::test_genesis_key.pub));
nano::block_hash latest2 (node2->latest (nano::test_genesis_key.pub));
ASSERT_EQ (latest1, latest2);
nano::keypair key2;
ASSERT_NE (nullptr, system.wallet (0)->send_action (nano::test_genesis_key.pub, key2.pub, 100));
nano::block_hash latest3 (system.nodes[0]->latest (nano::test_genesis_key.pub));
nano::block_hash latest3 (node1->latest (nano::test_genesis_key.pub));
ASSERT_NE (latest1, latest3);
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
node2->bootstrap_initiator.bootstrap (node1->network.endpoint ());
system.deadline_set (10s);
while (node1->latest (nano::test_genesis_key.pub) != system.nodes[0]->latest (nano::test_genesis_key.pub))
while (node2->latest (nano::test_genesis_key.pub) != node1->latest (nano::test_genesis_key.pub))
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (node1->latest (nano::test_genesis_key.pub), system.nodes[0]->latest (nano::test_genesis_key.pub));
node1->stop ();
ASSERT_EQ (node2->latest (nano::test_genesis_key.pub), node1->latest (nano::test_genesis_key.pub));
node2->stop ();
}

TEST (bulk, offline_send)
{
nano::system system (1);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::get_available_port (), nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
node1->start ();
system.nodes.push_back (node1);
auto node1 = system.nodes[0];
auto node2 (std::make_shared<nano::node> (system.io_ctx, nano::get_available_port (), nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node2->init_error ());
node2->start ();
system.nodes.push_back (node2);
nano::keypair key2;
auto wallet (node1->wallets.create (nano::random_wallet_id ()));
auto wallet (node2->wallets.create (nano::random_wallet_id ()));
wallet->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (nano::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_NE (std::numeric_limits<nano::uint256_t>::max (), system.nodes[0]->balance (nano::test_genesis_key.pub));
ASSERT_NE (nullptr, system.wallet (0)->send_action (nano::test_genesis_key.pub, key2.pub, node1->config.receive_minimum.number ()));
ASSERT_NE (std::numeric_limits<nano::uint256_t>::max (), node1->balance (nano::test_genesis_key.pub));
// Wait to finish election background tasks
system.deadline_set (10s);
while (!system.nodes[0]->active.empty ())
while (!node1->active.empty ())
{
ASSERT_NO_ERROR (system.poll ());
}
// Initiate bootstrap
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
node2->bootstrap_initiator.bootstrap (node1->network.endpoint ());
// Nodes should find each other
do
{
ASSERT_NO_ERROR (system.poll ());
} while (system.nodes[0]->network.empty () || node1->network.empty ());
} while (node1->network.empty () || node2->network.empty ());
// Send block arrival via bootstrap
while (node1->balance (nano::test_genesis_key.pub) == std::numeric_limits<nano::uint256_t>::max ())
while (node2->balance (nano::test_genesis_key.pub) == std::numeric_limits<nano::uint256_t>::max ())
{
ASSERT_NO_ERROR (system.poll ());
}
// Receiving send block
system.deadline_set (20s);
while (node1->balance (key2.pub) != system.nodes[0]->config.receive_minimum.number ())
while (node2->balance (key2.pub) != node1->config.receive_minimum.number ())
{
ASSERT_NO_ERROR (system.poll ());
}
node1->stop ();
node2->stop ();
}

TEST (bulk_pull_account, basics)
Expand Down
74 changes: 37 additions & 37 deletions nano/core_test/confirmation_height.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ TEST (confirmation_height, gap_live)
nano::system system;
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.frontiers_confirmation = nano::frontiers_confirmation_mode::disabled;
system.add_node (node_config);
auto node1 = system.add_node (node_config);
node_config.peering_port = nano::get_available_port ();
system.add_node (node_config);
nano::keypair destination;
Expand All @@ -280,18 +280,18 @@ TEST (confirmation_height, gap_live)

nano::genesis genesis;
auto send1 (std::make_shared<nano::state_block> (nano::genesis_account, genesis.hash (), nano::genesis_account, nano::genesis_amount - nano::Gxrb_ratio, destination.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0));
system.nodes[0]->work_generate_blocking (*send1);
node1->work_generate_blocking (*send1);
auto send2 (std::make_shared<nano::state_block> (nano::genesis_account, send1->hash (), nano::genesis_account, nano::genesis_amount - 2 * nano::Gxrb_ratio, destination.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0));
system.nodes[0]->work_generate_blocking (*send2);
node1->work_generate_blocking (*send2);
auto send3 (std::make_shared<nano::state_block> (nano::genesis_account, send2->hash (), nano::genesis_account, nano::genesis_amount - 3 * nano::Gxrb_ratio, destination.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0));
system.nodes[0]->work_generate_blocking (*send3);
node1->work_generate_blocking (*send3);

auto open1 (std::make_shared<nano::open_block> (send1->hash (), destination.pub, destination.pub, destination.prv, destination.pub, 0));
system.nodes[0]->work_generate_blocking (*open1);
node1->work_generate_blocking (*open1);
auto receive1 (std::make_shared<nano::receive_block> (open1->hash (), send2->hash (), destination.prv, destination.pub, 0));
system.nodes[0]->work_generate_blocking (*receive1);
node1->work_generate_blocking (*receive1);
auto receive2 (std::make_shared<nano::receive_block> (receive1->hash (), send3->hash (), destination.prv, destination.pub, 0));
system.nodes[0]->work_generate_blocking (*receive2);
node1->work_generate_blocking (*receive2);

for (auto & node : system.nodes)
{
Expand Down Expand Up @@ -600,46 +600,46 @@ TEST (confirmation_height, conflict_rollback_cemented)
sb.open (nano::stringstream_mt_sink{});
nano::boost_log_cerr_redirect redirect_cerr (&sb);
nano::system system (2);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
ASSERT_EQ (1, node1.network.size ());
auto node1 (system.nodes[0]);
auto node2 (system.nodes[1]);
ASSERT_EQ (1, node1->network.size ());
nano::keypair key1;
nano::genesis genesis;
auto send1 (std::make_shared<nano::send_block> (genesis.hash (), key1.pub, nano::genesis_amount - 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (genesis.hash ())));
nano::publish publish1 (send1);
nano::keypair key2;
auto send2 (std::make_shared<nano::send_block> (genesis.hash (), key2.pub, nano::genesis_amount - 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (genesis.hash ())));
nano::publish publish2 (send2);
auto channel1 (node1.network.udp_channels.create (node1.network.endpoint ()));
node1.network.process_message (publish1, channel1);
node1.block_processor.flush ();
auto channel2 (node2.network.udp_channels.create (node1.network.endpoint ()));
node2.network.process_message (publish2, channel2);
node2.block_processor.flush ();
ASSERT_EQ (1, node1.active.size ());
ASSERT_EQ (1, node2.active.size ());
auto channel1 (node1->network.udp_channels.create (node1->network.endpoint ()));
node1->network.process_message (publish1, channel1);
node1->block_processor.flush ();
auto channel2 (node2->network.udp_channels.create (node1->network.endpoint ()));
node2->network.process_message (publish2, channel2);
node2->block_processor.flush ();
ASSERT_EQ (1, node1->active.size ());
ASSERT_EQ (1, node2->active.size ());
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
node1.network.process_message (publish2, channel1);
node1.block_processor.flush ();
node2.network.process_message (publish1, channel2);
node2.block_processor.flush ();
nano::unique_lock<std::mutex> lock (node2.active.mutex);
auto conflict (node2.active.roots.find (nano::qualified_root (genesis.hash (), genesis.hash ())));
ASSERT_NE (node2.active.roots.end (), conflict);
node1->network.process_message (publish2, channel1);
node1->block_processor.flush ();
node2->network.process_message (publish1, channel2);
node2->block_processor.flush ();
nano::unique_lock<std::mutex> lock (node2->active.mutex);
auto conflict (node2->active.roots.find (nano::qualified_root (genesis.hash (), genesis.hash ())));
ASSERT_NE (node2->active.roots.end (), conflict);
auto votes1 (conflict->election);
ASSERT_NE (nullptr, votes1);
ASSERT_EQ (1, votes1->last_votes.size ());
lock.unlock ();
// Force blocks to be cemented on both nodes
{
auto transaction (system.nodes[0]->store.tx_begin_write ());
ASSERT_TRUE (node1.store.block_exists (transaction, publish1.block->hash ()));
node1.store.confirmation_height_put (transaction, nano::genesis_account, 2);
auto transaction (node1->store.tx_begin_write ());
ASSERT_TRUE (node1->store.block_exists (transaction, publish1.block->hash ()));
node1->store.confirmation_height_put (transaction, nano::genesis_account, 2);
}
{
auto transaction (system.nodes[1]->store.tx_begin_write ());
ASSERT_TRUE (node2.store.block_exists (transaction, publish2.block->hash ()));
node2.store.confirmation_height_put (transaction, nano::genesis_account, 2);
auto transaction (node2->store.tx_begin_write ());
ASSERT_TRUE (node2->store.block_exists (transaction, publish2.block->hash ()));
node2->store.confirmation_height_put (transaction, nano::genesis_account, 2);
}

auto rollback_log_entry = boost::str (boost::format ("Failed to roll back %1%") % send2->hash ().to_string ());
Expand All @@ -650,15 +650,15 @@ TEST (confirmation_height, conflict_rollback_cemented)
ASSERT_NO_ERROR (system.poll ());
done = (sb.component ()->str ().find (rollback_log_entry) != std::string::npos);
}
auto transaction1 (system.nodes[0]->store.tx_begin_read ());
auto transaction2 (system.nodes[1]->store.tx_begin_read ());
auto transaction1 (node1->store.tx_begin_read ());
auto transaction2 (node2->store.tx_begin_read ());
lock.lock ();
auto winner (*votes1->tally ().begin ());
ASSERT_EQ (*publish1.block, *winner.second);
ASSERT_EQ (nano::genesis_amount - 100, winner.first);
ASSERT_TRUE (node1.store.block_exists (transaction1, publish1.block->hash ()));
ASSERT_TRUE (node2.store.block_exists (transaction2, publish2.block->hash ()));
ASSERT_FALSE (node2.store.block_exists (transaction2, publish1.block->hash ()));
ASSERT_TRUE (node1->store.block_exists (transaction1, publish1.block->hash ()));
ASSERT_TRUE (node2->store.block_exists (transaction2, publish2.block->hash ()));
ASSERT_FALSE (node2->store.block_exists (transaction2, publish1.block->hash ()));
}

TEST (confirmation_height, observers)
Expand Down Expand Up @@ -787,7 +787,7 @@ TEST (confirmation_height, prioritize_frontiers)
nano::keypair key2;
nano::keypair key3;
nano::keypair key4;
nano::block_hash latest1 (system.nodes[0]->latest (nano::test_genesis_key.pub));
nano::block_hash latest1 (node->latest (nano::test_genesis_key.pub));

// Send different numbers of blocks all accounts
nano::send_block send1 (latest1, key1.pub, node->config.online_weight_minimum.number () + 10000, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (latest1));
Expand Down
Loading