Expose errors from test utilities

Change-Id: I9dbe732bcff004dc85b79a8fe6e9226848de468f
This commit is contained in:
Reisen 2021-07-05 08:24:07 +00:00
parent b4d7265342
commit 20af0ae03a
2 changed files with 24 additions and 22 deletions

View File

@ -14,6 +14,7 @@ use secp256k1::{
use sha3::Digest; use sha3::Digest;
use solana_client::{ use solana_client::{
rpc_client::RpcClient, rpc_client::RpcClient,
client_error::ClientError,
rpc_config::RpcSendTransactionConfig, rpc_config::RpcSendTransactionConfig,
}; };
use solana_program::{ use solana_program::{
@ -39,6 +40,7 @@ use solana_sdk::{
read_keypair_file, read_keypair_file,
Keypair, Keypair,
Signer, Signer,
Signature,
}, },
transaction::Transaction, transaction::Transaction,
}; };
@ -92,7 +94,7 @@ fn execute(
payer: &Keypair, payer: &Keypair,
signers: &[&Keypair], signers: &[&Keypair],
instructions: &[Instruction], instructions: &[Instruction],
) { ) -> Result<Signature, ClientError> {
let mut transaction = Transaction::new_with_payer(instructions, Some(&payer.pubkey())); let mut transaction = Transaction::new_with_payer(instructions, Some(&payer.pubkey()));
let recent_blockhash = client.get_recent_blockhash().unwrap().0; let recent_blockhash = client.get_recent_blockhash().unwrap().0;
transaction.sign(&signers.to_vec(), recent_blockhash); transaction.sign(&signers.to_vec(), recent_blockhash);
@ -106,7 +108,6 @@ fn execute(
encoding: None, encoding: None,
}, },
) )
.unwrap();
} }
mod helpers { mod helpers {
@ -202,13 +203,13 @@ mod helpers {
(vaa, body, body_hash) (vaa, body, body_hash)
} }
pub fn transfer(client: &RpcClient, from: &Keypair, to: &Pubkey, lamports: u64) { pub fn transfer(client: &RpcClient, from: &Keypair, to: &Pubkey, lamports: u64) -> Result<Signature, ClientError> {
execute( execute(
client, client,
from, from,
&[from], &[from],
&[system_instruction::transfer(&from.pubkey(), to, lamports)], &[system_instruction::transfer(&from.pubkey(), to, lamports)],
); )
} }
pub fn initialize( pub fn initialize(
@ -216,7 +217,7 @@ mod helpers {
program: &Pubkey, program: &Pubkey,
payer: &Keypair, payer: &Keypair,
initial_guardians: &[[u8; 20]], initial_guardians: &[[u8; 20]],
) { ) -> Result<Signature, ClientError> {
execute( execute(
client, client,
payer, payer,
@ -229,7 +230,7 @@ mod helpers {
initial_guardians, initial_guardians,
) )
.unwrap()], .unwrap()],
); )
} }
pub fn post_message( pub fn post_message(
@ -239,7 +240,7 @@ mod helpers {
emitter: &Keypair, emitter: &Keypair,
nonce: u32, nonce: u32,
data: Vec<u8>, data: Vec<u8>,
) -> Pubkey { ) -> Result<Pubkey, ClientError> {
// Transfer money into the fee collector as it needs a balance/must exist. // Transfer money into the fee collector as it needs a balance/must exist.
let fee_collector = FeeCollector::<'_>::key(None, program); let fee_collector = FeeCollector::<'_>::key(None, program);
@ -256,9 +257,9 @@ mod helpers {
system_instruction::transfer(&payer.pubkey(), &fee_collector, 10_000), system_instruction::transfer(&payer.pubkey(), &fee_collector, 10_000),
instruction, instruction,
], ],
); )?;
message_key Ok(message_key)
} }
pub fn verify_signatures( pub fn verify_signatures(
@ -269,7 +270,7 @@ mod helpers {
body_hash: [u8; 32], body_hash: [u8; 32],
secret_keys: &[SecretKey], secret_keys: &[SecretKey],
guardian_set_version: u32, guardian_set_version: u32,
) { ) -> Result<(), ClientError> {
// Push Secp256k1 instructions for each signature we want to verify. // Push Secp256k1 instructions for each signature we want to verify.
for (i, key) in secret_keys.iter().enumerate() { for (i, key) in secret_keys.iter().enumerate() {
// Set this signers signature position as present at 0. // Set this signers signature position as present at 0.
@ -294,17 +295,18 @@ mod helpers {
) )
.unwrap(), .unwrap(),
], ],
); )?;
} }
Ok(())
} }
pub fn post_vaa(client: &RpcClient, program: &Pubkey, payer: &Keypair, vaa: PostVAAData) { pub fn post_vaa(client: &RpcClient, program: &Pubkey, payer: &Keypair, vaa: PostVAAData) -> Result<Signature, ClientError> {
execute( execute(
client, client,
payer, payer,
&[payer], &[payer],
&[instructions::post_vaa(*program, payer.pubkey(), vaa)], &[instructions::post_vaa(*program, payer.pubkey(), vaa)],
); )
} }
pub fn upgrade_contract( pub fn upgrade_contract(
@ -313,7 +315,7 @@ mod helpers {
payer: &Keypair, payer: &Keypair,
payload_message: Pubkey, payload_message: Pubkey,
spill: Pubkey, spill: Pubkey,
) { ) -> Result<Signature, ClientError> {
execute( execute(
client, client,
payer, payer,
@ -324,7 +326,7 @@ mod helpers {
payload_message, payload_message,
spill, spill,
)], )],
); )
} }
pub fn upgrade_guardian_set( pub fn upgrade_guardian_set(
@ -335,7 +337,7 @@ mod helpers {
emitter: Pubkey, emitter: Pubkey,
old_index: u32, old_index: u32,
new_index: u32, new_index: u32,
) { ) -> Result<Signature, ClientError> {
execute( execute(
client, client,
payer, payer,
@ -348,16 +350,16 @@ mod helpers {
old_index, old_index,
new_index, new_index,
)], )],
); )
} }
pub fn set_fees(client: &RpcClient, program: &Pubkey, payer: &Keypair, fee: u32) { pub fn set_fees(client: &RpcClient, program: &Pubkey, payer: &Keypair, fee: u32) -> Result<Signature, ClientError> {
execute( execute(
client, client,
payer, payer,
&[payer], &[payer],
&[instructions::set_fees(*program, payer.pubkey(), fee)], &[instructions::set_fees(*program, payer.pubkey(), fee)],
); )
} }
pub fn transfer_fees( pub fn transfer_fees(
@ -365,7 +367,7 @@ mod helpers {
program: &Pubkey, program: &Pubkey,
payer: &Keypair, payer: &Keypair,
recipient: &Pubkey, recipient: &Pubkey,
) { ) -> Result<Signature, ClientError> {
execute( execute(
client, client,
payer, payer,
@ -375,6 +377,6 @@ mod helpers {
payer.pubkey(), payer.pubkey(),
*recipient, *recipient,
)], )],
); )
} }
} }

View File

@ -122,7 +122,7 @@ fn test_guardian_set_change(public_keys: &[[u8; 20]], secret_keys: &[SecretKey])
new_guardian_set: new_public_keys.clone(), new_guardian_set: new_public_keys.clone(),
}.try_to_vec().unwrap(); }.try_to_vec().unwrap();
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone()); let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone()).unwrap();
let (vaa, body, body_hash) = common::generate_vaa(&emitter, message.clone(), nonce, 0); let (vaa, body, body_hash) = common::generate_vaa(&emitter, message.clone(), nonce, 0);
common::verify_signatures(client, program, payer, body, body_hash, &secret_keys, 0); common::verify_signatures(client, program, payer, body, body_hash, &secret_keys, 0);
common::post_vaa(client, program, payer, vaa); common::post_vaa(client, program, payer, vaa);