From 692da33d0c70fb96e7bff922e33c57d28ff987e3 Mon Sep 17 00:00:00 2001 From: Reisen Date: Fri, 18 Jun 2021 09:59:06 +0000 Subject: [PATCH] Add integration test around PostMessage Change-Id: I8c41db5f3d110ee445ed66d2599d87afb42fde7e --- solana/bridge/program/src/accounts.rs | 5 +- solana/bridge/program/tests/common.rs | 91 ++++++++++++++++++++++ solana/bridge/program/tests/integration.rs | 23 ++++++ 3 files changed, 118 insertions(+), 1 deletion(-) diff --git a/solana/bridge/program/src/accounts.rs b/solana/bridge/program/src/accounts.rs index 324d3384..ace712b7 100644 --- a/solana/bridge/program/src/accounts.rs +++ b/solana/bridge/program/src/accounts.rs @@ -28,7 +28,10 @@ impl<'b, const State: AccountState> Seeded<&GuardianSetDerivationData> for GuardianSet<'b, { State }> { fn seeds(data: &GuardianSetDerivationData) -> Vec> { - vec![data.index.to_be_bytes().to_vec()] + vec![ + "GuardianSet".as_bytes().to_vec(), + data.index.to_be_bytes().to_vec() + ] } } diff --git a/solana/bridge/program/tests/common.rs b/solana/bridge/program/tests/common.rs index 8ccef8a1..0042ba55 100644 --- a/solana/bridge/program/tests/common.rs +++ b/solana/bridge/program/tests/common.rs @@ -62,6 +62,17 @@ mod helpers { (payer, rpc, program) } + pub fn transfer(client: &RpcClient, from: &Keypair, to: &Pubkey, lamports: u64) { + let signers = vec![from]; + let instructions = [system_instruction::transfer(&from.pubkey(), to, lamports)]; + + let mut transaction = Transaction::new_with_payer(&instructions, Some(&from.pubkey())); + let recent_blockhash = client.get_recent_blockhash().unwrap().0; + + transaction.sign(&signers, recent_blockhash); + client.send_and_confirm_transaction(&transaction).unwrap(); + } + pub fn initialize( client: &RpcClient, program: &Pubkey, @@ -88,6 +99,49 @@ mod helpers { transaction.sign(&signers, recent_blockhash); client.send_and_confirm_transaction(&transaction).unwrap(); } + + pub fn post_message( + client: &RpcClient, + program: &Pubkey, + payer: &Keypair, + message: PostedMessage, + sequence: u64, + ) { + let emitter = Keypair::new(); + let (bridge, _) = Pubkey::find_program_address(&["Bridge".as_ref()], program); + let (fee_vault, _) = Pubkey::find_program_address(&["Fees".as_ref()], program); + let (fee_collector, _) = Pubkey::find_program_address(&["fee_collector".as_ref()], program); + let (sequence_key, _) = + Pubkey::find_program_address(&[&emitter.pubkey().to_bytes()], program); + let (message_key, _) = Pubkey::find_program_address( + &[ + &emitter.pubkey().to_bytes(), + sequence.to_be_bytes().as_ref(), + ], + program, + ); + + // Top up the fee collector with some base funds. + transfer(client, payer, &fee_collector, 1000000); + + let signers = vec![payer, &emitter]; + let instructions = [instructions::create_post_message( + *program, + payer.pubkey(), + bridge, + fee_vault, + message_key, + emitter.pubkey(), + sequence_key, + fee_collector, + )]; + + let mut transaction = Transaction::new_with_payer(&instructions, Some(&payer.pubkey())); + let recent_blockhash = client.get_recent_blockhash().unwrap().0; + + transaction.sign(&signers, recent_blockhash); + client.send_and_confirm_transaction(&transaction).unwrap(); + } } mod instructions { @@ -118,4 +172,41 @@ mod instructions { .unwrap(), } } + + pub fn create_post_message( + program_id: Pubkey, + payer: Pubkey, + bridge: Pubkey, + fee_vault: Pubkey, + message: Pubkey, + emitter: Pubkey, + sequence: Pubkey, + fee_collector: Pubkey, + ) -> Instruction { + Instruction { + program_id, + + accounts: vec![ + AccountMeta::new(bridge, false), + AccountMeta::new(fee_vault, false), + AccountMeta::new(message, false), + AccountMeta::new(emitter, true), + AccountMeta::new(sequence, false), + AccountMeta::new(payer, true), + AccountMeta::new(fee_collector, false), + AccountMeta::new_readonly(sysvar::instructions::id(), false), + AccountMeta::new_readonly(sysvar::clock::id(), false), + AccountMeta::new_readonly(sysvar::rent::id(), false), + AccountMeta::new_readonly(solana_program::system_program::id(), false), + ], + + data: instruction::Instruction::PostMessage(PostMessageData { + nonce: 0, + payload: vec![], + emitter, + }) + .try_to_vec() + .unwrap(), + } + } } diff --git a/solana/bridge/program/tests/integration.rs b/solana/bridge/program/tests/integration.rs index 459d3a2e..4628d7f5 100644 --- a/solana/bridge/program/tests/integration.rs +++ b/solana/bridge/program/tests/integration.rs @@ -55,4 +55,27 @@ fn test_bridge_messages() { payer, GuardianSetDerivationData { index: 0 }, ); + + // Post a Message + common::post_message( + client, + program, + payer, + PostedMessage { + vaa_version: 0, + vaa_time: 0, + vaa_signature_account: Pubkey::new_unique(), + + nonce: 0, + sequence: 0, + emitter_chain: 1, + emitter_address: [0u8; 32], + payload: vec![], + submission_time: SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs() as u32, + }, + 0, + ); }