Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List of rolled back blocks in block_processor #1581

Merged
merged 4 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,8 +1226,9 @@ void nano::block_processor::add (std::shared_ptr<nano::block> block_a, std::chro
if (!nano::work_validate (block_a->root (), block_a->block_work ()))
{
{
auto hash (block_a->hash ());
std::lock_guard<std::mutex> lock (mutex);
if (blocks_hashes.find (block_a->hash ()) == blocks_hashes.end ())
if (blocks_hashes.find (hash) == blocks_hashes.end () && rolled_back.get<1> ().find (hash) == rolled_back.get<1> ().end ())
{
if (block_a->type () == nano::block_type::state && !node.ledger.is_epoch_link (block_a->link ()))
{
Expand All @@ -1237,7 +1238,7 @@ void nano::block_processor::add (std::shared_ptr<nano::block> block_a, std::chro
{
blocks.push_back (std::make_pair (block_a, origination));
}
blocks_hashes.insert (block_a->hash ());
blocks_hashes.insert (hash);
}
condition.notify_all ();
}
Expand Down Expand Up @@ -1425,6 +1426,20 @@ void nano::block_processor::process_batch (std::unique_lock<std::mutex> & lock_a
// Replace our block with the winner and roll back any dependent blocks
BOOST_LOG (node.log) << boost::str (boost::format ("Rolling back %1% and replacing with %2%") % successor->hash ().to_string () % hash.to_string ());
node.ledger.rollback (transaction, successor->hash ());
lock_a.lock ();
// Prevent rolled back blocks second insertion
auto inserted (rolled_back.insert (nano::rolled_hash{ std::chrono::steady_clock::now (), successor->hash () }));
if (inserted.second)
{
// Possible election winner change
rolled_back.get<1> ().erase (hash);
// Prevent overflow
if (rolled_back.size () > rolled_back_max)
{
rolled_back.erase (rolled_back.begin ());
SergiySW marked this conversation as resolved.
Show resolved Hide resolved
}
}
lock_a.unlock ();
}
}
/* Forced state blocks are not validated in verify_state_blocks () function
Expand Down
13 changes: 13 additions & 0 deletions nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ class signature_checker
std::condition_variable condition;
std::thread thread;
};
class rolled_hash
{
public:
std::chrono::steady_clock::time_point time;
nano::block_hash hash;
};
// Processing blocks is a potentially long IO operation
// This class isolates block insertion from other operations like servicing network operations
class block_processor
Expand Down Expand Up @@ -438,6 +444,13 @@ class block_processor
std::deque<std::pair<std::shared_ptr<nano::block>, std::chrono::steady_clock::time_point>> blocks;
std::unordered_set<nano::block_hash> blocks_hashes;
std::deque<std::shared_ptr<nano::block>> forced;
boost::multi_index_container<
nano::rolled_hash,
boost::multi_index::indexed_by<
boost::multi_index::ordered_non_unique<boost::multi_index::member<nano::rolled_hash, std::chrono::steady_clock::time_point, &nano::rolled_hash::time>>,
boost::multi_index::hashed_unique<boost::multi_index::member<nano::rolled_hash, nano::block_hash, &nano::rolled_hash::hash>>>>
rolled_back;
static size_t const rolled_back_max = 1024;
std::condition_variable condition;
nano::node & node;
nano::vote_generator generator;
Expand Down