From e35af6ee34344340a2ff65b2496435b3156744e4 Mon Sep 17 00:00:00 2001 From: Nimrod Weiss Date: Mon, 19 Aug 2024 11:51:38 +0300 Subject: [PATCH] refactor(fee): change valid resource bound to tupled enum --- .../src/execution/syscalls/hint_processor.rs | 20 ++++++--------- crates/papyrus_common/src/transaction_hash.rs | 4 ++- .../src/serialization/serializers.rs | 10 ++++++++ crates/papyrus_test_utils/src/lib.rs | 8 +++++- crates/starknet_api/src/transaction.rs | 25 ++++++++++++------- crates/starknet_api/src/transaction_hash.rs | 4 ++- 6 files changed, 47 insertions(+), 24 deletions(-) diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index 2f99a2f884c..c72eab6caa5 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -469,7 +469,7 @@ 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 { + let flat_resource_bounds = match &tx_info.resource_bounds { starknet_api::transaction::ValidResourceBounds::L1Gas(l1_bounds) => { vec![ l1_gas_as_felt, @@ -480,21 +480,17 @@ impl<'a> SyscallHintProcessor<'a> { Felt::ZERO, ] } - starknet_api::transaction::ValidResourceBounds::AllResources { - l1_gas, - l2_gas, - l1_data_gas, - } => { + starknet_api::transaction::ValidResourceBounds::AllResources(resources) => { vec![ l1_gas_as_felt, - Felt::from(l1_gas.max_amount), - Felt::from(l1_gas.max_price_per_unit), + Felt::from(resources.l1_gas.max_amount), + Felt::from(resources.l1_gas.max_price_per_unit), l2_gas_as_felt, - Felt::from(l2_gas.max_amount), - Felt::from(l2_gas.max_price_per_unit), + Felt::from(resources.l2_gas.max_amount), + Felt::from(resources.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), + Felt::from(resources.l1_data_gas.max_amount), + Felt::from(resources.l1_data_gas.max_price_per_unit), ] } }; diff --git a/crates/papyrus_common/src/transaction_hash.rs b/crates/papyrus_common/src/transaction_hash.rs index 5c7c14dc67d..1e3accc4b0e 100644 --- a/crates/papyrus_common/src/transaction_hash.rs +++ b/crates/papyrus_common/src/transaction_hash.rs @@ -224,7 +224,9 @@ fn get_tip_resource_bounds_hash( ) -> Result { let (l1_resource_bounds, l2_resource_bounds) = match resource_bounds { ValidResourceBounds::L1Gas(l1_gas_bounds) => (l1_gas_bounds, &ResourceBounds::default()), - ValidResourceBounds::AllResources { l1_gas, l2_gas, .. } => (l1_gas, l2_gas), + ValidResourceBounds::AllResources(all_resources) => { + (&all_resources.l1_gas, &all_resources.l2_gas) + } }; let l1_resource = get_concat_resource(l1_resource_bounds, L1_GAS)?; diff --git a/crates/papyrus_storage/src/serialization/serializers.rs b/crates/papyrus_storage/src/serialization/serializers.rs index 6e5751ddd62..e1b8ce8540a 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, + AllResourcesBounds, Calldata, ContractAddressSalt, DeclareTransaction, @@ -148,6 +149,11 @@ const COMPRESSION_THRESHOLD_BYTES: usize = 384; auto_storage_serde! { pub struct AccountDeploymentData(pub Vec); + pub struct AllResourcesBounds { + 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, @@ -420,6 +426,10 @@ auto_storage_serde! { } pub struct TransactionSignature(pub Vec); pub struct TransactionVersion(pub Felt); + pub enum ValidResourceBounds { + L1Gas(ResourceBounds) = 0, + AllResources(AllResourcesBounds) = 1, + } pub struct Version{ pub major: u32, pub minor: u32, diff --git a/crates/papyrus_test_utils/src/lib.rs b/crates/papyrus_test_utils/src/lib.rs index db4df4a1797..55d3c428898 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, + AllResourcesBounds, Calldata, ContractAddressSalt, DeclareTransaction, @@ -421,6 +422,11 @@ pub trait GetTestInstance: Sized { auto_impl_get_test_instance! { pub struct AccountDeploymentData(pub Vec); + pub struct AllResourcesBounds { + 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, @@ -786,7 +792,7 @@ auto_impl_get_test_instance! { } pub enum ValidResourceBounds { L1Gas(ResourceBounds) = 0, - AllResources { l1_gas: ResourceBounds, l2_gas: ResourceBounds, l1_data_gas: ResourceBounds } = 1, + AllResources(AllResourcesBounds) = 1, } pub struct CasmContractClass { diff --git a/crates/starknet_api/src/transaction.rs b/crates/starknet_api/src/transaction.rs index 14d95931c2e..1f4de0a2f74 100644 --- a/crates/starknet_api/src/transaction.rs +++ b/crates/starknet_api/src/transaction.rs @@ -955,7 +955,14 @@ impl TryFrom> for DeprecatedResourceBoundsMappin #[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub enum ValidResourceBounds { L1Gas(ResourceBounds), // Pre 0.13.3. L2 bounds are signed but never used. - AllResources { l1_gas: ResourceBounds, l2_gas: ResourceBounds, l1_data_gas: ResourceBounds }, + AllResources(AllResourcesBounds), +} + +#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub struct AllResourcesBounds { + pub l1_gas: ResourceBounds, + pub l2_gas: ResourceBounds, + pub l1_data_gas: ResourceBounds, } /// Deserializes raw resource bounds, given as map, into valid resource bounds. @@ -979,10 +986,10 @@ impl Serialize for ValidResourceBounds { (Resource::L1Gas, *l1_gas), (Resource::L2Gas, ResourceBounds::default()), ]), - ValidResourceBounds::AllResources { l1_gas, l2_gas, l1_data_gas } => BTreeMap::from([ - (Resource::L1Gas, *l1_gas), - (Resource::L2Gas, *l2_gas), - (Resource::L1DataGas, *l1_data_gas), + ValidResourceBounds::AllResources(all_resources) => BTreeMap::from([ + (Resource::L1Gas, all_resources.l1_gas), + (Resource::L2Gas, all_resources.l2_gas), + (Resource::L1DataGas, all_resources.l1_data_gas), ]), }; DeprecatedResourceBoundsMapping(map).serialize(s) @@ -998,20 +1005,20 @@ impl TryFrom> for ValidResourceBounds { (raw_resource_bounds.get(&Resource::L1Gas), raw_resource_bounds.get(&Resource::L2Gas)) { match raw_resource_bounds.get(&Resource::L1DataGas) { - Some(data_bounds) => Ok(Self::AllResources { + Some(data_bounds) => Ok(Self::AllResources(AllResourcesBounds { l1_gas: *l1_bounds, l1_data_gas: *data_bounds, l2_gas: *l2_bounds, - }), + })), None => { if l2_bounds.is_zero() { Ok(Self::L1Gas(*l1_bounds)) } else { - Ok(Self::AllResources { + Ok(Self::AllResources(AllResourcesBounds { l1_gas: *l1_bounds, l2_gas: *l2_bounds, l1_data_gas: ResourceBounds::default(), - }) + })) } } } diff --git a/crates/starknet_api/src/transaction_hash.rs b/crates/starknet_api/src/transaction_hash.rs index dc8fc2da0c5..d0046acd251 100644 --- a/crates/starknet_api/src/transaction_hash.rs +++ b/crates/starknet_api/src/transaction_hash.rs @@ -179,7 +179,9 @@ fn get_tip_resource_bounds_hash( ) -> Result { let (l1_resource_bounds, l2_resource_bounds) = match resource_bounds { ValidResourceBounds::L1Gas(l1_gas_bounds) => (l1_gas_bounds, &ResourceBounds::default()), - ValidResourceBounds::AllResources { l1_gas, l2_gas, .. } => (l1_gas, l2_gas), + ValidResourceBounds::AllResources(all_resources) => { + (&all_resources.l1_gas, &all_resources.l2_gas) + } }; let l1_resource = get_concat_resource(l1_resource_bounds, L1_GAS)?;