From e8ec9dc55effbf515a0f825b8165610ccc5519b7 Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Thu, 1 Aug 2024 13:45:49 -0400 Subject: [PATCH] SVM: account_loader.rs generic over SVMMessage (#2303) --- svm/src/account_loader.rs | 52 ++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/svm/src/account_loader.rs b/svm/src/account_loader.rs index 8d1fef920798cc..4338a5e77d8925 100644 --- a/svm/src/account_loader.rs +++ b/svm/src/account_loader.rs @@ -11,7 +11,6 @@ use { account::{Account, AccountSharedData, ReadableAccount, WritableAccount}, feature_set::{self, FeatureSet}, fee::FeeDetails, - message::SanitizedMessage, native_loader, nonce::State as NonceState, pubkey::Pubkey, @@ -19,10 +18,14 @@ use { 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, }; @@ -154,7 +157,7 @@ pub fn validate_fee_payer( /// second element. pub(crate) fn load_accounts( callbacks: &CB, - txs: &[SanitizedTransaction], + txs: &[impl SVMMessage], validation_results: Vec, error_metrics: &mut TransactionErrorMetrics, account_overrides: Option<&AccountOverrides>, @@ -166,12 +169,10 @@ pub(crate) fn load_accounts( .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, @@ -187,7 +188,7 @@ pub(crate) fn load_accounts( fn load_transaction_accounts( callbacks: &CB, - message: &SanitizedMessage, + message: &impl SVMMessage, tx_details: ValidatedTransactionDetails, error_metrics: &mut TransactionErrorMetrics, account_overrides: Option<&AccountOverrides>, @@ -202,9 +203,8 @@ fn load_transaction_accounts( 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::>(); @@ -290,8 +290,7 @@ fn load_transaction_accounts( 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; @@ -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() })