Skip to content

Commit

Permalink
Simplify program loading error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Aug 15, 2024
1 parent 92acf94 commit f1ac8a6
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions svm/src/account_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ fn load_transaction_accounts<CB: TransactionProcessingCallback>(
let mut tx_rent: TransactionRent = 0;
let account_keys = message.account_keys();
let mut accounts = Vec::with_capacity(account_keys.len());
let mut accounts_found = Vec::with_capacity(account_keys.len());
let mut rent_debits = RentDebits::default();
let mut accumulated_accounts_data_size: u32 = 0;

Expand All @@ -287,7 +286,7 @@ fn load_transaction_accounts<CB: TransactionProcessingCallback>(
.unique()
.collect::<Vec<&u8>>();

let mut collect_loaded_account = |key, (loaded_account, found)| -> Result<()> {
let mut collect_loaded_account = |key, loaded_account| -> Result<()> {
let LoadedTransactionAccount {
account,
loaded_size,
Expand All @@ -305,19 +304,18 @@ fn load_transaction_accounts<CB: TransactionProcessingCallback>(
rent_debits.insert(key, rent_collected, account.lamports());

accounts.push((*key, account));
accounts_found.push(found);
Ok(())
};

// Since the fee payer is always the first account, collect it first. Note
// that account overrides are already applied during fee payer validation so
// it's fine to use the fee payer directly here rather than checking account
// overrides again.
collect_loaded_account(message.fee_payer(), (loaded_fee_payer_account, true))?;
collect_loaded_account(message.fee_payer(), loaded_fee_payer_account)?;

// Attempt to load and collect remaining non-fee payer accounts
for (account_index, account_key) in account_keys.iter().enumerate().skip(1) {
let (loaded_account, account_found) = load_transaction_account(
let loaded_account = load_transaction_account(
callbacks,
message,
account_key,
Expand All @@ -328,7 +326,7 @@ fn load_transaction_accounts<CB: TransactionProcessingCallback>(
rent_collector,
loaded_programs,
)?;
collect_loaded_account(account_key, (loaded_account, account_found))?;
collect_loaded_account(account_key, loaded_account)?;
}

let builtins_start_index = accounts.len();
Expand All @@ -345,12 +343,6 @@ fn load_transaction_accounts<CB: TransactionProcessingCallback>(
return Ok(account_indices);
}

let account_found = accounts_found.get(program_index).unwrap_or(&true);
if !account_found {
error_metrics.account_not_found += 1;
return Err(TransactionError::ProgramAccountNotFound);
}

if !program_account.executable() {
error_metrics.invalid_program_for_execution += 1;
return Err(TransactionError::InvalidProgramForExecution);
Expand Down Expand Up @@ -466,7 +458,6 @@ fn load_transaction_account<CB: TransactionProcessingCallback>(
}
})
.unwrap_or_else(|| {
account_found = false;
let mut default_account = AccountSharedData::default();
// All new accounts must be rent-exempt (enforced in Bank::execute_loaded_transaction).
// Currently, rent collection sets rent_epoch to u64::MAX, but initializing the account
Expand Down Expand Up @@ -705,12 +696,12 @@ mod tests {

let load_results = load_accounts_aux_test(tx, &accounts, &mut error_metrics);

assert_eq!(error_metrics.account_not_found, 1);
assert_eq!(error_metrics.invalid_program_for_execution, 1);
assert_eq!(load_results.len(), 1);
assert!(matches!(
load_results[0],
TransactionLoadResult::FeesOnly(FeesOnlyTransaction {
load_error: TransactionError::ProgramAccountNotFound,
load_error: TransactionError::InvalidProgramForExecution,
..
}),
));
Expand Down Expand Up @@ -935,7 +926,7 @@ mod tests {
assert!(matches!(
load_results[0],
TransactionLoadResult::FeesOnly(FeesOnlyTransaction {
load_error: TransactionError::ProgramAccountNotFound,
load_error: TransactionError::InvalidProgramForExecution,
..
}),
));
Expand Down Expand Up @@ -1381,7 +1372,10 @@ mod tests {
&loaded_programs,
);

assert_eq!(result.err(), Some(TransactionError::ProgramAccountNotFound));
assert_eq!(
result.err(),
Some(TransactionError::InvalidProgramForExecution)
);
}

#[test]
Expand Down

0 comments on commit f1ac8a6

Please sign in to comment.