Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit c9f8dda

Browse files
authored
Governance: Use saturating sub to decrease voting proposal count (#2840)
* fix: use saturating_sub for voting_proposal_count * chore: update voting_proposal_count comments
1 parent 2600963 commit c9f8dda

File tree

5 files changed

+10
-15
lines changed

5 files changed

+10
-15
lines changed

governance/program/src/processor/process_cancel_proposal.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ pub fn process_cancel_proposal(program_id: &Pubkey, accounts: &[AccountInfo]) ->
5050

5151
if proposal_data.state == ProposalState::Voting {
5252
// Update Realm voting_proposal_count
53-
realm_data.voting_proposal_count = realm_data.voting_proposal_count.checked_sub(1).unwrap();
53+
realm_data.voting_proposal_count = realm_data.voting_proposal_count.saturating_sub(1);
5454
realm_data.serialize(&mut *realm_info.data.borrow_mut())?;
5555

5656
// Update Governance voting_proposal_count
57-
governance_data.voting_proposal_count = governance_data
58-
.voting_proposal_count
59-
.checked_sub(1)
60-
.unwrap();
57+
governance_data.voting_proposal_count =
58+
governance_data.voting_proposal_count.saturating_sub(1);
6159
governance_data.serialize(&mut *governance_info.data.borrow_mut())?;
6260
}
6361

governance/program/src/processor/process_cast_vote.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,12 @@ pub fn process_cast_vote(
167167
};
168168

169169
// Update Realm voting_proposal_count
170-
realm_data.voting_proposal_count = realm_data.voting_proposal_count.checked_sub(1).unwrap();
170+
realm_data.voting_proposal_count = realm_data.voting_proposal_count.saturating_sub(1);
171171
realm_data.serialize(&mut *realm_info.data.borrow_mut())?;
172172

173173
// Update Governance voting_proposal_count
174-
governance_data.voting_proposal_count = governance_data
175-
.voting_proposal_count
176-
.checked_sub(1)
177-
.unwrap();
174+
governance_data.voting_proposal_count =
175+
governance_data.voting_proposal_count.saturating_sub(1);
178176
governance_data.serialize(&mut *governance_info.data.borrow_mut())?;
179177
}
180178

governance/program/src/processor/process_finalize_vote.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,11 @@ pub fn process_finalize_vote(program_id: &Pubkey, accounts: &[AccountInfo]) -> P
7474
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
7575

7676
// Update Realm voting_proposal_count
77-
realm_data.voting_proposal_count = realm_data.voting_proposal_count.checked_sub(1).unwrap();
77+
realm_data.voting_proposal_count = realm_data.voting_proposal_count.saturating_sub(1);
7878
realm_data.serialize(&mut *realm_info.data.borrow_mut())?;
7979

8080
// Update Governance voting_proposal_count
81-
governance_data.voting_proposal_count = governance_data
82-
.voting_proposal_count
83-
.checked_sub(1)
84-
.unwrap();
81+
governance_data.voting_proposal_count = governance_data.voting_proposal_count.saturating_sub(1);
8582
governance_data.serialize(&mut *governance_info.data.borrow_mut())?;
8683

8784
Ok(())

governance/program/src/processor/process_set_governance_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub fn process_set_governance_config(
3535
// Note: Config change leaves voting proposals in unpredictable state and it's DAOs responsibility
3636
// to ensure the changes are made when there are no proposals in voting state
3737
// For example changing approval quorum could accidentally make proposals to succeed which would otherwise be defeated
38+
// The check wouldn't have any effect when upgrading from V1 to V2 because it was not tracked in V1
3839

3940
// if governance_data.voting_proposal_count > 0 {
4041
// return Err(GovernanceError::GovernanceConfigChangeNotAllowed.into());

governance/program/src/processor/process_set_realm_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub fn process_set_realm_config(
4343
// Note: Config change leaves voting proposals in unpredictable state and it's DAOs responsibility
4444
// to ensure the changes are made when there are no proposals in voting state
4545
// For example changing voter-weight or max-voter-weight addin could accidentally make proposals to succeed which would otherwise be defeated
46+
// The check wouldn't have any effect when upgrading from V1 to V2 because it was not tracked in V1
4647

4748
// if realm_data.voting_proposal_count > 0 {
4849
// return Err(GovernanceError::RealmConfigChangeNotAllowed.into());

0 commit comments

Comments
 (0)