From 386a12b053b30772d8815aedba110aa4e2829ac6 Mon Sep 17 00:00:00 2001 From: Nimrod Weiss Date: Mon, 19 Aug 2024 11:38:05 +0300 Subject: [PATCH] build(fee): tx_args holds valid resource bounds --- .../src/execution/syscalls/hint_processor.rs | 48 ++++++-------- crates/blockifier/src/test_utils/declare.rs | 2 +- .../src/test_utils/deploy_account.rs | 2 +- crates/blockifier/src/test_utils/invoke.rs | 2 +- .../src/transaction/transactions.rs | 6 +- crates/mempool/src/mempool.rs | 12 ++-- .../src/starknet_api_test_utils.rs | 4 +- crates/native_blockifier/src/py_declare.rs | 3 +- .../src/py_deploy_account.rs | 3 +- .../src/py_invoke_function.rs | 3 +- .../native_blockifier/src/py_transaction.rs | 14 +++- crates/papyrus_common/src/transaction_hash.rs | 15 ++--- crates/papyrus_rpc/src/v0_6/transaction.rs | 17 +++++ crates/papyrus_rpc/src/v0_7/transaction.rs | 17 +++++ .../src/serialization/serializers.rs | 17 ++++- crates/papyrus_test_utils/src/lib.rs | 18 +++++- .../src/executable_transaction.rs | 9 +-- crates/starknet_api/src/rpc_transaction.rs | 17 +++++ crates/starknet_api/src/transaction.rs | 64 +++++++++++++++++-- crates/starknet_api/src/transaction_hash.rs | 15 ++--- .../src/reader/objects/test_utils.rs | 8 +-- .../src/reader/objects/transaction.rs | 8 +-- 22 files changed, 211 insertions(+), 93 deletions(-) diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index 146f3183f7..77bca1d5ca 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -469,35 +469,25 @@ impl<'a> SyscallHintProcessor<'a> { 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 { - 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, - ] - } - 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), - ] - } - }; + let l1_gas_bounds = tx_info.resource_bounds.get_l1_bounds(); + let l2_gas_bounds = tx_info.resource_bounds.get_l2_bounds(); + let mut flat_resource_bounds = vec![ + l1_gas_as_felt, + Felt::from(l1_gas_bounds.max_amount), + Felt::from(l1_gas_bounds.max_price_per_unit), + l2_gas_as_felt, + Felt::from(l2_gas_bounds.max_amount), + Felt::from(l2_gas_bounds.max_price_per_unit), + ]; + if let ValidResourceBounds::AllResources(AllResourceBounds { l1_data_gas, .. }) = + tx_info.resource_bounds + { + flat_resource_bounds.extend(vec![ + 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/test_utils/declare.rs b/crates/blockifier/src/test_utils/declare.rs index ab5470c130..eb794a97f6 100644 --- a/crates/blockifier/src/test_utils/declare.rs +++ b/crates/blockifier/src/test_utils/declare.rs @@ -108,7 +108,7 @@ pub fn declare_tx(declare_tx_args: DeclareTxArgs, class_info: ClassInfo) -> Acco starknet_api::transaction::DeclareTransaction::V3(DeclareTransactionV3 { signature: declare_tx_args.signature, sender_address: declare_tx_args.sender_address, - resource_bounds: declare_tx_args.resource_bounds, + resource_bounds: declare_tx_args.resource_bounds.try_into().expect("todo"), tip: declare_tx_args.tip, nonce_data_availability_mode: declare_tx_args.nonce_data_availability_mode, fee_data_availability_mode: declare_tx_args.fee_data_availability_mode, diff --git a/crates/blockifier/src/test_utils/deploy_account.rs b/crates/blockifier/src/test_utils/deploy_account.rs index 1ae7a58340..128a430945 100644 --- a/crates/blockifier/src/test_utils/deploy_account.rs +++ b/crates/blockifier/src/test_utils/deploy_account.rs @@ -96,7 +96,7 @@ pub fn deploy_account_tx( } else if deploy_tx_args.version == TransactionVersion::THREE { starknet_api::transaction::DeployAccountTransaction::V3(DeployAccountTransactionV3 { signature: deploy_tx_args.signature, - resource_bounds: deploy_tx_args.resource_bounds, + resource_bounds: deploy_tx_args.resource_bounds.try_into().expect("todo"), tip: deploy_tx_args.tip, nonce_data_availability_mode: deploy_tx_args.nonce_data_availability_mode, fee_data_availability_mode: deploy_tx_args.fee_data_availability_mode, diff --git a/crates/blockifier/src/test_utils/invoke.rs b/crates/blockifier/src/test_utils/invoke.rs index b1e025bc0e..64b6847960 100644 --- a/crates/blockifier/src/test_utils/invoke.rs +++ b/crates/blockifier/src/test_utils/invoke.rs @@ -96,7 +96,7 @@ pub fn invoke_tx(invoke_args: InvokeTxArgs) -> InvokeTransaction { }) } else if invoke_args.version == TransactionVersion::THREE { starknet_api::transaction::InvokeTransaction::V3(InvokeTransactionV3 { - resource_bounds: invoke_args.resource_bounds, + resource_bounds: invoke_args.resource_bounds.try_into().expect("todo"), calldata: invoke_args.calldata, sender_address: invoke_args.sender_address, nonce: invoke_args.nonce, diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index b75965e8e7..098112ecdb 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) => { Ok(TransactionInfo::Current(CurrentTransactionInfo { common_fields, - resource_bounds: tx.resource_bounds.clone().try_into()?, + resource_bounds: tx.resource_bounds.clone(), tip: tx.tip, nonce_data_availability_mode: tx.nonce_data_availability_mode, fee_data_availability_mode: tx.fee_data_availability_mode, @@ -411,7 +411,7 @@ impl TransactionInfoCreator for DeployAccountTransaction { starknet_api::transaction::DeployAccountTransaction::V3(tx) => { Ok(TransactionInfo::Current(CurrentTransactionInfo { common_fields, - resource_bounds: tx.resource_bounds.clone().try_into()?, + resource_bounds: tx.resource_bounds.clone(), tip: tx.tip, nonce_data_availability_mode: tx.nonce_data_availability_mode, fee_data_availability_mode: tx.fee_data_availability_mode, @@ -535,7 +535,7 @@ impl TransactionInfoCreator for InvokeTransaction { starknet_api::transaction::InvokeTransaction::V3(tx) => { Ok(TransactionInfo::Current(CurrentTransactionInfo { common_fields, - resource_bounds: tx.resource_bounds.clone().try_into()?, + resource_bounds: tx.resource_bounds.clone(), 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/mempool/src/mempool.rs b/crates/mempool/src/mempool.rs index 9d600ebba8..fe9e6515f2 100644 --- a/crates/mempool/src/mempool.rs +++ b/crates/mempool/src/mempool.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::executable_transaction::Transaction; -use starknet_api::transaction::{DeprecatedResourceBoundsMapping, Resource, Tip, TransactionHash}; +use starknet_api::transaction::{Tip, TransactionHash, ValidResourceBounds}; use starknet_mempool_types::errors::MempoolError; use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput, MempoolResult}; @@ -194,13 +194,13 @@ impl Mempool { /// TODO(Mohammad): rename this struct to `ThinTransaction` once that name /// becomes available, to better reflect its purpose and usage. /// TODO(Mohammad): restore the Copy once ResourceBoundsMapping implements it. -#[derive(Clone, Debug, Default, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct TransactionReference { pub sender_address: ContractAddress, pub nonce: Nonce, pub tx_hash: TransactionHash, pub tip: Tip, - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, } impl TransactionReference { @@ -218,10 +218,6 @@ impl TransactionReference { } pub fn get_l2_gas_price(&self) -> u128 { - self.resource_bounds - .0 - .get(&Resource::L2Gas) - .map(|bounds| bounds.max_price_per_unit) - .expect("Expected a valid L2 gas resource bounds.") + self.resource_bounds.get_l2_bounds().max_price_per_unit } } diff --git a/crates/mempool_test_utils/src/starknet_api_test_utils.rs b/crates/mempool_test_utils/src/starknet_api_test_utils.rs index 7ae045da60..fbae2eeabc 100644 --- a/crates/mempool_test_utils/src/starknet_api_test_utils.rs +++ b/crates/mempool_test_utils/src/starknet_api_test_utils.rs @@ -23,13 +23,13 @@ use starknet_api::transaction::{ AccountDeploymentData, Calldata, ContractAddressSalt, - DeprecatedResourceBoundsMapping as ExecutableResourceBoundsMapping, PaymasterData, ResourceBounds, Tip, TransactionHash, TransactionSignature, TransactionVersion, + ValidResourceBounds, }; use starknet_api::{calldata, felt}; use starknet_types_core::felt::Felt; @@ -554,7 +554,7 @@ pub fn create_executable_tx( sender_address, tip, nonce, - resource_bounds: ExecutableResourceBoundsMapping::default(), + resource_bounds: ValidResourceBounds::L1Gas(ResourceBounds::default()), signature: TransactionSignature::default(), calldata: Calldata::default(), nonce_data_availability_mode: DataAvailabilityMode::L1, diff --git a/crates/native_blockifier/src/py_declare.rs b/crates/native_blockifier/src/py_declare.rs index 9e01caaaaf..b8d74535f0 100644 --- a/crates/native_blockifier/src/py_declare.rs +++ b/crates/native_blockifier/src/py_declare.rs @@ -10,7 +10,6 @@ use starknet_api::transaction::{ DeclareTransactionV0V1, DeclareTransactionV2, DeclareTransactionV3, - DeprecatedResourceBoundsMapping, Fee, PaymasterData, Tip, @@ -88,7 +87,7 @@ impl TryFrom for DeclareTransactionV3 { type Error = NativeBlockifierInputError; fn try_from(tx: PyDeclareTransactionV3) -> Result { Ok(Self { - resource_bounds: DeprecatedResourceBoundsMapping::try_from(tx.resource_bounds)?, + resource_bounds: tx.resource_bounds.try_into()?, tip: Tip(tx.tip), signature: TransactionSignature(from_py_felts(tx.signature)), nonce: Nonce(tx.nonce.0), diff --git a/crates/native_blockifier/src/py_deploy_account.rs b/crates/native_blockifier/src/py_deploy_account.rs index a5671842a0..b25b464103 100644 --- a/crates/native_blockifier/src/py_deploy_account.rs +++ b/crates/native_blockifier/src/py_deploy_account.rs @@ -10,7 +10,6 @@ use starknet_api::transaction::{ ContractAddressSalt, DeployAccountTransactionV1, DeployAccountTransactionV3, - DeprecatedResourceBoundsMapping, Fee, PaymasterData, Tip, @@ -64,7 +63,7 @@ impl TryFrom for DeployAccountTransactionV3 { type Error = NativeBlockifierInputError; fn try_from(tx: PyDeployAccountTransactionV3) -> Result { Ok(Self { - resource_bounds: DeprecatedResourceBoundsMapping::try_from(tx.resource_bounds)?, + resource_bounds: tx.resource_bounds.try_into()?, tip: Tip(tx.tip), signature: TransactionSignature(from_py_felts(tx.signature)), nonce: Nonce(tx.nonce.0), diff --git a/crates/native_blockifier/src/py_invoke_function.rs b/crates/native_blockifier/src/py_invoke_function.rs index 64f7559917..974d0b9b1d 100644 --- a/crates/native_blockifier/src/py_invoke_function.rs +++ b/crates/native_blockifier/src/py_invoke_function.rs @@ -9,7 +9,6 @@ use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::transaction::{ AccountDeploymentData, Calldata, - DeprecatedResourceBoundsMapping, Fee, InvokeTransactionV0, InvokeTransactionV1, @@ -87,7 +86,7 @@ impl TryFrom for InvokeTransactionV3 { type Error = NativeBlockifierInputError; fn try_from(tx: PyInvokeTransactionV3) -> Result { Ok(Self { - resource_bounds: DeprecatedResourceBoundsMapping::try_from(tx.resource_bounds)?, + resource_bounds: tx.resource_bounds.try_into()?, tip: Tip(tx.tip), signature: TransactionSignature(from_py_felts(tx.signature)), nonce: Nonce(tx.nonce.0), diff --git a/crates/native_blockifier/src/py_transaction.rs b/crates/native_blockifier/src/py_transaction.rs index 443ed57c90..aaa6e071e9 100644 --- a/crates/native_blockifier/src/py_transaction.rs +++ b/crates/native_blockifier/src/py_transaction.rs @@ -11,7 +11,12 @@ use blockifier::transaction::transaction_execution::Transaction; use blockifier::transaction::transaction_types::TransactionType; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; -use starknet_api::transaction::{Resource, ResourceBounds}; +use starknet_api::transaction::{ + DeprecatedResourceBoundsMapping, + Resource, + ResourceBounds, + ValidResourceBounds, +}; use starknet_api::StarknetApiError; use crate::errors::{NativeBlockifierInputError, NativeBlockifierResult}; @@ -84,6 +89,13 @@ impl TryFrom } } +impl TryFrom for ValidResourceBounds { + type Error = StarknetApiError; + fn try_from(py_resource_bounds_mapping: PyResourceBoundsMapping) -> Result { + DeprecatedResourceBoundsMapping::try_from(py_resource_bounds_mapping)?.try_into() + } +} + #[derive(Clone)] pub enum PyDataAvailabilityMode { L1 = 0, diff --git a/crates/papyrus_common/src/transaction_hash.rs b/crates/papyrus_common/src/transaction_hash.rs index 6b4da55441..23ef713879 100644 --- a/crates/papyrus_common/src/transaction_hash.rs +++ b/crates/papyrus_common/src/transaction_hash.rs @@ -16,18 +16,17 @@ use starknet_api::transaction::{ DeployAccountTransactionV1, DeployAccountTransactionV3, DeployTransaction, - DeprecatedResourceBoundsMapping, InvokeTransaction, InvokeTransactionV0, InvokeTransactionV1, InvokeTransactionV3, L1HandlerTransaction, - Resource, ResourceBounds, Tip, Transaction, TransactionHash, TransactionVersion, + ValidResourceBounds, }; use starknet_api::StarknetApiError; use starknet_types_core::felt::Felt; @@ -220,16 +219,14 @@ pub(crate) fn ascii_as_felt(ascii_str: &str) -> Result { // An implementation of the SNIP: https://github.com/EvyatarO/SNIPs/blob/snip-8/SNIPS/snip-8.md fn get_tip_resource_bounds_hash( - resource_bounds_mapping: &DeprecatedResourceBoundsMapping, + resource_bounds: &ValidResourceBounds, tip: &Tip, ) -> Result { - let l1_resource_bounds = - resource_bounds_mapping.0.get(&Resource::L1Gas).expect("Missing l1 resource"); - let l1_resource = get_concat_resource(l1_resource_bounds, L1_GAS)?; + let l1_resource_bounds = resource_bounds.get_l1_bounds(); + let l2_resource_bounds = resource_bounds.get_l2_bounds(); - let l2_resource_bounds = - resource_bounds_mapping.0.get(&Resource::L2Gas).expect("Missing l2 resource"); - let l2_resource = get_concat_resource(l2_resource_bounds, L2_GAS)?; + let l1_resource = get_concat_resource(&l1_resource_bounds, L1_GAS)?; + let l2_resource = get_concat_resource(&l2_resource_bounds, L2_GAS)?; Ok(HashChain::new() .chain(&tip.0.into()) diff --git a/crates/papyrus_rpc/src/v0_6/transaction.rs b/crates/papyrus_rpc/src/v0_6/transaction.rs index 9bed607338..7601abab7d 100644 --- a/crates/papyrus_rpc/src/v0_6/transaction.rs +++ b/crates/papyrus_rpc/src/v0_6/transaction.rs @@ -157,6 +157,23 @@ impl From for starknet_api::transaction::DeprecatedResour } } +impl From for starknet_api::transaction::ValidResourceBounds { + fn from(value: ResourceBoundsMapping) -> Self { + Self::L1Gas(value.l1_gas) + } +} + +impl From for ResourceBoundsMapping { + fn from(value: starknet_api::transaction::ValidResourceBounds) -> Self { + match value { + starknet_api::transaction::ValidResourceBounds::L1Gas(l1_gas) => { + Self { l1_gas, l2_gas: ResourceBounds::default() } + } + starknet_api::transaction::ValidResourceBounds::AllResources(_) => todo!(), + } + } +} + impl From for ResourceBoundsMapping { fn from(value: starknet_api::transaction::DeprecatedResourceBoundsMapping) -> Self { Self { diff --git a/crates/papyrus_rpc/src/v0_7/transaction.rs b/crates/papyrus_rpc/src/v0_7/transaction.rs index 8f4596b664..7e3e4eb8a4 100644 --- a/crates/papyrus_rpc/src/v0_7/transaction.rs +++ b/crates/papyrus_rpc/src/v0_7/transaction.rs @@ -169,6 +169,23 @@ impl From for Resour } } +impl From for starknet_api::transaction::ValidResourceBounds { + fn from(value: ResourceBoundsMapping) -> Self { + Self::L1Gas(value.l1_gas) + } +} + +impl From for ResourceBoundsMapping { + fn from(value: starknet_api::transaction::ValidResourceBounds) -> Self { + match value { + starknet_api::transaction::ValidResourceBounds::L1Gas(l1_gas) => { + Self { l1_gas, l2_gas: ResourceBounds::default() } + } + starknet_api::transaction::ValidResourceBounds::AllResources(_) => todo!(), + } + } +} + #[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] pub struct DeclareTransactionV3 { pub resource_bounds: ResourceBoundsMapping, diff --git a/crates/papyrus_storage/src/serialization/serializers.rs b/crates/papyrus_storage/src/serialization/serializers.rs index 2b19e9b01b..e58e5366be 100644 --- a/crates/papyrus_storage/src/serialization/serializers.rs +++ b/crates/papyrus_storage/src/serialization/serializers.rs @@ -76,6 +76,7 @@ use starknet_api::state::{ }; use starknet_api::transaction::{ AccountDeploymentData, + AllResourceBounds, Calldata, ContractAddressSalt, DeclareTransaction, @@ -119,6 +120,7 @@ use starknet_api::transaction::{ TransactionOutput, TransactionSignature, TransactionVersion, + ValidResourceBounds, }; use starknet_types_core::felt::Felt; use tracing::warn; @@ -147,6 +149,11 @@ const COMPRESSION_THRESHOLD_BYTES: usize = 384; auto_storage_serde! { pub struct AccountDeploymentData(pub Vec); + pub struct AllResourceBounds { + pub l1_gas: ResourceBounds, + pub l2_gas: ResourceBounds, + pub l1_data_gas: ResourceBounds, + } pub struct BlockHash(pub StarkHash); pub struct StorageBlockHeader { pub block_hash: BlockHash, @@ -211,7 +218,7 @@ auto_storage_serde! { pub sender_address: ContractAddress, } pub struct DeclareTransactionV3 { - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, pub tip: Tip, pub signature: TransactionSignature, pub nonce: Nonce, @@ -419,6 +426,10 @@ auto_storage_serde! { } pub struct TransactionSignature(pub Vec); pub struct TransactionVersion(pub Felt); + pub enum ValidResourceBounds { + L1Gas(ResourceBounds) = 0, + AllResources(AllResourceBounds) = 1, + } pub struct Version{ pub major: u32, pub minor: u32, @@ -1162,7 +1173,7 @@ auto_storage_serde_conditionally_compressed! { } pub struct DeployAccountTransactionV3 { - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, pub tip: Tip, pub signature: TransactionSignature, pub nonce: Nonce, @@ -1198,7 +1209,7 @@ auto_storage_serde_conditionally_compressed! { } pub struct InvokeTransactionV3 { - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, pub tip: Tip, pub signature: TransactionSignature, pub nonce: Nonce, diff --git a/crates/papyrus_test_utils/src/lib.rs b/crates/papyrus_test_utils/src/lib.rs index 20c39d27ee..d62893e045 100644 --- a/crates/papyrus_test_utils/src/lib.rs +++ b/crates/papyrus_test_utils/src/lib.rs @@ -99,6 +99,7 @@ use starknet_api::state::{ }; use starknet_api::transaction::{ AccountDeploymentData, + AllResourceBounds, Calldata, ContractAddressSalt, DeclareTransaction, @@ -142,6 +143,7 @@ use starknet_api::transaction::{ TransactionOutput, TransactionSignature, TransactionVersion, + ValidResourceBounds, }; use starknet_types_core::felt::Felt; @@ -420,6 +422,11 @@ pub trait GetTestInstance: Sized { auto_impl_get_test_instance! { pub struct AccountDeploymentData(pub Vec); + pub struct AllResourceBounds { + pub l1_gas: ResourceBounds, + pub l2_gas: ResourceBounds, + pub l1_data_gas: ResourceBounds, + } pub struct BlockHash(pub StarkHash); pub struct BlockHeader { pub block_hash: BlockHash, @@ -517,7 +524,7 @@ auto_impl_get_test_instance! { pub sender_address: ContractAddress, } pub struct DeclareTransactionV3 { - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, pub tip: Tip, pub signature: TransactionSignature, pub nonce: Nonce, @@ -550,7 +557,7 @@ auto_impl_get_test_instance! { pub constructor_calldata: Calldata, } pub struct DeployAccountTransactionV3 { - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, pub tip: Tip, pub signature: TransactionSignature, pub nonce: Nonce, @@ -657,7 +664,7 @@ auto_impl_get_test_instance! { pub calldata: Calldata, } pub struct InvokeTransactionV3 { - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, pub tip: Tip, pub signature: TransactionSignature, pub nonce: Nonce, @@ -784,6 +791,11 @@ auto_impl_get_test_instance! { pub r#type: String, } + pub enum ValidResourceBounds { + L1Gas(ResourceBounds) = 0, + AllResources(AllResourceBounds) = 1, + } + pub struct CasmContractClass { pub prime: BigUint, pub compiler_version: String, diff --git a/crates/starknet_api/src/executable_transaction.rs b/crates/starknet_api/src/executable_transaction.rs index 1e9c981a28..5506a74abd 100644 --- a/crates/starknet_api/src/executable_transaction.rs +++ b/crates/starknet_api/src/executable_transaction.rs @@ -8,12 +8,13 @@ use crate::transaction::{ AccountDeploymentData, Calldata, ContractAddressSalt, - DeprecatedResourceBoundsMapping, PaymasterData, + ResourceBounds, Tip, TransactionHash, TransactionSignature, TransactionVersion, + ValidResourceBounds, }; macro_rules! implement_inner_tx_getter_calls { @@ -29,7 +30,7 @@ macro_rules! implement_getter_calls { $(pub fn $field(&self) -> $field_type { self.$field })* - }; +}; } /// Represents a paid Starknet transaction. @@ -83,7 +84,7 @@ impl Transaction { } } - pub fn resource_bounds(&self) -> Option<&DeprecatedResourceBoundsMapping> { + pub fn resource_bounds(&self) -> Option<&ValidResourceBounds> { match self { Transaction::Declare(declare_tx) => match &declare_tx.tx { crate::transaction::DeclareTransaction::V3(tx_v3) => Some(&tx_v3.resource_bounds), @@ -114,7 +115,7 @@ impl Transaction { sender_address, tip: *rpc_tx.tip(), nonce: *rpc_tx.nonce(), - resource_bounds: DeprecatedResourceBoundsMapping::default(), + resource_bounds: ValidResourceBounds::L1Gas(ResourceBounds::default()), signature: TransactionSignature::default(), calldata: Calldata::default(), nonce_data_availability_mode: DataAvailabilityMode::L1, diff --git a/crates/starknet_api/src/rpc_transaction.rs b/crates/starknet_api/src/rpc_transaction.rs index 2c402c2d9b..d1453a0a33 100644 --- a/crates/starknet_api/src/rpc_transaction.rs +++ b/crates/starknet_api/src/rpc_transaction.rs @@ -206,3 +206,20 @@ impl From for crate::transaction::DeprecatedResourceBound crate::transaction::DeprecatedResourceBoundsMapping(map) } } + +impl From for crate::transaction::ValidResourceBounds { + fn from(value: ResourceBoundsMapping) -> Self { + Self::L1Gas(value.l1_gas) + } +} + +impl From for ResourceBoundsMapping { + fn from(value: crate::transaction::ValidResourceBounds) -> Self { + match value { + crate::transaction::ValidResourceBounds::L1Gas(l1_gas) => { + Self { l1_gas, l2_gas: ResourceBounds::default() } + } + crate::transaction::ValidResourceBounds::AllResources(_) => todo!(), + } + } +} diff --git a/crates/starknet_api/src/transaction.rs b/crates/starknet_api/src/transaction.rs index 9cf1c94110..6527c74932 100644 --- a/crates/starknet_api/src/transaction.rs +++ b/crates/starknet_api/src/transaction.rs @@ -209,7 +209,7 @@ impl TransactionHasher for DeclareTransactionV2 { /// A declare V3 transaction. #[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] pub struct DeclareTransactionV3 { - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, pub tip: Tip, pub signature: TransactionSignature, pub nonce: Nonce, @@ -318,7 +318,7 @@ impl TransactionHasher for DeployAccountTransactionV1 { /// A deploy account V3 transaction. #[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] pub struct DeployAccountTransactionV3 { - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, pub tip: Tip, pub signature: TransactionSignature, pub nonce: Nonce, @@ -455,7 +455,7 @@ impl TransactionHasher for InvokeTransactionV1 { /// An invoke V3 transaction. #[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] pub struct InvokeTransactionV3 { - pub resource_bounds: DeprecatedResourceBoundsMapping, + pub resource_bounds: ValidResourceBounds, pub tip: Tip, pub signature: TransactionSignature, pub nonce: Nonce, @@ -951,13 +951,29 @@ impl TryFrom> for DeprecatedResourceBoundsMappin } } -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub enum ValidResourceBounds { L1Gas(ResourceBounds), // Pre 0.13.3. Only L1 gas. L2 bounds are signed but never used. AllResources(AllResourceBounds), } -#[derive(Clone, Debug, Eq, PartialEq)] +impl ValidResourceBounds { + pub fn get_l1_bounds(&self) -> ResourceBounds { + match self { + Self::L1Gas(l1_bounds) => *l1_bounds, + Self::AllResources(AllResourceBounds { l1_gas, .. }) => *l1_gas, + } + } + + pub fn get_l2_bounds(&self) -> ResourceBounds { + match self { + Self::L1Gas(_) => ResourceBounds::default(), + Self::AllResources(AllResourceBounds { l2_gas, .. }) => *l2_gas, + } + } +} + +#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct AllResourceBounds { pub l1_gas: ResourceBounds, pub l2_gas: ResourceBounds, @@ -975,6 +991,44 @@ impl AllResourceBounds { } } +/// Deserializes raw resource bounds, given as map, into valid resource bounds. +impl<'de> Deserialize<'de> for ValidResourceBounds { + fn deserialize(de: D) -> Result + where + D: Deserializer<'de>, + { + let raw_resource_bounds: BTreeMap = Deserialize::deserialize(de)?; + ValidResourceBounds::try_from(DeprecatedResourceBoundsMapping(raw_resource_bounds)) + .map_err(serde::de::Error::custom) + } +} + +/// Serializes ValidResourceBounds as map for BC. +// TODO(Nimrod): Can this be removed? +impl Serialize for ValidResourceBounds { + fn serialize(&self, s: S) -> Result + where + S: Serializer, + { + let map = match self { + ValidResourceBounds::L1Gas(l1_gas) => BTreeMap::from([ + (Resource::L1Gas, *l1_gas), + (Resource::L2Gas, ResourceBounds::default()), + ]), + ValidResourceBounds::AllResources(AllResourceBounds { + l1_gas, + l2_gas, + l1_data_gas, + }) => BTreeMap::from([ + (Resource::L1Gas, *l1_gas), + (Resource::L2Gas, *l2_gas), + (Resource::L1DataGas, *l1_data_gas), + ]), + }; + DeprecatedResourceBoundsMapping(map).serialize(s) + } +} + impl TryFrom for ValidResourceBounds { type Error = StarknetApiError; fn try_from( diff --git a/crates/starknet_api/src/transaction_hash.rs b/crates/starknet_api/src/transaction_hash.rs index 55cdc33caa..7d08337e53 100644 --- a/crates/starknet_api/src/transaction_hash.rs +++ b/crates/starknet_api/src/transaction_hash.rs @@ -14,18 +14,17 @@ use crate::transaction::{ DeployAccountTransactionV1, DeployAccountTransactionV3, DeployTransaction, - DeprecatedResourceBoundsMapping, InvokeTransaction, InvokeTransactionV0, InvokeTransactionV1, InvokeTransactionV3, L1HandlerTransaction, - Resource, ResourceBounds, Tip, Transaction, TransactionHash, TransactionVersion, + ValidResourceBounds, }; use crate::StarknetApiError; @@ -175,16 +174,14 @@ pub(crate) fn ascii_as_felt(ascii_str: &str) -> Result { // An implementation of the SNIP: https://github.com/EvyatarO/SNIPs/blob/snip-8/SNIPS/snip-8.md fn get_tip_resource_bounds_hash( - resource_bounds_mapping: &DeprecatedResourceBoundsMapping, + resource_bounds: &ValidResourceBounds, tip: &Tip, ) -> Result { - let l1_resource_bounds = - resource_bounds_mapping.0.get(&Resource::L1Gas).expect("Missing l1 resource"); - let l1_resource = get_concat_resource(l1_resource_bounds, L1_GAS)?; + let l1_resource_bounds = resource_bounds.get_l1_bounds(); + let l2_resource_bounds = resource_bounds.get_l2_bounds(); - let l2_resource_bounds = - resource_bounds_mapping.0.get(&Resource::L2Gas).expect("Missing l2 resource"); - let l2_resource = get_concat_resource(l2_resource_bounds, L2_GAS)?; + let l1_resource = get_concat_resource(&l1_resource_bounds, L1_GAS)?; + let l2_resource = get_concat_resource(&l2_resource_bounds, L2_GAS)?; Ok(HashChain::new() .chain(&tip.0.into()) diff --git a/crates/starknet_client/src/reader/objects/test_utils.rs b/crates/starknet_client/src/reader/objects/test_utils.rs index 3876eeb723..f4d0f8491e 100644 --- a/crates/starknet_client/src/reader/objects/test_utils.rs +++ b/crates/starknet_client/src/reader/objects/test_utils.rs @@ -16,7 +16,6 @@ use starknet_api::transaction::{ AccountDeploymentData, Calldata, ContractAddressSalt, - DeprecatedResourceBoundsMapping, Event, Fee, L1ToL2Payload, @@ -27,6 +26,7 @@ use starknet_api::transaction::{ TransactionOffsetInBlock, TransactionSignature, TransactionVersion, + ValidResourceBounds, }; use starknet_types_core::felt::Felt; @@ -60,7 +60,7 @@ auto_impl_get_test_instance! { Reserved = 0, } pub struct IntermediateDeclareTransaction { - pub resource_bounds: Option, + pub resource_bounds: Option, pub tip: Option, pub signature: TransactionSignature, pub nonce: Nonce, @@ -76,7 +76,7 @@ auto_impl_get_test_instance! { pub transaction_hash: TransactionHash, } pub struct IntermediateDeployAccountTransaction { - pub resource_bounds: Option, + pub resource_bounds: Option, pub tip: Option, pub signature: TransactionSignature, pub nonce: Nonce, @@ -100,7 +100,7 @@ auto_impl_get_test_instance! { pub version: TransactionVersion, } pub struct IntermediateInvokeTransaction { - pub resource_bounds: Option, + pub resource_bounds: Option, pub tip: Option, pub calldata: Calldata, pub sender_address: ContractAddress, diff --git a/crates/starknet_client/src/reader/objects/transaction.rs b/crates/starknet_client/src/reader/objects/transaction.rs index 839ce41a74..4121cb26d8 100644 --- a/crates/starknet_client/src/reader/objects/transaction.rs +++ b/crates/starknet_client/src/reader/objects/transaction.rs @@ -23,7 +23,6 @@ use starknet_api::transaction::{ DeclareTransactionOutput, DeployAccountTransactionOutput, DeployTransactionOutput, - DeprecatedResourceBoundsMapping, Event, Fee, InvokeTransactionOutput, @@ -40,6 +39,7 @@ use starknet_api::transaction::{ TransactionOutput, TransactionSignature, TransactionVersion, + ValidResourceBounds, }; use strum_macros::EnumIter; use tracing::error; @@ -178,7 +178,7 @@ impl From for starknet_api::data_availability::Dat #[serde(deny_unknown_fields)] pub struct IntermediateDeclareTransaction { #[serde(skip_serializing_if = "Option::is_none")] - pub resource_bounds: Option, + pub resource_bounds: Option, #[serde(skip_serializing_if = "Option::is_none")] pub tip: Option, pub signature: TransactionSignature, @@ -347,7 +347,7 @@ impl From for starknet_api::transaction::DeployTransaction { #[serde(deny_unknown_fields)] pub struct IntermediateDeployAccountTransaction { #[serde(skip_serializing_if = "Option::is_none")] - pub resource_bounds: Option, + pub resource_bounds: Option, #[serde(skip_serializing_if = "Option::is_none")] pub tip: Option, pub signature: TransactionSignature, @@ -470,7 +470,7 @@ impl TryFrom #[serde(deny_unknown_fields)] pub struct IntermediateInvokeTransaction { #[serde(skip_serializing_if = "Option::is_none")] - pub resource_bounds: Option, + pub resource_bounds: Option, #[serde(skip_serializing_if = "Option::is_none")] pub tip: Option, pub calldata: Calldata,