Skip to content

Commit

Permalink
try_new_from_sanitized_transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
apfitzge committed Aug 20, 2024
1 parent 4daf93c commit c6d5228
Showing 1 changed file with 64 additions and 10 deletions.
74 changes: 64 additions & 10 deletions runtime-transaction/src/runtime_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -82,20 +82,13 @@ impl RuntimeTransaction<SanitizedVersionedTransaction> {
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,
Expand All @@ -105,6 +98,28 @@ impl RuntimeTransaction<SanitizedVersionedTransaction> {
}

impl RuntimeTransaction<SanitizedTransaction> {
pub fn try_from_sanitized_transaction(
sanitized_transaction: SanitizedTransaction,
) -> Result<Self> {
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<SanitizedVersionedTransaction>,
address_loader: impl AddressLoader,
Expand Down Expand Up @@ -134,6 +149,22 @@ impl RuntimeTransaction<SanitizedTransaction> {
}
}

fn populate_compute_budget_meta<'a>(
meta: &mut TransactionMeta,
program_instructions_iter: impl Iterator<Item = (&'a Pubkey, SVMInstruction<'a>)>,
) -> 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 {
Expand Down Expand Up @@ -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::<SanitizedTransaction>::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();
Expand Down

0 comments on commit c6d5228

Please sign in to comment.