Skip to content

Commit

Permalink
fix: ensure the gov program stores the config bump (#622)
Browse files Browse the repository at this point in the history
Also remove the need of passing bump in constructor.
  • Loading branch information
eloylp authored Jan 16, 2025
1 parent 2688729 commit 033bd17
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
9 changes: 2 additions & 7 deletions solana/programs/axelar-solana-governance/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,13 +1073,8 @@ pub mod builder {
fn simplest_use_case() {
let payer = Pubkey::new_unique();
let config_pda = Pubkey::new_unique();
let config = GovernanceConfig::new(
1,
[0_u8; 32],
[0_u8; 32],
1,
Pubkey::new_unique().to_bytes(),
);
let config =
GovernanceConfig::new([0_u8; 32], [0_u8; 32], 1, Pubkey::new_unique().to_bytes());

let _ix = IxBuilder::new()
.initialize_config(&payer, &config_pda, config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::state::GovernanceConfig;
pub(crate) fn process(
program_id: &Pubkey,
accounts: &[AccountInfo<'_>],
governance_config: GovernanceConfig,
mut governance_config: GovernanceConfig,
) -> Result<(), ProgramError> {
let accounts_iter = &mut accounts.iter();
let payer = next_account_info(accounts_iter)?;
Expand All @@ -39,6 +39,8 @@ pub(crate) fn process(
return Err(ProgramError::InvalidArgument);
}

governance_config.bump = bump;

// Check: PDA Account is not initialized
root_pda.check_uninitialized_pda()?;

Expand Down
3 changes: 1 addition & 2 deletions solana/programs/axelar-solana-governance/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ impl GovernanceConfig {
/// Creates a new governance program config.
#[must_use]
pub const fn new(
bump: u8,
chain_hash: Hash,
address_hash: Hash,
minimum_proposal_eta_delay: u32,
operator: Address,
) -> Self {
Self {
bump,
bump: 0, // This will be set by the program
chain_hash,
address_hash,
minimum_proposal_eta_delay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub(crate) async fn init_contract_with_operator(
let (config_pda, bump) = GovernanceConfig::pda();

let config = axelar_solana_governance::state::GovernanceConfig::new(
bump,
SOURCE_CHAIN_NAME_KECCAK_HASH,
SOURCE_CHAIN_ADDRESS_KECCAK_HASH,
MINIMUM_PROPOSAL_DELAY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ use crate::helpers::{assert_msg_present_in_logs, program_test};
async fn test_successfully_initialize_config() {
// Setup
let mut fixture = TestFixture::new(program_test()).await;
let (config_pda, bump) = GovernanceConfig::pda();
let (config_pda, _) = GovernanceConfig::pda();

let config = axelar_solana_governance::state::GovernanceConfig::new(
bump,
[0_u8; 32],
[0_u8; 32],
MINIMUM_PROPOSAL_DELAY,
Expand All @@ -34,17 +33,21 @@ async fn test_successfully_initialize_config() {
.get_account_with_borsh::<axelar_solana_governance::state::GovernanceConfig>(&config_pda)
.await
.unwrap();
assert_eq!(&config, &root_pda_data);
assert_eq!(&config.address_hash, &root_pda_data.address_hash);
assert_eq!(&config.chain_hash, &root_pda_data.chain_hash);
assert_eq!(
&config.minimum_proposal_eta_delay,
&root_pda_data.minimum_proposal_eta_delay
);
assert_eq!(&config.operator, &root_pda_data.operator);
}

#[tokio::test]
async fn test_program_checks_config_pda_successfully_derived() {
// Setup
let mut fixture = TestFixture::new(program_test()).await;
let (_, bump) = GovernanceConfig::pda();

let config = axelar_solana_governance::state::GovernanceConfig::new(
bump,
[0_u8; 32],
[0_u8; 32],
MINIMUM_PROPOSAL_DELAY,
Expand All @@ -68,3 +71,33 @@ async fn test_program_checks_config_pda_successfully_derived() {
"Derived PDA does not match provided PDA",
);
}

#[tokio::test]
async fn test_program_overrides_config_bump() {
// Setup
let mut fixture = TestFixture::new(program_test()).await;

let (config_pda, _) = GovernanceConfig::pda();

let config = axelar_solana_governance::state::GovernanceConfig::new(
[0_u8; 32],
[0_u8; 32],
MINIMUM_PROPOSAL_DELAY,
Pubkey::new_unique().to_bytes(),
);

let ix = IxBuilder::new()
.initialize_config(&fixture.payer.pubkey(), &config_pda, config.clone())
.build(); // Wrong PDA

let res = fixture.send_tx(&[ix]).await;
assert!(res.is_ok());

let config = fixture
.get_account_with_borsh::<axelar_solana_governance::state::GovernanceConfig>(&config_pda)
.await
.unwrap();

// Assert
assert!(config.bump != 0);
}

0 comments on commit 033bd17

Please sign in to comment.