From c8be06ae6e607ecdcc7a75809fe55444d98261a3 Mon Sep 17 00:00:00 2001 From: Nimrod Weiss Date: Sun, 18 Aug 2024 14:30:24 +0300 Subject: [PATCH] build(fee): current tx_info holds validResourceBounds --- .../src/execution/syscalls/hint_processor.rs | 57 ++++++++++++------- .../syscall_tests/get_execution_info.rs | 7 ++- crates/blockifier/src/transaction/objects.rs | 13 +++-- .../src/transaction/transactions.rs | 6 +- crates/starknet_api/src/transaction.rs | 8 ++- 5 files changed, 55 insertions(+), 36 deletions(-) diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index df847f4c59a..1ec7683f977 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -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; @@ -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. @@ -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 = 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) } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs index fb6f4d0c9d2..ff6dc64a5d7 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs @@ -8,7 +8,6 @@ use starknet_api::felt; use starknet_api::transaction::{ AccountDeploymentData, Calldata, - DeprecatedResourceBoundsMapping, Fee, PaymasterData, Resource, @@ -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 @@ -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, diff --git a/crates/blockifier/src/transaction/objects.rs b/crates/blockifier/src/transaction/objects.rs index 5b6a697211d..088e6da5107 100644 --- a/crates/blockifier/src/transaction/objects.rs +++ b/crates/blockifier/src/transaction/objects.rs @@ -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; @@ -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, @@ -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 { - 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!(), } } } diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 262acbb657d..6ad86911bdf 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -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, @@ -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, @@ -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, diff --git a/crates/starknet_api/src/transaction.rs b/crates/starknet_api/src/transaction.rs index f06b96709b5..d8e6d2437b2 100644 --- a/crates/starknet_api/src/transaction.rs +++ b/crates/starknet_api/src/transaction.rs @@ -1,4 +1,4 @@ -use std::collections::{BTreeMap, HashMap, HashSet}; +use std::collections::{BTreeMap, HashSet}; use std::fmt::Display; use std::sync::Arc; @@ -949,21 +949,23 @@ impl TryFrom> 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> for ValidResourceBounds { +impl TryFrom> for ValidResourceBounds { type Error = StarknetApiError; fn try_from( - raw_resource_bounds: HashMap, + raw_resource_bounds: BTreeMap, ) -> Result { if let (Some(l1_bounds), Some(l2_bounds)) = (raw_resource_bounds.get(&Resource::L1Gas), raw_resource_bounds.get(&Resource::L2Gas))