diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index 8360a623c2..42197b7bb8 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -3274,9 +3274,11 @@ struct controller_impl { if ( s == controller::block_status::incomplete || s == controller::block_status::complete || s == controller::block_status::validated ) { if (!my_finalizers.empty()) { apply_s(chain_head, [&](const auto& head) { - boost::asio::post(thread_pool.get_executor(), [this, head=head]() { - create_and_send_vote_msg(head); - }); + if (head->is_recent() || my_finalizers.is_active()) { + boost::asio::post(thread_pool.get_executor(), [this, head=head]() { + create_and_send_vote_msg(head); + }); + } }); } } @@ -3978,18 +3980,20 @@ struct controller_impl { // 3. Otherwise, consider voting for that block according to the decide_vote rules. if (!my_finalizers.empty() && bsp->core.final_on_strong_qc_block_num > 0) { - if (use_thread_pool == use_thread_pool_t::yes) { - boost::asio::post(thread_pool.get_executor(), [this, bsp=bsp]() { + if (bsp->is_recent() || my_finalizers.is_active()) { + if (use_thread_pool == use_thread_pool_t::yes) { + boost::asio::post(thread_pool.get_executor(), [this, bsp=bsp]() { + const auto& final_on_strong_qc_block_ref = bsp->core.get_block_reference(bsp->core.final_on_strong_qc_block_num); + if (fork_db_validated_block_exists(final_on_strong_qc_block_ref.block_id)) { + create_and_send_vote_msg(bsp); + } + }); + } else { + // bsp can be used directly instead of copy needed for post const auto& final_on_strong_qc_block_ref = bsp->core.get_block_reference(bsp->core.final_on_strong_qc_block_num); if (fork_db_validated_block_exists(final_on_strong_qc_block_ref.block_id)) { create_and_send_vote_msg(bsp); } - }); - } else { - // bsp can be used directly instead of copy needed for post - const auto& final_on_strong_qc_block_ref = bsp->core.get_block_reference(bsp->core.final_on_strong_qc_block_num); - if (fork_db_validated_block_exists(final_on_strong_qc_block_ref.block_id)) { - create_and_send_vote_msg(bsp); } } } diff --git a/libraries/chain/include/eosio/chain/block_state.hpp b/libraries/chain/include/eosio/chain/block_state.hpp index dde798a574..d582beecc1 100644 --- a/libraries/chain/include/eosio/chain/block_state.hpp +++ b/libraries/chain/include/eosio/chain/block_state.hpp @@ -111,6 +111,11 @@ struct block_state : public block_header_state { // block_header_state provi bool valid_qc_is_strong() const { return pending_qc.valid_qc_is_strong(); } // thread safe void set_valid_qc(const valid_quorum_certificate& qc) { pending_qc.set_valid_qc(qc); } + // heuristic for determination if we are syncing or replaying for optimizations + bool is_recent() const { + return timestamp() > fc::time_point::now() - fc::seconds(30); + } + protocol_feature_activation_set_ptr get_activated_protocol_features() const { return block_header_state::activated_protocol_features; } uint32_t last_qc_block_num() const { return core.latest_qc_claim().block_num; } uint32_t final_on_strong_qc_block_num() const { return core.final_on_strong_qc_block_num; } diff --git a/libraries/chain/include/eosio/chain/finality/finalizer.hpp b/libraries/chain/include/eosio/chain/finality/finalizer.hpp index 4df3c12591..9017ecd22c 100644 --- a/libraries/chain/include/eosio/chain/finality/finalizer.hpp +++ b/libraries/chain/include/eosio/chain/finality/finalizer.hpp @@ -95,7 +95,7 @@ namespace eosio::chain { return; if (!enable_voting.load(std::memory_order_relaxed)) { // Avoid extra processing while syncing. Once caught up, consider voting - if (bsp->timestamp() < fc::time_point::now() - fc::seconds(30)) + if (!bsp->is_recent()) return; enable_voting.store(true, std::memory_order_relaxed); } @@ -132,6 +132,7 @@ namespace eosio::chain { size_t size() const { return finalizers.size(); } // doesn't change, thread safe bool empty() const { return finalizers.empty(); } // doesn't change, thread safe + bool is_active() const { return !empty() && enable_voting.load(std::memory_order_relaxed); } // thread safe template bool all_of_public_keys(F&& f) const { // only access keys which do not change, thread safe