Skip to content

Commit

Permalink
Minimize work validation calls (#2577)
Browse files Browse the repository at this point in the history
With this PR there should only be one work_validate call per block until it is processed (then more are done to get the difficulty, a future PR will also remove the need for those).
  • Loading branch information
guilhermelawless authored Feb 26, 2020
1 parent 6db4625 commit 75b2d2e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
4 changes: 3 additions & 1 deletion nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,9 @@ std::pair<std::shared_ptr<nano::election>, bool> nano::active_transactions::inse
auto hash (block_a->hash ());
result.first = nano::make_shared<nano::election> (node, block_a, skip_delay_a, confirmation_action_a);
uint64_t difficulty (0);
release_assert (!nano::work_validate (*block_a, &difficulty));
auto error (nano::work_validate (*block_a, &difficulty));
(void)error;
debug_assert (!error);
roots.get<tag_root> ().emplace (nano::conflict_info{ root, difficulty, difficulty, result.first });
blocks.emplace (hash, result.first);
adjust_difficulty (hash);
Expand Down
33 changes: 13 additions & 20 deletions nano/node/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,25 @@ void nano::block_processor::add (std::shared_ptr<nano::block> block_a, uint64_t

void nano::block_processor::add (nano::unchecked_info const & info_a)
{
if (!nano::work_validate (*info_a.block))
debug_assert (!nano::work_validate (*info_a.block));
{
auto hash (info_a.block->hash ());
auto filter_hash (filter_item (hash, info_a.block->block_signature ()));
nano::lock_guard<std::mutex> lock (mutex);
if (blocks_filter.find (filter_hash) == blocks_filter.end ())
{
auto hash (info_a.block->hash ());
auto filter_hash (filter_item (hash, info_a.block->block_signature ()));
nano::lock_guard<std::mutex> lock (mutex);
if (blocks_filter.find (filter_hash) == blocks_filter.end ())
if (info_a.verified == nano::signature_verification::unknown && (info_a.block->type () == nano::block_type::state || info_a.block->type () == nano::block_type::open || !info_a.account.is_zero ()))
{
if (info_a.verified == nano::signature_verification::unknown && (info_a.block->type () == nano::block_type::state || info_a.block->type () == nano::block_type::open || !info_a.account.is_zero ()))
{
state_blocks.push_back (info_a);
}
else
{
blocks.push_back (info_a);
}
blocks_filter.insert (filter_hash);
state_blocks.push_back (info_a);
}
else
{
blocks.push_back (info_a);
}
blocks_filter.insert (filter_hash);
}
condition.notify_all ();
}
else
{
node.logger.try_log ("nano::block_processor::add called for hash ", info_a.block->hash ().to_string (), " with invalid work ", nano::to_string_hex (info_a.block->block_work ()));
debug_assert (false && "nano::block_processor::add called with invalid work");
}
condition.notify_all ();
}

void nano::block_processor::force (std::shared_ptr<nano::block> block_a)
Expand Down
10 changes: 9 additions & 1 deletion nano/qt/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,15 @@ wallet (wallet_a)
{
show_label_ok (*status);
this->status->setText ("");
this->wallet.node.process_active (std::move (block_l));
if (!nano::work_validate (*block_l))
{
this->wallet.node.process_active (std::move (block_l));
}
else
{
show_label_error (*status);
this->status->setText ("Invalid work");
}
}
else
{
Expand Down

0 comments on commit 75b2d2e

Please sign in to comment.