Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Dec 2, 2024
1 parent ab923d0 commit 7508d01
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
26 changes: 26 additions & 0 deletions crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ 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,
ValidResourceBounds,
};

use crate::bouncer::BouncerConfig;
use crate::execution::call_info::CallInfo;
use crate::transaction::objects::{
CurrentTransactionInfo,
HasRelatedFeeType,
Expand Down Expand Up @@ -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.
Expand Down
56 changes: 43 additions & 13 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -384,12 +384,28 @@ impl AccountTransaction {
&self,
state: &mut dyn State,
tx_context: Arc<TransactionContext>,
remaining_gas: &mut u64,
remaining_gas: &mut GasCounter,
validate: bool,
limit_steps_by_resources: bool,
) -> TransactionExecutionResult<Option<CallInfo>> {
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)
}
Expand Down Expand Up @@ -516,20 +532,34 @@ impl AccountTransaction {
&self,
state: &mut S,
context: &mut EntryPointExecutionContext,
remaining_gas: &mut u64,
remaining_gas: &mut GasCounter,
) -> TransactionExecutionResult<Option<CallInfo>> {
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),
}
}

fn run_non_revertible<S: StateReader>(
&self,
state: &mut TransactionalState<'_, S>,
tx_context: Arc<TransactionContext>,
remaining_gas: &mut u64,
remaining_gas: &mut GasCounter,
validate: bool,
charge_fee: bool,
) -> TransactionExecutionResult<ValidateExecuteCallInfo> {
Expand Down Expand Up @@ -589,7 +619,7 @@ impl AccountTransaction {
&self,
state: &mut TransactionalState<'_, S>,
tx_context: Arc<TransactionContext>,
remaining_gas: &mut u64,
remaining_gas: &mut GasCounter,
validate: bool,
charge_fee: bool,
) -> TransactionExecutionResult<ValidateExecuteCallInfo> {
Expand Down Expand Up @@ -727,7 +757,7 @@ impl AccountTransaction {
fn run_or_revert<S: StateReader>(
&self,
state: &mut TransactionalState<'_, S>,
remaining_gas: &mut u64,
remaining_gas: &mut GasCounter,
tx_context: Arc<TransactionContext>,
validate: bool,
charge_fee: bool,
Expand Down Expand Up @@ -760,7 +790,7 @@ impl<U: UpdatableState> ExecutableTransaction<U> 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,
Expand All @@ -774,7 +804,7 @@ impl<U: UpdatableState> ExecutableTransaction<U> 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,
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet_api/src/execution_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::transaction::fields::{Fee, Resource};
)]
#[derive(
derive_more::Display,
derive_more::Sub,
derive_more::SubAssign,
Clone,
Copy,
Debug,
Expand Down

0 comments on commit 7508d01

Please sign in to comment.