Skip to content

Commit

Permalink
Improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
max-dfinity committed Nov 15, 2024
1 parent 1832771 commit 2cb808a
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions rs/nns/governance/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ impl ProposalVotingStateMachine {
}

fn cast_vote(&mut self, ballots: &mut HashMap<u64, Ballot>, neuron_id: NeuronId, vote: Vote) {
// It is not legitimate to cast an unspecified vote, but we cannot error out, so we
// just ignore it.
if vote == Vote::Unspecified {
return;
}

if let Some(ballot) = ballots.get_mut(&neuron_id.id) {
// The following conditional is CRITICAL, as it prevents a neuron's vote from
// being overwritten by a later vote. This is important because otherwse
Expand Down Expand Up @@ -134,11 +140,10 @@ impl ProposalVotingStateMachine {
Vote::Unspecified
}
};
if vote != Vote::Unspecified {
// Casting vote immediately might affect other follower votes, which makes
// voting resolution take fewer iterations.
self.cast_vote(ballots, follower, vote);
}
// Casting vote immediately might affect other follower votes, which makes
// voting resolution take fewer iterations.
// Vote::Unspecified is ignored by cast_vote.
self.cast_vote(ballots, follower, vote);
}

while let Some((neuron_id, vote)) = self.recent_neuron_ballots_to_record.pop_first() {
Expand Down Expand Up @@ -414,34 +419,14 @@ mod test {
),
);

// We assert it is immediately done after casting an unspecified vote b/c there
// is no work to do.
state_machine.cast_vote(&mut ballots, NeuronId { id: 1 }, Vote::Unspecified);
assert!(state_machine.is_done());

// We assert it is done after checking both sets of followers
state_machine.cast_vote(&mut ballots, NeuronId { id: 1 }, Vote::Yes);
state_machine.continue_processing(&mut neuron_store, &mut ballots);

assert_eq!(
ballots,
hashmap! {
1 => Ballot { vote: Vote::Yes as i32, voting_power: 101 },
2 => Ballot { vote: Vote::Yes as i32, voting_power: 102 }}
);
assert_eq!(
neuron_store
.with_neuron(&NeuronId { id: 1 }, |n| {
n.recent_ballots.first().unwrap().vote
})
.unwrap(),
Vote::Yes as i32
);
assert_eq!(
neuron_store
.with_neuron(&NeuronId { id: 2 }, |n| {
n.recent_ballots.first().unwrap().vote
})
.unwrap(),
Vote::Yes as i32
);

assert!(!state_machine.is_done());

state_machine.continue_processing(&mut neuron_store, &mut ballots);
assert!(state_machine.is_done());
}
Expand Down

0 comments on commit 2cb808a

Please sign in to comment.