Skip to content

Commit

Permalink
Do not use expose election dummy vote in non election related unit te…
Browse files Browse the repository at this point in the history
…sts (#4098)

* Do not use expose election dummy vote in non election related unit tests

Election objects always have one dummy vote in them, they never have
zero votes. However that is an implementation detail of the election
class, which should not be leaked outside.

By using votes_with_weights, we filter out that dummy votes, which always
has zero weight and we do not need to leak out its existence.

It also makes those unit tests easier to reason about. The dummy vote is
often a confusing complication.

* Fixed race condition where genesis private key was given too early
  • Loading branch information
dsiganos committed Feb 3, 2023
1 parent 296e91b commit f029fb7
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3619,16 +3619,16 @@ TEST (node, rollback_vote_self)
// The write guard prevents the block processor from performing the rollback
auto write_guard = node.write_database_queue.wait (nano::writer::testing);

// Insert genesis key in the wallet
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);

ASSERT_EQ (1, election->votes ().size ());
ASSERT_EQ (0, election->votes_with_weight ().size ());
// Vote with key to switch the winner
election->vote (key.pub, 0, fork->hash ());
ASSERT_EQ (2, election->votes ().size ());
ASSERT_EQ (1, election->votes_with_weight ().size ());
// The winner changed
ASSERT_EQ (election->winner ()->hash (), fork->hash ());

// Insert genesis key in the wallet
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);

// Even without the rollback being finished, the aggregator must reply with a vote for the new winner, not the old one
ASSERT_TRUE (node.history.votes (send2->root (), send2->hash ()).empty ());
ASSERT_TRUE (node.history.votes (fork->root (), fork->hash ()).empty ());
Expand All @@ -3641,11 +3641,15 @@ TEST (node, rollback_vote_self)
}

// A vote is eventually generated from the local representative
ASSERT_TIMELY_EQ (5s, 3, election->votes ().size ());
auto votes = election->votes ();
auto vote = votes.find (nano::dev::genesis_key.pub);
ASSERT_NE (votes.end (), vote);
ASSERT_EQ (fork->hash (), vote->second.hash);
auto is_genesis_vote = [] (nano::vote_with_weight_info info) {
return info.representative == nano::dev::genesis_key.pub;
};
ASSERT_TIMELY_EQ (5s, 2, election->votes_with_weight ().size ());
auto votes_with_weight = election->votes_with_weight ();
ASSERT_EQ (1, std::count_if (votes_with_weight.begin (), votes_with_weight.end (), is_genesis_vote));
auto vote = std::find_if (votes_with_weight.begin (), votes_with_weight.end (), is_genesis_vote);
ASSERT_NE (votes_with_weight.end (), vote);
ASSERT_EQ (fork->hash (), vote->hash);
}

TEST (node, rollback_gap_source)
Expand Down

0 comments on commit f029fb7

Please sign in to comment.