From 75b2d2ea8f4752462409ad261924403285f7102e Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Wed, 26 Feb 2020 14:45:57 +0000 Subject: [PATCH] Minimize work validation calls (#2577) 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). --- nano/node/active_transactions.cpp | 4 +++- nano/node/blockprocessor.cpp | 33 ++++++++++++------------------- nano/qt/qt.cpp | 10 +++++++++- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index be61400ed4..7161cfcb14 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -580,7 +580,9 @@ std::pair, bool> nano::active_transactions::inse auto hash (block_a->hash ()); result.first = nano::make_shared (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 ().emplace (nano::conflict_info{ root, difficulty, difficulty, result.first }); blocks.emplace (hash, result.first); adjust_difficulty (hash); diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index b3c19f99bc..d7ec060f5a 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -67,32 +67,25 @@ void nano::block_processor::add (std::shared_ptr 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 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 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 block_a) diff --git a/nano/qt/qt.cpp b/nano/qt/qt.cpp index 4813d71821..60cac071ec 100644 --- a/nano/qt/qt.cpp +++ b/nano/qt/qt.cpp @@ -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 {