Skip to content

Commit

Permalink
SVM: account_loader.rs generic over SVMMessage (#2303)
Browse files Browse the repository at this point in the history
  • Loading branch information
apfitzge authored Aug 1, 2024
1 parent 3ca6719 commit e8ec9dc
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions svm/src/account_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ use {
account::{Account, AccountSharedData, ReadableAccount, WritableAccount},
feature_set::{self, FeatureSet},
fee::FeeDetails,
message::SanitizedMessage,
native_loader,
nonce::State as NonceState,
pubkey::Pubkey,
rent::RentDue,
rent_collector::{CollectedInfo, RentCollector, RENT_EXEMPT_RENT_EPOCH},
rent_debits::RentDebits,
saturating_add_assign,
sysvar::{self, instructions::construct_instructions_data},
transaction::{Result, SanitizedTransaction, TransactionError},
sysvar::{
self,
instructions::{construct_instructions_data, BorrowedAccountMeta, BorrowedInstruction},
},
transaction::{Result, TransactionError},
transaction_context::{IndexOfAccount, TransactionAccount},
},
solana_svm_transaction::svm_message::SVMMessage,
solana_system_program::{get_system_account_kind, SystemAccountKind},
std::num::NonZeroU32,
};
Expand Down Expand Up @@ -154,7 +157,7 @@ pub fn validate_fee_payer(
/// second element.
pub(crate) fn load_accounts<CB: TransactionProcessingCallback>(
callbacks: &CB,
txs: &[SanitizedTransaction],
txs: &[impl SVMMessage],
validation_results: Vec<TransactionValidationResult>,
error_metrics: &mut TransactionErrorMetrics,
account_overrides: Option<&AccountOverrides>,
Expand All @@ -166,12 +169,10 @@ pub(crate) fn load_accounts<CB: TransactionProcessingCallback>(
.zip(validation_results)
.map(|etx| match etx {
(tx, Ok(tx_details)) => {
let message = tx.message();

// load transactions
load_transaction_accounts(
callbacks,
message,
tx,
tx_details,
error_metrics,
account_overrides,
Expand All @@ -187,7 +188,7 @@ pub(crate) fn load_accounts<CB: TransactionProcessingCallback>(

fn load_transaction_accounts<CB: TransactionProcessingCallback>(
callbacks: &CB,
message: &SanitizedMessage,
message: &impl SVMMessage,
tx_details: ValidatedTransactionDetails,
error_metrics: &mut TransactionErrorMetrics,
account_overrides: Option<&AccountOverrides>,
Expand All @@ -202,9 +203,8 @@ fn load_transaction_accounts<CB: TransactionProcessingCallback>(
let mut accumulated_accounts_data_size: u32 = 0;

let instruction_accounts = message
.instructions()
.iter()
.flat_map(|instruction| &instruction.accounts)
.instructions_iter()
.flat_map(|instruction| instruction.accounts)
.unique()
.collect::<Vec<&u8>>();

Expand Down Expand Up @@ -290,8 +290,7 @@ fn load_transaction_accounts<CB: TransactionProcessingCallback>(

let builtins_start_index = accounts.len();
let program_indices = message
.instructions()
.iter()
.instructions_iter()
.map(|instruction| {
let mut account_indices = Vec::with_capacity(2);
let program_index = instruction.program_id_index as usize;
Expand Down Expand Up @@ -392,9 +391,32 @@ fn accumulate_and_check_loaded_account_data_size(
}
}

fn construct_instructions_account(message: &SanitizedMessage) -> AccountSharedData {
fn construct_instructions_account(message: &impl SVMMessage) -> AccountSharedData {
let account_keys = message.account_keys();
let mut decompiled_instructions = Vec::with_capacity(message.num_instructions());
for (program_id, instruction) in message.program_instructions_iter() {
let accounts = instruction
.accounts
.iter()
.map(|account_index| {
let account_index = usize::from(*account_index);
BorrowedAccountMeta {
is_signer: message.is_signer(account_index),
is_writable: message.is_writable(account_index),
pubkey: account_keys.get(account_index).unwrap(),
}
})
.collect();

decompiled_instructions.push(BorrowedInstruction {
accounts,
data: instruction.data,
program_id,
});
}

AccountSharedData::from(Account {
data: construct_instructions_data(&message.decompile_instructions()),
data: construct_instructions_data(&decompiled_instructions),
owner: sysvar::id(),
..Account::default()
})
Expand Down

0 comments on commit e8ec9dc

Please sign in to comment.