Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 20, 2024
1 parent 46114c5 commit 331d210
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
27 changes: 9 additions & 18 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,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 @@ -450,15 +452,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 @@ -470,7 +473,9 @@ nano::election_insertion_result nano::active_transactions::insert (std::shared_p
{
result.election->broadcast_vote ();
}

trim ();

return result;
}

Expand Down Expand Up @@ -504,12 +509,6 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr<nano::vote> con
}
}

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

if (!process.empty ())
{
bool replay = false;
Expand Down Expand Up @@ -680,14 +679,6 @@ boost::optional<nano::election_status_type> nano::active_transactions::confirm_b
return status_type;
}

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 @@ -190,11 +190,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);
boost::optional<nano::election_status_type> election_status (std::shared_ptr<nano::block> const & block);
void process_inactive_confirmation (nano::store::read_transaction const & transaction, std::shared_ptr<nano::block> const & block);
void process_active_confirmation (nano::store::read_transaction const & transaction, std::shared_ptr<nano::block> const & block, nano::election_status_type status);
Expand Down
11 changes: 4 additions & 7 deletions nano/node/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,8 @@ nano::vote_cache::vote_cache (vote_cache_config const & config_a, nano::stats &
{
}

void nano::vote_cache::vote (std::shared_ptr<nano::vote> const & vote, std::unordered_set<nano::block_hash> const & filter)
void nano::vote_cache::vote (std::shared_ptr<nano::vote> const & vote, std::function<bool (nano::block_hash const &)> const & filter)
{
// Assert that supplied hash corresponds to a one of the hashes stored in vote
debug_assert (std::find (vote->hashes.begin (), vote->hashes.end (), hash) != vote->hashes.end ());

auto const representative = vote->account;
auto const timestamp = vote->timestamp ();
auto const rep_weight = rep_weight_query (representative);
Expand All @@ -140,7 +137,7 @@ void nano::vote_cache::vote (std::shared_ptr<nano::vote> const & vote, std::unor

for (auto const & hash : vote->hashes)
{
if (!filter.contains (hash))
if (!filter (hash))
{
continue;
}
Expand Down Expand Up @@ -182,14 +179,14 @@ std::size_t nano::vote_cache::size () const
return cache.size ();
}

std::optional<nano::vote_cache::entry> nano::vote_cache::find (const nano::block_hash & hash) const
std::vector<std::shared_ptr<nano::vote>> nano::vote_cache::find (const nano::block_hash & hash) const
{
nano::lock_guard<nano::mutex> lock{ mutex };

auto & cache_by_hash = cache.get<tag_hash> ();
if (auto existing = cache_by_hash.find (hash); existing != cache_by_hash.end ())
{
return *existing;
return existing->votes ();
}
return {};
}
Expand Down
4 changes: 2 additions & 2 deletions nano/node/vote_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ class vote_cache final
/**
* Adds a new vote to cache
*/
void vote (std::shared_ptr<nano::vote> const & vote, std::unordered_set<nano::block_hash> const & filter);
void vote (std::shared_ptr<nano::vote> const & vote, std::function<bool (nano::block_hash const &)> const & filter);

/**
* Tries to find an entry associated with block hash
*/
std::optional<entry> find (nano::block_hash const & hash) const;
std::vector<std::shared_ptr<nano::vote>> find (nano::block_hash const & hash) const;

/**
* Removes an entry associated with block hash, does nothing if entry does not exist
Expand Down
14 changes: 12 additions & 2 deletions nano/node/vote_processor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <nano/lib/stats.hpp>
#include <nano/lib/timer.hpp>
#include <nano/node/active_transactions.hpp>
#include <nano/node/node.hpp>
#include <nano/node/node_observers.hpp>
#include <nano/node/nodeconfig.hpp>
#include <nano/node/online_reps.hpp>
Expand All @@ -10,8 +11,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 @@ -167,8 +166,19 @@ nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr<nano::vote>
if (validated || !vote_a->validate ())
{
result = active.vote (vote_a);

if (result != nano::vote_code::replay)
{

}
if (ledger.weight (vote_a->account) > node.minimum_principal_weight ())
{
node.vote_cache.vote (vote_a);
}

observers.vote.notify (vote_a, channel_a, result);
}

std::string status;
switch (result)
{
Expand Down
1 change: 1 addition & 0 deletions nano/node/vote_processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class vote_processor final
std::atomic<uint64_t> total_processed{ 0 };

private: // Dependencies
nano::node & node;
nano::active_transactions & active;
nano::node_observers & observers;
nano::stats & stats;
Expand Down

0 comments on commit 331d210

Please sign in to comment.