Skip to content

Commit

Permalink
Merge branch '@anchpop/NNS1-2283' into 'master'
Browse files Browse the repository at this point in the history
NNS1-2283: Recalculate tallies for SNS proposals if the deadline hasn't passed yet

Closes NNS1-2283 

Closes NNS1-2283

See merge request dfinity-lab/public/ic!14699
  • Loading branch information
anchpop committed Sep 8, 2023
2 parents 5231129 + 8afad79 commit 3e2f1a0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
25 changes: 14 additions & 11 deletions rs/sns/governance/src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1928,17 +1928,18 @@ impl Governance {
Some(p) => p,
};

if proposal_data.status() != ProposalDecisionStatus::Open {
return;
// Recompute the tally here. It should correctly reflect all votes until
// the deadline, even after the proposal has been decided.
if proposal_data.status() == ProposalDecisionStatus::Open
|| proposal_data.accepts_vote(now_seconds)
{
proposal_data.recompute_tally(now_seconds);
}

// Recompute the tally here. It is imperative that only
// 'open' proposals have their tally recomputed. Votes may
// arrive after a decision has been made: such votes count
// for voting rewards, but shall not make it into the
// tally.
proposal_data.recompute_tally(now_seconds);
if !proposal_data.can_make_decision(now_seconds) {
// If the status is open
if proposal_data.status() != ProposalDecisionStatus::Open
|| !proposal_data.can_make_decision(now_seconds)
{
return;
}

Expand Down Expand Up @@ -1983,7 +1984,7 @@ impl Governance {
}

/// Processes all proposals with decision status ProposalStatusOpen
fn process_proposals(&mut self) {
pub fn process_proposals(&mut self) {
if self.env.now() < self.closest_proposal_deadline_timestamp_seconds {
// Nothing to do.
return;
Expand All @@ -1993,7 +1994,9 @@ impl Governance {
.proto
.proposals
.iter()
.filter(|(_, info)| info.status() == ProposalDecisionStatus::Open)
.filter(|(_, info)| {
info.status() == ProposalDecisionStatus::Open || info.accepts_vote(self.env.now())
})
.map(|(pid, _)| *pid)
.collect::<Vec<u64>>();

Expand Down
70 changes: 70 additions & 0 deletions rs/sns/governance/tests/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2830,3 +2830,73 @@ async fn test_mint_tokens() {

assert_eq!(balance_original + E8S_TO_MINT, balance_new);
}

#[tokio::test]
async fn test_process_proposals_tallies_votes_for_proposals_where_voting_is_possible() {
let mut canister_fixture = GovernanceCanisterFixtureBuilder::new()
.set_start_time(10)
.create();

canister_fixture.governance.proto.proposals.insert(
10,
ProposalData {
decided_timestamp_seconds: 9, // proposal is decided
action: 1,
wait_for_quiet_state: Some(WaitForQuietState {
current_deadline_timestamp_seconds: 20, // voting period is still open
}),
proposal: Some(Proposal {
action: Some(Action::Motion(Motion {
motion_text: "Test".to_string(),
})),
..Proposal::default()
}),
latest_tally: None,
..ProposalData::default()
},
);
canister_fixture.governance.process_proposals();
let proposal = canister_fixture
.governance
.proto
.proposals
.get(&10)
.unwrap();

assert!(proposal.latest_tally.is_some());
}

#[tokio::test]
async fn test_process_proposals_doesnt_tally_votes_for_proposals_where_voting_is_impossible() {
let mut canister_fixture = GovernanceCanisterFixtureBuilder::new()
.set_start_time(30)
.create();

canister_fixture.governance.proto.proposals.insert(
10,
ProposalData {
decided_timestamp_seconds: 9, // proposal is decided
action: 1,
wait_for_quiet_state: Some(WaitForQuietState {
current_deadline_timestamp_seconds: 20, // voting period is still open
}),
proposal: Some(Proposal {
action: Some(Action::Motion(Motion {
motion_text: "Test".to_string(),
})),
..Proposal::default()
}),
latest_tally: None,
..ProposalData::default()
},
);
canister_fixture.governance.process_proposals();
let proposal = canister_fixture
.governance
.proto
.proposals
.get(&10)
.unwrap();

assert_eq!(proposal.latest_tally, None);
}

0 comments on commit 3e2f1a0

Please sign in to comment.