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

Commit 79b59a3

Browse files
authored
Governance: Remove proposal counter (#127)
* feat: Use proposal seed instead of index * chore: Rename and reset legacy1 to reserved1 * chore: Make Clippy happy * chore: Fix Chat tests compilation
1 parent 21cfaff commit 79b59a3

File tree

12 files changed

+45
-51
lines changed

12 files changed

+45
-51
lines changed

governance/chat/program/tests/program_test/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ impl GovernanceChatProgramTest {
259259
let proposal_name = "Proposal #1".to_string();
260260
let description_link = "Proposal Description".to_string();
261261
let options = vec!["Yes".to_string()];
262-
let proposal_index: u32 = 0;
262+
263263
let use_deny_option = true;
264+
let proposal_seed = Pubkey::new_unique();
264265

265266
let create_proposal_ix = create_proposal(
266267
&self.governance_program_id,
@@ -276,7 +277,7 @@ impl GovernanceChatProgramTest {
276277
VoteType::SingleChoice,
277278
options,
278279
use_deny_option,
279-
proposal_index,
280+
&proposal_seed,
280281
);
281282

282283
self.bench
@@ -288,7 +289,7 @@ impl GovernanceChatProgramTest {
288289
&self.governance_program_id,
289290
&governance_address,
290291
&governing_token_mint_keypair.pubkey(),
291-
&proposal_index.to_le_bytes(),
292+
&proposal_seed,
292293
);
293294

294295
ProposalCookie {

governance/program/src/instruction.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ pub enum GovernanceInstruction {
165165
/// Creates Proposal account for Transactions which will be executed at some point in the future
166166
///
167167
/// 0. `[]` Realm account the created Proposal belongs to
168-
/// 1. `[writable]` Proposal account. PDA seeds ['governance',governance, governing_token_mint, proposal_index]
169-
/// 2. `[writable]` Governance account
168+
/// 1. `[writable]` Proposal account. PDA seeds ['governance',governance, governing_token_mint, proposal_seed]
169+
/// 2. `[]` Governance account
170170
/// 3. `[writable]` TokenOwnerRecord account of the Proposal owner
171171
/// 4. `[]` Governing Token Mint the Proposal is created for
172172
/// 5. `[signer]` Governance Authority (Token Owner or Governance Delegate)
@@ -196,6 +196,10 @@ pub enum GovernanceInstruction {
196196
/// A proposal without the rejecting option is a non binding survey
197197
/// Only proposals with the rejecting option can have executable transactions
198198
use_deny_option: bool,
199+
200+
#[allow(dead_code)]
201+
/// Unique seed for the Proposal PDA
202+
proposal_seed: Pubkey,
199203
},
200204

201205
/// Adds a signatory to the Proposal which means this Proposal can't leave Draft state until yet another Signatory signs
@@ -897,19 +901,15 @@ pub fn create_proposal(
897901
vote_type: VoteType,
898902
options: Vec<String>,
899903
use_deny_option: bool,
900-
proposal_index: u32,
904+
proposal_seed: &Pubkey,
901905
) -> Instruction {
902-
let proposal_address = get_proposal_address(
903-
program_id,
904-
governance,
905-
governing_token_mint,
906-
&proposal_index.to_le_bytes(),
907-
);
906+
let proposal_address =
907+
get_proposal_address(program_id, governance, governing_token_mint, proposal_seed);
908908

909909
let mut accounts = vec![
910910
AccountMeta::new_readonly(*realm, false),
911911
AccountMeta::new(proposal_address, false),
912-
AccountMeta::new(*governance, false),
912+
AccountMeta::new_readonly(*governance, false),
913913
AccountMeta::new(*proposal_owner_record, false),
914914
AccountMeta::new_readonly(*governing_token_mint, false),
915915
AccountMeta::new_readonly(*governance_authority, true),
@@ -925,6 +925,7 @@ pub fn create_proposal(
925925
vote_type,
926926
options,
927927
use_deny_option,
928+
proposal_seed: *proposal_seed,
928929
};
929930

930931
Instruction {

governance/program/src/processor/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub fn process_instruction(
146146
vote_type: proposal_type,
147147
options,
148148
use_deny_option,
149+
proposal_seed,
149150
} => process_create_proposal(
150151
program_id,
151152
accounts,
@@ -154,6 +155,7 @@ pub fn process_instruction(
154155
proposal_type,
155156
options,
156157
use_deny_option,
158+
proposal_seed,
157159
),
158160
GovernanceInstruction::AddSignatory { signatory } => {
159161
process_add_signatory(program_id, accounts, signatory)

governance/program/src/processor/process_create_governance.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ pub fn process_create_governance(
5656
realm: *realm_info.key,
5757
governed_account: *governed_account_info.key,
5858
config,
59-
proposals_count: 0,
60-
59+
reserved1: 0,
6160
reserved_v2: [0; 128],
6261
};
6362

governance/program/src/processor/process_create_mint_governance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn process_create_mint_governance(
6868
realm: *realm_info.key,
6969
governed_account: *governed_mint_info.key,
7070
config,
71-
proposals_count: 0,
71+
reserved1: 0,
7272
reserved_v2: [0; 128],
7373
};
7474

governance/program/src/processor/process_create_program_governance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn process_create_program_governance(
6868
realm: *realm_info.key,
6969
governed_account: *governed_program_info.key,
7070
config,
71-
proposals_count: 0,
71+
reserved1: 0,
7272
reserved_v2: [0; 128],
7373
};
7474

governance/program/src/processor/process_create_proposal.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::{
2828
};
2929

3030
/// Processes CreateProposal instruction
31+
#[allow(clippy::too_many_arguments)]
3132
pub fn process_create_proposal(
3233
program_id: &Pubkey,
3334
accounts: &[AccountInfo],
@@ -36,6 +37,7 @@ pub fn process_create_proposal(
3637
vote_type: VoteType,
3738
options: Vec<String>,
3839
use_deny_option: bool,
40+
proposal_seed: Pubkey,
3941
) -> ProgramResult {
4042
let account_info_iter = &mut accounts.iter();
4143

@@ -63,7 +65,7 @@ pub fn process_create_proposal(
6365
governing_token_mint_info.key,
6466
)?;
6567

66-
let mut governance_data =
68+
let governance_data =
6769
get_governance_data_for_realm(program_id, governance_info, realm_info.key)?;
6870

6971
governance_data.assert_governing_token_mint_can_vote(
@@ -169,15 +171,12 @@ pub fn process_create_proposal(
169171
&get_proposal_address_seeds(
170172
governance_info.key,
171173
governing_token_mint_info.key,
172-
&governance_data.proposals_count.to_le_bytes(),
174+
&proposal_seed,
173175
),
174176
program_id,
175177
system_info,
176178
&rent,
177179
)?;
178180

179-
governance_data.proposals_count = governance_data.proposals_count.checked_add(1).unwrap();
180-
governance_data.serialize(&mut *governance_info.data.borrow_mut())?;
181-
182181
Ok(())
183182
}

governance/program/src/processor/process_create_token_governance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn process_create_token_governance(
6666
realm: *realm_info.key,
6767
governed_account: *governed_token_info.key,
6868
config,
69-
proposals_count: 0,
69+
reserved1: 0,
7070
reserved_v2: [0; 128],
7171
};
7272

governance/program/src/state/governance.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ pub struct GovernanceV2 {
8484
/// or upgrade_authority for a Program account should be transferred to the Governance PDA
8585
pub governed_account: Pubkey,
8686

87-
/// Running count of proposals
88-
pub proposals_count: u32,
87+
/// Reserved space for future versions
88+
pub reserved1: u32,
8989

9090
/// Governance config
9191
pub config: GovernanceConfig,
@@ -192,7 +192,7 @@ impl GovernanceV2 {
192192
account_type: self.account_type,
193193
realm: self.realm,
194194
governed_account: self.governed_account,
195-
proposals_count: self.proposals_count,
195+
proposals_count: 0,
196196
config: self.config,
197197
};
198198

@@ -282,9 +282,8 @@ pub fn get_governance_data(
282282
account_type,
283283
realm: governance_data_v1.realm,
284284
governed_account: governance_data_v1.governed_account,
285-
proposals_count: governance_data_v1.proposals_count,
285+
reserved1: 0,
286286
config: governance_data_v1.config,
287-
// Add the extra reserved_v2 padding
288287
reserved_v2: [0; 128],
289288
}
290289
} else {
@@ -318,6 +317,9 @@ pub fn get_governance_data(
318317
// Reset voting_cool_off_time and reserved space previously used for voting_proposal_count
319318
governance_data.config.voting_cool_off_time = 0;
320319
governance_data.config.reserved = 0;
320+
321+
// Reset reserved space previously used for proposal_count
322+
governance_data.reserved1 = 0;
321323
}
322324

323325
Ok(governance_data)
@@ -537,7 +539,7 @@ mod test {
537539
account_type: GovernanceAccountType::GovernanceV2,
538540
realm: Pubkey::new_unique(),
539541
governed_account: Pubkey::new_unique(),
540-
proposals_count: 10,
542+
reserved1: 0,
541543
config: create_test_governance_config(),
542544
reserved_v2: [0; 128],
543545
}

governance/program/src/state/proposal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,13 +1023,13 @@ pub fn get_proposal_data_for_governance(
10231023
pub fn get_proposal_address_seeds<'a>(
10241024
governance: &'a Pubkey,
10251025
governing_token_mint: &'a Pubkey,
1026-
proposal_index_le_bytes: &'a [u8],
1026+
proposal_seed: &'a Pubkey,
10271027
) -> [&'a [u8]; 4] {
10281028
[
10291029
PROGRAM_AUTHORITY_SEED,
10301030
governance.as_ref(),
10311031
governing_token_mint.as_ref(),
1032-
proposal_index_le_bytes,
1032+
proposal_seed.as_ref(),
10331033
]
10341034
}
10351035

@@ -1038,10 +1038,10 @@ pub fn get_proposal_address<'a>(
10381038
program_id: &Pubkey,
10391039
governance: &'a Pubkey,
10401040
governing_token_mint: &'a Pubkey,
1041-
proposal_index_le_bytes: &'a [u8],
1041+
proposal_seed: &'a Pubkey,
10421042
) -> Pubkey {
10431043
Pubkey::find_program_address(
1044-
&get_proposal_address_seeds(governance, governing_token_mint, proposal_index_le_bytes),
1044+
&get_proposal_address_seeds(governance, governing_token_mint, proposal_seed),
10451045
program_id,
10461046
)
10471047
.0

0 commit comments

Comments
 (0)