From c6d52282f369a8f6a3d15dc7a6995d32464e94dd Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Tue, 20 Aug 2024 12:22:40 -0500 Subject: [PATCH] try_new_from_sanitized_transaction --- .../src/runtime_transaction.rs | 74 ++++++++++++++++--- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/runtime-transaction/src/runtime_transaction.rs b/runtime-transaction/src/runtime_transaction.rs index 2a8772ce168977..3c0af47980672d 100644 --- a/runtime-transaction/src/runtime_transaction.rs +++ b/runtime-transaction/src/runtime_transaction.rs @@ -22,7 +22,7 @@ use { simple_vote_transaction_checker::is_simple_vote_transaction, transaction::{Result, SanitizedTransaction, SanitizedVersionedTransaction}, }, - solana_svm_transaction::instruction::SVMInstruction, + solana_svm_transaction::{instruction::SVMInstruction, svm_message::SVMMessage}, std::collections::HashSet, }; @@ -82,20 +82,13 @@ impl RuntimeTransaction { message_hash.unwrap_or_else(|| sanitized_versioned_tx.get_message().message.hash()), ); - let ComputeBudgetLimits { - compute_unit_limit, - compute_unit_price, - loaded_accounts_bytes, - .. - } = process_compute_budget_instructions( + populate_compute_budget_meta( + &mut meta, sanitized_versioned_tx .get_message() .program_instructions_iter() .map(|(program_id, ix)| (program_id, SVMInstruction::from(ix))), )?; - meta.set_compute_unit_limit(compute_unit_limit); - meta.set_compute_unit_price(compute_unit_price); - meta.set_loaded_accounts_bytes(loaded_accounts_bytes.get()); Ok(Self { transaction: sanitized_versioned_tx, @@ -105,6 +98,28 @@ impl RuntimeTransaction { } impl RuntimeTransaction { + pub fn try_from_sanitized_transaction( + sanitized_transaction: SanitizedTransaction, + ) -> Result { + let mut meta = TransactionMeta::default(); + + meta.set_message_hash(*sanitized_transaction.message_hash()); + meta.set_is_simple_vote_tx(sanitized_transaction.is_simple_vote_transaction()); + + populate_compute_budget_meta( + &mut meta, + SVMMessage::program_instructions_iter(&sanitized_transaction), + )?; + + let mut tx = Self { + transaction: sanitized_transaction, + meta, + }; + tx.load_dynamic_metadata()?; + + Ok(tx) + } + pub fn try_from( statically_loaded_runtime_tx: RuntimeTransaction, address_loader: impl AddressLoader, @@ -134,6 +149,22 @@ impl RuntimeTransaction { } } +fn populate_compute_budget_meta<'a>( + meta: &mut TransactionMeta, + program_instructions_iter: impl Iterator)>, +) -> Result<()> { + let ComputeBudgetLimits { + compute_unit_limit, + compute_unit_price, + loaded_accounts_bytes, + .. + } = process_compute_budget_instructions(program_instructions_iter)?; + meta.set_compute_unit_limit(compute_unit_limit); + meta.set_compute_unit_price(compute_unit_price); + meta.set_loaded_accounts_bytes(loaded_accounts_bytes.get()); + Ok(()) +} + #[cfg(test)] mod tests { use { @@ -280,6 +311,29 @@ mod tests { assert!(!dynamically_loaded_transaction.is_simple_vote_tx()); } + #[test] + fn try_from_sanitized_transaction() { + let sanitized_versioned_transaction = non_vote_sanitized_versioned_transaction(); + let message_hash = sanitized_versioned_transaction.get_message().message.hash(); + let sanitized_transaction = SanitizedTransaction::try_new( + sanitized_versioned_transaction, + message_hash, + false, + SimpleAddressLoader::Disabled, + &ReservedAccountKeys::empty_key_set(), + ) + .unwrap(); + + let runtime_transaction = + RuntimeTransaction::::try_from_sanitized_transaction( + sanitized_transaction, + ) + .unwrap(); + + assert_eq!(message_hash, *runtime_transaction.message_hash()); + assert!(!runtime_transaction.is_simple_vote_tx()); + } + #[test] fn test_runtime_transaction_static_meta() { let hash = Hash::new_unique();