Using a less-than operator instead of greater-than and using vote_info instead of a tuple.

This commit is contained in:
clemahieu 2018-05-06 10:54:34 +01:00
commit 6c82496b51
3 changed files with 23 additions and 10 deletions

View file

@ -853,18 +853,18 @@ TEST (votes, add_existing)
auto votes1 (node1.active.roots.find (send1->root ())->election);
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
votes1->vote (vote1);
ASSERT_EQ (1, votes1->last_votes[rai::test_genesis_key.pub].second);
ASSERT_EQ (1, votes1->last_votes[rai::test_genesis_key.pub].sequence);
rai::keypair key2;
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send2));
// Pretend we've waited the timeout
votes1->last_votes[rai::test_genesis_key.pub].first = std::chrono::steady_clock::now () - std::chrono::seconds (20);
votes1->last_votes[rai::test_genesis_key.pub].time = std::chrono::steady_clock::now () - std::chrono::seconds (20);
votes1->vote (vote2);
ASSERT_EQ (2, votes1->last_votes[rai::test_genesis_key.pub].second);
ASSERT_EQ (2, votes1->last_votes[rai::test_genesis_key.pub].sequence);
// Also resend the old vote, and see if we respect the sequence number
votes1->last_votes[rai::test_genesis_key.pub].first = std::chrono::steady_clock::now () - std::chrono::seconds (20);
votes1->last_votes[rai::test_genesis_key.pub].time = std::chrono::steady_clock::now () - std::chrono::seconds (20);
votes1->vote (vote1);
ASSERT_EQ (2, votes1->last_votes[rai::test_genesis_key.pub].second);
ASSERT_EQ (2, votes1->last_votes[rai::test_genesis_key.pub].sequence);
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (*send2, *votes1->votes.rep_votes[rai::test_genesis_key.pub]);
@ -893,7 +893,7 @@ TEST (votes, add_old)
rai::keypair key2;
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send2));
votes1->last_votes[rai::test_genesis_key.pub].first = std::chrono::steady_clock::now () - std::chrono::seconds (20);
votes1->last_votes[rai::test_genesis_key.pub].time = std::chrono::steady_clock::now () - std::chrono::seconds (20);
node1.vote_processor.vote (vote2, rai::endpoint ());
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));

View file

@ -3027,6 +3027,11 @@ std::shared_ptr<rai::node> rai::node::shared ()
return shared_from_this ();
}
bool rai::vote_info::operator < (rai::vote const & vote_a) const
{
return sequence < vote_a.sequence || (sequence == vote_a.sequence && hash < vote_a.block->hash ());
}
rai::election::election (rai::node & node_a, std::shared_ptr<rai::block> block_a, std::function<void(std::shared_ptr<rai::block>)> const & confirmation_action_a) :
confirmation_action (confirmation_action_a),
votes (block_a),
@ -3161,9 +3166,9 @@ bool rai::election::vote (std::shared_ptr<rai::vote> vote_a)
else
{
auto last_vote (last_vote_it->second);
if (vote_a->sequence > last_vote.second)
if (last_vote < *vote_a)
{
if (last_vote.first <= std::chrono::steady_clock::now () - std::chrono::seconds (cooldown))
if (last_vote.time <= std::chrono::steady_clock::now () - std::chrono::seconds (cooldown))
{
should_process = true;
}
@ -3175,7 +3180,7 @@ bool rai::election::vote (std::shared_ptr<rai::vote> vote_a)
}
if (should_process)
{
last_votes[vote_a->account] = std::make_pair (std::chrono::steady_clock::now (), vote_a->sequence);
last_votes[vote_a->account] = { std::chrono::steady_clock::now (), vote_a->sequence, vote_a->block->hash () };
node.network.republish_vote (vote_a);
votes.vote (vote_a);
confirm_if_quorum (transaction);

View file

@ -42,6 +42,14 @@ public:
std::shared_ptr<rai::block> winner;
rai::amount tally;
};
class vote_info
{
public:
std::chrono::steady_clock::time_point time;
uint64_t sequence;
rai::block_hash hash;
bool operator < (rai::vote const &) const;
};
class election : public std::enable_shared_from_this<rai::election>
{
std::function<void(std::shared_ptr<rai::block>)> confirmation_action;
@ -62,7 +70,7 @@ public:
void confirm_cutoff (MDB_txn *);
rai::votes votes;
rai::node & node;
std::unordered_map<rai::account, std::pair<std::chrono::steady_clock::time_point, uint64_t>> last_votes;
std::unordered_map<rai::account, rai::vote_info> last_votes;
rai::election_status status;
std::atomic<bool> confirmed;
};