Skip to content

Commit

Permalink
Extract rollback_competitor function and simplify proces_batch. (#4252)
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu authored Jun 25, 2023
1 parent 54593ee commit c7aa465
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
64 changes: 35 additions & 29 deletions nano/node/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,40 @@ std::optional<nano::process_return> nano::block_processor::add_blocking (std::sh
return result;
}

void nano::block_processor::rollback_competitor (nano::write_transaction const & transaction, nano::block const & block)
{
auto hash = block.hash ();
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
if (node.config.logging.ledger_rollback_logging ())
{
node.logger.always_log (boost::str (boost::format ("Rolling back %1% and replacing with %2%") % successor->hash ().to_string () % hash.to_string ()));
}
std::vector<std::shared_ptr<nano::block>> rollback_list;
if (node.ledger.rollback (transaction, successor->hash (), rollback_list))
{
node.stats.inc (nano::stat::type::ledger, nano::stat::detail::rollback_failed);
node.logger.always_log (nano::severity_level::error, boost::str (boost::format ("Failed to roll back %1% because it or a successor was confirmed") % successor->hash ().to_string ()));
}
else if (node.config.logging.ledger_rollback_logging ())
{
node.logger.always_log (boost::str (boost::format ("%1% blocks rolled back") % rollback_list.size ()));
}
// Deleting from votes cache, stop active transaction
for (auto & i : rollback_list)
{
node.history.erase (i->root ());
// Stop all rolled back active transactions except initial
if (i->hash () != successor->hash ())
{
node.active.erase (*i);
}
}
}
}

void nano::block_processor::force (std::shared_ptr<nano::block> const & block_a)
{
{
Expand Down Expand Up @@ -266,35 +300,7 @@ auto nano::block_processor::process_batch (nano::unique_lock<nano::mutex> & lock
lock_a.unlock ();
if (force)
{
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
if (node.config.logging.ledger_rollback_logging ())
{
node.logger.always_log (boost::str (boost::format ("Rolling back %1% and replacing with %2%") % successor->hash ().to_string () % hash.to_string ()));
}
std::vector<std::shared_ptr<nano::block>> rollback_list;
if (node.ledger.rollback (transaction, successor->hash (), rollback_list))
{
node.stats.inc (nano::stat::type::ledger, nano::stat::detail::rollback_failed);
node.logger.always_log (nano::severity_level::error, boost::str (boost::format ("Failed to roll back %1% because it or a successor was confirmed") % successor->hash ().to_string ()));
}
else if (node.config.logging.ledger_rollback_logging ())
{
node.logger.always_log (boost::str (boost::format ("%1% blocks rolled back") % rollback_list.size ()));
}
// Deleting from votes cache, stop active transaction
for (auto & i : rollback_list)
{
node.history.erase (i->root ());
// Stop all rolled back active transactions except initial
if (i->hash () != successor->hash ())
{
node.active.erase (*i);
}
}
}
rollback_competitor (transaction, *block);
}
number_of_blocks_processed++;
auto result = process_one (transaction, block, force);
Expand Down
2 changes: 2 additions & 0 deletions nano/node/blockprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class block_processor final
blocking_observer blocking;

private:
// Roll back block in the ledger that conflicts with 'block'
void rollback_competitor (nano::write_transaction const & transaction, nano::block const & block);
nano::process_return process_one (nano::write_transaction const &, std::shared_ptr<nano::block> block, bool const = false);
void queue_unchecked (nano::write_transaction const &, nano::hash_or_account const &);
std::deque<processed_t> process_batch (nano::unique_lock<nano::mutex> &);
Expand Down

0 comments on commit c7aa465

Please sign in to comment.