From 83cfe23a813ff381292ff3f7a7aa64daad336aa2 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Fri, 13 Sep 2024 17:28:01 +0100 Subject: [PATCH 1/2] close publisher caps --- .../src/publisher_caps/instructions.rs | 29 +++++++++++ .../integration-tests/tests/publisher_caps.rs | 50 +++++++++++++++++++ staking/programs/publisher-caps/src/lib.rs | 12 +++++ 3 files changed, 91 insertions(+) create mode 100644 staking/integration-tests/tests/publisher_caps.rs diff --git a/staking/integration-tests/src/publisher_caps/instructions.rs b/staking/integration-tests/src/publisher_caps/instructions.rs index a44e7c74f..0feb41754 100644 --- a/staking/integration-tests/src/publisher_caps/instructions.rs +++ b/staking/integration-tests/src/publisher_caps/instructions.rs @@ -117,3 +117,32 @@ pub fn verify_publisher_caps( ); svm.send_transaction(transaction) } + +pub fn close_publisher_caps( + svm: &mut LiteSVM, + signer: &Keypair, + payer: &Keypair, + publisher_caps: Pubkey, +) -> TransactionResult { + let accounts = publisher_caps::accounts::ClosePublisherCaps { + write_authority: signer.pubkey(), + publisher_caps, + }; + + let instruction_data = publisher_caps::instruction::ClosePublisherCaps {}; + + let instruction = Instruction { + program_id: publisher_caps::ID, + accounts: accounts.to_account_metas(None), + data: instruction_data.data(), + }; + + let transaction = Transaction::new_signed_with_payer( + &[instruction], + Some(&payer.pubkey()), + &[payer, signer], + svm.latest_blockhash(), + ); + + svm.send_transaction(transaction) +} diff --git a/staking/integration-tests/tests/publisher_caps.rs b/staking/integration-tests/tests/publisher_caps.rs new file mode 100644 index 000000000..a6aa5450d --- /dev/null +++ b/staking/integration-tests/tests/publisher_caps.rs @@ -0,0 +1,50 @@ +use { + integration_tests::{ + assert_anchor_program_error, + publisher_caps::{ + helper_functions::post_dummy_publisher_caps, + instructions::close_publisher_caps, + }, + setup::{ + setup, + SetupProps, + SetupResult, + }, + }, + publisher_caps::PublisherCapsError, + solana_sdk::{ + signature::Keypair, + signer::Signer, + }, +}; + + +#[test] +fn test_close_publisher_caps() { + let SetupResult { + mut svm, + payer, + publisher_keypair, + .. + } = setup(SetupProps { + init_config: true, + init_target: true, + init_mint: true, + init_pool_data: true, + init_publishers: true, + reward_amount_override: None, + }); + + let publisher_caps = + post_dummy_publisher_caps(&mut svm, &payer, publisher_keypair.pubkey(), 10); + + assert_anchor_program_error!( + close_publisher_caps(&mut svm, &Keypair::new(), &payer, publisher_caps), + PublisherCapsError::WrongWriteAuthority, + 0 + ); + + close_publisher_caps(&mut svm, &payer, &payer, publisher_caps).unwrap(); + assert_eq!(svm.get_account(&publisher_caps).unwrap().data.len(), 0); + assert_eq!(svm.get_account(&publisher_caps).unwrap().lamports, 0); +} diff --git a/staking/programs/publisher-caps/src/lib.rs b/staking/programs/publisher-caps/src/lib.rs index 4f14a6300..0ef11259f 100644 --- a/staking/programs/publisher-caps/src/lib.rs +++ b/staking/programs/publisher-caps/src/lib.rs @@ -123,6 +123,10 @@ pub mod publisher_caps { Ok(()) } + + pub fn close_publisher_caps(_ctx: Context) -> Result<()> { + Ok(()) + } } #[repr(C)] @@ -222,6 +226,14 @@ pub struct VerifyPublisherCaps<'info> { pub encoded_vaa: AccountInfo<'info>, } +#[derive(Accounts)] +pub struct ClosePublisherCaps<'info> { + pub write_authority: Signer<'info>, + #[account(mut, close = write_authority, has_one = write_authority @ PublisherCapsError::WrongWriteAuthority)] + pub publisher_caps: AccountLoader<'info, PublisherCaps>, +} + + #[error_code] pub enum PublisherCapsError { InvalidWormholeMessage, From 95b98a2032c835207fcbacf3e50fab65b27ecf68 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Fri, 13 Sep 2024 18:06:12 +0100 Subject: [PATCH 2/2] add some minimal tests --- staking/integration-tests/tests/publisher_caps.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/staking/integration-tests/tests/publisher_caps.rs b/staking/integration-tests/tests/publisher_caps.rs index a6aa5450d..5d8c307f3 100644 --- a/staking/integration-tests/tests/publisher_caps.rs +++ b/staking/integration-tests/tests/publisher_caps.rs @@ -38,6 +38,9 @@ fn test_close_publisher_caps() { let publisher_caps = post_dummy_publisher_caps(&mut svm, &payer, publisher_keypair.pubkey(), 10); + assert!(svm.get_account(&publisher_caps).unwrap().lamports > 0); + let payer_balance_before = svm.get_account(&payer.pubkey()).unwrap().lamports; + assert_anchor_program_error!( close_publisher_caps(&mut svm, &Keypair::new(), &payer, publisher_caps), PublisherCapsError::WrongWriteAuthority, @@ -47,4 +50,6 @@ fn test_close_publisher_caps() { close_publisher_caps(&mut svm, &payer, &payer, publisher_caps).unwrap(); assert_eq!(svm.get_account(&publisher_caps).unwrap().data.len(), 0); assert_eq!(svm.get_account(&publisher_caps).unwrap().lamports, 0); + + assert!(svm.get_account(&payer.pubkey()).unwrap().lamports > payer_balance_before); }