Skip to content

Commit

Permalink
SVM: add check for check results length mismatch (anza-xyz#3259)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec authored Oct 22, 2024
1 parent 2013eb6 commit a2d20e5
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
environment: &TransactionProcessingEnvironment,
config: &TransactionProcessingConfig,
) -> LoadAndExecuteSanitizedTransactionsOutput {
// If `check_results` does not have the same length as `sanitized_txs`,
// transactions could be truncated as a result of `.iter().zip()` in
// many of the below methods.
// See <https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.zip>.
debug_assert_eq!(
sanitized_txs.len(),
check_results.len(),
"Length of check_results does not match length of sanitized_txs"
);

// Initialize metrics.
let mut error_metrics = TransactionErrorMetrics::default();
let mut execute_timings = ExecuteTimings::default();
Expand Down Expand Up @@ -1038,6 +1048,7 @@ mod tests {
transaction::{SanitizedTransaction, Transaction, TransactionError},
transaction_context::TransactionContext,
},
test_case::test_case,
};

fn new_unchecked_sanitized_message(message: Message) -> SanitizedMessage {
Expand Down Expand Up @@ -1112,6 +1123,51 @@ mod tests {
}
}

#[test_case(1; "Check results too small")]
#[test_case(3; "Check results too large")]
#[should_panic(expected = "Length of check_results does not match length of sanitized_txs")]
fn test_check_results_txs_length_mismatch(check_results_len: usize) {
let sanitized_message = new_unchecked_sanitized_message(Message {
account_keys: vec![Pubkey::new_from_array([0; 32])],
header: MessageHeader::default(),
instructions: vec![CompiledInstruction {
program_id_index: 0,
accounts: vec![],
data: vec![],
}],
recent_blockhash: Hash::default(),
});

// Transactions, length 2.
let sanitized_txs = vec![
SanitizedTransaction::new_for_tests(
sanitized_message,
vec![Signature::new_unique()],
false,
);
2
];

let check_results = vec![
TransactionCheckResult::Ok(CheckedTransactionDetails {
nonce: None,
lamports_per_signature: 0
});
check_results_len
];

let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let callback = MockBankCallback::default();

batch_processor.load_and_execute_sanitized_transactions(
&callback,
&sanitized_txs,
check_results,
&TransactionProcessingEnvironment::default(),
&TransactionProcessingConfig::default(),
);
}

#[test]
fn test_inner_instructions_list_from_instruction_trace() {
let instruction_trace = [1, 2, 1, 1, 2, 3, 2];
Expand Down

0 comments on commit a2d20e5

Please sign in to comment.