From f6932bebca73ca50907f495d219b8020c57f0828 Mon Sep 17 00:00:00 2001 From: Hendrik Hofstadt Date: Mon, 26 Jul 2021 19:49:45 +0200 Subject: [PATCH] Set correct mutability on Transfer Native Change-Id: I50a34645de34cd97b801015748331cfba7cdfac9 --- .../token_bridge/program/src/api/transfer.rs | 30 +++--- .../program/src/instructions/mod.rs | 2 +- .../token_bridge/program/tests/common.rs | 93 +++++++++++++++++++ .../token_bridge/program/tests/integration.rs | 73 +++++++++++++++ 4 files changed, 184 insertions(+), 14 deletions(-) diff --git a/solana/modules/token_bridge/program/src/api/transfer.rs b/solana/modules/token_bridge/program/src/api/transfer.rs index d23a9e71..06aa94fc 100644 --- a/solana/modules/token_bridge/program/src/api/transfer.rs +++ b/solana/modules/token_bridge/program/src/api/transfer.rs @@ -11,6 +11,7 @@ use crate::{ }; use bridge::{ api::{PostMessage, PostMessageData}, + types::ConsistencyLevel, vaa::SerializePayload, }; use primitive_types::U256; @@ -36,33 +37,36 @@ use std::ops::{Deref, DerefMut}; #[derive(FromAccounts)] pub struct TransferNative<'b> { - pub payer: Signer>, - pub config: ConfigAccount<'b, { AccountState::Initialized }>, + pub payer: Mut>>, - pub from: Data<'b, SplAccount, { AccountState::Initialized }>, - pub mint: Data<'b, SplMint, { AccountState::Initialized }>, + pub config: Mut>, - pub custody: CustodyAccount<'b, { AccountState::MaybeInitialized }>, + pub from: Mut>, + + pub mint: Mut>, + + pub custody: Mut>, // This could allow someone to race someone else's tx if they do the approval in a separate tx. - // Therefore the approval must be set in the same tx + // Therefore the approval must be set in the same tx. pub authority_signer: AuthoritySigner<'b>, + pub custody_signer: CustodySigner<'b>, /// CPI Context - pub bridge: Info<'b>, + pub bridge: Mut>, /// Account to store the posted message - pub message: Info<'b>, + pub message: Mut>, /// Emitter of the VAA pub emitter: EmitterAccount<'b>, /// Tracker for the emitter sequence - pub sequence: Info<'b>, + pub sequence: Mut>, /// Account to collect tx fee - pub fee_collector: Info<'b>, + pub fee_collector: Mut>, pub clock: Sysvar<'b, Clock>, } @@ -151,14 +155,14 @@ pub fn transfer_native( let params = (bridge::instruction::Instruction::PostMessage, PostMessageData { nonce: data.nonce, payload: payload.try_to_vec()?, - persist: true, + consistency_level: ConsistencyLevel::Confirmed, }); let ix = Instruction::new_with_bytes( accs.config.wormhole_bridge, params.try_to_vec()?.as_slice(), vec![ - AccountMeta::new_readonly(*accs.bridge.key, false), + AccountMeta::new(*accs.bridge.key, false), AccountMeta::new(*accs.message.key, false), AccountMeta::new_readonly(*accs.emitter.key, true), AccountMeta::new(*accs.sequence.key, false), @@ -275,7 +279,7 @@ pub fn transfer_wrapped( let params = (bridge::instruction::Instruction::PostMessage, PostMessageData { nonce: data.nonce, payload: payload.try_to_vec()?, - persist: true, + consistency_level: ConsistencyLevel::Confirmed, }); let ix = Instruction::new_with_bytes( diff --git a/solana/modules/token_bridge/program/src/instructions/mod.rs b/solana/modules/token_bridge/program/src/instructions/mod.rs index 7e167965..b882dc3b 100644 --- a/solana/modules/token_bridge/program/src/instructions/mod.rs +++ b/solana/modules/token_bridge/program/src/instructions/mod.rs @@ -316,7 +316,7 @@ pub fn transfer_native( AccountMeta::new_readonly(emitter_key, false), AccountMeta::new(sequence_key, false), AccountMeta::new(fee_collector_key, false), - AccountMeta::new(solana_program::sysvar::clock::id(), false), + AccountMeta::new_readonly(solana_program::sysvar::clock::id(), false), // Dependencies AccountMeta::new(solana_program::sysvar::rent::id(), false), AccountMeta::new(solana_program::system_program::id(), false), diff --git a/solana/modules/token_bridge/program/tests/common.rs b/solana/modules/token_bridge/program/tests/common.rs index f8417e26..1372d527 100644 --- a/solana/modules/token_bridge/program/tests/common.rs +++ b/solana/modules/token_bridge/program/tests/common.rs @@ -100,6 +100,7 @@ pub fn execute( } mod helpers { + use bridge::types::PostedMessage; use token_bridge::{CompleteNativeData, TransferNativeData}; use super::*; @@ -171,6 +172,98 @@ mod helpers { ) } + pub fn transfer( + client: &RpcClient, + from: &Keypair, + to: &Pubkey, + lamports: u64, + ) -> Result { + execute( + client, + from, + &[from], + &[system_instruction::transfer(&from.pubkey(), to, lamports)], + CommitmentConfig::processed(), + ) + } + + pub fn initialize( + client: &RpcClient, + program: &Pubkey, + payer: &Keypair, + bridge: &Pubkey, + ) -> Result { + execute( + client, + payer, + &[payer], + &[instructions::initialize(*program, payer.pubkey(), *bridge).unwrap()], + CommitmentConfig::processed(), + ) + } + + pub fn attest( + client: &RpcClient, + program: &Pubkey, + bridge: &Pubkey, + payer: &Keypair, + mint: Pubkey, + mint_meta: Pubkey, + nonce: u32, + ) -> Result { + let mint_data = Mint::unpack( + &client.get_account(&mint)?.data + ).expect("Could not unpack Mint"); + + execute( + client, + payer, + &[payer], + &[instructions::attest( + *program, + *bridge, + payer.pubkey(), + mint, + mint_data, + mint_meta, + nonce, + ) + .unwrap()], + CommitmentConfig::processed(), + ) + } + + pub fn transfer_native( + client: &RpcClient, + program: &Pubkey, + bridge: &Pubkey, + payer: &Keypair, + from: &Keypair, + mint: Pubkey, + ) -> Result { + execute( + client, + payer, + &[payer], + &[instructions::transfer_native( + *program, + *bridge, + payer.pubkey(), + from.pubkey(), + mint, + TransferNativeData { + nonce: 0, + amount: 0, + fee: 0, + target_address: [0u8; 32], + target_chain: 1, + }, + ) + .unwrap()], + CommitmentConfig::processed(), + ) + } + pub fn create_mint( client: &RpcClient, payer: &Keypair, diff --git a/solana/modules/token_bridge/program/tests/integration.rs b/solana/modules/token_bridge/program/tests/integration.rs index 23862896..506d89cc 100644 --- a/solana/modules/token_bridge/program/tests/integration.rs +++ b/solana/modules/token_bridge/program/tests/integration.rs @@ -181,6 +181,79 @@ fn run_integration_tests() { // Initialize the bridge and verify the bridges state. test_initialize(&mut context); + test_transfer_native(&mut context); + test_attest(&mut context); + test_complete_native(&mut context); + //test_transfer_wrapped(&mut context); + //test_complete_wrapped(&mut context); + //test_register_chain(&mut context); + //test_create_wrapped(&mut context); +} + +fn test_attest(context: &mut Context) -> () { + println!("Attest"); + use token_bridge::{ + accounts::ConfigAccount, + types::{ + Config, + FeeStructure, + }, + }; + + let Context { + ref payer, + ref client, + ref bridge, + ref token_bridge, + ref mint_authority, + ref mint, + ref mint_meta, + .. + } = context; + + common::attest( + client, + token_bridge, + bridge, + payer, + mint.pubkey(), + mint_meta.pubkey(), + 0, + ) + .unwrap(); +} + +fn test_transfer_native(context: &mut Context) -> () { + println!("Transfer Native"); + use token_bridge::{ + accounts::ConfigAccount, + types::{ + Config, + FeeStructure, + }, + }; + + let Context { + ref payer, + ref client, + ref bridge, + ref token_bridge, + ref mint_authority, + ref mint, + ref mint_meta, + ref token_account, + .. + } = context; + + common::transfer_native( + client, + token_bridge, + bridge, + payer, + token_account, + mint.pubkey(), + ) + .unwrap(); } fn test_initialize(context: &mut Context) {