Skip to content

Commit

Permalink
NDEV-3329. Inconsistent gas data when cancel happens (#555)
Browse files Browse the repository at this point in the history
* NDEV-3329. Inconsistent gas data when cancel happens

* NDEV-3329. Show correct GAS in logs when insufficient gas happen
  • Loading branch information
ancientmage authored Nov 22, 2024
1 parent f09800b commit 01731f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion evm_loader/program/src/account/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl<'a> StateAccount<'a> {
}

#[must_use]
fn gas_available(&self) -> U256 {
pub fn gas_available(&self) -> U256 {
self.trx().gas_limit().saturating_sub(self.gas_used())
}

Expand Down
15 changes: 10 additions & 5 deletions evm_loader/program/src/instruction/transaction_cancel.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cmp::min;

use crate::account::{AccountsDB, BalanceAccount, Operator, OperatorBalanceAccount, StateAccount};
use crate::config::DEFAULT_CHAIN_ID;
use crate::debug::log_data;
Expand Down Expand Up @@ -53,17 +55,20 @@ fn execute<'a>(
) -> Result<()> {
let trx_chain_id = storage.trx().chain_id().unwrap_or(DEFAULT_CHAIN_ID);

let used_gas = U256::ZERO;
let total_used_gas = storage.gas_used();
let used_gas = min(
storage.gas_available(),
U256::from(CANCEL_TRX_COST + LAST_ITERATION_COST),
);
let total_used_gas = storage.gas_used() + used_gas;

log_data(&[
b"GAS",
&used_gas.to_le_bytes(),
&total_used_gas.to_le_bytes(),
]);

let gas = U256::from(CANCEL_TRX_COST + LAST_ITERATION_COST);
let priority_fee = priority_fee_txn_calculator::handle_priority_fee(storage.trx(), gas)?;
let _ = storage.consume_gas(gas, priority_fee, accounts.try_operator_balance()); // ignore error
let priority_fee = priority_fee_txn_calculator::handle_priority_fee(storage.trx(), used_gas)?;
let _ = storage.consume_gas(used_gas, priority_fee, accounts.try_operator_balance()); // ignore error

let origin = storage.trx_origin();
let (origin_pubkey, _) = origin.find_balance_address(program_id, trx_chain_id);
Expand Down

0 comments on commit 01731f3

Please sign in to comment.