Skip to content

Commit

Permalink
build(fee): remove more occurnces of resource mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Sep 4, 2024
1 parent 6aec949 commit db8074f
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::BTreeMap;

use cairo_vm::Felt252;
use num_traits::Pow;
use starknet_api::core::ChainId;
Expand All @@ -8,14 +6,13 @@ use starknet_api::felt;
use starknet_api::transaction::{
AccountDeploymentData,
Calldata,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
Resource,
ResourceBounds,
Tip,
TransactionHash,
TransactionVersion,
ValidResourceBounds,
};
use starknet_types_core::felt::Felt;
use test_case::test_case;
Expand Down Expand Up @@ -213,23 +210,10 @@ fn test_get_execution_info(
only_query,
..Default::default()
},
resource_bounds: DeprecatedResourceBoundsMapping(BTreeMap::from([
(
Resource::L1Gas,
// TODO(Ori, 1/2/2024): Write an indicative expect message explaining why
// the convertion works.
ResourceBounds {
max_amount: max_amount
.0
.try_into()
.expect("Failed to convert u128 to u64."),
max_price_per_unit: max_price_per_unit.0,
},
),
(Resource::L2Gas, ResourceBounds { max_amount: 0, max_price_per_unit: 0 }),
]))
.try_into()
.unwrap(),
resource_bounds: ValidResourceBounds::L1Gas(ResourceBounds {
max_amount: max_amount.0.try_into().expect("Failed to convert u128 to u64."),
max_price_per_unit: max_price_per_unit.0,
}),
tip: Tip::default(),
nonce_data_availability_mode: DataAvailabilityMode::L1,
fee_data_availability_mode: DataAvailabilityMode::L1,
Expand Down
15 changes: 3 additions & 12 deletions crates/native_blockifier/src/py_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,17 @@ impl From<PyResourceBounds> for starknet_api::transaction::ResourceBounds {
#[derive(Clone, FromPyObject)]
pub struct PyResourceBoundsMapping(pub BTreeMap<PyResource, PyResourceBounds>);

impl TryFrom<PyResourceBoundsMapping>
for starknet_api::transaction::DeprecatedResourceBoundsMapping
{
impl TryFrom<PyResourceBoundsMapping> for ValidResourceBounds {
type Error = StarknetApiError;
fn try_from(py_resource_bounds_mapping: PyResourceBoundsMapping) -> Result<Self, Self::Error> {
let resource_bounds_vec: Vec<(Resource, ResourceBounds)> = py_resource_bounds_mapping
let map = py_resource_bounds_mapping
.0
.into_iter()
.map(|(py_resource_type, py_resource_bounds)| {
(Resource::from(py_resource_type), ResourceBounds::from(py_resource_bounds))
})
.collect();
Self::try_from(resource_bounds_vec)
}
}

impl TryFrom<PyResourceBoundsMapping> for ValidResourceBounds {
type Error = StarknetApiError;
fn try_from(py_resource_bounds_mapping: PyResourceBoundsMapping) -> Result<Self, Self::Error> {
DeprecatedResourceBoundsMapping::try_from(py_resource_bounds_mapping)?.try_into()
DeprecatedResourceBoundsMapping(map).try_into()
}
}

Expand Down
2 changes: 0 additions & 2 deletions crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ use starknet_api::transaction::{
DeployAccountTransactionV3,
DeployTransaction,
DeployTransactionOutput,
DeprecatedResourceBoundsMapping,
Event,
EventContent,
EventData,
Expand Down Expand Up @@ -368,7 +367,6 @@ auto_storage_serde! {
pub max_amount: u64,
pub max_price_per_unit: u128,
}
pub struct DeprecatedResourceBoundsMapping(pub BTreeMap<Resource, ResourceBounds>);
pub struct SequencerContractAddress(pub ContractAddress);
pub struct Signature {
pub r: Felt,
Expand Down
2 changes: 0 additions & 2 deletions crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ use starknet_api::transaction::{
DeployAccountTransactionV3,
DeployTransaction,
DeployTransactionOutput,
DeprecatedResourceBoundsMapping,
Event,
EventContent,
EventData,
Expand Down Expand Up @@ -729,7 +728,6 @@ auto_impl_get_test_instance! {
pub max_amount: u64,
pub max_price_per_unit: u128,
}
pub struct DeprecatedResourceBoundsMapping(pub BTreeMap<Resource, ResourceBounds>);
pub struct SequencerContractAddress(pub ContractAddress);
pub struct Signature {
pub r: Felt,
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ thiserror.workspace = true
[dev-dependencies]
assert_matches.workspace = true
rstest.workspace = true

[package.metadata.cargo-machete]
ignored = ["strum"]
17 changes: 0 additions & 17 deletions crates/starknet_api/src/rpc_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#[path = "rpc_transaction_test.rs"]
mod rpc_transaction_test;

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;

Expand All @@ -28,7 +26,6 @@ use crate::transaction::{
InvokeTransaction,
InvokeTransactionV3,
PaymasterData,
Resource,
Tip,
Transaction,
TransactionSignature,
Expand Down Expand Up @@ -286,17 +283,3 @@ pub struct EntryPointByType {
#[serde(rename = "L1_HANDLER")]
pub l1handler: Vec<EntryPoint>,
}

// TODO(Nimrod): Remove this conversion.
impl From<AllResourceBounds> for crate::transaction::DeprecatedResourceBoundsMapping {
fn from(
all_resource_bounds: AllResourceBounds,
) -> crate::transaction::DeprecatedResourceBoundsMapping {
let map = BTreeMap::from([
(Resource::L1Gas, all_resource_bounds.l1_gas),
(Resource::L2Gas, all_resource_bounds.l2_gas),
(Resource::L1DataGas, all_resource_bounds.l1_data_gas),
]);
crate::transaction::DeprecatedResourceBoundsMapping(map)
}
}
26 changes: 1 addition & 25 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::collections::{BTreeMap, HashSet};
use std::collections::BTreeMap;
use std::fmt::Display;
use std::sync::Arc;

use derive_more::{Display, From};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use starknet_types_core::felt::Felt;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

use crate::block::{BlockHash, BlockNumber};
Expand Down Expand Up @@ -936,29 +935,6 @@ where
// TODO(Nimrod): Remove this struct definition.
pub struct DeprecatedResourceBoundsMapping(pub BTreeMap<Resource, ResourceBounds>);

impl TryFrom<Vec<(Resource, ResourceBounds)>> for DeprecatedResourceBoundsMapping {
type Error = StarknetApiError;
fn try_from(
resource_resource_bounds_pairs: Vec<(Resource, ResourceBounds)>,
) -> Result<Self, Self::Error> {
let n_variants = Resource::iter().count();
let allowed_signed_variants = [n_variants, n_variants - 1];
let unique_resources: HashSet<Resource> =
HashSet::from_iter(resource_resource_bounds_pairs.iter().map(|(k, _)| *k));
if !allowed_signed_variants.contains(&unique_resources.len())
|| !allowed_signed_variants.contains(&resource_resource_bounds_pairs.len())
{
// TODO(Nimrod): Consider making this check more strict.
Err(StarknetApiError::InvalidResourceMappingInitializer(format!(
"{:?}",
resource_resource_bounds_pairs
)))
} else {
Ok(Self(resource_resource_bounds_pairs.into_iter().collect::<BTreeMap<_, _>>()))
}
}
}

#[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.
Expand Down
8 changes: 4 additions & 4 deletions crates/starknet_client/src/writer/objects/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ use starknet_api::transaction::{
AccountDeploymentData,
Calldata,
ContractAddressSalt,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
Tip,
TransactionSignature,
TransactionVersion,
ValidResourceBounds,
};

// Each transaction type has a field called `type`. This field needs to be of a type that
Expand Down Expand Up @@ -104,7 +104,7 @@ pub struct DeployAccountV1Transaction {
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct DeployAccountV3Transaction {
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub contract_address_salt: ContractAddressSalt,
pub class_hash: ClassHash,
Expand Down Expand Up @@ -169,7 +169,7 @@ pub struct InvokeV1Transaction {
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct InvokeV3Transaction {
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub calldata: Calldata,
pub sender_address: ContractAddress,
Expand Down Expand Up @@ -235,7 +235,7 @@ pub struct DeclareV2Transaction {
#[serde(deny_unknown_fields)]
pub struct DeclareV3Transaction {
pub contract_class: ContractClass,
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down

0 comments on commit db8074f

Please sign in to comment.