Skip to content

Commit

Permalink
solana: add some more receive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcsongor committed Mar 14, 2024
1 parent a0dbfa7 commit fb22598
Show file tree
Hide file tree
Showing 9 changed files with 542 additions and 125 deletions.
152 changes: 42 additions & 110 deletions solana/programs/example-native-token-transfers/tests/cancel_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,40 @@
#![feature(type_changing_struct_update)]

use anchor_lang::prelude::*;
use common::setup::{TestData, OTHER_CHAIN, OTHER_MANAGER, OTHER_TRANSCEIVER, THIS_CHAIN};
use common::setup::{TestData, OTHER_CHAIN};
use example_native_token_transfers::{
error::NTTError,
instructions::{RedeemArgs, TransferArgs},
queue::{inbox::InboxRateLimit, outbox::OutboxRateLimit},
};
use ntt_messages::{
chain_id::ChainId, mode::Mode, ntt::NativeTokenTransfer, ntt_manager::NttManagerMessage,
transceiver::TransceiverMessage, transceivers::wormhole::WormholeTransceiver,
trimmed_amount::TrimmedAmount,
};
use sdk::{
accounts::{good_ntt, NTTAccounts},
transceivers::wormhole::instructions::receive_message::ReceiveMessage,
};
use solana_program::instruction::InstructionError;
use solana_program_test::*;
use solana_sdk::{signature::Keypair, signer::Signer, transaction::TransactionError};
use wormhole_sdk::{Address, Vaa};
use solana_sdk::{signature::Keypair, signer::Signer};
use wormhole_sdk::Address;

use crate::{
common::submit::Submittable,
sdk::instructions::transfer::{approve_token_authority, transfer},
};
use crate::{
common::{query::GetAccountDataAnchor, setup::setup},
common::{
query::GetAccountDataAnchor,
setup::{setup, OTHER_TRANSCEIVER},
utils::make_transfer_message,
},
sdk::{
instructions::{
post_vaa::post_vaa,
redeem::{redeem, Redeem},
transfer::Transfer,
},
transceivers::wormhole::instructions::receive_message::receive_message,
},
};
use crate::{
common::{submit::Submittable, utils::post_vaa_helper},
sdk::instructions::transfer::{approve_token_authority, transfer},
};

pub mod common;
pub mod sdk;
Expand Down Expand Up @@ -99,58 +98,6 @@ fn init_receive_message_accs(
}
}

async fn post_transfer_vaa(
ctx: &mut ProgramTestContext,
id: [u8; 32],
amount: u64,
// TODO: this is used for a negative testing of the recipient ntt_manager
// address. this should not be done in the cancel flow tests, but instead a
// dedicated receive transfer test suite
recipient_ntt_manager: Option<&Pubkey>,
recipient: &Keypair,
) -> (Pubkey, NttManagerMessage<NativeTokenTransfer>) {
let ntt_manager_message = NttManagerMessage {
id,
sender: [4u8; 32],
payload: NativeTokenTransfer {
amount: TrimmedAmount {
amount,
decimals: 9,
},
source_token: [3u8; 32],
to_chain: ChainId { id: THIS_CHAIN },
to: recipient.pubkey().to_bytes(),
},
};

let transceiver_message: TransceiverMessage<WormholeTransceiver, NativeTokenTransfer> =
TransceiverMessage::new(
OTHER_MANAGER,
recipient_ntt_manager
.map(|k| k.to_bytes())
.unwrap_or_else(|| good_ntt.program().to_bytes()),
ntt_manager_message.clone(),
vec![],
);

let vaa = Vaa {
version: 1,
guardian_set_index: 0,
signatures: vec![],
timestamp: 123232,
nonce: 0,
emitter_chain: OTHER_CHAIN.into(),
emitter_address: Address(OTHER_TRANSCEIVER),
sequence: 0,
consistency_level: 0,
payload: transceiver_message,
};

let posted_vaa = post_vaa(&good_ntt.wormhole(), ctx, vaa).await;

(posted_vaa, ntt_manager_message)
}

async fn outbound_capacity(ctx: &mut ProgramTestContext) -> u64 {
let clock: Clock = ctx.banks_client.get_sysvar().await.unwrap();
let rate_limit: OutboxRateLimit = ctx
Expand All @@ -174,8 +121,24 @@ async fn test_cancel() {
let recipient = Keypair::new();
let (mut ctx, test_data) = setup(Mode::Locking).await;

let (vaa0, msg0) = post_transfer_vaa(&mut ctx, [0u8; 32], 1000, None, &recipient).await;
let (vaa1, msg1) = post_transfer_vaa(&mut ctx, [1u8; 32], 2000, None, &recipient).await;
let msg0 = make_transfer_message(&good_ntt, [0u8; 32], 1000, &recipient.pubkey());
let msg1 = make_transfer_message(&good_ntt, [1u8; 32], 2000, &recipient.pubkey());
let vaa0 = post_vaa_helper(
&good_ntt,
OTHER_CHAIN.into(),
Address(OTHER_TRANSCEIVER),
msg0.clone(),
&mut ctx,
)
.await;
let vaa1 = post_vaa_helper(
&good_ntt,
OTHER_CHAIN.into(),
Address(OTHER_TRANSCEIVER),
msg1.clone(),
&mut ctx,
)
.await;

let inbound_limit_before = inbound_capacity(&mut ctx).await;
let outbound_limit_before = outbound_capacity(&mut ctx).await;
Expand All @@ -190,7 +153,12 @@ async fn test_cancel() {

redeem(
&good_ntt,
init_redeem_accs(&mut ctx, &test_data, OTHER_CHAIN, msg0),
init_redeem_accs(
&mut ctx,
&test_data,
OTHER_CHAIN,
msg0.ntt_manager_payload.clone(),
),
RedeemArgs {},
)
.submit(&mut ctx)
Expand Down Expand Up @@ -241,7 +209,12 @@ async fn test_cancel() {

redeem(
&good_ntt,
init_redeem_accs(&mut ctx, &test_data, OTHER_CHAIN, msg1),
init_redeem_accs(
&mut ctx,
&test_data,
OTHER_CHAIN,
msg1.ntt_manager_payload.clone(),
),
RedeemArgs {},
)
.submit(&mut ctx)
Expand All @@ -258,44 +231,3 @@ async fn test_cancel() {
inbound_capacity(&mut ctx).await
);
}

// TODO: this should not live in this file, move to a dedicated receive test suite
#[tokio::test]
async fn test_wrong_recipient_ntt_manager() {
let recipient = Keypair::new();
let (mut ctx, test_data) = setup(Mode::Locking).await;

let (vaa0, msg0) = post_transfer_vaa(
&mut ctx,
[0u8; 32],
1000,
Some(&Pubkey::default()),
&recipient,
)
.await;

receive_message(
&good_ntt,
init_receive_message_accs(&mut ctx, vaa0, OTHER_CHAIN, [0u8; 32]),
)
.submit(&mut ctx)
.await
.unwrap();

let err = redeem(
&good_ntt,
init_redeem_accs(&mut ctx, &test_data, OTHER_CHAIN, msg0),
RedeemArgs {},
)
.submit(&mut ctx)
.await
.unwrap_err();

assert_eq!(
err.unwrap(),
TransactionError::InstructionError(
0,
InstructionError::Custom(NTTError::InvalidRecipientNttManager.into())
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod account_json_utils;
pub mod query;
pub mod setup;
pub mod submit;
pub mod utils;
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub async fn setup_ntt(ctx: &mut ProgramTestContext, test_data: &TestData, mode:
.unwrap();

set_peer(
&GoodNTT {},
&good_ntt,
SetPeer {
payer: ctx.payer.pubkey(),
owner: test_data.program_owner.pubkey(),
Expand Down Expand Up @@ -268,8 +268,11 @@ pub async fn setup_accounts(ctx: &mut ProgramTestContext, program_owner: Keypair
.await
.unwrap();

let bad_user_token_account =
get_associated_token_address_with_program_id(&user.pubkey(), &bad_mint.pubkey(), &Token::id());
let bad_user_token_account = get_associated_token_address_with_program_id(
&user.pubkey(),
&bad_mint.pubkey(),
&Token::id(),
);

spl_associated_token_account::instruction::create_associated_token_account(
&payer,
Expand All @@ -281,7 +284,6 @@ pub async fn setup_accounts(ctx: &mut ProgramTestContext, program_owner: Keypair
.await
.unwrap();


spl_token::instruction::mint_to(
&Token::id(),
&mint.pubkey(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::sync::atomic::AtomicU64;

use anchor_lang::AnchorSerialize;
use ntt_messages::{
chain_id::ChainId, ntt::NativeTokenTransfer, ntt_manager::NttManagerMessage,
transceiver::TransceiverMessage, transceivers::wormhole::WormholeTransceiver,
trimmed_amount::TrimmedAmount,
};
use solana_program::pubkey::Pubkey;
use solana_program_test::ProgramTestContext;
use wormhole_sdk::{Address, Chain, Vaa};

use crate::sdk::accounts::NTT;

use super::setup::{OTHER_MANAGER, THIS_CHAIN};
use crate::sdk::instructions::post_vaa::post_vaa;

pub fn make_transfer_message(
ntt: &NTT,
id: [u8; 32],
amount: u64,
recipient: &Pubkey,
) -> TransceiverMessage<WormholeTransceiver, NativeTokenTransfer> {
let ntt_manager_message = NttManagerMessage {
id,
sender: [4u8; 32],
payload: NativeTokenTransfer {
amount: TrimmedAmount {
amount,
decimals: 9,
},
source_token: [3u8; 32],
to_chain: ChainId { id: THIS_CHAIN },
to: recipient.to_bytes(),
},
};

TransceiverMessage::new(
OTHER_MANAGER,
ntt.program().to_bytes(),
ntt_manager_message.clone(),
vec![],
)
}

pub async fn post_vaa_helper<A: AnchorSerialize + Clone>(
ntt: &NTT,
emitter_chain: Chain,
emitter_address: Address,
msg: A,
ctx: &mut ProgramTestContext,
) -> Pubkey {
static I: AtomicU64 = AtomicU64::new(0);

let sequence = I.fetch_add(1, std::sync::atomic::Ordering::Acquire);

let vaa = Vaa {
version: 1,
guardian_set_index: 0,
signatures: vec![],
timestamp: 123232,
nonce: 0,
emitter_chain,
emitter_address,
sequence,
consistency_level: 0,
payload: msg,
};

let posted_vaa = post_vaa(&ntt.wormhole(), ctx, vaa).await;

posted_vaa
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ async fn transfer_ownership_to_gov_program(
(vaa, inner_ix)
}


#[tokio::test]
async fn test_governance() {
let (mut ctx, test_data) = setup(Mode::Locking).await;
Expand Down
Loading

0 comments on commit fb22598

Please sign in to comment.