Skip to content

Commit

Permalink
update RuntimeTransaction to use ComputeBudgetInstructionDetails in s…
Browse files Browse the repository at this point in the history
…tatic meta (#2772)

refactor: update RuntiemTransaction to use ComputeBudgetInstructionDetails in static meta
  • Loading branch information
tao-stones authored Aug 29, 2024
1 parent 1acdfde commit ae93256
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 69 deletions.
63 changes: 24 additions & 39 deletions runtime-transaction/src/runtime_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,7 +27,8 @@ use {
std::collections::HashSet,
};

#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Eq, PartialEq))]
#[derive(Debug)]
pub struct RuntimeTransaction<T> {
transaction: T,
// transaction meta is a collection of fields, it is updated
Expand All @@ -53,14 +55,10 @@ impl<T: StaticMetaAccess> StaticMeta for RuntimeTransaction<T> {
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<ComputeBudgetLimits> {
self.meta
.compute_budget_instruction_details
.sanitize_and_convert_to_compute_budget_limits()
}
}

Expand All @@ -72,34 +70,24 @@ impl RuntimeTransaction<SanitizedVersionedTransaction> {
message_hash: Option<Hash>,
is_simple_vote_tx: Option<bool>,
) -> Result<Self> {
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,
},
})
}
}
Expand Down Expand Up @@ -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()
);
}
}
39 changes: 9 additions & 30 deletions runtime-transaction/src/transaction_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComputeBudgetLimits>;
}

/// Statically loaded meta is a supertrait of Dynamically loaded meta, when
Expand All @@ -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,
}

0 comments on commit ae93256

Please sign in to comment.