Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 21, 2024
1 parent 03d02e6 commit f6bdf58
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
34 changes: 14 additions & 20 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,11 @@ void nano::active_transactions::trim ()

nano::election_insertion_result nano::active_transactions::insert (std::shared_ptr<nano::block> const & block_a, nano::election_behavior election_behavior_a)
{
nano::unique_lock<nano::mutex> lock{ mutex };
debug_assert (block_a);
debug_assert (block_a->has_sideband ());

nano::unique_lock<nano::mutex> lock{ mutex };

nano::election_insertion_result result;

if (stopped)
Expand Down Expand Up @@ -413,15 +415,16 @@ nano::election_insertion_result nano::active_transactions::insert (std::shared_p
result.election = existing->election;
}

lock.unlock (); // end of critical section
lock.unlock ();

if (result.inserted)
{
release_assert (result.election);

if (auto const cache = node.vote_cache.find (hash); cache)
auto cached = node.vote_cache.find (hash);
for (auto const & cached_vote : cached)
{
cache->fill (result.election);
vote (cached_vote);
}

node.observers.active_started.notify (hash);
Expand All @@ -433,7 +436,9 @@ nano::election_insertion_result nano::active_transactions::insert (std::shared_p
{
result.election->broadcast_vote ();
}

trim ();

return result;
}

Expand Down Expand Up @@ -469,12 +474,6 @@ std::unordered_map<nano::block_hash, nano::vote_code> nano::active_transactions:
}
}

// Process inactive votes outside of the critical section
for (auto & hash : inactive)
{
add_vote_cache (hash, vote);
}

if (!process.empty ())
{
bool processed = false;
Expand Down Expand Up @@ -609,24 +608,19 @@ bool nano::active_transactions::publish (std::shared_ptr<nano::block> const & bl
lock.lock ();
blocks.emplace (block_a->hash (), election);
lock.unlock ();
if (auto const cache = node.vote_cache.find (block_a->hash ()); cache)

auto cached = node.vote_cache.find (block_a->hash ());
for (auto const & cached_vote : cached)
{
cache->fill (election);
vote (cached_vote);
}

node.stats.inc (nano::stat::type::active, nano::stat::detail::election_block_conflict);
}
}
return result;
}

void nano::active_transactions::add_vote_cache (nano::block_hash const & hash, std::shared_ptr<nano::vote> const vote)
{
if (node.ledger.weight (vote->account) > node.minimum_principal_weight ())
{
node.vote_cache.vote (hash, vote);
}
}

std::size_t nano::active_transactions::election_winner_details_size ()
{
nano::lock_guard<nano::mutex> guard{ election_winner_details_mutex };
Expand Down
5 changes: 0 additions & 5 deletions nano/node/active_transactions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ class active_transactions final
nano::stat::type completion_type (nano::election const & election) const;
// Returns a list of elections sorted by difficulty, mutex must be locked
std::vector<std::shared_ptr<nano::election>> list_active_impl (std::size_t) const;
/**
* Checks if vote passes minimum representative weight threshold and adds it to inactive vote cache
* TODO: Should be moved to `vote_cache` class
*/
void add_vote_cache (nano::block_hash const & hash, std::shared_ptr<nano::vote> vote);
void activate_successors (nano::store::read_transaction const & transaction, std::shared_ptr<nano::block> const & block);
void notify_observers (nano::store::read_transaction const & transaction, nano::election_status const & status, std::vector<nano::vote_with_weight_info> const & votes);

Expand Down
13 changes: 12 additions & 1 deletion nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,11 +644,22 @@ bool nano::election::replace_by_weight (nano::unique_lock<nano::mutex> & lock_a,
sorted.reserve (last_tally.size ());
std::copy (last_tally.begin (), last_tally.end (), std::back_inserter (sorted));
lock_a.unlock ();

// Sort in ascending order
std::sort (sorted.begin (), sorted.end (), [] (auto const & left, auto const & right) { return left.second < right.second; });

auto votes_tally = [this] (std::vector<std::shared_ptr<nano::vote>> const & votes) {
nano::uint128_t result{ 0 };
for (auto const & vote : votes)
{
result += node.ledger.weight (vote->account);
}
return result;
};

// Replace if lowest tally is below inactive cache new block weight
auto inactive_existing = node.vote_cache.find (hash_a);
auto inactive_tally = inactive_existing ? inactive_existing->tally () : 0;
auto inactive_tally = votes_tally (inactive_existing);
if (inactive_tally > 0 && sorted.size () < max_blocks)
{
// If count of tally items is less than 10, remove any block without tally
Expand Down
10 changes: 4 additions & 6 deletions nano/node/vote_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <nano/secure/common.hpp>
#include <nano/secure/ledger.hpp>

#include <boost/format.hpp>

#include <chrono>

using namespace std::chrono_literals;
Expand Down Expand Up @@ -161,12 +159,12 @@ void nano::vote_processor::verify_votes (decltype (votes) const & votes_a)
}
}

nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr<nano::vote> const & vote_a, std::shared_ptr<nano::transport::channel> const & channel_a, bool validated)
nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr<nano::vote> const & vote, std::shared_ptr<nano::transport::channel> const & channel, bool validated)
{
auto result = nano::vote_code::invalid;
if (validated || !vote_a->validate ())
if (validated || !vote->validate ())
{
auto vote_results = active.vote (vote_a);
auto vote_results = active.vote (vote);

// Aggregate results for individual hashes
bool replay = false;
Expand All @@ -184,7 +182,7 @@ nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr<nano::vote>
stats.inc (nano::stat::type::vote, to_stat_detail (result));

logger.trace (nano::log::type::vote_processor, nano::log::detail::vote_processed,
nano::log::arg{ "vote", vote_a },
nano::log::arg{ "vote", vote },
nano::log::arg{ "result", result });

return result;
Expand Down

0 comments on commit f6bdf58

Please sign in to comment.