From 303c9ea5c3df3ee0c7865c2cff7574a0e0cb2758 Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Thu, 9 Feb 2023 11:53:24 +0000 Subject: [PATCH] Convert nano::test::start_election to take a hash rather than a block (#4116) nano::test::start_election does need to take the block, a hash suffices and makes the function more flexible and reusable --- nano/core_test/active_transactions.cpp | 6 +++--- nano/core_test/confirmation_height.cpp | 20 ++++++++++---------- nano/core_test/gap_cache.cpp | 2 +- nano/core_test/vote_processor.cpp | 2 +- nano/rpc_test/rpc.cpp | 2 +- nano/test_common/testutil.cpp | 12 +++++++----- nano/test_common/testutil.hpp | 8 +++++--- 7 files changed, 28 insertions(+), 24 deletions(-) diff --git a/nano/core_test/active_transactions.cpp b/nano/core_test/active_transactions.cpp index cdff951fa..3ef9d6d01 100644 --- a/nano/core_test/active_transactions.cpp +++ b/nano/core_test/active_transactions.cpp @@ -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 @@ -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 ()); @@ -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 (); diff --git a/nano/core_test/confirmation_height.cpp b/nano/core_test/confirmation_height.cpp index b87c5e4e6..49b5262f1 100644 --- a/nano/core_test/confirmation_height.cpp +++ b/nano/core_test/confirmation_height.cpp @@ -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 (); @@ -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 @@ -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 (); @@ -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 (); @@ -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 (); @@ -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 (); @@ -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 (); @@ -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); @@ -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); diff --git a/nano/core_test/gap_cache.cpp b/nano/core_test/gap_cache.cpp index 490748e2e..0c31c4a37 100644 --- a/nano/core_test/gap_cache.cpp +++ b/nano/core_test/gap_cache.cpp @@ -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 ())); diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index 6eae46e2e..63951f76d 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -67,7 +67,7 @@ TEST (vote_processor, invalid_signature) vote_invalid->signature.bytes[0] ^= 1; auto channel = std::make_shared (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 ()); diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 91f157eaa..bcfc9e5ca 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -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 ()); diff --git a/nano/test_common/testutil.cpp b/nano/test_common/testutil.cpp index 2f5231d6b..90e9e6d2b 100644 --- a/nano/test_common/testutil.cpp +++ b/nano/test_common/testutil.cpp @@ -220,30 +220,32 @@ std::shared_ptr nano::test::fake_channel (nano::node & return channel; } -std::shared_ptr nano::test::start_election (nano::test::system & system_a, nano::node & node_a, const std::shared_ptr & block_a) +std::shared_ptr 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 election = node_a.active.election (block_a->qualified_root ()); + std::shared_ptr 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 (); diff --git a/nano/test_common/testutil.hpp b/nano/test_common/testutil.hpp index 1c434ecca..ed7040ebc 100644 --- a/nano/test_common/testutil.hpp +++ b/nano/test_common/testutil.hpp @@ -408,10 +408,12 @@ namespace test */ std::shared_ptr 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 start_election (nano::test::system & system_a, nano::node & node_a, const std::shared_ptr & block_a); + std::shared_ptr start_election (nano::test::system & system_a, nano::node & node_a, const nano::block_hash & hash_a); } }