From ae932561565d34179bc26f6f739cc87e93191107 Mon Sep 17 00:00:00 2001 From: Tao Zhu <82401714+tao-stones@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:59:48 -0400 Subject: [PATCH] update RuntimeTransaction to use ComputeBudgetInstructionDetails in static meta (#2772) refactor: update RuntiemTransaction to use ComputeBudgetInstructionDetails in static meta --- .../src/runtime_transaction.rs | 63 +++++++------------ runtime-transaction/src/transaction_meta.rs | 39 +++--------- 2 files changed, 33 insertions(+), 69 deletions(-) diff --git a/runtime-transaction/src/runtime_transaction.rs b/runtime-transaction/src/runtime_transaction.rs index 2a8772ce168977..966a24156d084f 100644 --- a/runtime-transaction/src/runtime_transaction.rs +++ b/runtime-transaction/src/runtime_transaction.rs @@ -11,11 +11,12 @@ //! with its dynamic metadata loaded. use { crate::{ - instructions_processor::process_compute_budget_instructions, + compute_budget_instruction_details::*, transaction_meta::{DynamicMeta, StaticMeta, TransactionMeta}, }, solana_compute_budget::compute_budget_limits::ComputeBudgetLimits, solana_sdk::{ + feature_set::FeatureSet, hash::Hash, message::AddressLoader, pubkey::Pubkey, @@ -26,7 +27,8 @@ use { std::collections::HashSet, }; -#[derive(Debug, Clone, Eq, PartialEq)] +#[cfg_attr(test, derive(Eq, PartialEq))] +#[derive(Debug)] pub struct RuntimeTransaction { transaction: T, // transaction meta is a collection of fields, it is updated @@ -53,14 +55,10 @@ impl StaticMeta for RuntimeTransaction { fn is_simple_vote_tx(&self) -> bool { self.meta.is_simple_vote_tx } - fn compute_unit_limit(&self) -> u32 { - self.meta.compute_unit_limit - } - fn compute_unit_price(&self) -> u64 { - self.meta.compute_unit_price - } - fn loaded_accounts_bytes(&self) -> u32 { - self.meta.loaded_accounts_bytes + fn compute_budget_limits(&self, _feature_set: &FeatureSet) -> Result { + self.meta + .compute_budget_instruction_details + .sanitize_and_convert_to_compute_budget_limits() } } @@ -72,34 +70,24 @@ impl RuntimeTransaction { message_hash: Option, is_simple_vote_tx: Option, ) -> Result { - let mut meta = TransactionMeta::default(); - meta.set_is_simple_vote_tx( - is_simple_vote_tx - .unwrap_or_else(|| is_simple_vote_transaction(&sanitized_versioned_tx)), - ); - - meta.set_message_hash( - 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( + let is_simple_vote_tx = is_simple_vote_tx + .unwrap_or_else(|| is_simple_vote_transaction(&sanitized_versioned_tx)); + let message_hash = + message_hash.unwrap_or_else(|| sanitized_versioned_tx.get_message().message.hash()); + let compute_budget_instruction_details = ComputeBudgetInstructionDetails::try_from( 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, - meta, + meta: TransactionMeta { + message_hash, + is_simple_vote_tx, + compute_budget_instruction_details, + }, }) } } @@ -302,17 +290,14 @@ mod tests { assert_eq!(&hash, runtime_transaction_static.message_hash()); assert!(!runtime_transaction_static.is_simple_vote_tx()); - assert_eq!( - compute_unit_limit, - runtime_transaction_static.compute_unit_limit() - ); - assert_eq!( - compute_unit_price, - runtime_transaction_static.compute_unit_price() - ); + let compute_budget_limits = runtime_transaction_static + .compute_budget_limits(&FeatureSet::default()) + .unwrap(); + assert_eq!(compute_unit_limit, compute_budget_limits.compute_unit_limit); + assert_eq!(compute_unit_price, compute_budget_limits.compute_unit_price); assert_eq!( loaded_accounts_bytes, - runtime_transaction_static.loaded_accounts_bytes() + compute_budget_limits.loaded_accounts_bytes.get() ); } } diff --git a/runtime-transaction/src/transaction_meta.rs b/runtime-transaction/src/transaction_meta.rs index f46fa39c3ab71b..a2b3a746c5ebae 100644 --- a/runtime-transaction/src/transaction_meta.rs +++ b/runtime-transaction/src/transaction_meta.rs @@ -11,16 +11,18 @@ //! The StaticMeta and DynamicMeta traits are accessor traits on the //! RuntimeTransaction types, not the TransactionMeta itself. //! -use solana_sdk::hash::Hash; +use { + crate::compute_budget_instruction_details::ComputeBudgetInstructionDetails, + solana_compute_budget::compute_budget_limits::ComputeBudgetLimits, + solana_sdk::{feature_set::FeatureSet, hash::Hash, transaction::Result}, +}; /// metadata can be extracted statically from sanitized transaction, /// for example: message hash, simple-vote-tx flag, limits set by instructions pub trait StaticMeta { fn message_hash(&self) -> &Hash; fn is_simple_vote_tx(&self) -> bool; - fn compute_unit_limit(&self) -> u32; - fn compute_unit_price(&self) -> u64; - fn loaded_accounts_bytes(&self) -> u32; + fn compute_budget_limits(&self, feature_set: &FeatureSet) -> Result; } /// Statically loaded meta is a supertrait of Dynamically loaded meta, when @@ -30,33 +32,10 @@ pub trait StaticMeta { /// on-chain ALT, examples are: transaction usage costs, nonce account. pub trait DynamicMeta: StaticMeta {} -#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[cfg_attr(test, derive(Eq, PartialEq))] +#[derive(Debug, Default)] pub struct TransactionMeta { pub(crate) message_hash: Hash, pub(crate) is_simple_vote_tx: bool, - pub(crate) compute_unit_limit: u32, - pub(crate) compute_unit_price: u64, - pub(crate) loaded_accounts_bytes: u32, -} - -impl TransactionMeta { - pub(crate) fn set_message_hash(&mut self, message_hash: Hash) { - self.message_hash = message_hash; - } - - pub(crate) fn set_is_simple_vote_tx(&mut self, is_simple_vote_tx: bool) { - self.is_simple_vote_tx = is_simple_vote_tx; - } - - pub(crate) fn set_compute_unit_limit(&mut self, compute_unit_limit: u32) { - self.compute_unit_limit = compute_unit_limit; - } - - pub(crate) fn set_compute_unit_price(&mut self, compute_unit_price: u64) { - self.compute_unit_price = compute_unit_price; - } - - pub(crate) fn set_loaded_accounts_bytes(&mut self, loaded_accounts_bytes: u32) { - self.loaded_accounts_bytes = loaded_accounts_bytes; - } + pub(crate) compute_budget_instruction_details: ComputeBudgetInstructionDetails, }