Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(fee): remove more occurnces of resource mapping #547

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading