diff --git a/nano/core_test/election.cpp b/nano/core_test/election.cpp index 689bb21d..e30a53d4 100644 --- a/nano/core_test/election.cpp +++ b/nano/core_test/election.cpp @@ -13,7 +13,7 @@ TEST (election, construction) { nano::test::system system (1); auto & node = *system.nodes[0]; - node.block_confirm (nano::dev::genesis); + node.start_election (nano::dev::genesis); ASSERT_TIMELY (5s, node.active.election (nano::dev::genesis->qualified_root ())); auto election = node.active.election (nano::dev::genesis->qualified_root ()); election->transition_active (); diff --git a/nano/core_test/ledger.cpp b/nano/core_test/ledger.cpp index 43fe65c3..b51e564d 100644 --- a/nano/core_test/ledger.cpp +++ b/nano/core_test/ledger.cpp @@ -942,7 +942,7 @@ TEST (votes, add_one) node1.work_generate_blocking (*send1); auto transaction (node1.store.tx_begin_write ()); ASSERT_EQ (nano::process_result::progress, node1.ledger.process (transaction, *send1).code); - node1.block_confirm (send1); + node1.start_election (send1); ASSERT_TIMELY (5s, node1.active.election (send1->qualified_root ())); auto election1 = node1.active.election (send1->qualified_root ()); ASSERT_EQ (1, election1->votes ().size ()); @@ -1043,7 +1043,7 @@ TEST (votes, add_old) node1.work_generate_blocking (*send1); auto transaction (node1.store.tx_begin_write ()); ASSERT_EQ (nano::process_result::progress, node1.ledger.process (transaction, *send1).code); - node1.block_confirm (send1); + node1.start_election (send1); ASSERT_TIMELY (5s, node1.active.election (send1->qualified_root ())); auto election1 = node1.active.election (send1->qualified_root ()); auto vote1 (std::make_shared (nano::dev::genesis_key.pub, nano::dev::genesis_key.prv, nano::vote::timestamp_min * 2, 0, std::vector{ send1->hash () })); @@ -1149,7 +1149,7 @@ TEST (votes, add_cooldown) node1.work_generate_blocking (*send1); auto transaction (node1.store.tx_begin_write ()); ASSERT_EQ (nano::process_result::progress, node1.ledger.process (transaction, *send1).code); - node1.block_confirm (send1); + node1.start_election (send1); ASSERT_TIMELY (5s, node1.active.election (send1->qualified_root ())); auto election1 = node1.active.election (send1->qualified_root ()); auto vote1 (std::make_shared (nano::dev::genesis_key.pub, nano::dev::genesis_key.prv, nano::vote::timestamp_min * 1, 0, std::vector{ send1->hash () })); diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index e5a1abfc..b7d116a0 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -1329,7 +1329,7 @@ TEST (node, DISABLED_broadcast_elected) { auto block (node->block (node->latest (nano::dev::genesis_key.pub))); ASSERT_NE (nullptr, block); - node->block_confirm (block); + node->start_election (block); auto election (node->active.election (block->qualified_root ())); ASSERT_NE (nullptr, election); election->force_confirm (); @@ -1397,9 +1397,9 @@ TEST (node, rep_self_vote) ASSERT_EQ (nano::process_result::progress, node0->process (fund_big).code); ASSERT_EQ (nano::process_result::progress, node0->process (open_big).code); // Confirm both blocks, allowing voting on the upcoming block - node0->block_confirm (node0->block (open_big.hash ())); - auto election = node0->active.election (open_big.qualified_root ()); - ASSERT_NE (nullptr, election); + node0->start_election (node0->block (open_big.hash ())); + std::shared_ptr election; + ASSERT_TIMELY (5s, election = node0->active.election (open_big.qualified_root ())); election->force_confirm (); system.wallet (0)->insert_adhoc (rep_big.prv); @@ -1545,7 +1545,7 @@ TEST (node, bootstrap_fork_open) // Confirm send0 to allow starting and voting on the following blocks for (auto node : system.nodes) { - node->block_confirm (node->block (node->latest (nano::dev::genesis_key.pub))); + node->start_election (node->block (node->latest (nano::dev::genesis_key.pub))); ASSERT_TIMELY (1s, node->active.election (send0.qualified_root ())); auto election = node->active.election (send0.qualified_root ()); ASSERT_NE (nullptr, election); @@ -2147,9 +2147,9 @@ TEST (node, block_confirm) ASSERT_TRUE (node1.ledger.block_or_pruned_exists (send1->hash ())); ASSERT_TRUE (node2.ledger.block_or_pruned_exists (send1_copy->hash ())); // Confirm send1 on node2 so it can vote for send2 - node2.block_confirm (send1_copy); - auto election = node2.active.election (send1_copy->qualified_root ()); - ASSERT_NE (nullptr, election); + node2.start_election (send1_copy); + std::shared_ptr election; + ASSERT_TIMELY (5s, election = node2.active.election (send1_copy->qualified_root ())); ASSERT_TIMELY (10s, node1.active.recently_cemented.list ().size () == 1); } @@ -2267,9 +2267,9 @@ TEST (node, local_votes_cache) ASSERT_EQ (nano::process_result::progress, node.ledger.process (transaction, *send2).code); } // Confirm blocks to allow voting - node.block_confirm (send2); - auto election = node.active.election (send2->qualified_root ()); - ASSERT_NE (nullptr, election); + node.start_election (send2); + std::shared_ptr election; + ASSERT_TIMELY (5s, election = node.active.election (send2->qualified_root ())); election->force_confirm (); ASSERT_TIMELY (3s, node.ledger.cache.cemented_count == 3); system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv); @@ -3768,7 +3768,7 @@ TEST (node, dependency_graph) // Start an election for the first block of the dependency graph, and ensure all blocks are eventually confirmed system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv); - node.block_confirm (gen_send1); + node.start_election (gen_send1); ASSERT_NO_ERROR (system.poll_until_true (15s, [&] { // Not many blocks should be active simultaneously @@ -3957,7 +3957,7 @@ TEST (node, dependency_graph_frontier) system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv); ASSERT_TIMELY (10s, node2.active.active (gen_send1->qualified_root ())); - node1.block_confirm (gen_send1); + node1.start_election (gen_send1); ASSERT_TIMELY (15s, node1.ledger.cache.cemented_count == node1.ledger.cache.block_count); ASSERT_TIMELY (15s, node2.ledger.cache.cemented_count == node2.ledger.cache.block_count); diff --git a/nano/core_test/request_aggregator.cpp b/nano/core_test/request_aggregator.cpp index f2714b30..344b6316 100644 --- a/nano/core_test/request_aggregator.cpp +++ b/nano/core_test/request_aggregator.cpp @@ -283,9 +283,9 @@ TEST (request_aggregator, split) request.emplace_back (block->hash (), block->root ()); } // Confirm all blocks - node.block_confirm (blocks.back ()); - auto election (node.active.election (blocks.back ()->qualified_root ())); - ASSERT_NE (nullptr, election); + node.start_election (blocks.back ()); + std::shared_ptr election; + ASSERT_TIMELY (5s, election = node.active.election (blocks.back ()->qualified_root ())); election->force_confirm (); ASSERT_TIMELY (5s, max_vbh + 2 == node.ledger.cache.cemented_count); ASSERT_EQ (max_vbh + 1, request.size ()); @@ -488,7 +488,7 @@ TEST (request_aggregator, cannot_vote) ASSERT_EQ (0, node.stats.count (nano::stat::type::message, nano::stat::detail::confirm_ack, nano::stat::dir::out)); // With an ongoing election - node.block_confirm (send2); + node.start_election (send2); node.aggregator.add (dummy_channel, request); ASSERT_EQ (1, node.aggregator.size ()); ASSERT_TIMELY (3s, node.aggregator.empty ()); @@ -501,9 +501,9 @@ TEST (request_aggregator, cannot_vote) ASSERT_EQ (0, node.stats.count (nano::stat::type::message, nano::stat::detail::confirm_ack, nano::stat::dir::out)); // Confirm send1 - node.block_confirm (send1); - auto election (node.active.election (send1->qualified_root ())); - ASSERT_NE (nullptr, election); + node.start_election (send1); + std::shared_ptr election; + ASSERT_TIMELY (5s, election = node.active.election (send1->qualified_root ())); election->force_confirm (); ASSERT_TIMELY (3s, node.ledger.dependents_confirmed (node.store.tx_begin_read (), *send2)); node.aggregator.add (dummy_channel, request); diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index 0a76b4ba..a444719b 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -13,8 +13,8 @@ TEST (vote_processor, codes) { nano::test::system system (1); auto & node (*system.nodes[0]); - nano::keypair key; - auto vote (std::make_shared (key.pub, key.prv, nano::vote::timestamp_min * 1, 0, std::vector{ nano::dev::genesis->hash () })); + auto blocks = nano::test::setup_chain (system, node, 1, nano::dev::genesis_key, false); + auto vote (std::make_shared (nano::dev::genesis_key.pub, nano::dev::genesis_key.prv, nano::vote::timestamp_min * 1, 0, std::vector{ blocks[0]->hash () })); auto vote_invalid = std::make_shared (*vote); vote_invalid->signature.bytes[0] ^= 1; auto channel (std::make_shared (node, node)); @@ -29,8 +29,9 @@ TEST (vote_processor, codes) ASSERT_EQ (nano::vote_code::indeterminate, node.vote_processor.vote_blocking (vote, channel)); // First vote from an account for an ongoing election - node.block_confirm (nano::dev::genesis); - ASSERT_NE (nullptr, node.active.election (nano::dev::genesis->qualified_root ())); + node.start_election (blocks[0]); + std::shared_ptr election; + ASSERT_TIMELY (5s, election = node.active.election (blocks[0]->qualified_root ())); ASSERT_EQ (nano::vote_code::vote, node.vote_processor.vote_blocking (vote, channel)); // Processing the same vote is a replay @@ -40,7 +41,7 @@ TEST (vote_processor, codes) ASSERT_EQ (nano::vote_code::invalid, node.vote_processor.vote_blocking (vote_invalid, channel)); // Once the election is removed (confirmed / dropped) the vote is again indeterminate - node.active.erase (*nano::dev::genesis); + node.active.erase (*blocks[0]); ASSERT_EQ (nano::vote_code::indeterminate, node.vote_processor.vote_blocking (vote, channel)); } @@ -256,13 +257,13 @@ TEST (vote_processor, local_broadcast_without_a_representative) ASSERT_EQ (nano::process_result::progress, node.process_local (send).value ().code); ASSERT_TIMELY (10s, !node.active.empty ()); ASSERT_EQ (node.config.vote_minimum, node.weight (nano::dev::genesis_key.pub)); - node.block_confirm (send); + node.start_election (send); // Process a vote without a representative auto vote = std::make_shared (nano::dev::genesis_key.pub, nano::dev::genesis_key.prv, nano::milliseconds_since_epoch (), nano::vote::duration_max, std::vector{ send->hash () }); ASSERT_EQ (nano::vote_code::vote, node.active.vote (vote)); // Make sure the vote was processed. - auto election (node.active.election (send->qualified_root ())); - ASSERT_NE (nullptr, election); + std::shared_ptr election; + ASSERT_TIMELY (5s, election = node.active.election (send->qualified_root ())); auto votes (election->votes ()); auto existing (votes.find (nano::dev::genesis_key.pub)); ASSERT_NE (votes.end (), existing); diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index 6ce16ad5..799466e2 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -1182,8 +1182,8 @@ TEST (wallet, search_receivable) // Pending search should start an election ASSERT_TRUE (node.active.empty ()); ASSERT_FALSE (wallet.search_receivable (wallet.wallets.tx_begin_read ())); - auto election = node.active.election (send->qualified_root ()); - ASSERT_NE (nullptr, election); + std::shared_ptr election; + ASSERT_TIMELY (5s, election = node.active.election (send->qualified_root ())); // Erase the key so the confirmation does not trigger an automatic receive wallet.store.erase (node.wallets.tx_begin_write (), nano::dev::genesis->account ()); diff --git a/nano/core_test/wallets.cpp b/nano/core_test/wallets.cpp index daa22074..ad9e6df4 100644 --- a/nano/core_test/wallets.cpp +++ b/nano/core_test/wallets.cpp @@ -222,8 +222,8 @@ TEST (wallets, search_receivable) { node.wallets.search_receivable (wallet_id); } - auto election = node.active.election (send->qualified_root ()); - ASSERT_NE (nullptr, election); + std::shared_ptr election; + ASSERT_TIMELY (5s, election = node.active.election (send->qualified_root ())); // Erase the key so the confirmation does not trigger an automatic receive wallet->store.erase (node.wallets.tx_begin_write (), nano::dev::genesis->account ()); diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index a4f7a6e8..a62f1ad3 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -1205,7 +1205,7 @@ void nano::json_handler::block_confirm () // Start new confirmation for unconfirmed (or not being confirmed) block if (!node.confirmation_height_processor.is_processing_block (hash)) { - node.block_confirm (std::move (block_l)); + node.start_election (std::move (block_l)); } } else diff --git a/nano/node/node.cpp b/nano/node/node.cpp index b0bc3e03..e3931b09 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -1263,17 +1263,10 @@ void nano::node::add_initial_peers () } } -std::shared_ptr nano::node::block_confirm (std::shared_ptr const & block_a) +void nano::node::start_election (std::shared_ptr const & block) { - scheduler.priority.manual (block_a); + scheduler.priority.manual (block); scheduler.priority.flush (); - auto election = active.election (block_a->qualified_root ()); - if (election != nullptr) - { - election->transition_active (); - return election; - } - return {}; } bool nano::node::block_confirmed (nano::block_hash const & hash_a) diff --git a/nano/node/node.hpp b/nano/node/node.hpp index 4067f15a..a52a04d3 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -121,11 +121,7 @@ public: boost::optional work_generate_blocking (nano::work_version const, nano::root const &, uint64_t, boost::optional const & = boost::none); void work_generate (nano::work_version const, nano::root const &, uint64_t, std::function)>, boost::optional const & = boost::none, bool const = false); void add_initial_peers (); - /* - * Starts an election for the block, DOES NOT confirm it - * TODO: Rename to `start_election` - */ - std::shared_ptr block_confirm (std::shared_ptr const &); + void start_election (std::shared_ptr const & block); bool block_confirmed (nano::block_hash const &); bool block_confirmed_or_being_confirmed (nano::block_hash const &); void do_rpc_callback (boost::asio::ip::tcp::resolver::iterator i_a, std::string const &, uint16_t, std::shared_ptr const &, std::shared_ptr const &, std::shared_ptr const &); diff --git a/nano/node/scheduler/priority.cpp b/nano/node/scheduler/priority.cpp index b2c7006c..b60bc394 100644 --- a/nano/node/scheduler/priority.cpp +++ b/nano/node/scheduler/priority.cpp @@ -133,7 +133,11 @@ void nano::scheduler::priority::run () manual_queue.pop_front (); lock.unlock (); stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::insert_manual); - node.active.insert (block, election_behavior); + auto result = node.active.insert (block, election_behavior); + if (result.election != nullptr) + { + result.election->transition_active (); + } } else if (priority_queue_predicate ()) { diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index f48c841d..804fe627 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -1195,7 +1195,7 @@ bool nano::wallet::search_receivable (nano::transaction const & wallet_transacti if (block) { // Request confirmation for block which is not being processed yet - wallets.node.block_confirm (block); + wallets.node.start_election (block); } } } diff --git a/nano/slow_test/node.cpp b/nano/slow_test/node.cpp index 280ce6df..054f2bcc 100644 --- a/nano/slow_test/node.cpp +++ b/nano/slow_test/node.cpp @@ -1132,7 +1132,7 @@ TEST (confirmation_height, many_accounts_send_receive_self) // Confirm all of the accounts for (auto & open_block : open_blocks) { - node->block_confirm (open_block); + node->start_election (open_block); std::shared_ptr election; ASSERT_TIMELY (10s, (election = node->active.election (open_block->qualified_root ())) != nullptr); election->force_confirm ();