From 13cacee7186a8966fd31a9b9975be13477357d07 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Alapont Date: Wed, 24 Aug 2022 22:49:39 -0500 Subject: [PATCH 1/2] Add test for resize instruction --- program/rust/src/tests/mod.rs | 1 + program/rust/src/tests/pyth_simulator.rs | 26 +++++++++++++++- program/rust/src/tests/test_resize_account.rs | 31 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 program/rust/src/tests/test_resize_account.rs diff --git a/program/rust/src/tests/mod.rs b/program/rust/src/tests/mod.rs index 9746dde91..060746f11 100644 --- a/program/rust/src/tests/mod.rs +++ b/program/rust/src/tests/mod.rs @@ -8,6 +8,7 @@ mod test_del_product; mod test_del_publisher; mod test_init_mapping; mod test_init_price; +mod test_resize_account; mod test_set_min_pub; mod test_sizes; mod test_upd_aggregate; diff --git a/program/rust/src/tests/pyth_simulator.rs b/program/rust/src/tests/pyth_simulator.rs index fdb987a3f..1fb7fa550 100644 --- a/program/rust/src/tests/pyth_simulator.rs +++ b/program/rust/src/tests/pyth_simulator.rs @@ -11,7 +11,10 @@ use solana_program::instruction::{ }; use solana_program::pubkey::Pubkey; use solana_program::rent::Rent; -use solana_program::system_instruction; +use solana_program::{ + system_instruction, + system_program, +}; use solana_program_test::{ processor, BanksClient, @@ -224,6 +227,27 @@ impl PythSimulator { .await } + /// Resize a price account (using the resize_price_account + /// instruction). Returns the keypair associated with the newly-created account. + pub async fn resize_price_account( + &mut self, + price_keypair: &Keypair, + ) -> Result<(), BanksClientError> { + let cmd: CommandHeader = OracleCommand::ResizePriceAccount.into(); + let instruction = Instruction::new_with_bytes( + self.program_id, + bytes_of(&cmd), + vec![ + AccountMeta::new(self.payer.pubkey(), true), + AccountMeta::new(price_keypair.pubkey(), true), + AccountMeta::new(system_program::id(), false), + ], + ); + + self.process_ix(instruction, &vec![&price_keypair]).await + } + + /// Get the account at `key`. Returns `None` if no such account exists. pub async fn get_account(&mut self, key: Pubkey) -> Option { self.banks_client.get_account(key).await.unwrap() diff --git a/program/rust/src/tests/test_resize_account.rs b/program/rust/src/tests/test_resize_account.rs new file mode 100644 index 000000000..a1d5b46bb --- /dev/null +++ b/program/rust/src/tests/test_resize_account.rs @@ -0,0 +1,31 @@ +use solana_sdk::signer::Signer; +use std::mem::size_of; + +use crate::c_oracle_header::pc_price_t; +use crate::tests::pyth_simulator::PythSimulator; +use crate::time_machine_types::PriceAccountWrapper; + + +/// Warning : This test will fail if you run cargo test instead of cargo test-bpf +#[tokio::test] +async fn test_resize_account() { + let mut sim = PythSimulator::new().await; + let mapping_keypair = sim.init_mapping().await.unwrap(); + let product1 = sim.add_product(&mapping_keypair).await.unwrap(); + let price1 = sim.add_price(&product1, -8).await.unwrap(); + + // Check size after initialization + let price1_account = sim.get_account(price1.pubkey()).await.unwrap(); + assert_eq!(price1_account.data.len(), size_of::()); + + // Run the instruction once + assert!(sim.resize_price_account(&price1).await.is_ok()); + // Check new size + let price1_account = sim.get_account(price1.pubkey()).await.unwrap(); + assert_eq!(price1_account.data.len(), size_of::()); + + // Future calls don't change the size + assert!(sim.resize_price_account(&price1).await.is_ok()); + let price1_account = sim.get_account(price1.pubkey()).await.unwrap(); + assert_eq!(price1_account.data.len(), size_of::()); +} From 0a4fed62fa273de1f90b1701176c25aa9ae8cdeb Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Alapont Date: Wed, 24 Aug 2022 22:53:00 -0500 Subject: [PATCH 2/2] fixup! Add test for resize instruction --- program/rust/src/tests/pyth_simulator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/program/rust/src/tests/pyth_simulator.rs b/program/rust/src/tests/pyth_simulator.rs index 1fb7fa550..f812c2785 100644 --- a/program/rust/src/tests/pyth_simulator.rs +++ b/program/rust/src/tests/pyth_simulator.rs @@ -228,7 +228,7 @@ impl PythSimulator { } /// Resize a price account (using the resize_price_account - /// instruction). Returns the keypair associated with the newly-created account. + /// instruction). pub async fn resize_price_account( &mut self, price_keypair: &Keypair,