Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
feat(fee): add DA GasVector to TransactionExecutionInfo
Browse files Browse the repository at this point in the history
Signed-off-by: Dori Medini <[email protected]>
  • Loading branch information
dorimedini-starkware committed Jan 31, 2024
1 parent e5c746b commit ec0d79a
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 18 deletions.
11 changes: 8 additions & 3 deletions crates/blockifier/src/fee/actual_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ use crate::abi::constants as abi_constants;
use crate::block_context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::execution::entry_point::ExecutionResources;
use crate::fee::gas_usage::calculate_tx_gas_usage_vector;
use crate::fee::gas_usage::{calculate_da_gas_usage, calculate_tx_gas_usage_vector};
use crate::state::cached_state::{CachedState, StateChanges, StateChangesCount};
use crate::state::state_api::{StateReader, StateResult};
use crate::transaction::objects::{
AccountTransactionContext, HasRelatedFeeType, ResourcesMapping, TransactionExecutionResult,
AccountTransactionContext, GasVector, HasRelatedFeeType, ResourcesMapping,
TransactionExecutionResult,
};
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transaction_utils::calculate_tx_resources;

// TODO(Gilad): Use everywhere instead of passing the `actual_{fee,resources}` tuple, which often
// get passed around together.
#[derive(Default)]
pub struct ActualCost {
pub actual_fee: Fee,
pub da_gas: GasVector,
pub actual_resources: ResourcesMapping,
}

Expand Down Expand Up @@ -133,6 +136,8 @@ impl<'a> ActualCostBuilder<'a> {
n_reverted_steps: usize,
) -> TransactionExecutionResult<ActualCost> {
let state_changes_count = StateChangesCount::from(&self.state_changes);
let da_gas =
calculate_da_gas_usage(state_changes_count, self.block_context.block_info.use_kzg_da);
let non_optional_call_infos =
self.validate_call_info.into_iter().chain(self.execute_call_info);
// Gas usage for SHARP costs and Starknet L1-L2 messages. Includes gas usage for data
Expand Down Expand Up @@ -164,6 +169,6 @@ impl<'a> ActualCostBuilder<'a> {
Fee(0)
};

Ok(ActualCost { actual_fee, actual_resources })
Ok(ActualCost { actual_fee, da_gas, actual_resources })
}
}
4 changes: 2 additions & 2 deletions crates/blockifier/src/fee/fee_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use starknet_api::hash::StarkFelt;
use starknet_api::transaction::Fee;
use thiserror::Error;

use super::gas_usage::compute_discounted_gas_from_gas_vector;
use crate::block_context::{BlockContext, BlockInfo, ChainInfo};
use crate::fee::actual_cost::ActualCost;
use crate::fee::fee_utils::{
calculate_tx_gas_vector, get_balance_and_if_covers_fee, get_fee_by_gas_vector,
};
use crate::fee::gas_usage::compute_discounted_gas_from_gas_vector;
use crate::state::state_api::StateReader;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::objects::{
Expand Down Expand Up @@ -95,7 +95,7 @@ impl FeeCheckReport {
account_tx_context: &AccountTransactionContext,
actual_cost: &ActualCost,
) -> TransactionExecutionResult<()> {
let ActualCost { actual_fee, actual_resources } = actual_cost;
let ActualCost { actual_fee, actual_resources, .. } = actual_cost;

// First, compare the actual resources used against the upper bound(s) defined by the
// sender.
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn test_discounted_gas_overdraft(
(constants::L1_GAS_USAGE.to_string(), l1_gas_used),
(constants::BLOB_GAS_USAGE.to_string(), l1_data_gas_used),
])),
..Default::default()
};
let report = PostExecutionReport::new(
&mut state,
Expand Down
16 changes: 16 additions & 0 deletions crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ pub fn calculate_tx_gas_usage_vector<'a>(
})
}

pub fn calculate_da_gas_usage(
state_changes_count: StateChangesCount,
use_kzg_da: bool,
) -> GasVector {
GasVector {
l1_gas: if use_kzg_da {
0
} else {
u128_from_usize(get_onchain_data_cost(state_changes_count))
.expect("Onchain DA segment length conversion failure.")
},
blob_gas: u128_from_usize(calculate_tx_blob_gas_usage(state_changes_count, use_kzg_da))
.expect("Blob gas for DA conversion failed."),
}
}

/// Returns the blob-gas (data-gas) needed to publish the transaction's state diff (if use_kzg_da is
/// false, zero blob is needed).
// TODO(Aner, 30/1/24) Refactor: create a new function get_da_gas_cost(state_changes_count,
Expand Down
8 changes: 5 additions & 3 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ impl AccountTransaction {
post_execution_error.to_string(),
ActualCost {
actual_fee: post_execution_report.recommended_fee(),
actual_resources: revert_cost.actual_resources,
..revert_cost
},
))
}
Expand Down Expand Up @@ -550,7 +550,7 @@ impl AccountTransaction {
execution_context.error_trace(),
ActualCost {
actual_fee: post_execution_report.recommended_fee(),
actual_resources: revert_cost.actual_resources,
..revert_cost
},
))
}
Expand Down Expand Up @@ -640,7 +640,8 @@ impl<S: StateReader> ExecutableTransaction<S> for AccountTransaction {
validate_call_info,
execute_call_info,
revert_error,
final_cost: ActualCost { actual_fee: final_fee, actual_resources: final_resources },
final_cost:
ActualCost { actual_fee: final_fee, da_gas, actual_resources: final_resources },
} = self.run_or_revert(state, &mut remaining_gas, block_context, validate, charge_fee)?;

let fee_transfer_call_info =
Expand All @@ -651,6 +652,7 @@ impl<S: StateReader> ExecutableTransaction<S> for AccountTransaction {
execute_call_info,
fee_transfer_call_info,
actual_fee: final_fee,
da_gas,
actual_resources: final_resources,
revert_error,
};
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ pub struct TransactionExecutionInfo {
pub fee_transfer_call_info: Option<CallInfo>,
/// The actual fee that was charged (in Wei).
pub actual_fee: Fee,
/// Actual gas consumption the transaction is charged for.
pub da_gas: GasVector,
/// Actual execution resources the transaction is charged for,
/// including L1 gas and additional OS resources estimation.
pub actual_resources: ResourcesMapping,
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<S: StateReader> ExecutableTransaction<S> for L1HandlerTransaction {
self.run_execute(state, &mut execution_resources, &mut context, &mut remaining_gas)?;
let l1_handler_payload_size = self.payload_size();

let ActualCost { actual_fee, actual_resources } =
let ActualCost { actual_fee, da_gas, actual_resources } =
ActualCost::builder_for_l1_handler(block_context, tx_context, l1_handler_payload_size)
.with_execute_call_info(&execute_call_info)
.try_add_state_changes(state)?
Expand All @@ -123,6 +123,7 @@ impl<S: StateReader> ExecutableTransaction<S> for L1HandlerTransaction {
execute_call_info,
fee_transfer_call_info: None,
actual_fee: Fee::default(),
da_gas,
actual_resources,
revert_error: None,
})
Expand Down
37 changes: 29 additions & 8 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use crate::execution::errors::{EntryPointExecutionError, VirtualMachineExecution
use crate::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_felt};
use crate::fee::fee_utils::calculate_tx_fee;
use crate::fee::gas_usage::{
calculate_tx_blob_gas_usage, calculate_tx_gas_usage_vector, estimate_minimal_gas_vector,
get_onchain_data_cost,
calculate_da_gas_usage, calculate_tx_blob_gas_usage, calculate_tx_gas_usage_vector,
estimate_minimal_gas_vector, get_onchain_data_cost,
};
use crate::state::cached_state::{CachedState, StateChangesCount};
use crate::state::errors::StateError;
Expand Down Expand Up @@ -414,6 +414,8 @@ fn test_invoke_tx(
n_modified_contracts: 1,
..StateChangesCount::default()
};
let expected_da_gas =
calculate_da_gas_usage(state_changes_count, block_context.block_info.use_kzg_da);
let (expected_gas_usage, expected_blob_gas_usage) = match use_kzg_da {
true => (0, calculate_tx_blob_gas_usage(state_changes_count, use_kzg_da)),
false => (get_onchain_data_cost(state_changes_count), 0),
Expand All @@ -423,6 +425,7 @@ fn test_invoke_tx(
execute_call_info: expected_execute_call_info,
fee_transfer_call_info: expected_fee_transfer_call_info,
actual_fee: expected_actual_fee,
da_gas: expected_da_gas,
actual_resources: ResourcesMapping(HashMap::from([
(abi_constants::BLOB_GAS_USAGE.to_string(), expected_blob_gas_usage),
(abi_constants::L1_GAS_USAGE.to_string(), expected_gas_usage),
Expand Down Expand Up @@ -1047,10 +1050,8 @@ fn declare_validate_callinfo(
}
}

/// Returns the expected used L1 gas and blob gas (according to use_kzg_da flag) due to execution of
/// a declare transaction.
fn declare_expected_gas_vector(version: TransactionVersion, use_kzg_da: bool) -> GasVector {
let state_changes_count = match version {
fn declare_expected_state_changes(version: TransactionVersion) -> StateChangesCount {
match version {
TransactionVersion::ZERO => StateChangesCount {
n_storage_updates: 1, // Sender balance.
..StateChangesCount::default()
Expand All @@ -1067,7 +1068,13 @@ fn declare_expected_gas_vector(version: TransactionVersion, use_kzg_da: bool) ->
..StateChangesCount::default()
},
version => panic!("Unsupported version {version:?}."),
};
}
}

/// Returns the expected used L1 gas and blob gas (according to use_kzg_da flag) due to execution of
/// a declare transaction.
fn declare_expected_gas_vector(version: TransactionVersion, use_kzg_da: bool) -> GasVector {
let state_changes_count = declare_expected_state_changes(version);

let expected_gas_usage = match use_kzg_da {
true => 0,
Expand Down Expand Up @@ -1128,6 +1135,10 @@ fn test_declare_tx(
);

// Build expected fee transfer call info.
let expected_da_gas = calculate_da_gas_usage(
declare_expected_state_changes(tx_version),
block_context.block_info.use_kzg_da,
);
let expected_actual_fee =
calculate_tx_fee(&actual_execution_info.actual_resources, block_context, fee_type).unwrap();
let expected_fee_transfer_call_info = expected_fee_transfer_call_info(
Expand All @@ -1146,6 +1157,7 @@ fn test_declare_tx(
execute_call_info: None,
fee_transfer_call_info: expected_fee_transfer_call_info,
actual_fee: expected_actual_fee,
da_gas: expected_da_gas,
revert_error: None,
actual_resources: ResourcesMapping(HashMap::from([
(abi_constants::L1_GAS_USAGE.to_string(), expected_gas_usage.try_into().unwrap()),
Expand Down Expand Up @@ -1278,6 +1290,8 @@ fn test_deploy_account_tx(
n_class_hash_updates: 1,
..StateChangesCount::default()
};
let expected_da_gas =
calculate_da_gas_usage(state_changes_count, block_context.block_info.use_kzg_da);
let (expected_gas_usage, expected_blob_gas_usage) = match use_kzg_da {
true => (0, calculate_tx_blob_gas_usage(state_changes_count, use_kzg_da)),
false => (get_onchain_data_cost(state_changes_count), 0),
Expand All @@ -1288,6 +1302,7 @@ fn test_deploy_account_tx(
execute_call_info: expected_execute_call_info,
fee_transfer_call_info: expected_fee_transfer_call_info,
actual_fee: expected_actual_fee,
da_gas: expected_da_gas,
revert_error: None,
actual_resources: ResourcesMapping(HashMap::from([
(abi_constants::L1_GAS_USAGE.to_string(), expected_gas_usage),
Expand Down Expand Up @@ -1721,11 +1736,16 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
..Default::default()
};

// Build the expected resource mapping.
// Build the expected resource mapping and gas usage.
let (expected_gas_usage, expected_blob_gas_usage) = match use_kzg_da {
true => (16023, 128),
false => (17675, 0),
};
let expected_da_gas = if use_kzg_da {
GasVector { l1_gas: 0, blob_gas: 128 }
} else {
GasVector { l1_gas: 1652, blob_gas: 0 }
};

let expected_resource_mapping = ResourcesMapping(HashMap::from([
(HASH_BUILTIN_NAME.to_string(), 11),
Expand All @@ -1741,6 +1761,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
execute_call_info: Some(expected_call_info),
fee_transfer_call_info: None,
actual_fee: Fee(0),
da_gas: expected_da_gas,
actual_resources: expected_resource_mapping,
revert_error: None,
};
Expand Down
20 changes: 19 additions & 1 deletion crates/native_blockifier/src/py_transaction_execution_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};

use blockifier::execution::call_info::{CallInfo, OrderedEvent, OrderedL2ToL1Message};
use blockifier::execution::entry_point::CallType;
use blockifier::transaction::objects::TransactionExecutionInfo;
use blockifier::transaction::objects::{GasVector, TransactionExecutionInfo};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources;
use pyo3::prelude::*;
use starknet_api::deprecated_contract_class::EntryPointType;
Expand All @@ -21,6 +21,8 @@ pub struct PyTransactionExecutionInfo {
#[pyo3(get)]
pub actual_fee: u128,
#[pyo3(get)]
pub da_gas: PyGasVector,
#[pyo3(get)]
pub actual_resources: HashMap<String, usize>,
#[pyo3(get)]
pub revert_error: Option<String>,
Expand All @@ -34,12 +36,28 @@ impl From<TransactionExecutionInfo> for PyTransactionExecutionInfo {
execute_call_info: info.execute_call_info.map(PyCallInfo::from),
fee_transfer_call_info: info.fee_transfer_call_info.map(PyCallInfo::from),
actual_fee: info.actual_fee.0,
da_gas: info.da_gas.into(),
actual_resources: info.actual_resources.0,
revert_error: info.revert_error,
}
}
}

#[pyclass]
#[derive(Clone)]
pub struct PyGasVector {
#[pyo3(get)]
pub l1_gas: u128,
#[pyo3(get)]
pub blob_gas: u128,
}

impl From<GasVector> for PyGasVector {
fn from(value: GasVector) -> Self {
Self { l1_gas: value.l1_gas, blob_gas: value.blob_gas }
}
}

#[pyclass]
#[derive(Clone)]
pub struct PyCallInfo {
Expand Down

0 comments on commit ec0d79a

Please sign in to comment.