From 295e9ff91ac14119c7c4268e97a096426fc7fe3b 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 --- 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 +-- .../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 | 18 +++---- 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 | 7 +-- crates/starknet_api/src/rpc_transaction.rs | 17 +++++++ crates/starknet_api/src/transaction.rs | 48 +++++++++++++++++-- crates/starknet_api/src/transaction_hash.rs | 18 +++---- .../src/reader/objects/test_utils.rs | 8 ++-- .../src/reader/objects/transaction.rs | 8 ++-- 20 files changed, 179 insertions(+), 53 deletions(-) diff --git a/crates/blockifier/src/test_utils/declare.rs b/crates/blockifier/src/test_utils/declare.rs index ab5470c1306..eb794a97f6d 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 1ae7a583402..128a4309457 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 b1e025bc0e1..64b68479603 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 b75965e8e71..098112ecdb7 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_test_utils/src/starknet_api_test_utils.rs b/crates/mempool_test_utils/src/starknet_api_test_utils.rs index 7ae045da608..fbae2eeabc5 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 9e01caaaaf4..b8d74535f08 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 a5671842a01..b25b464103d 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 64f75599179..974d0b9b1de 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 443ed57c90f..aaa6e071e92 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 6b4da55441d..9df057dfdac 100644 --- a/crates/papyrus_common/src/transaction_hash.rs +++ b/crates/papyrus_common/src/transaction_hash.rs @@ -8,6 +8,7 @@ use starknet_api::core::{calculate_contract_address, ChainId, ContractAddress}; use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::hash::StarkHash; use starknet_api::transaction::{ + AllResourceBounds, DeclareTransaction, DeclareTransactionV0V1, DeclareTransactionV2, @@ -16,18 +17,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,15 +220,17 @@ 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, l2_resource_bounds) = match resource_bounds { + ValidResourceBounds::L1Gas(l1_gas_bounds) => (l1_gas_bounds, &ResourceBounds::default()), + ValidResourceBounds::AllResources(AllResourceBounds { l1_gas, l2_gas, .. }) => { + (l1_gas, l2_gas) + } + }; - let l2_resource_bounds = - resource_bounds_mapping.0.get(&Resource::L2Gas).expect("Missing l2 resource"); + 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() diff --git a/crates/papyrus_rpc/src/v0_6/transaction.rs b/crates/papyrus_rpc/src/v0_6/transaction.rs index 9bed6073382..7601abab7d8 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 8f4596b664d..7e3e4eb8a44 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 2b19e9b01bd..e58e5366be8 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 20c39d27ee8..d62893e0451 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 555c8809cd1..ac255aad502 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. @@ -94,7 +95,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 2c402c2d9b7..d1453a0a331 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 9cf1c941105..d91786e7760 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,13 @@ 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)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct AllResourceBounds { pub l1_gas: ResourceBounds, pub l2_gas: ResourceBounds, @@ -975,6 +975,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 55cdc33caac..7531ce9c51f 100644 --- a/crates/starknet_api/src/transaction_hash.rs +++ b/crates/starknet_api/src/transaction_hash.rs @@ -6,6 +6,7 @@ use crate::core::{calculate_contract_address, ChainId, ContractAddress}; use crate::crypto::utils::HashChain; use crate::data_availability::DataAvailabilityMode; use crate::transaction::{ + AllResourceBounds, DeclareTransaction, DeclareTransactionV0V1, DeclareTransactionV2, @@ -14,18 +15,17 @@ use crate::transaction::{ DeployAccountTransactionV1, DeployAccountTransactionV3, DeployTransaction, - DeprecatedResourceBoundsMapping, InvokeTransaction, InvokeTransactionV0, InvokeTransactionV1, InvokeTransactionV3, L1HandlerTransaction, - Resource, ResourceBounds, Tip, Transaction, TransactionHash, TransactionVersion, + ValidResourceBounds, }; use crate::StarknetApiError; @@ -175,15 +175,17 @@ 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, l2_resource_bounds) = match resource_bounds { + ValidResourceBounds::L1Gas(l1_gas_bounds) => (l1_gas_bounds, &ResourceBounds::default()), + ValidResourceBounds::AllResources(AllResourceBounds { l1_gas, l2_gas, .. }) => { + (l1_gas, l2_gas) + } + }; - let l2_resource_bounds = - resource_bounds_mapping.0.get(&Resource::L2Gas).expect("Missing l2 resource"); + 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() diff --git a/crates/starknet_client/src/reader/objects/test_utils.rs b/crates/starknet_client/src/reader/objects/test_utils.rs index 3876eeb723d..f4d0f8491e5 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 839ce41a743..4121cb26d8f 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,