From 34d5152cf686028f3ba73c154c578ca25b806bf2 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Tue, 28 Jan 2020 13:58:56 +0000 Subject: [PATCH 1/4] Modifying update_difficulty test so it doesn't directly manipulate the election and instead relies on active_transactions updating itself on receipt of a higher difficulty block. --- nano/core_test/active_transactions.cpp | 15 --------------- nano/node/active_transactions.cpp | 7 ++++++- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/nano/core_test/active_transactions.cpp b/nano/core_test/active_transactions.cpp index 34936344f4..3931d04e26 100644 --- a/nano/core_test/active_transactions.cpp +++ b/nano/core_test/active_transactions.cpp @@ -548,21 +548,6 @@ TEST (active_transactions, update_difficulty) send2 = std::shared_ptr (builder1.from (*send2).work (*work2).build (ec)); ASSERT_FALSE (ec); - auto modify_election = [&node1](auto block) { - auto hash (block->hash ()); - nano::lock_guard active_guard (node1.active.mutex); - auto existing (node1.active.roots.find (block->qualified_root ())); - ASSERT_NE (existing, node1.active.roots.end ()); - auto election (existing->election); - ASSERT_EQ (election->status.winner->hash (), hash); - election->status.winner = block; - auto current (election->blocks.find (hash)); - assert (current != election->blocks.end ()); - current->second = block; - }; - - modify_election (send1); - modify_election (send2); node1.process_active (send1); node1.process_active (send2); node1.block_processor.flush (); diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index 76cbad8b6e..9e00d79f28 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -632,8 +632,13 @@ void nano::active_transactions::update_difficulty (std::shared_ptr { node.logger.try_log (boost::str (boost::format ("Block %1% was updated from difficulty %2% to %3%") % block_a->hash ().to_string () % nano::to_string_hex (existing_election->difficulty) % nano::to_string_hex (difficulty))); } - roots.get ().modify (existing_election, [difficulty](nano::conflict_info & info_a) { + roots.get ().modify (existing_election, [election = existing_election->election, &block_a, difficulty](nano::conflict_info & info_a) { info_a.difficulty = difficulty; + election->blocks[block_a->hash ()] = block_a; + if (election->status.winner->hash () == block_a->hash ()) + { + election->status.winner = block_a; + } }); adjust_difficulty (block_a->hash ()); } From 945c8ca3a12b331fbada1217e52ff8b7f0eb13bb Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 30 Jan 2020 11:35:53 +0000 Subject: [PATCH 2/4] Using election attached to iterator. --- nano/node/active_transactions.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index 9e00d79f28..d9da6fdb4e 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -632,12 +632,12 @@ void nano::active_transactions::update_difficulty (std::shared_ptr { node.logger.try_log (boost::str (boost::format ("Block %1% was updated from difficulty %2% to %3%") % block_a->hash ().to_string () % nano::to_string_hex (existing_election->difficulty) % nano::to_string_hex (difficulty))); } - roots.get ().modify (existing_election, [election = existing_election->election, &block_a, difficulty](nano::conflict_info & info_a) { + roots.get ().modify (existing_election, [&block_a, difficulty](nano::conflict_info & info_a) { info_a.difficulty = difficulty; - election->blocks[block_a->hash ()] = block_a; - if (election->status.winner->hash () == block_a->hash ()) + info_a.election->blocks[block_a->hash ()] = block_a; + if (info_a.election->status.winner->hash () == block_a->hash ()) { - election->status.winner = block_a; + info_a.election->status.winner = block_a; } }); adjust_difficulty (block_a->hash ()); From c0a09fa0e9242e9f32b6d8a913c1d35ce9a4882c Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 30 Jan 2020 15:16:55 +0000 Subject: [PATCH 3/4] Updating election fields inside election class. --- nano/node/active_transactions.cpp | 8 ++------ nano/node/election.cpp | 8 +++++++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index d9da6fdb4e..c32dc0dbfc 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -632,14 +632,10 @@ void nano::active_transactions::update_difficulty (std::shared_ptr { node.logger.try_log (boost::str (boost::format ("Block %1% was updated from difficulty %2% to %3%") % block_a->hash ().to_string () % nano::to_string_hex (existing_election->difficulty) % nano::to_string_hex (difficulty))); } - roots.get ().modify (existing_election, [&block_a, difficulty](nano::conflict_info & info_a) { + roots.get ().modify (existing_election, [difficulty](nano::conflict_info & info_a) { info_a.difficulty = difficulty; - info_a.election->blocks[block_a->hash ()] = block_a; - if (info_a.election->status.winner->hash () == block_a->hash ()) - { - info_a.election->status.winner = block_a; - } }); + existing_election->election->publish (block_a); adjust_difficulty (block_a->hash ()); } } diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 028b579edc..7794bbf417 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -211,7 +211,8 @@ bool nano::election::publish (std::shared_ptr block_a) } if (!result) { - if (blocks.find (block_a->hash ()) == blocks.end ()) + auto existing = blocks.find (block_a->hash ()); + if (existing == blocks.end ()) { blocks.emplace (std::make_pair (block_a->hash (), block_a)); insert_inactive_votes_cache (block_a->hash ()); @@ -221,6 +222,11 @@ bool nano::election::publish (std::shared_ptr block_a) else { result = true; + existing->second = block_a; + if (status.winner->hash () == block_a->hash ()) + { + status.winner = block_a; + } } } return result; From 089862cb3fe66216b87c9a7118f8bee058124bf3 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Fri, 31 Jan 2020 11:27:42 +0000 Subject: [PATCH 4/4] Removing redundant code section. --- nano/node/wallet.cpp | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 3e3dc9ae5d..2de350668e 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -1455,23 +1455,6 @@ void nano::work_watcher::watching (nano::qualified_root const & root_a, std::sha if (!ec) { - { - auto hash (block_a->hash ()); - nano::lock_guard active_guard (watcher_l->node.active.mutex); - auto existing (watcher_l->node.active.roots.find (root_a)); - if (existing != watcher_l->node.active.roots.end ()) - { - auto election (existing->election); - if (election->status.winner->hash () == hash) - { - election->status.winner = block; - } - auto current (election->blocks.find (hash)); - assert (current != election->blocks.end ()); - current->second = block; - } - } - watcher_l->node.network.flood_block (block, false); watcher_l->node.active.update_difficulty (block); watcher_l->update (root_a, block); updated_l = true;