From 60d750d09828afed7f100306d74cc248ed92edf8 Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Thu, 2 Feb 2023 21:39:37 +0000 Subject: [PATCH 1/7] Introduce the method systen::start_election() In many tests, we currently use the method node::block_confirm(), which uses block processor flush and is therefore broken. It is also terribly named. I introduce the method start_election with the aim of replacing node::block_confirm in unit tests. I converted a few unit tests to use this new function. --- nano/core_test/vote_processor.cpp | 12 +++++------- nano/test_common/system.cpp | 30 ++++++++++++++++++++++++++++++ nano/test_common/system.hpp | 7 +++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index ea92a93e47..38a6c6d71f 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -67,16 +67,14 @@ TEST (vote_processor, invalid_signature) vote_invalid->signature.bytes[0] ^= 1; auto channel = std::make_shared (node, node); - node.block_confirm (nano::dev::genesis); - auto election = node.active.election (nano::dev::genesis->qualified_root ()); - ASSERT_TRUE (election); + auto election = system.start_election (node, nano::dev::genesis); + ASSERT_NE (election, nullptr); ASSERT_EQ (1, election->votes ().size ()); + node.vote_processor.vote (vote_invalid, channel); - node.vote_processor.flush (); - ASSERT_TIMELY (3s, 1 == election->votes ().size ()); + ASSERT_TIMELY (5s, 1 == election->votes ().size ()); node.vote_processor.vote (vote, channel); - node.vote_processor.flush (); - ASSERT_TIMELY (3s, 2 == election->votes ().size ()); + ASSERT_TIMELY (5s, 2 == election->votes ().size ()); } TEST (vote_processor, no_capacity) diff --git a/nano/test_common/system.cpp b/nano/test_common/system.cpp index dc2f2882f7..ffcb607afa 100644 --- a/nano/test_common/system.cpp +++ b/nano/test_common/system.cpp @@ -630,3 +630,33 @@ void nano::test::cleanup_dev_directories_on_exit () nano::remove_temporary_directories (); } } + +std::shared_ptr nano::test::system::start_election (nano::node & node_a, const std::shared_ptr & block_a) +{ + deadline_set (5s); + + // wait until and ensure that the block is in the ledger + while (!node_a.block (block_a->hash ())) + { + if (poll ()) + { + return {}; + } + } + + node_a.scheduler.manual (block_a); + + // wait for the election to appear + std::shared_ptr election = node_a.active.election (block_a->qualified_root ()); + while (!election) + { + if (poll ()) + { + return {}; + } + election = node_a.active.election (block_a->qualified_root ()); + } + + election->transition_active (); + return election; +} \ No newline at end of file diff --git a/nano/test_common/system.hpp b/nano/test_common/system.hpp index 931d05d057..ee175e86ab 100644 --- a/nano/test_common/system.hpp +++ b/nano/test_common/system.hpp @@ -62,6 +62,13 @@ namespace test */ nano::node_config default_config (); + /** + * Start an election on 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. + * Returns nullptr if the election did not start within the timeframe. + */ + std::shared_ptr start_election (nano::node & node_a, const std::shared_ptr & block_a); + public: boost::asio::io_context io_ctx; std::vector> nodes; From c5105ae464c72e351a349fe2ae021d3f52a39299 Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Thu, 2 Feb 2023 22:38:00 +0000 Subject: [PATCH 2/7] gap_cache.gap_bootstrap --- nano/core_test/gap_cache.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/nano/core_test/gap_cache.cpp b/nano/core_test/gap_cache.cpp index 01761302a8..b32199e45d 100644 --- a/nano/core_test/gap_cache.cpp +++ b/nano/core_test/gap_cache.cpp @@ -111,18 +111,17 @@ 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 - node1.block_confirm (send); - auto election = node1.active.election (send->qualified_root ()); + auto election = system.start_election (node1, send); ASSERT_NE (nullptr, election); election->force_confirm (); - ASSERT_TIMELY (2s, node1.block_confirmed (send->hash ())); + ASSERT_TIMELY (5s, node1.block_confirmed (send->hash ())); node1.active.erase (*send); system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv); auto latest_block (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key.pub, 100)); ASSERT_NE (nullptr, latest_block); ASSERT_EQ (nano::dev::constants.genesis_amount - 200, node1.balance (nano::dev::genesis->account ())); ASSERT_EQ (nano::dev::constants.genesis_amount, node2.balance (nano::dev::genesis->account ())); - ASSERT_TIMELY (10s, node2.balance (nano::dev::genesis->account ()) == nano::dev::constants.genesis_amount - 200); + ASSERT_TIMELY (5s, node2.balance (nano::dev::genesis->account ()) == nano::dev::constants.genesis_amount - 200); } TEST (gap_cache, two_dependencies) From 7ca30eaf7afcb0c9141912b8c834b6b779866ac5 Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Thu, 2 Feb 2023 22:38:52 +0000 Subject: [PATCH 3/7] convert active transactions tests --- nano/core_test/active_transactions.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/nano/core_test/active_transactions.cpp b/nano/core_test/active_transactions.cpp index dce860202f..aa3a6d8298 100644 --- a/nano/core_test/active_transactions.cpp +++ b/nano/core_test/active_transactions.cpp @@ -630,9 +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 ())); - node.block_confirm (nano::dev::genesis); - ASSERT_TIMELY (5s, node.active.election (nano::dev::genesis->qualified_root ())); - auto election = node.active.election (nano::dev::genesis->qualified_root ()); + auto election = system.start_election (node, nano::dev::genesis); ASSERT_NE (nullptr, election); // Not yet removed @@ -654,9 +652,8 @@ TEST (active_transactions, dropped_cleanup) // Repeat test for a confirmed election ASSERT_TRUE (node.network.publish_filter.apply (block_bytes.data (), block_bytes.size ())); - node.block_confirm (nano::dev::genesis); - ASSERT_TIMELY (5s, node.active.election (nano::dev::genesis->qualified_root ())); - election = node.active.election (nano::dev::genesis->qualified_root ()); + + election = system.start_election (node, nano::dev::genesis); ASSERT_NE (nullptr, election); election->force_confirm (); ASSERT_TRUE (election->confirmed ()); @@ -1222,8 +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); - node.block_confirm (send2); - auto election = node.active.election (send2->qualified_root ()); + auto election = system.start_election (node, send2); ASSERT_NE (nullptr, election); election->force_confirm (); From dbeea3a9b49e6c21a888c10c04c694d7970a82e8 Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Fri, 3 Feb 2023 01:30:48 +0000 Subject: [PATCH 4/7] confirmation heights tests --- nano/core_test/confirmation_height.cpp | 40 ++++++++++---------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/nano/core_test/confirmation_height.cpp b/nano/core_test/confirmation_height.cpp index e7f2904df1..326ac245a2 100644 --- a/nano/core_test/confirmation_height.cpp +++ b/nano/core_test/confirmation_height.cpp @@ -241,9 +241,7 @@ TEST (confirmation_height, multiple_accounts) .work (*system.work.generate (open3->hash ())) .build_shared (); node->process_active (receive3); - node->block_processor.flush (); - node->block_confirm (receive3); - auto election = node->active.election (receive3->qualified_root ()); + auto election = system.start_election (*node, receive3); ASSERT_NE (nullptr, election); election->force_confirm (); @@ -535,7 +533,7 @@ TEST (confirmation_height, gap_live) } // Vote and confirm all existing blocks - node->block_confirm (send1); + system.start_election (*node, send1); 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 @@ -690,9 +688,7 @@ TEST (confirmation_height, send_receive_between_2_accounts) add_callback_stats (*node); node->process_active (receive4); - node->block_processor.flush (); - node->block_confirm (receive4); - auto election = node->active.election (receive4->qualified_root ()); + auto election = system.start_election (*node, receive4); ASSERT_NE (nullptr, election); election->force_confirm (); @@ -809,12 +805,11 @@ TEST (confirmation_height, send_receive_self) add_callback_stats (*node); - node->block_confirm (receive3); - auto election = node->active.election (receive3->qualified_root ()); + auto election = system.start_election (*node, receive3); ASSERT_NE (nullptr, election); election->force_confirm (); - ASSERT_TIMELY (10s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 6); + ASSERT_TIMELY (5s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 6); auto transaction (node->store.tx_begin_read ()); ASSERT_TRUE (node->ledger.block_confirmed (transaction, receive3->hash ())); @@ -1055,12 +1050,11 @@ TEST (confirmation_height, all_block_types) } add_callback_stats (*node); - node->block_confirm (state_send2); - auto election = node->active.election (state_send2->qualified_root ()); + auto election = system.start_election (*node, state_send2); ASSERT_NE (nullptr, election); election->force_confirm (); - ASSERT_TIMELY (10s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 15); + ASSERT_TIMELY (5s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 15); auto transaction (node->store.tx_begin_read ()); ASSERT_TRUE (node->ledger.block_confirmed (transaction, state_send2->hash ())); @@ -1552,8 +1546,7 @@ TEST (confirmation_height, callback_confirmed_history) add_callback_stats (*node); node->process_active (send1); - node->block_processor.flush (); - node->block_confirm (send1); + ASSERT_NE (system.start_election (*node, send1), nullptr); { node->process_active (send); node->block_processor.flush (); @@ -1654,14 +1647,13 @@ TEST (confirmation_height, dependent_election) add_callback_stats (*node); // This election should be confirmed as active_conf_height - node->block_confirm (send1); + ASSERT_TRUE (system.start_election (*node, send1)); // Start an election and confirm it - node->block_confirm (send2); - auto election = node->active.election (send2->qualified_root ()); + auto election = system.start_election (*node, send2); ASSERT_NE (nullptr, election); election->force_confirm (); - ASSERT_TIMELY (10s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 3); + ASSERT_TIMELY (5s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 3); ASSERT_EQ (1, node->stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::active_quorum, nano::stat::dir::out)); ASSERT_EQ (1, node->stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::active_conf_height, nano::stat::dir::out)); @@ -1811,11 +1803,10 @@ TEST (confirmation_height, cemented_gap_below_receive) nano::mutex mutex; add_callback_stats (*node, &observer_order, &mutex); - node->block_confirm (open1); - auto election = node->active.election (open1->qualified_root ()); + auto election = system.start_election (*node, open1); ASSERT_NE (nullptr, election); election->force_confirm (); - ASSERT_TIMELY (10s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 10); + ASSERT_TIMELY (5s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 10); auto transaction = node->store.tx_begin_read (); ASSERT_TRUE (node->ledger.block_confirmed (transaction, open1->hash ())); @@ -1977,11 +1968,10 @@ TEST (confirmation_height, cemented_gap_below_no_cache) add_callback_stats (*node); - node->block_confirm (open1); - auto election = node->active.election (open1->qualified_root ()); + auto election = system.start_election (*node, open1); ASSERT_NE (nullptr, election); election->force_confirm (); - ASSERT_TIMELY (10s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 6); + ASSERT_TIMELY (5s, node->stats.count (nano::stat::type::http_callback, nano::stat::detail::http_callback, nano::stat::dir::out) == 6); auto transaction = node->store.tx_begin_read (); ASSERT_TRUE (node->ledger.block_confirmed (transaction, open1->hash ())); From 1670bd756519bb38300af1226f75990807853add Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Tue, 7 Feb 2023 02:19:33 +0000 Subject: [PATCH 5/7] Explicitly return nullptr --- nano/test_common/system.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nano/test_common/system.cpp b/nano/test_common/system.cpp index ffcb607afa..05fa09ff09 100644 --- a/nano/test_common/system.cpp +++ b/nano/test_common/system.cpp @@ -640,7 +640,7 @@ std::shared_ptr nano::test::system::start_election (nano::node & { if (poll ()) { - return {}; + return nullptr; } } @@ -652,7 +652,7 @@ std::shared_ptr nano::test::system::start_election (nano::node & { if (poll ()) { - return {}; + return nullptr; } election = node_a.active.election (block_a->qualified_root ()); } From c118f3574bed19f53b5e2b4a2cfb1f5cb09c7cb7 Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Tue, 7 Feb 2023 02:55:56 +0000 Subject: [PATCH 6/7] Move nano::test::system::start_election to nano::test::start_election --- 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/test_common/system.cpp | 30 -------------------------- nano/test_common/system.hpp | 7 ------ nano/test_common/testutil.cpp | 30 ++++++++++++++++++++++++++ nano/test_common/testutil.hpp | 7 ++++++ 8 files changed, 52 insertions(+), 52 deletions(-) diff --git a/nano/core_test/active_transactions.cpp b/nano/core_test/active_transactions.cpp index aa3a6d8298..cdff951fa1 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 = system.start_election (node, nano::dev::genesis); + auto election = nano::test::start_election (system, node, nano::dev::genesis); 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 = system.start_election (node, nano::dev::genesis); + election = nano::test::start_election (system, node, nano::dev::genesis); 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 = system.start_election (node, send2); + auto election = nano::test::start_election (system, node, send2); ASSERT_NE (nullptr, election); election->force_confirm (); diff --git a/nano/core_test/confirmation_height.cpp b/nano/core_test/confirmation_height.cpp index 326ac245a2..693b10e0dc 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 = system.start_election (*node, receive3); + auto election = nano::test::start_election (system, *node, receive3); ASSERT_NE (nullptr, election); election->force_confirm (); @@ -533,7 +533,7 @@ TEST (confirmation_height, gap_live) } // Vote and confirm all existing blocks - system.start_election (*node, send1); + nano::test::start_election (system, *node, send1); 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 = system.start_election (*node, receive4); + auto election = nano::test::start_election (system, *node, receive4); ASSERT_NE (nullptr, election); election->force_confirm (); @@ -805,7 +805,7 @@ TEST (confirmation_height, send_receive_self) add_callback_stats (*node); - auto election = system.start_election (*node, receive3); + auto election = nano::test::start_election (system, *node, receive3); ASSERT_NE (nullptr, election); election->force_confirm (); @@ -1050,7 +1050,7 @@ TEST (confirmation_height, all_block_types) } add_callback_stats (*node); - auto election = system.start_election (*node, state_send2); + auto election = nano::test::start_election (system, *node, state_send2); 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 (system.start_election (*node, send1), nullptr); + ASSERT_NE (nano::test::start_election (system, *node, send1), 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 (system.start_election (*node, send1)); + ASSERT_TRUE (nano::test::start_election (system, *node, send1)); // Start an election and confirm it - auto election = system.start_election (*node, send2); + auto election = nano::test::start_election (system, *node, send2); 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 = system.start_election (*node, open1); + auto election = nano::test::start_election (system, *node, open1); 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 = system.start_election (*node, open1); + auto election = nano::test::start_election (system, *node, open1); 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 b32199e45d..490748e2e5 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 = system.start_election (node1, send); + auto election = nano::test::start_election (system, node1, send); 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 38a6c6d71f..6eae46e2e2 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 = system.start_election (node, nano::dev::genesis); + auto election = nano::test::start_election (system, node, nano::dev::genesis); ASSERT_NE (election, nullptr); ASSERT_EQ (1, election->votes ().size ()); diff --git a/nano/test_common/system.cpp b/nano/test_common/system.cpp index 05fa09ff09..dc2f2882f7 100644 --- a/nano/test_common/system.cpp +++ b/nano/test_common/system.cpp @@ -630,33 +630,3 @@ void nano::test::cleanup_dev_directories_on_exit () nano::remove_temporary_directories (); } } - -std::shared_ptr nano::test::system::start_election (nano::node & node_a, const std::shared_ptr & block_a) -{ - deadline_set (5s); - - // wait until and ensure that the block is in the ledger - while (!node_a.block (block_a->hash ())) - { - if (poll ()) - { - return nullptr; - } - } - - node_a.scheduler.manual (block_a); - - // wait for the election to appear - std::shared_ptr election = node_a.active.election (block_a->qualified_root ()); - while (!election) - { - if (poll ()) - { - return nullptr; - } - election = node_a.active.election (block_a->qualified_root ()); - } - - election->transition_active (); - return election; -} \ No newline at end of file diff --git a/nano/test_common/system.hpp b/nano/test_common/system.hpp index ee175e86ab..931d05d057 100644 --- a/nano/test_common/system.hpp +++ b/nano/test_common/system.hpp @@ -62,13 +62,6 @@ namespace test */ nano::node_config default_config (); - /** - * Start an election on 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. - * Returns nullptr if the election did not start within the timeframe. - */ - std::shared_ptr start_election (nano::node & node_a, const std::shared_ptr & block_a); - public: boost::asio::io_context io_ctx; std::vector> nodes; diff --git a/nano/test_common/testutil.cpp b/nano/test_common/testutil.cpp index d932073b30..2f5231d6bb 100644 --- a/nano/test_common/testutil.cpp +++ b/nano/test_common/testutil.cpp @@ -218,4 +218,34 @@ std::shared_ptr nano::test::fake_channel (nano::node & channel->set_node_id (node_id); } return channel; +} + +std::shared_ptr nano::test::start_election (nano::test::system & system_a, nano::node & node_a, const std::shared_ptr & block_a) +{ + system_a.deadline_set (5s); + + // wait until and ensure that the block is in the ledger + while (!node_a.block (block_a->hash ())) + { + if (system_a.poll ()) + { + return nullptr; + } + } + + node_a.scheduler.manual (block_a); + + // wait for the election to appear + std::shared_ptr election = node_a.active.election (block_a->qualified_root ()); + while (!election) + { + if (system_a.poll ()) + { + return nullptr; + } + election = node_a.active.election (block_a->qualified_root ()); + } + + election->transition_active (); + return election; } \ No newline at end of file diff --git a/nano/test_common/testutil.hpp b/nano/test_common/testutil.hpp index e82a5ca875..1c434ecca5 100644 --- a/nano/test_common/testutil.hpp +++ b/nano/test_common/testutil.hpp @@ -129,6 +129,7 @@ class telemetry_data; class network_params; class vote; class block; +class election; extern nano::uint128_t const & genesis_amount; @@ -406,5 +407,11 @@ namespace test * Creates a new fake channel associated with `node` */ 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. + * 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); } } From c340b103b2f53a8c4b32f80cc23659a617763e1a Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Tue, 7 Feb 2023 03:14:27 +0000 Subject: [PATCH 7/7] convert rpc unit test functions --- nano/rpc_test/rpc.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 0bcb9af1f8..91f157eaa1 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -5874,9 +5874,9 @@ 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); - node->block_confirm (frontier); + nano::test::start_election (system, *node, frontier); - ASSERT_TIMELY (10s, node->confirmation_height_processor.current () == frontier->hash ()); + ASSERT_TIMELY (5s, node->confirmation_height_processor.current () == frontier->hash ()); // Make the request { @@ -6516,14 +6516,10 @@ TEST (rpc, block_confirmed) .work (*system.work.generate (latest)) .build_shared (); node->process_active (send); - node->block_processor.flush (); - node->block_confirm (send); - auto election = node->active.election (send->qualified_root ()); - ASSERT_NE (nullptr, election); - election->force_confirm (); + ASSERT_TIMELY (5s, nano::test::confirm (*node, { send })); // Wait until the confirmation height has been set - ASSERT_TIMELY (10s, node->ledger.block_confirmed (node->store.tx_begin_read (), send->hash ()) && !node->confirmation_height_processor.is_processing_block (send->hash ())); + ASSERT_TIMELY (5s, node->ledger.block_confirmed (node->store.tx_begin_read (), send->hash ()) && !node->confirmation_height_processor.is_processing_block (send->hash ())); // Requesting confirmation for this should now succeed request.put ("hash", send->hash ().to_string ());