Skip to content

Commit

Permalink
Check fee payer before loading all accounts (#25371)
Browse files Browse the repository at this point in the history
* fix vote account loading

* fix clippy and rename some stuff

* fix bug

Co-authored-by: Justin Starry <justin@solana.com>
  • Loading branch information
buffalu and jstarry authored May 27, 2022
1 parent 6de2884 commit e58de2c
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl Accounts {
} else {
// There is no way to predict what program will execute without an error
// If a fee can pay for execution then the program will be scheduled
let mut payer_index = None;
let mut validated_fee_payer = false;
let mut tx_rent: TransactionRent = 0;
let account_keys = message.account_keys();
let mut accounts = Vec::with_capacity(account_keys.len());
Expand All @@ -263,18 +263,15 @@ impl Accounts {
// Fill in an empty account for the program slots.
AccountSharedData::default()
} else {
if payer_index.is_none() {
payer_index = Some(i);
}

#[allow(clippy::collapsible_else_if)]
if solana_sdk::sysvar::instructions::check_id(key) {
Self::construct_instructions_account(
message,
feature_set
.is_active(&feature_set::instructions_sysvar_owned_by_sysvar::id()),
)
} else {
let (account, rent) = if let Some(account_override) =
let (mut account, rent) = if let Some(account_override) =
account_overrides.and_then(|overrides| overrides.get(key))
{
(account_override.clone(), 0)
Expand All @@ -298,6 +295,24 @@ impl Accounts {
.unwrap_or_default()
};

if !validated_fee_payer {
if i != 0 {
warn!("Payer index should be 0! {:?}", tx);
}

Self::validate_fee_payer(
key,
&mut account,
i,
error_counters,
rent_collector,
feature_set,
fee,
)?;

validated_fee_payer = true;
}

if bpf_loader_upgradeable::check_id(account.owner()) {
if message.is_writable(i) && !message.is_upgradeable_loader_present() {
error_counters.invalid_writable_account += 1;
Expand Down Expand Up @@ -346,20 +361,7 @@ impl Accounts {
// accounts.iter().take(message.account_keys.len())
accounts.append(&mut account_deps);

if let Some(payer_index) = payer_index {
if payer_index != 0 {
warn!("Payer index should be 0! {:?}", tx);
}

Self::check_fee_payer_balance(
&mut accounts,
payer_index,
error_counters,
rent_collector,
feature_set,
fee,
)?;

if validated_fee_payer {
let program_indices = message
.instructions()
.iter()
Expand All @@ -386,15 +388,15 @@ impl Accounts {
}
}

fn check_fee_payer_balance(
accounts: &mut [(Pubkey, AccountSharedData)],
fn validate_fee_payer(
payer_address: &Pubkey,
payer_account: &mut AccountSharedData,
payer_index: usize,
error_counters: &mut TransactionErrorMetrics,
rent_collector: &RentCollector,
feature_set: &FeatureSet,
fee: u64,
) -> Result<()> {
let (ref payer_address, ref mut payer_account) = accounts[payer_index];
if payer_account.lamports() == 0 {
error_counters.account_not_found += 1;
return Err(TransactionError::AccountNotFound);
Expand Down

0 comments on commit e58de2c

Please sign in to comment.