From 97abf643c8393a9a372e7c17b4158ffdfe6085c5 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 2 Mar 2023 11:33:28 +0000 Subject: [PATCH] Block processor cleanup (#4165) * Removing unused function. * Removing duplicate functionality in block_processor::add_local. * Removing usages of unchecked_info in block_processor. --- nano/nano_node/entry.cpp | 11 ++-- nano/node/blockprocessor.cpp | 74 +++++------------------ nano/node/blockprocessor.hpp | 8 +-- nano/node/bootstrap/bootstrap_attempt.cpp | 3 +- nano/node/bootstrap/bootstrap_lazy.cpp | 3 +- nano/node/node.cpp | 4 +- 6 files changed, 28 insertions(+), 75 deletions(-) diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index 93efa000..f04b2f55 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -1793,7 +1793,7 @@ int main (int argc, char * const * argv) auto begin (std::chrono::high_resolution_clock::now ()); uint64_t block_count (0); size_t count (0); - std::deque epoch_open_blocks; + std::deque> epoch_open_blocks; { auto node_flags = nano::inactive_node_flag_defaults (); nano::update_flags (node_flags, vm); @@ -1819,12 +1819,11 @@ int main (int argc, char * const * argv) { std::cout << boost::str (boost::format ("%1% blocks retrieved") % count) << std::endl; } - nano::unchecked_info unchecked_info (block); - node.node->block_processor.add (unchecked_info); + node.node->block_processor.add (block); if (block->type () == nano::block_type::state && block->previous ().is_zero () && source_node->ledger.is_epoch_link (block->link ())) { // Epoch open blocks can be rejected without processed pending blocks to account, push it later again - epoch_open_blocks.push_back (unchecked_info); + epoch_open_blocks.push_back (block); } // Retrieving previous block hash hash = block->previous (); @@ -1839,9 +1838,9 @@ int main (int argc, char * const * argv) // Add epoch open blocks again if required if (node.node->block_processor.size () == 0) { - for (auto & unchecked_info : epoch_open_blocks) + for (auto & block : epoch_open_blocks) { - node.node->block_processor.add (unchecked_info); + node.node->block_processor.add (block); } } // Message each 60 seconds diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index 6ef728ca..071cf7b7 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -89,27 +89,18 @@ bool nano::block_processor::half_full () return size () >= node.flags.block_processor_full_size / 2; } -void nano::block_processor::add (std::shared_ptr const & block_a) -{ - nano::unchecked_info info (block_a); - add (info); -} - -void nano::block_processor::add (nano::unchecked_info const & info_a) +void nano::block_processor::add (std::shared_ptr const & block) { if (full ()) { node.stats.inc (nano::stat::type::blockprocessor, nano::stat::detail::overfill); return; } - if (node.network_params.work.validate_entry (*info_a.block)) // true => error + if (node.network_params.work.validate_entry (*block)) // true => error { node.stats.inc (nano::stat::type::blockprocessor, nano::stat::detail::insufficient_work); return; } - - auto const & block = info_a.block; - if (block->type () == nano::block_type::state || block->type () == nano::block_type::open) { state_block_signature_verification.add ({ block }); @@ -118,28 +109,12 @@ void nano::block_processor::add (nano::unchecked_info const & info_a) { { nano::lock_guard guard{ mutex }; - blocks.emplace_back (info_a); + blocks.emplace_back (block); } condition.notify_all (); } } -void nano::block_processor::add_local (nano::unchecked_info const & info_a) -{ - if (full ()) - { - node.stats.inc (nano::stat::type::blockprocessor, nano::stat::detail::overfill); - return; - } - if (node.network_params.work.validate_entry (*info_a.block)) // true => error - { - node.stats.inc (nano::stat::type::blockprocessor, nano::stat::detail::insufficient_work); - return; - } - - state_block_signature_verification.add ({ info_a.block }); -} - void nano::block_processor::force (std::shared_ptr const & block_a) { { @@ -252,27 +227,27 @@ void nano::block_processor::process_batch (nano::unique_lock & lock { node.logger.always_log (boost::str (boost::format ("%1% blocks (+ %2% state blocks) (+ %3% forced) in processing queue") % blocks.size () % state_block_signature_verification.size () % forced.size ())); } - nano::unchecked_info info; + std::shared_ptr block; nano::block_hash hash (0); bool force (false); if (forced.empty ()) { - info = blocks.front (); + block = blocks.front (); blocks.pop_front (); - hash = info.block->hash (); + hash = block->hash (); } else { - info = nano::unchecked_info (forced.front ()); + block = forced.front (); forced.pop_front (); - hash = info.block->hash (); + hash = block->hash (); force = true; number_of_forced_processed++; } lock_a.unlock (); if (force) { - auto successor (node.ledger.successor (transaction, info.block->qualified_root ())); + auto successor = node.ledger.successor (transaction, block->qualified_root ()); if (successor != nullptr && successor->hash () != hash) { // Replace our block with the winner and roll back any dependent blocks @@ -303,7 +278,7 @@ void nano::block_processor::process_batch (nano::unique_lock & lock } } number_of_blocks_processed++; - process_one (transaction, post_events, info, force); + process_one (transaction, post_events, block, force); lock_a.lock (); } awaiting_write = false; @@ -343,13 +318,12 @@ void nano::block_processor::process_live (nano::transaction const & transaction_ } } -nano::process_return nano::block_processor::process_one (nano::write_transaction const & transaction_a, block_post_events & events_a, nano::unchecked_info info_a, bool const forced_a, nano::block_origin const origin_a) +nano::process_return nano::block_processor::process_one (nano::write_transaction const & transaction_a, block_post_events & events_a, std::shared_ptr block, bool const forced_a, nano::block_origin const origin_a) { nano::process_return result; - auto block (info_a.block); auto hash (block->hash ()); result = node.ledger.process (transaction_a, *block); - events_a.events.emplace_back ([this, result, block = info_a.block] (nano::transaction const & tx) { + events_a.events.emplace_back ([this, result, block] (nano::transaction const & tx) { processed.notify (tx, result, *block); }); switch (result.code) @@ -362,7 +336,7 @@ nano::process_return nano::block_processor::process_one (nano::write_transaction block->serialize_json (block_string, node.config.logging.single_line_record ()); node.logger.try_log (boost::str (boost::format ("Processing block %1%: %2%") % hash.to_string () % block_string)); } - events_a.events.emplace_back ([this, hash, block = info_a.block, result, origin_a] (nano::transaction const & post_event_transaction_a) { + events_a.events.emplace_back ([this, hash, block, result, origin_a] (nano::transaction const & post_event_transaction_a) { process_live (post_event_transaction_a, hash, block, result, origin_a); }); queue_unchecked (transaction_a, hash); @@ -383,10 +357,7 @@ nano::process_return nano::block_processor::process_one (nano::write_transaction { node.logger.try_log (boost::str (boost::format ("Gap previous for: %1%") % hash.to_string ())); } - - debug_assert (info_a.modified () != 0); - node.unchecked.put (block->previous (), info_a); - + node.unchecked.put (block->previous (), block); events_a.events.emplace_back ([this, hash] (nano::transaction const & /* unused */) { this->node.gap_cache.add (hash); }); node.stats.inc (nano::stat::type::ledger, nano::stat::detail::gap_previous); break; @@ -397,10 +368,7 @@ nano::process_return nano::block_processor::process_one (nano::write_transaction { node.logger.try_log (boost::str (boost::format ("Gap source for: %1%") % hash.to_string ())); } - - debug_assert (info_a.modified () != 0); - node.unchecked.put (node.ledger.block_source (transaction_a, *(block)), info_a); - + node.unchecked.put (node.ledger.block_source (transaction_a, *block), block); events_a.events.emplace_back ([this, hash] (nano::transaction const & /* unused */) { this->node.gap_cache.add (hash); }); node.stats.inc (nano::stat::type::ledger, nano::stat::detail::gap_source); break; @@ -411,10 +379,7 @@ nano::process_return nano::block_processor::process_one (nano::write_transaction { node.logger.try_log (boost::str (boost::format ("Gap pending entries for epoch open: %1%") % hash.to_string ())); } - - debug_assert (info_a.modified () != 0); - node.unchecked.put (block->account (), info_a); // Specific unchecked key starting with epoch open block account public key - + node.unchecked.put (block->account (), block); // Specific unchecked key starting with epoch open block account public key node.stats.inc (nano::stat::type::ledger, nano::stat::detail::gap_source); break; } @@ -508,13 +473,6 @@ nano::process_return nano::block_processor::process_one (nano::write_transaction return result; } -nano::process_return nano::block_processor::process_one (nano::write_transaction const & transaction_a, block_post_events & events_a, std::shared_ptr const & block_a) -{ - nano::unchecked_info info (block_a); - auto result (process_one (transaction_a, events_a, info)); - return result; -} - void nano::block_processor::queue_unchecked (nano::write_transaction const & transaction_a, nano::hash_or_account const & hash_or_account_a) { node.unchecked.trigger (hash_or_account_a); diff --git a/nano/node/blockprocessor.hpp b/nano/node/blockprocessor.hpp index 5c38614e..9f0e863c 100644 --- a/nano/node/blockprocessor.hpp +++ b/nano/node/blockprocessor.hpp @@ -46,8 +46,6 @@ public: std::size_t size (); bool full (); bool half_full (); - void add_local (nano::unchecked_info const & info_a); - void add (nano::unchecked_info const &); void add (std::shared_ptr const &); void force (std::shared_ptr const &); void wait_write (); @@ -55,8 +53,8 @@ public: bool have_blocks_ready (); bool have_blocks (); void process_blocks (); - nano::process_return process_one (nano::write_transaction const &, block_post_events &, nano::unchecked_info, bool const = false, nano::block_origin const = nano::block_origin::remote); - nano::process_return process_one (nano::write_transaction const &, block_post_events &, std::shared_ptr const &); + nano::process_return process_one (nano::write_transaction const &, block_post_events &, std::shared_ptr block, bool const = false, nano::block_origin const = nano::block_origin::remote); + std::atomic flushing{ false }; // Delay required for average network propagartion before requesting confirmation static std::chrono::milliseconds constexpr confirmation_request_delay{ 1500 }; @@ -71,7 +69,7 @@ private: bool active{ false }; bool awaiting_write{ false }; std::chrono::steady_clock::time_point next_log; - std::deque blocks; + std::deque> blocks; std::deque> forced; nano::condition_variable condition; nano::node & node; diff --git a/nano/node/bootstrap/bootstrap_attempt.cpp b/nano/node/bootstrap/bootstrap_attempt.cpp index fe48c302..58990698 100644 --- a/nano/node/bootstrap/bootstrap_attempt.cpp +++ b/nano/node/bootstrap/bootstrap_attempt.cpp @@ -113,8 +113,7 @@ bool nano::bootstrap_attempt::process_block (std::shared_ptr const } else { - nano::unchecked_info info (block_a); - node->block_processor.add (info); + node->block_processor.add (block_a); } return stop_pull; } diff --git a/nano/node/bootstrap/bootstrap_lazy.cpp b/nano/node/bootstrap/bootstrap_lazy.cpp index 7bc5e5d1..0a0dec86 100644 --- a/nano/node/bootstrap/bootstrap_lazy.cpp +++ b/nano/node/bootstrap/bootstrap_lazy.cpp @@ -266,8 +266,7 @@ bool nano::bootstrap_attempt_lazy::process_block_lazy (std::shared_ptrblock_processor.add (info); + node->block_processor.add (block_a); } // Force drop lazy bootstrap connection for long bulk_pull if (pull_blocks_processed > max_blocks) diff --git a/nano/node/node.cpp b/nano/node/node.cpp index eca026cc..43ad4a53 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -208,7 +208,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, boost::filesystem::path co { unchecked.use_memory = [this] () { return ledger.bootstrap_weight_reached (); }; unchecked.satisfied = [this] (nano::unchecked_info const & info) { - this->block_processor.add (info); + this->block_processor.add (info.block); }; inactive_vote_cache.rep_weight_query = [this] (nano::account const & rep) { @@ -614,7 +614,7 @@ void nano::node::process_local_async (std::shared_ptr const & block // Add block hash as recently arrived to trigger automatic rebroadcast and election block_arrival.add (block_a->hash ()); // Set current time to trigger automatic rebroadcast and election - block_processor.add_local (block_a); + block_processor.add (block_a); } void nano::node::start ()