From 2b80b29318ae8894ce62a2a67b7e08d4edbb3333 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 | 4 ++-- crates/native_blockifier/src/py_declare.rs | 3 +-- .../native_blockifier/src/py_deploy_account.rs | 3 +-- .../src/py_invoke_function.rs | 3 +-- crates/native_blockifier/src/py_transaction.rs | 18 ++++++++++++++++-- crates/papyrus_common/src/transaction_hash.rs | 4 +++- .../src/serialization/serializers.rs | 10 ++++++++++ crates/papyrus_test_utils/src/lib.rs | 11 +++++++++++ crates/starknet_api/src/transaction.rs | 16 ++++++++-------- crates/starknet_api/src/transaction_hash.rs | 4 +++- 10 files changed, 56 insertions(+), 20 deletions(-) diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index 1ec7683f97..da99764afb 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::{AllResourceBounds, Calldata}; +use starknet_api::transaction::{AllResourcesBounds, Calldata}; use starknet_api::StarknetApiError; use starknet_types_core::felt::{Felt, FromStrError}; use thiserror::Error; @@ -482,7 +482,7 @@ impl<'a> SyscallHintProcessor<'a> { ] } starknet_api::transaction::ValidResourceBounds::AllResources( - AllResourceBounds { l1_gas, l2_gas, l1_data_gas }, + AllResourcesBounds { l1_gas, l2_gas, l1_data_gas }, ) => { vec![ l1_gas_as_felt, diff --git a/crates/native_blockifier/src/py_declare.rs b/crates/native_blockifier/src/py_declare.rs index bfbd2894a6..b8d74535f0 100644 --- a/crates/native_blockifier/src/py_declare.rs +++ b/crates/native_blockifier/src/py_declare.rs @@ -12,7 +12,6 @@ use starknet_api::transaction::{ DeclareTransactionV3, Fee, PaymasterData, - DeprecatedResourceBoundsMapping, Tip, TransactionHash, TransactionSignature, @@ -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 749f19586a..b25b464103 100644 --- a/crates/native_blockifier/src/py_deploy_account.rs +++ b/crates/native_blockifier/src/py_deploy_account.rs @@ -12,7 +12,6 @@ use starknet_api::transaction::{ DeployAccountTransactionV3, Fee, PaymasterData, - DeprecatedResourceBoundsMapping, Tip, TransactionHash, TransactionSignature, @@ -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 111d53b0f1..974d0b9b1d 100644 --- a/crates/native_blockifier/src/py_invoke_function.rs +++ b/crates/native_blockifier/src/py_invoke_function.rs @@ -14,7 +14,6 @@ use starknet_api::transaction::{ InvokeTransactionV1, InvokeTransactionV3, PaymasterData, - DeprecatedResourceBoundsMapping, Tip, TransactionHash, TransactionSignature, @@ -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 15254e1181..a3b43bc5ed 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}; @@ -68,7 +73,9 @@ impl From for starknet_api::transaction::ResourceBounds { #[derive(Clone, FromPyObject)] pub struct PyResourceBoundsMapping(pub BTreeMap); -impl TryFrom for starknet_api::transaction::DeprecatedResourceBoundsMapping { +impl TryFrom + for starknet_api::transaction::DeprecatedResourceBoundsMapping +{ type Error = StarknetApiError; fn try_from(py_resource_bounds_mapping: PyResourceBoundsMapping) -> Result { let resource_bounds_vec: Vec<(Resource, ResourceBounds)> = py_resource_bounds_mapping @@ -82,6 +89,13 @@ impl TryFrom for starknet_api::transaction::DeprecatedR } } +impl TryFrom for ValidResourceBounds { + type Error = StarknetApiError; + fn try_from(py_resource_bounds_mapping: PyResourceBoundsMapping) -> Result { + DeprecatedResourceBoundsMapping::try_from(py_resource_bounds_mapping)?.0.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 5c7c14dc67..1e3accc4b0 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 6e5751ddd6..e1b8ce8540 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 48ac9481c1..7943f57e85 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, @@ -785,6 +791,11 @@ auto_impl_get_test_instance! { pub r#type: String, } + pub enum ValidResourceBounds { + L1Gas(ResourceBounds) = 0, + AllResources(AllResourcesBounds) = 1, + } + pub struct CasmContractClass { pub prime: BigUint, pub compiler_version: String, diff --git a/crates/starknet_api/src/transaction.rs b/crates/starknet_api/src/transaction.rs index 35977efdd3..0d3d751d68 100644 --- a/crates/starknet_api/src/transaction.rs +++ b/crates/starknet_api/src/transaction.rs @@ -955,11 +955,11 @@ 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(AllResourceBounds), + AllResources(AllResourcesBounds), } -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct AllResourceBounds { +#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub struct AllResourcesBounds { pub l1_gas: ResourceBounds, pub l2_gas: ResourceBounds, pub l1_data_gas: ResourceBounds, @@ -986,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) @@ -1005,7 +1005,7 @@ 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(AllResourceBounds { + Some(data_bounds) => Ok(Self::AllResources(AllResourcesBounds { l1_gas: *l1_bounds, l1_data_gas: *data_bounds, l2_gas: *l2_bounds, diff --git a/crates/starknet_api/src/transaction_hash.rs b/crates/starknet_api/src/transaction_hash.rs index dc8fc2da0c..d0046acd25 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)?;