Skip to content

Commit

Permalink
Simplify testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 8, 2024
1 parent 83d0962 commit 9e815ef
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
22 changes: 4 additions & 18 deletions nano/core_test/vote_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ TEST (vote_processor, overflow)
ASSERT_LT (std::chrono::system_clock::now () - start_time, 10s);
}

namespace nano
{
TEST (vote_processor, weights)
{
nano::test::system system (4);
Expand Down Expand Up @@ -158,22 +156,10 @@ TEST (vote_processor, weights)
ASSERT_TIMELY_EQ (5s, node.online_reps.online (), total);
node.vote_processor.calculate_weights ();

ASSERT_EQ (node.vote_processor.representatives_1.end (), node.vote_processor.representatives_1.find (key0.pub));
ASSERT_EQ (node.vote_processor.representatives_2.end (), node.vote_processor.representatives_2.find (key0.pub));
ASSERT_EQ (node.vote_processor.representatives_3.end (), node.vote_processor.representatives_3.find (key0.pub));

ASSERT_NE (node.vote_processor.representatives_1.end (), node.vote_processor.representatives_1.find (key1.pub));
ASSERT_EQ (node.vote_processor.representatives_2.end (), node.vote_processor.representatives_2.find (key1.pub));
ASSERT_EQ (node.vote_processor.representatives_3.end (), node.vote_processor.representatives_3.find (key1.pub));

ASSERT_NE (node.vote_processor.representatives_1.end (), node.vote_processor.representatives_1.find (key2.pub));
ASSERT_NE (node.vote_processor.representatives_2.end (), node.vote_processor.representatives_2.find (key2.pub));
ASSERT_EQ (node.vote_processor.representatives_3.end (), node.vote_processor.representatives_3.find (key2.pub));

ASSERT_NE (node.vote_processor.representatives_1.end (), node.vote_processor.representatives_1.find (nano::dev::genesis_key.pub));
ASSERT_NE (node.vote_processor.representatives_2.end (), node.vote_processor.representatives_2.find (nano::dev::genesis_key.pub));
ASSERT_NE (node.vote_processor.representatives_3.end (), node.vote_processor.representatives_3.find (nano::dev::genesis_key.pub));
}
ASSERT_EQ (node.vote_processor.representative_tier (key0.pub), nano::representative_tier::none);
ASSERT_EQ (node.vote_processor.representative_tier (key1.pub), nano::representative_tier::tier_1);
ASSERT_EQ (node.vote_processor.representative_tier (key2.pub), nano::representative_tier::tier_2);
ASSERT_EQ (node.vote_processor.representative_tier (nano::dev::genesis_key.pub), nano::representative_tier::tier_3);
}

// Issue that tracks last changes on this test: https://github.com/nanocurrency/nano-node/issues/3485
Expand Down
26 changes: 17 additions & 9 deletions nano/node/vote_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,29 @@ void nano::vote_processor::run ()
}
}

auto nano::vote_processor::representative_tier (const nano::account & representative) const -> rep_tier
nano::representative_tier nano::vote_processor::representative_tier (const nano::account & representative) const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return representative_tier_locked (representative);
}

nano::representative_tier nano::vote_processor::representative_tier_locked (const nano::account & representative) const
{
debug_assert (!mutex.try_lock ());

if (representatives_3.find (representative) != representatives_3.end ())
{
return rep_tier::tier_3;
return nano::representative_tier::tier_3;
}
if (representatives_2.find (representative) != representatives_2.end ())
{
return rep_tier::tier_2;
return nano::representative_tier::tier_2;
}
if (representatives_1.find (representative) != representatives_1.end ())
{
return rep_tier::tier_1;
return nano::representative_tier::tier_1;
}
return rep_tier::tier_none;
return nano::representative_tier::none;
}

bool nano::vote_processor::vote (std::shared_ptr<nano::vote> const & vote_a, std::shared_ptr<nano::transport::channel> const & channel_a)
Expand All @@ -128,7 +136,7 @@ bool nano::vote_processor::vote (std::shared_ptr<nano::vote> const & vote_a, std
nano::unique_lock<nano::mutex> lock{ mutex };
if (!stopped)
{
auto tier = representative_tier (vote_a->account);
auto tier = representative_tier_locked (vote_a->account);

// Level 0 (< 0.1%)
if (votes.size () < 6.0 / 9.0 * max_votes)
Expand All @@ -138,17 +146,17 @@ bool nano::vote_processor::vote (std::shared_ptr<nano::vote> const & vote_a, std
// Level 1 (0.1-1%)
else if (votes.size () < 7.0 / 9.0 * max_votes)
{
process = (tier == rep_tier::tier_1);
process = (tier == nano::representative_tier::tier_1);
}
// Level 2 (1-5%)
else if (votes.size () < 8.0 / 9.0 * max_votes)
{
process = (tier == rep_tier::tier_2);
process = (tier == nano::representative_tier::tier_2);
}
// Level 3 (> 5%)
else if (votes.size () < max_votes)
{
process = (tier == rep_tier::tier_3);
process = (tier == nano::representative_tier::tier_3);
}
if (process)
{
Expand Down
25 changes: 12 additions & 13 deletions nano/node/vote_processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ namespace transport
class channel;
}

// Higher number means higher priority
enum class representative_tier
{
none, // Not a principal representative
tier_1, // (0.1-1%) of online stake
tier_2, // (1-5%) of online stake
tier_3, // (> 5%) of online stake
};

class vote_processor final
{
public:
Expand Down Expand Up @@ -60,6 +69,8 @@ class vote_processor final
bool empty () const;
void calculate_weights ();

nano::representative_tier representative_tier (nano::account const & representative) const;

std::atomic<uint64_t> total_processed{ 0 };

private: // Dependencies
Expand All @@ -76,18 +87,7 @@ class vote_processor final
private:
void run ();
void verify_votes (std::deque<std::pair<std::shared_ptr<nano::vote>, std::shared_ptr<nano::transport::channel>>> const &);

private:
// Higher number means higher priority
enum class rep_tier
{
tier_none, // Not a principal representative
tier_1, // (0.1-1%)
tier_2, // (1-5%)
tier_3, // (> 5%)
};

rep_tier representative_tier (nano::account const & representative) const;
nano::representative_tier representative_tier_locked (nano::account const & representative) const;

private:
std::size_t const max_votes;
Expand All @@ -105,7 +105,6 @@ class vote_processor final
std::thread thread;

friend std::unique_ptr<container_info_component> collect_container_info (vote_processor & vote_processor, std::string const & name);
friend class vote_processor_weights_Test;
};

std::unique_ptr<container_info_component> collect_container_info (vote_processor & vote_processor, std::string const & name);
Expand Down

0 comments on commit 9e815ef

Please sign in to comment.