Skip to content

Commit

Permalink
Convert nano::test::start_election to take a hash rather than a block (
Browse files Browse the repository at this point in the history
…#4116)

nano::test::start_election does need to take the block, a hash suffices
and makes the function more flexible and reusable
  • Loading branch information
dsiganos committed Feb 9, 2023
1 parent 8b68216 commit 303c9ea
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
6 changes: 3 additions & 3 deletions nano/core_test/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ TEST (active_transactions, dropped_cleanup)
ASSERT_FALSE (node.network.publish_filter.apply (block_bytes.data (), block_bytes.size ()));
ASSERT_TRUE (node.network.publish_filter.apply (block_bytes.data (), block_bytes.size ()));

auto election = nano::test::start_election (system, node, nano::dev::genesis);
auto election = nano::test::start_election (system, node, nano::dev::genesis->hash ());
ASSERT_NE (nullptr, election);

// Not yet removed
Expand All @@ -653,7 +653,7 @@ TEST (active_transactions, dropped_cleanup)
// Repeat test for a confirmed election
ASSERT_TRUE (node.network.publish_filter.apply (block_bytes.data (), block_bytes.size ()));

election = nano::test::start_election (system, node, nano::dev::genesis);
election = nano::test::start_election (system, node, nano::dev::genesis->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();
ASSERT_TRUE (election->confirmed ());
Expand Down Expand Up @@ -1219,7 +1219,7 @@ TEST (active_transactions, activate_inactive)
ASSERT_EQ (nano::process_result::progress, node.process (*send2).code);
ASSERT_EQ (nano::process_result::progress, node.process (*open).code);

auto election = nano::test::start_election (system, node, send2);
auto election = nano::test::start_election (system, node, send2->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();

Expand Down
20 changes: 10 additions & 10 deletions nano/core_test/confirmation_height.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ TEST (confirmation_height, multiple_accounts)
.work (*system.work.generate (open3->hash ()))
.build_shared ();
node->process_active (receive3);
auto election = nano::test::start_election (system, *node, receive3);
auto election = nano::test::start_election (system, *node, receive3->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();

Expand Down Expand Up @@ -533,7 +533,7 @@ TEST (confirmation_height, gap_live)
}

// Vote and confirm all existing blocks
nano::test::start_election (system, *node, send1);
nano::test::start_election (system, *node, send1->hash ());
ASSERT_TIMELY (10s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 3);

// Now complete the chain where the block comes in on the live network
Expand Down Expand Up @@ -688,7 +688,7 @@ TEST (confirmation_height, send_receive_between_2_accounts)
add_callback_stats (*node);

node->process_active (receive4);
auto election = nano::test::start_election (system, *node, receive4);
auto election = nano::test::start_election (system, *node, receive4->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();

Expand Down Expand Up @@ -805,7 +805,7 @@ TEST (confirmation_height, send_receive_self)

add_callback_stats (*node);

auto election = nano::test::start_election (system, *node, receive3);
auto election = nano::test::start_election (system, *node, receive3->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();

Expand Down Expand Up @@ -1050,7 +1050,7 @@ TEST (confirmation_height, all_block_types)
}

add_callback_stats (*node);
auto election = nano::test::start_election (system, *node, state_send2);
auto election = nano::test::start_election (system, *node, state_send2->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();

Expand Down Expand Up @@ -1546,7 +1546,7 @@ TEST (confirmation_height, callback_confirmed_history)
add_callback_stats (*node);

node->process_active (send1);
ASSERT_NE (nano::test::start_election (system, *node, send1), nullptr);
ASSERT_NE (nano::test::start_election (system, *node, send1->hash ()), nullptr);
{
node->process_active (send);
node->block_processor.flush ();
Expand Down Expand Up @@ -1647,9 +1647,9 @@ TEST (confirmation_height, dependent_election)
add_callback_stats (*node);

// This election should be confirmed as active_conf_height
ASSERT_TRUE (nano::test::start_election (system, *node, send1));
ASSERT_TRUE (nano::test::start_election (system, *node, send1->hash ()));
// Start an election and confirm it
auto election = nano::test::start_election (system, *node, send2);
auto election = nano::test::start_election (system, *node, send2->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();

Expand Down Expand Up @@ -1803,7 +1803,7 @@ TEST (confirmation_height, cemented_gap_below_receive)
nano::mutex mutex;
add_callback_stats (*node, &observer_order, &mutex);

auto election = nano::test::start_election (system, *node, open1);
auto election = nano::test::start_election (system, *node, open1->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();
ASSERT_TIMELY (5s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 10);
Expand Down Expand Up @@ -1968,7 +1968,7 @@ TEST (confirmation_height, cemented_gap_below_no_cache)

add_callback_stats (*node);

auto election = nano::test::start_election (system, *node, open1);
auto election = nano::test::start_election (system, *node, open1->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();
ASSERT_TIMELY (5s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 6);
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/gap_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ TEST (gap_cache, gap_bootstrap)
ASSERT_EQ (nano::dev::constants.genesis_amount - 100, node1.balance (nano::dev::genesis->account ()));
ASSERT_EQ (nano::dev::constants.genesis_amount, node2.balance (nano::dev::genesis->account ()));
// Confirm send block, allowing voting on the upcoming block
auto election = nano::test::start_election (system, node1, send);
auto election = nano::test::start_election (system, node1, send->hash ());
ASSERT_NE (nullptr, election);
election->force_confirm ();
ASSERT_TIMELY (5s, node1.block_confirmed (send->hash ()));
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/vote_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TEST (vote_processor, invalid_signature)
vote_invalid->signature.bytes[0] ^= 1;
auto channel = std::make_shared<nano::transport::inproc::channel> (node, node);

auto election = nano::test::start_election (system, node, nano::dev::genesis);
auto election = nano::test::start_election (system, node, nano::dev::genesis->hash ());
ASSERT_NE (election, nullptr);
ASSERT_EQ (1, election->votes ().size ());

Expand Down
2 changes: 1 addition & 1 deletion nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5874,7 +5874,7 @@ TEST (rpc, confirmation_height_currently_processing)
{
// Write guard prevents the confirmation height processor writing the blocks, so that we can inspect contents during the response
auto write_guard = node->write_database_queue.wait (nano::writer::testing);
nano::test::start_election (system, *node, frontier);
nano::test::start_election (system, *node, frontier->hash ());

ASSERT_TIMELY (5s, node->confirmation_height_processor.current () == frontier->hash ());

Expand Down
12 changes: 7 additions & 5 deletions nano/test_common/testutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,30 +220,32 @@ std::shared_ptr<nano::transport::channel> nano::test::fake_channel (nano::node &
return channel;
}

std::shared_ptr<nano::election> nano::test::start_election (nano::test::system & system_a, nano::node & node_a, const std::shared_ptr<nano::block> & block_a)
std::shared_ptr<nano::election> nano::test::start_election (nano::test::system & system_a, nano::node & node_a, const nano::block_hash & hash_a)
{
system_a.deadline_set (5s);

// wait until and ensure that the block is in the ledger
while (!node_a.block (block_a->hash ()))
auto block_l = node_a.block (hash_a);
while (!block_l)
{
if (system_a.poll ())
{
return nullptr;
}
block_l = node_a.block (hash_a);
}

node_a.scheduler.manual (block_a);
node_a.scheduler.manual (block_l);

// wait for the election to appear
std::shared_ptr<nano::election> election = node_a.active.election (block_a->qualified_root ());
std::shared_ptr<nano::election> election = node_a.active.election (block_l->qualified_root ());
while (!election)
{
if (system_a.poll ())
{
return nullptr;
}
election = node_a.active.election (block_a->qualified_root ());
election = node_a.active.election (block_l->qualified_root ());
}

election->transition_active ();
Expand Down
8 changes: 5 additions & 3 deletions nano/test_common/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,12 @@ namespace test
*/
std::shared_ptr<nano::transport::channel> fake_channel (nano::node & node, nano::account node_id = { 0 });
/*
* Start an election on system system_a, node node_a and block block_a by adding the block to the manual election scheduler queue.
* It waits up to 5 seconds for the election to start and calls the system poll function while waiting.
* Start an election on system system_a, node node_a and hash hash_a by reading the block
* out of the ledger and adding it to the manual election scheduler queue.
* It waits up to 5 seconds for the block to appear in the ledger and the election to start
* and calls the system poll function while waiting.
* Returns nullptr if the election did not start within the timeframe.
*/
std::shared_ptr<nano::election> start_election (nano::test::system & system_a, nano::node & node_a, const std::shared_ptr<nano::block> & block_a);
std::shared_ptr<nano::election> start_election (nano::test::system & system_a, nano::node & node_a, const nano::block_hash & hash_a);
}
}

0 comments on commit 303c9ea

Please sign in to comment.