Skip to content

Commit

Permalink
Add examples of failing transactions to SVM integration tests (solana…
Browse files Browse the repository at this point in the history
…-labs#417)

Add examples of failing transactions
  • Loading branch information
LucasSte authored Mar 25, 2024
1 parent a916edb commit b884ea8
Showing 1 changed file with 83 additions and 6 deletions.
89 changes: 83 additions & 6 deletions svm/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ use {
pubkey::Pubkey,
signature::Signature,
sysvar::SysvarId,
transaction::{SanitizedTransaction, Transaction},
transaction::{SanitizedTransaction, Transaction, TransactionError},
},
solana_svm::{
account_loader::TransactionCheckResult,
transaction_error_metrics::TransactionErrorMetrics,
transaction_processor::{
ExecutionRecordingConfig, TransactionBatchProcessor, TransactionProcessingCallback,
},
transaction_results::TransactionExecutionResult,
},
std::{
cmp::Ordering,
Expand Down Expand Up @@ -277,7 +278,7 @@ fn prepare_transactions(
.insert(fee_payer, account_data);

// A simple funds transfer between accounts
let program_account = Pubkey::new_unique();
let transfer_program_account = Pubkey::new_unique();
let sender = Pubkey::new_unique();
let recipient = Pubkey::new_unique();
let fee_payer = Pubkey::new_unique();
Expand All @@ -286,7 +287,7 @@ fn prepare_transactions(
account_keys: vec![
fee_payer,
sender,
program_account,
transfer_program_account,
recipient,
system_account,
],
Expand Down Expand Up @@ -335,7 +336,7 @@ fn prepare_transactions(
account_data.set_lamports(25);
mock_bank
.account_shared_data
.insert(program_account, account_data);
.insert(transfer_program_account, account_data);

// sender
let mut account_data = AccountSharedData::default();
Expand Down Expand Up @@ -397,9 +398,67 @@ fn prepare_transactions(
.account_shared_data
.insert(program_account, account_data);

// TODO: Include these examples as well:
// A transaction that fails
let sender = Pubkey::new_unique();
let recipient = Pubkey::new_unique();
let fee_payer = Pubkey::new_unique();
let system_account = Pubkey::new_from_array([0; 32]);
let mut data = 900050u64.to_be_bytes().to_vec();
while data.len() < 8 {
data.insert(0, 0);
}

let message = Message {
account_keys: vec![
fee_payer,
sender,
transfer_program_account,
recipient,
system_account,
],
header: MessageHeader {
num_required_signatures: 2,
num_readonly_signed_accounts: 0,
num_readonly_unsigned_accounts: 0,
},
instructions: vec![CompiledInstruction {
program_id_index: 2,
accounts: vec![1, 3, 4],
data,
}],
recent_blockhash: Hash::default(),
};
let transaction = Transaction {
signatures: vec![Signature::new_unique(), Signature::new_unique()],
message,
};
let sanitized_transaction =
SanitizedTransaction::try_from_legacy_transaction(transaction).unwrap();
all_transactions.push(sanitized_transaction.clone());
transaction_checks.push((Ok(()), None, Some(20)));

// fee payer
let mut account_data = AccountSharedData::default();
account_data.set_lamports(80000);
mock_bank
.account_shared_data
.insert(fee_payer, account_data);

// Sender without enough funds
let mut account_data = AccountSharedData::default();
account_data.set_lamports(900000);
mock_bank.account_shared_data.insert(sender, account_data);

// recipient
let mut account_data = AccountSharedData::default();
account_data.set_lamports(900000);
mock_bank
.account_shared_data
.insert(recipient, account_data);

// A transaction whose verification has already failed
all_transactions.push(sanitized_transaction);
transaction_checks.push((Err(TransactionError::BlockhashNotFound), None, Some(20)));

(all_transactions, transaction_checks)
}
Expand Down Expand Up @@ -451,7 +510,7 @@ fn svm_integration() {
false,
);

assert_eq!(result.execution_results.len(), 3);
assert_eq!(result.execution_results.len(), 5);
assert!(result.execution_results[0]
.details()
.unwrap()
Expand Down Expand Up @@ -493,4 +552,22 @@ fn svm_integration() {
let clock_data = mock_bank.get_account_shared_data(&Clock::id()).unwrap();
let clock_info: Clock = bincode::deserialize(clock_data.data()).unwrap();
assert_eq!(clock_info.unix_timestamp, time);

assert!(result.execution_results[3]
.details()
.unwrap()
.status
.is_err());
assert!(result.execution_results[3]
.details()
.unwrap()
.log_messages
.as_ref()
.unwrap()
.contains(&"Transfer: insufficient lamports 900000, need 900050".to_string()));

assert!(matches!(
result.execution_results[4],
TransactionExecutionResult::NotExecuted(TransactionError::BlockhashNotFound)
));
}

0 comments on commit b884ea8

Please sign in to comment.