From 7508d01dc23e079549c8a394abc2e0b541b33d15 Mon Sep 17 00:00:00 2001 From: Aner Ben Efraim Date: Mon, 2 Dec 2024 13:44:57 +0200 Subject: [PATCH] WIP --- crates/blockifier/src/context.rs | 26 +++++++++ .../src/transaction/account_transaction.rs | 56 ++++++++++++++----- .../starknet_api/src/execution_resources.rs | 2 + 3 files changed, 71 insertions(+), 13 deletions(-) diff --git a/crates/blockifier/src/context.rs b/crates/blockifier/src/context.rs index bd69be574f5..4faa94da7ac 100644 --- a/crates/blockifier/src/context.rs +++ b/crates/blockifier/src/context.rs @@ -5,6 +5,7 @@ use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam}; use serde::{Deserialize, Serialize}; use starknet_api::block::{BlockInfo, FeeType, GasPriceVector}; use starknet_api::core::{ChainId, ContractAddress}; +use starknet_api::execution_resources::GasAmount; use starknet_api::transaction::fields::{ AllResourceBounds, GasVectorComputationMode, @@ -12,6 +13,7 @@ use starknet_api::transaction::fields::{ }; use crate::bouncer::BouncerConfig; +use crate::execution::call_info::CallInfo; use crate::transaction::objects::{ CurrentTransactionInfo, HasRelatedFeeType, @@ -58,6 +60,30 @@ impl TransactionContext { } } +pub(crate) struct GasCounter { + pub(crate) spent_gas: GasAmount, + pub(crate) remaining_gas: GasAmount, +} + +impl GasCounter { + pub(crate) fn new(initial_gas: u64) -> Self { + GasCounter { spent_gas: GasAmount(0), remaining_gas: GasAmount(initial_gas) } + } + + pub(crate) fn spend(&mut self, amount: GasAmount) { + self.spent_gas += amount; + self.remaining_gas -= amount; + } + + pub(crate) fn cap_usage(&self, amount: GasAmount) -> u64 { + self.remaining_gas.min(amount).0 + } + + pub(crate) fn subtract_used_gas(&mut self, call_info: &CallInfo) { + self.spend(call_info.charged_resources.gas_for_fee); + } +} + #[derive(Clone, Debug)] pub struct BlockContext { // TODO(Yoni, 1/10/2024): consider making these fields public. diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index ca61f75576c..4bab83a2387 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -26,7 +26,7 @@ use starknet_api::transaction::fields::{ use starknet_api::transaction::{constants, TransactionHash, TransactionVersion}; use starknet_types_core::felt::Felt; -use crate::context::{BlockContext, TransactionContext}; +use crate::context::{BlockContext, GasCounter, TransactionContext}; use crate::execution::call_info::CallInfo; use crate::execution::contract_class::RunnableCompiledClass; use crate::execution::entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext}; @@ -384,12 +384,28 @@ impl AccountTransaction { &self, state: &mut dyn State, tx_context: Arc, - remaining_gas: &mut u64, + remaining_gas: &mut GasCounter, validate: bool, limit_steps_by_resources: bool, ) -> TransactionExecutionResult> { if validate { - self.validate_tx(state, tx_context, remaining_gas, limit_steps_by_resources) + // TODO(Aner): cap the gas for validation. + let remaining_validation_gas = &mut remaining_gas + .cap_usage(tx_context.block_context.versioned_constants.validate_max_sierra_gas); + let call_info = self.validate_tx( + state, + tx_context, + remaining_validation_gas, + limit_steps_by_resources, + )?; + match call_info { + // TODO(Aner): Update the gas counter. + Some(call_info) => { + remaining_gas.subtract_used_gas(&call_info); + Ok(Some(call_info)) + } + None => Ok(None), + } } else { Ok(None) } @@ -516,12 +532,26 @@ impl AccountTransaction { &self, state: &mut S, context: &mut EntryPointExecutionContext, - remaining_gas: &mut u64, + remaining_gas: &mut GasCounter, ) -> TransactionExecutionResult> { - match &self.tx { - Transaction::Declare(tx) => tx.run_execute(state, context, remaining_gas), - Transaction::DeployAccount(tx) => tx.run_execute(state, context, remaining_gas), - Transaction::Invoke(tx) => tx.run_execute(state, context, remaining_gas), + // TODO(Aner): cap the gas usage for execution. + let remaining_execution_gas = &mut remaining_gas + .cap_usage(context.tx_context.block_context.versioned_constants.execute_max_sierra_gas); + + let call_info = match &self.tx { + Transaction::Declare(tx) => tx.run_execute(state, context, remaining_execution_gas), + Transaction::DeployAccount(tx) => { + tx.run_execute(state, context, remaining_execution_gas) + } + Transaction::Invoke(tx) => tx.run_execute(state, context, remaining_execution_gas), + }?; + match call_info { + // TODO(Aner): Update the gas counter. + Some(call_info) => { + remaining_gas.subtract_used_gas(&call_info); + Ok(Some(call_info)) + } + None => Ok(None), } } @@ -529,7 +559,7 @@ impl AccountTransaction { &self, state: &mut TransactionalState<'_, S>, tx_context: Arc, - remaining_gas: &mut u64, + remaining_gas: &mut GasCounter, validate: bool, charge_fee: bool, ) -> TransactionExecutionResult { @@ -589,7 +619,7 @@ impl AccountTransaction { &self, state: &mut TransactionalState<'_, S>, tx_context: Arc, - remaining_gas: &mut u64, + remaining_gas: &mut GasCounter, validate: bool, charge_fee: bool, ) -> TransactionExecutionResult { @@ -727,7 +757,7 @@ impl AccountTransaction { fn run_or_revert( &self, state: &mut TransactionalState<'_, S>, - remaining_gas: &mut u64, + remaining_gas: &mut GasCounter, tx_context: Arc, validate: bool, charge_fee: bool, @@ -760,7 +790,7 @@ impl ExecutableTransaction for AccountTransaction { )?; // Run validation and execution. - let mut remaining_gas = tx_context.initial_sierra_gas(); + let initial_gas = tx_context.initial_sierra_gas(); let ValidateExecuteCallInfo { validate_call_info, execute_call_info, @@ -774,7 +804,7 @@ impl ExecutableTransaction for AccountTransaction { }, } = self.run_or_revert( state, - &mut remaining_gas, + &mut GasCounter::new(initial_gas), tx_context.clone(), execution_flags.validate, execution_flags.charge_fee, diff --git a/crates/starknet_api/src/execution_resources.rs b/crates/starknet_api/src/execution_resources.rs index 9c564434643..49fa5d083c2 100644 --- a/crates/starknet_api/src/execution_resources.rs +++ b/crates/starknet_api/src/execution_resources.rs @@ -13,6 +13,8 @@ use crate::transaction::fields::{Fee, Resource}; )] #[derive( derive_more::Display, + derive_more::Sub, + derive_more::SubAssign, Clone, Copy, Debug,