Skip to content

Commit

Permalink
Check lamports balance before and after tx execution (#33188)
Browse files Browse the repository at this point in the history
Co-authored-by: Alessandro Decina <[email protected]>
  • Loading branch information
t-nelson and alessandrod authored Sep 8, 2023
1 parent b85abeb commit 3fe46af
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ use {
hash::{extend_and_hash, hashv, Hash},
incinerator,
inflation::Inflation,
instruction::CompiledInstruction,
instruction::{CompiledInstruction, InstructionError},
lamports::LamportsError,
message::{AccountKeys, SanitizedMessage},
native_loader,
Expand Down Expand Up @@ -4087,6 +4087,42 @@ impl Bank {

let prev_accounts_data_len = self.load_accounts_data_size();
let transaction_accounts = std::mem::take(&mut loaded_transaction.accounts);

fn transaction_accounts_lamports_sum(
accounts: &[(Pubkey, AccountSharedData)],
message: &SanitizedMessage,
) -> Option<u128> {
let mut lamports_sum = 0u128;
for i in 0..message.account_keys().len() {
let account = match accounts.get(i) {
Some((_, account)) => account,
None => return None,
};

lamports_sum = match lamports_sum.checked_add(u128::from(account.lamports())) {
Some(lamports_sum) => lamports_sum,
None => {
return None;
}
}
}

Some(lamports_sum)
}

let lamports_before_tx =
match transaction_accounts_lamports_sum(&transaction_accounts, tx.message()) {
Some(lamports) => lamports,
None => {
return TransactionExecutionResult::NotExecuted(
TransactionError::InstructionError(
0,
InstructionError::UnbalancedInstruction,
),
)
}
};

let mut transaction_context = TransactionContext::new(
transaction_accounts,
if self
Expand Down Expand Up @@ -4199,6 +4235,18 @@ impl Bank {
total_size_of_touched_accounts,
accounts_resize_delta,
} = transaction_context.into();

if status.is_ok()
&& transaction_accounts_lamports_sum(&accounts, tx.message())
.filter(|lamports_after_tx| lamports_before_tx == *lamports_after_tx)
.is_none()
{
return TransactionExecutionResult::NotExecuted(TransactionError::InstructionError(
0,
InstructionError::UnbalancedInstruction,
));
}

loaded_transaction.accounts = accounts;
if self
.feature_set
Expand Down

0 comments on commit 3fe46af

Please sign in to comment.