From d18f135a40c53e8ccdc1d8f9f283cee4b4841430 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Tue, 5 Sep 2023 11:43:14 +0100 Subject: [PATCH] Return error code from nano::test::start_election --- nano/core_test/active_transactions.cpp | 6 +++--- nano/core_test/bootstrap.cpp | 8 ++++---- nano/core_test/confirmation_height.cpp | 2 +- nano/core_test/election.cpp | 2 +- nano/core_test/ledger.cpp | 4 ++-- nano/core_test/node.cpp | 10 +++++----- nano/core_test/optimistic_scheduler.cpp | 4 ++-- nano/rpc_test/receivable.cpp | 4 ++-- nano/rpc_test/rpc.cpp | 6 +++--- nano/slow_test/node.cpp | 4 ++-- nano/slow_test/vote_cache.cpp | 4 ++-- nano/test_common/chains.cpp | 8 ++++---- nano/test_common/testutil.cpp | 12 ++++++++---- nano/test_common/testutil.hpp | 10 ++++++---- 14 files changed, 45 insertions(+), 39 deletions(-) diff --git a/nano/core_test/active_transactions.cpp b/nano/core_test/active_transactions.cpp index 99f3abd228..9842f8bf28 100644 --- a/nano/core_test/active_transactions.cpp +++ b/nano/core_test/active_transactions.cpp @@ -556,7 +556,7 @@ TEST (active_transactions, vote_replays) ASSERT_NE (nullptr, open1); node.process_active (send1); node.process_active (open1); - nano::test::start_elections (system, node, { send1, open1 }); + ASSERT_TRUE (nano::test::start_elections (system, node, { send1, open1 })); ASSERT_EQ (2, node.active.size ()); // First vote is not a replay and confirms the election, second vote should be a replay since the election has confirmed but not yet removed auto vote_send1 (std::make_shared (nano::dev::genesis_key.pub, nano::dev::genesis_key.prv, nano::vote::timestamp_max, nano::vote::duration_max, std::vector{ send1->hash () })); @@ -585,7 +585,7 @@ TEST (active_transactions, vote_replays) .build_shared (); ASSERT_NE (nullptr, send2); node.process_active (send2); - nano::test::start_elections (system, node, { send2 }); + ASSERT_TRUE (nano::test::start_elections (system, node, { send2 })); ASSERT_EQ (1, node.active.size ()); auto vote1_send2 (std::make_shared (nano::dev::genesis_key.pub, nano::dev::genesis_key.prv, nano::vote::timestamp_max, nano::vote::duration_max, std::vector{ send2->hash () })); auto vote2_send2 (std::make_shared (key.pub, key.prv, 0, 0, std::vector{ send2->hash () })); @@ -1280,7 +1280,7 @@ TEST (active_transactions, list_active) ASSERT_EQ (nano::process_result::progress, node.process (*open).code); - nano::test::start_elections (system, node, { send, send2, open }); + ASSERT_TRUE (nano::test::start_elections (system, node, { send, send2, open })); ASSERT_EQ (3, node.active.size ()); ASSERT_EQ (1, node.active.list_active (1).size ()); ASSERT_EQ (2, node.active.list_active (2).size ()); diff --git a/nano/core_test/bootstrap.cpp b/nano/core_test/bootstrap.cpp index 4c20ea7f02..c5536c34a5 100644 --- a/nano/core_test/bootstrap.cpp +++ b/nano/core_test/bootstrap.cpp @@ -939,7 +939,7 @@ TEST (bootstrap_processor, lazy_hash_pruning) node1->process_active (change2); ASSERT_TIMELY (5s, node1->block (change2->hash ()) != nullptr); // Confirm last block to prune previous - nano::test::start_elections (system, *node1, { send1, receive1, change1, change2 }, true); + ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1, receive1, change1, change2 }, true)); ASSERT_TIMELY (5s, node1->block_confirmed (send1->hash ()) && node1->block_confirmed (receive1->hash ()) && node1->block_confirmed (change1->hash ()) && node1->block_confirmed (change2->hash ()) && node1->active.empty ()); ASSERT_EQ (5, node1->ledger.cache.block_count); ASSERT_EQ (5, node1->ledger.cache.cemented_count); @@ -1321,7 +1321,7 @@ TEST (bootstrap_processor, lazy_pruning_missing_block) node1->process_active (state_open); ASSERT_TIMELY (5s, node1->block (state_open->hash ()) != nullptr); // Confirm last block to prune previous - nano::test::start_elections (system, *node1, { send1, send2, open, state_open }, true); + ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1, send2, open, state_open }, true)); ASSERT_TIMELY (5s, node1->block_confirmed (send2->hash ()) && node1->block_confirmed (open->hash ()) && node1->block_confirmed (state_open->hash ())); ASSERT_EQ (5, node1->ledger.cache.block_count); ASSERT_EQ (5, node1->ledger.cache.cemented_count); @@ -1878,7 +1878,7 @@ TEST (frontier_req, confirmed_frontier) ASSERT_EQ (receive2->hash (), request5->frontier); // Confirm account before genesis (confirmed only) - nano::test::start_elections (system, *node1, { send1, receive1 }, true); + ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1, receive1 }, true)); ASSERT_TIMELY (5s, node1->block_confirmed (send1->hash ()) && node1->block_confirmed (receive1->hash ())); auto connection6 (std::make_shared (std::make_shared (*node1, nano::transport::socket::endpoint_type_t::server), node1)); auto req6 = std::make_unique (nano::dev::network_params.network); @@ -1893,7 +1893,7 @@ TEST (frontier_req, confirmed_frontier) ASSERT_EQ (receive1->hash (), request6->frontier); // Confirm account after genesis (confirmed only) - nano::test::start_elections (system, *node1, { send2, receive2 }, true); + ASSERT_TRUE (nano::test::start_elections (system, *node1, { send2, receive2 }, true)); ASSERT_TIMELY (5s, node1->block_confirmed (send2->hash ()) && node1->block_confirmed (receive2->hash ())); auto connection7 (std::make_shared (std::make_shared (*node1, nano::transport::socket::endpoint_type_t::server), node1)); auto req7 = std::make_unique (nano::dev::network_params.network); diff --git a/nano/core_test/confirmation_height.cpp b/nano/core_test/confirmation_height.cpp index 3ed1157430..508697f4ee 100644 --- a/nano/core_test/confirmation_height.cpp +++ b/nano/core_test/confirmation_height.cpp @@ -1118,7 +1118,7 @@ TEST (confirmation_height, conflict_rollback_cemented) .work (*system.work.generate (genesis_hash)) .build_shared (); ASSERT_EQ (nano::process_result::progress, node1->process (*fork1a).code); - nano::test::start_elections (system, *node1, { fork1a }, true); + ASSERT_TRUE (nano::test::start_elections (system, *node1, { fork1a }, true)); ASSERT_TIMELY (5s, nano::test::confirmed (*node1, { fork1a })); // create the other side of the fork on node2 diff --git a/nano/core_test/election.cpp b/nano/core_test/election.cpp index 13fc1f1be2..94f33a034d 100644 --- a/nano/core_test/election.cpp +++ b/nano/core_test/election.cpp @@ -286,7 +286,7 @@ TEST (election, continuous_voting) .build_shared (); ASSERT_TRUE (nano::test::process (node1, { send1 })); - nano::test::start_elections (system, node1, { send1 }, true); + ASSERT_TRUE (nano::test::start_elections (system, node1, { send1 }, true)); ASSERT_TIMELY (5s, nano::test::confirmed (node1, { send1 })); node1.stats.clear (); diff --git a/nano/core_test/ledger.cpp b/nano/core_test/ledger.cpp index b51e564d4c..52e7c289fa 100644 --- a/nano/core_test/ledger.cpp +++ b/nano/core_test/ledger.cpp @@ -1103,7 +1103,7 @@ TEST (votes, DISABLED_add_old_different_account) node1.work_generate_blocking (*send2); ASSERT_EQ (nano::process_result::progress, node1.process (*send1).code); ASSERT_EQ (nano::process_result::progress, node1.process (*send2).code); - nano::test::start_elections (system, node1, { send1, send2 }); + ASSERT_TRUE (nano::test::start_elections (system, node1, { send1, send2 })); auto election1 = node1.active.election (send1->qualified_root ()); ASSERT_NE (nullptr, election1); auto election2 = node1.active.election (send2->qualified_root ()); @@ -4092,7 +4092,7 @@ TEST (ledger, block_hash_account_conflict) ASSERT_EQ (nano::process_result::progress, node1.process (*receive1).code); ASSERT_EQ (nano::process_result::progress, node1.process (*send2).code); ASSERT_EQ (nano::process_result::progress, node1.process (*open_epoch1).code); - nano::test::start_elections (system, node1, { send1, receive1, send2, open_epoch1 }); + ASSERT_TRUE (nano::test::start_elections (system, node1, { send1, receive1, send2, open_epoch1 })); auto election1 = node1.active.election (send1->qualified_root ()); ASSERT_NE (nullptr, election1); auto election2 = node1.active.election (receive1->qualified_root ()); diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index b7d116a0a1..7b8468bf9c 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -2573,7 +2573,7 @@ TEST (node, vote_by_hash_bundle) } // Confirming last block will confirm whole chain and allow us to generate votes for those blocks later - nano::test::start_elections (system, node, { blocks.back () }, true); + ASSERT_TRUE (nano::test::start_elections (system, node, { blocks.back () }, true)); ASSERT_TIMELY (5s, nano::test::confirmed (node, { blocks.back () })); system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv); @@ -2752,7 +2752,7 @@ TEST (node, epoch_conflict_confirm) ASSERT_TRUE (nano::test::process (node1, { send, send2, open })); // Confirm open block in node1 to allow generating votes - nano::test::start_elections (system, node1, { open }, true); + ASSERT_TRUE (nano::test::start_elections (system, node1, { open }, true)); ASSERT_TIMELY (5s, nano::test::confirmed (node1, { open })); // Process initial blocks on node0 @@ -2766,7 +2766,7 @@ TEST (node, epoch_conflict_confirm) ASSERT_TIMELY (5s, nano::test::exists (node1, { change, epoch_open })); // Confirm initial blocks in node1 to allow generating votes later - nano::test::start_elections (system, node1, { change, epoch_open, send2 }, true); + ASSERT_TRUE (nano::test::start_elections (system, node1, { change, epoch_open, send2 }, true)); ASSERT_TIMELY (5s, nano::test::confirmed (node1, { change, epoch_open, send2 })); // Start elections for node0 for conflicting change and epoch_open blocks (those two blocks have the same root) @@ -3147,7 +3147,7 @@ TEST (node, confirm_back) node.process_active (open); node.process_active (send2); ASSERT_TIMELY (5s, node.block (send2->hash ()) != nullptr); - nano::test::start_elections (system, node, { send1, open, send2 }); + ASSERT_TRUE (nano::test::start_elections (system, node, { send1, open, send2 })); ASSERT_EQ (3, node.active.size ()); std::vector vote_blocks; vote_blocks.push_back (send2->hash ()); @@ -3481,7 +3481,7 @@ TEST (node, rollback_vote_self) // Process and mark the first 2 blocks as confirmed to allow voting ASSERT_TRUE (nano::test::process (node, { send1, open })); - nano::test::start_elections (system, node, { send1, open }, true); + ASSERT_TRUE (nano::test::start_elections (system, node, { send1, open }, true)); ASSERT_TIMELY (5s, node.ledger.cache.cemented_count == 3); // wait until the rep weights have caught up with the weight transfer diff --git a/nano/core_test/optimistic_scheduler.cpp b/nano/core_test/optimistic_scheduler.cpp index 13bbac8be7..dfcd6fb819 100644 --- a/nano/core_test/optimistic_scheduler.cpp +++ b/nano/core_test/optimistic_scheduler.cpp @@ -24,7 +24,7 @@ TEST (optimistic_scheduler, activate_one) auto & [account, blocks] = chains.front (); // Confirm block towards at the beginning the chain, so gap between confirmation and account frontier is larger than `gap_threshold` - nano::test::start_elections (system, node, { blocks.at (11) }, true); + ASSERT_TRUE (nano::test::start_elections (system, node, { blocks.at (11) }, true)); ASSERT_TIMELY (5s, nano::test::confirmed (node, { blocks.at (11) })); // Ensure unconfirmed account head block gets activated @@ -93,7 +93,7 @@ TEST (optimistic_scheduler, under_gap_threshold) auto & [account, blocks] = chains.front (); // Confirm block towards the end of the chain, so gap between confirmation and account frontier is less than `gap_threshold` - nano::test::start_elections (system, node, { blocks.at (55) }, true); + ASSERT_TRUE (nano::test::start_elections (system, node, { blocks.at (55) }, true)); ASSERT_TIMELY (5s, nano::test::confirmed (node, { blocks.at (55) })); // Manually trigger backlog scan diff --git a/nano/rpc_test/receivable.cpp b/nano/rpc_test/receivable.cpp index 5bbe4bab35..788ccd98b3 100644 --- a/nano/rpc_test/receivable.cpp +++ b/nano/rpc_test/receivable.cpp @@ -144,7 +144,7 @@ TEST (rpc, receivable_unconfirmed) ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 0)); request.put ("include_only_confirmed", "false"); ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); - nano::test::start_elections (system, *node, { block1->hash () }, true); + ASSERT_TRUE (nano::test::start_elections (system, *node, { block1->hash () }, true)); ASSERT_TIMELY (5s, !node->active.active (*block1)); request.put ("include_only_confirmed", "true"); ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); @@ -548,7 +548,7 @@ TEST (rpc, accounts_receivable_confirmed) ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 0)); request.put ("include_only_confirmed", "false"); ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); - nano::test::start_elections (system, *node, { block1->hash () }, true); + ASSERT_TRUE (nano::test::start_elections (system, *node, { block1->hash () }, true)); ASSERT_TIMELY (5s, !node->active.active (*block1)); request.put ("include_only_confirmed", "true"); ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index bcbb97109a..3c01236e3c 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -1246,7 +1246,7 @@ TEST (rpc, history_pruning) ASSERT_TIMELY (5s, nano::test::exists (*node0, blocks)); system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv); - nano::test::start_elections (system, *node0, blocks, true); + ASSERT_TRUE (nano::test::start_elections (system, *node0, blocks, true)); ASSERT_TIMELY (5s, node0->block_confirmed (uchange->hash ())); nano::confirmation_height_info confirmation_height_info; node0->store.confirmation_height.get (node0->store.tx_begin_read (), nano::dev::genesis_key.pub, confirmation_height_info); @@ -5877,7 +5877,7 @@ TEST (rpc, block_confirmed) .work (*system.work.generate (latest)) .build_shared (); node->process_active (send); - nano::test::start_elections (system, *node, { send }, true); + ASSERT_TRUE (nano::test::start_elections (system, *node, { send }, true)); // Wait until the confirmation height has been set ASSERT_TIMELY (5s, node->ledger.block_confirmed (node->store.tx_begin_read (), send->hash ()) && !node->confirmation_height_processor.is_processing_block (send->hash ())); @@ -6879,7 +6879,7 @@ TEST (rpc, confirmation_active) .build_shared (); node1->process_active (send1); node1->process_active (send2); - nano::test::start_elections (system, *node1, { send1, send2 }); + ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1, send2 })); ASSERT_EQ (2, node1->active.size ()); auto election (node1->active.election (send1->qualified_root ())); ASSERT_NE (nullptr, election); diff --git a/nano/slow_test/node.cpp b/nano/slow_test/node.cpp index 054f2bcc0d..c9dd6c09aa 100644 --- a/nano/slow_test/node.cpp +++ b/nano/slow_test/node.cpp @@ -2110,9 +2110,9 @@ TEST (node, aggressive_flooding) ASSERT_EQ (node1.latest (nano::dev::genesis_key.pub), node_wallet.first->latest (nano::dev::genesis_key.pub)); ASSERT_EQ (genesis_blocks.back ()->hash (), node_wallet.first->latest (nano::dev::genesis_key.pub)); // Confirm blocks for rep crawler & receiving - nano::test::start_elections (system, *node_wallet.first, { genesis_blocks.back () }, true); + ASSERT_TRUE (nano::test::start_elections (system, *node_wallet.first, { genesis_blocks.back () }, true)); } - nano::test::start_elections (system, node1, { genesis_blocks.back () }, true); + ASSERT_TRUE (nano::test::start_elections (system, node1, { genesis_blocks.back () }, true)); // Wait until all genesis blocks are received auto all_received = [&nodes_wallets] () { diff --git a/nano/slow_test/vote_cache.cpp b/nano/slow_test/vote_cache.cpp index 934c226bac..f0ee8035ed 100644 --- a/nano/slow_test/vote_cache.cpp +++ b/nano/slow_test/vote_cache.cpp @@ -38,7 +38,7 @@ nano::keypair setup_rep (nano::test::system & system, nano::node & node, nano::u .build_shared (); EXPECT_TRUE (nano::test::process (node, { send, open })); - nano::test::start_elections (system, node, { send, open }, true); + EXPECT_TRUE (nano::test::start_elections (system, node, { send, open }, true)); EXPECT_TIMELY (5s, nano::test::confirmed (node, { send, open })); return key; @@ -104,7 +104,7 @@ std::vector> setup_blocks (nano::test::system & sys EXPECT_TRUE (nano::test::process (node, receives)); // Confirm whole genesis chain at once - nano::test::start_elections (system, node, { sends.back () }, true); + EXPECT_TRUE (nano::test::start_elections (system, node, { sends.back () }, true)); EXPECT_TIMELY (5s, nano::test::confirmed (node, { sends })); std::cout << "setup_blocks done" << std::endl; diff --git a/nano/test_common/chains.cpp b/nano/test_common/chains.cpp index 8e1a721598..af528e92a0 100644 --- a/nano/test_common/chains.cpp +++ b/nano/test_common/chains.cpp @@ -35,7 +35,7 @@ nano::block_list_t nano::test::setup_chain (nano::test::system & system, nano::n if (confirm) { // Confirm whole chain at once - nano::test::start_elections (system, node, { blocks.back () }, true); + EXPECT_TRUE (nano::test::start_elections (system, node, { blocks.back () }, true)); EXPECT_TIMELY (5s, nano::test::confirmed (node, blocks)); } @@ -83,7 +83,7 @@ std::vector> nano::test::setup_chai if (confirm) { // Ensure blocks are in the ledger and confirmed - nano::test::start_elections (system, node, { send, open }, true); + EXPECT_TRUE (nano::test::start_elections (system, node, { send, open }, true)); EXPECT_TIMELY (5s, nano::test::confirmed (node, { send, open })); } @@ -142,7 +142,7 @@ nano::block_list_t nano::test::setup_independent_blocks (nano::test::system & sy } // Confirm whole genesis chain at once - nano::test::start_elections (system, node, { latest }, true); + EXPECT_TRUE (nano::test::start_elections (system, node, { latest }, true)); EXPECT_TIMELY (5s, nano::test::confirmed (node, { latest })); return blocks; @@ -179,7 +179,7 @@ nano::keypair nano::test::setup_rep (nano::test::system & system, nano::node & n .build_shared (); EXPECT_TRUE (nano::test::process (node, { send, open })); - nano::test::start_elections (system, node, { send, open }, true); + EXPECT_TRUE (nano::test::start_elections (system, node, { send, open }, true)); EXPECT_TIMELY (5s, nano::test::confirmed (node, { send, open })); return key; diff --git a/nano/test_common/testutil.cpp b/nano/test_common/testutil.cpp index 1f8aa1fdb9..f3aba3d3f4 100644 --- a/nano/test_common/testutil.cpp +++ b/nano/test_common/testutil.cpp @@ -222,20 +222,24 @@ std::shared_ptr nano::test::start_election (nano::test::system & return election; } -void nano::test::start_elections (nano::test::system & system_a, nano::node & node_a, std::vector const & hashes_a, bool const forced_a) +bool nano::test::start_elections (nano::test::system & system_a, nano::node & node_a, std::vector const & hashes_a, bool const forced_a) { for (auto const & hash_l : hashes_a) { auto election = nano::test::start_election (system_a, node_a, hash_l); - release_assert (election); + if (!election) + { + return false; + } if (forced_a) { election->force_confirm (); } } + return true; } -void nano::test::start_elections (nano::test::system & system_a, nano::node & node_a, std::vector> const & blocks_a, bool const forced_a) +bool nano::test::start_elections (nano::test::system & system_a, nano::node & node_a, std::vector> const & blocks_a, bool const forced_a) { - nano::test::start_elections (system_a, node_a, blocks_to_hashes (blocks_a), forced_a); + return nano::test::start_elections (system_a, node_a, blocks_to_hashes (blocks_a), forced_a); } diff --git a/nano/test_common/testutil.hpp b/nano/test_common/testutil.hpp index 5e3793a59d..6a5ab1e379 100644 --- a/nano/test_common/testutil.hpp +++ b/nano/test_common/testutil.hpp @@ -407,14 +407,16 @@ namespace test /* * Call start_election for every block identified in the hash vector. * Optionally, force confirm the election if forced_a is set. - * NOTE: Each election is given 5 seconds to complete, if it does not complete in 5 seconds, it will assert. + * @return true if all elections were successfully started + * NOTE: Each election is given 5 seconds to complete, if it does not complete in 5 seconds, it will return an error */ - void start_elections (nano::test::system &, nano::node &, std::vector const &, bool const forced_a = false); + [[nodiscard]] bool start_elections (nano::test::system &, nano::node &, std::vector const &, bool const forced_a = false); /* * Call start_election for every block in the vector. * Optionally, force confirm the election if forced_a is set. - * NOTE: Each election is given 5 seconds to complete, if it does not complete in 5 seconds, it will assert. + * @return true if all elections were successfully started + * NOTE: Each election is given 5 seconds to complete, if it does not complete in 5 seconds, it will return an error. */ - void start_elections (nano::test::system &, nano::node &, std::vector> const &, bool const forced_a = false); + [[nodiscard]] bool start_elections (nano::test::system &, nano::node &, std::vector> const &, bool const forced_a = false); } }