Skip to content

Commit

Permalink
build(fee): current tx_info holds validResourceBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Aug 20, 2024
1 parent 59b3b0b commit c8be06a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 36 deletions.
57 changes: 36 additions & 21 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cairo_vm::vm::vm_core::VirtualMachine;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::state::StorageKey;
use starknet_api::transaction::{Calldata, Resource};
use starknet_api::transaction::{AllResourceBounds, Calldata};
use starknet_api::StarknetApiError;
use starknet_types_core::felt::{Felt, FromStrError};
use thiserror::Error;
Expand Down Expand Up @@ -205,6 +205,8 @@ pub const INVALID_ARGUMENT: &str =
pub const L1_GAS: &str = "0x00000000000000000000000000000000000000000000000000004c315f474153";
// "L2_GAS";
pub const L2_GAS: &str = "0x00000000000000000000000000000000000000000000000000004c325f474153";
// "L1_DATA_GAS"
pub const L1_DATA_GAS: &str = "0x0000000000000000000000000000000000000000004c315f444154415f474153";

/// Executes Starknet syscalls (stateful protocol hints) during the execution of an entry point
/// call.
Expand Down Expand Up @@ -462,26 +464,39 @@ impl<'a> SyscallHintProcessor<'a> {
vm: &mut VirtualMachine,
tx_info: &CurrentTransactionInfo,
) -> SyscallResult<(Relocatable, Relocatable)> {
let l1_gas = Felt::from_hex(L1_GAS).map_err(SyscallExecutionError::from)?;
let l2_gas = Felt::from_hex(L2_GAS).map_err(SyscallExecutionError::from)?;
let flat_resource_bounds: Vec<Felt> = tx_info
.resource_bounds
.0
.iter()
.flat_map(|(resource, resource_bounds)| {
let resource = match resource {
Resource::L1Gas => l1_gas,
Resource::L2Gas => l2_gas,
Resource::L1DataGas => todo!(),
};

vec![
resource,
Felt::from(resource_bounds.max_amount),
Felt::from(resource_bounds.max_price_per_unit),
]
})
.collect();
let l1_gas_as_felt = Felt::from_hex(L1_GAS).map_err(SyscallExecutionError::from)?;
let l2_gas_as_felt = Felt::from_hex(L2_GAS).map_err(SyscallExecutionError::from)?;
let l1_data_gas_as_felt =
Felt::from_hex(L1_DATA_GAS).map_err(SyscallExecutionError::from)?;

let flat_resource_bounds =
match tx_info.resource_bounds {
starknet_api::transaction::ValidResourceBounds::L1Gas(l1_bounds) => {
vec![
l1_gas_as_felt,
Felt::from(l1_bounds.max_amount),
Felt::from(l1_bounds.max_price_per_unit),
l2_gas_as_felt,
Felt::ZERO,
Felt::ZERO,
]
}
starknet_api::transaction::ValidResourceBounds::AllResources(
AllResourceBounds { l1_gas, l2_gas, l1_data_gas },
) => {
vec![
l1_gas_as_felt,
Felt::from(l1_gas.max_amount),
Felt::from(l1_gas.max_price_per_unit),
l2_gas_as_felt,
Felt::from(l2_gas.max_amount),
Felt::from(l2_gas.max_price_per_unit),
l1_data_gas_as_felt,
Felt::from(l1_data_gas.max_amount),
Felt::from(l1_data_gas.max_price_per_unit),
]
}
};

self.allocate_data_segment(vm, &flat_resource_bounds)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use starknet_api::felt;
use starknet_api::transaction::{
AccountDeploymentData,
Calldata,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
Resource,
Expand Down Expand Up @@ -213,7 +212,7 @@ fn test_get_execution_info(
only_query,
..Default::default()
},
resource_bounds: DeprecatedResourceBoundsMapping(BTreeMap::from([
resource_bounds: BTreeMap::from([
(
Resource::L1Gas,
// TODO(Ori, 1/2/2024): Write an indicative expect message explaining why
Expand All @@ -227,7 +226,9 @@ fn test_get_execution_info(
},
),
(Resource::L2Gas, ResourceBounds { max_amount: 0, max_price_per_unit: 0 }),
])),
])
.try_into()
.unwrap(),
tip: Tip::default(),
nonce_data_availability_mode: DataAvailabilityMode::L1,
fee_data_availability_mode: DataAvailabilityMode::L1,
Expand Down
13 changes: 7 additions & 6 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::transaction::{
AccountDeploymentData,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
Resource,
ResourceBounds,
Tip,
TransactionHash,
TransactionSignature,
TransactionVersion,
ValidResourceBounds,
};
use starknet_types_core::felt::Felt;
use strum_macros::EnumIter;
Expand Down Expand Up @@ -125,7 +124,7 @@ impl HasRelatedFeeType for TransactionInfo {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CurrentTransactionInfo {
pub common_fields: CommonAccountFields,
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
Expand All @@ -135,10 +134,12 @@ pub struct CurrentTransactionInfo {

impl CurrentTransactionInfo {
/// Fetch the L1 resource bounds, if they exist.
// TODO(Nimrod): Consider removing this function and add equivalent method to
// `ValidResourceBounds`.
pub fn l1_resource_bounds(&self) -> TransactionFeeResult<ResourceBounds> {
match self.resource_bounds.0.get(&Resource::L1Gas).copied() {
Some(bounds) => Ok(bounds),
None => Err(TransactionFeeError::MissingL1GasBounds),
match self.resource_bounds {
ValidResourceBounds::L1Gas(bounds) => Ok(bounds),
ValidResourceBounds::AllResources { .. } => todo!(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl TransactionInfoCreator for DeclareTransaction {
starknet_api::transaction::DeclareTransaction::V3(tx) => {
TransactionInfo::Current(CurrentTransactionInfo {
common_fields,
resource_bounds: tx.resource_bounds.clone(),
resource_bounds: tx.resource_bounds.0.clone().try_into().expect("todo"),
tip: tx.tip,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
Expand Down Expand Up @@ -401,7 +401,7 @@ impl TransactionInfoCreator for DeployAccountTransaction {
starknet_api::transaction::DeployAccountTransaction::V3(tx) => {
TransactionInfo::Current(CurrentTransactionInfo {
common_fields,
resource_bounds: tx.resource_bounds.clone(),
resource_bounds: tx.resource_bounds.0.clone().try_into().expect("todo"),
tip: tx.tip,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
Expand Down Expand Up @@ -519,7 +519,7 @@ impl TransactionInfoCreator for InvokeTransaction {
starknet_api::transaction::InvokeTransaction::V3(tx) => {
TransactionInfo::Current(CurrentTransactionInfo {
common_fields,
resource_bounds: tx.resource_bounds.clone(),
resource_bounds: tx.resource_bounds.0.clone().try_into().expect("todo"),
tip: tx.tip,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
Expand Down
8 changes: 5 additions & 3 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::{BTreeMap, HashSet};
use std::fmt::Display;
use std::sync::Arc;

Expand Down Expand Up @@ -949,21 +949,23 @@ impl TryFrom<Vec<(Resource, ResourceBounds)>> for DeprecatedResourceBoundsMappin
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ValidResourceBounds {
L1Gas(ResourceBounds), // Pre 0.13.3. L2 bounds are signed but never used.
AllResources(AllResourceBounds),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AllResourceBounds {
pub l1_gas: ResourceBounds,
pub l2_gas: ResourceBounds,
pub l1_data_gas: ResourceBounds,
}

impl TryFrom<HashMap<Resource, ResourceBounds>> for ValidResourceBounds {
impl TryFrom<BTreeMap<Resource, ResourceBounds>> for ValidResourceBounds {
type Error = StarknetApiError;
fn try_from(
raw_resource_bounds: HashMap<Resource, ResourceBounds>,
raw_resource_bounds: BTreeMap<Resource, ResourceBounds>,
) -> Result<Self, Self::Error> {
if let (Some(l1_bounds), Some(l2_bounds)) =
(raw_resource_bounds.get(&Resource::L1Gas), raw_resource_bounds.get(&Resource::L2Gas))
Expand Down

0 comments on commit c8be06a

Please sign in to comment.