Skip to content

Commit

Permalink
refactor(fee): change valid resource bound to tupled enum
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Aug 19, 2024
1 parent 181dc7a commit e35af6e
Showing 6 changed files with 47 additions and 24 deletions.
20 changes: 8 additions & 12 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
@@ -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),
]
}
};
4 changes: 3 additions & 1 deletion crates/papyrus_common/src/transaction_hash.rs
Original file line number Diff line number Diff line change
@@ -224,7 +224,9 @@ fn get_tip_resource_bounds_hash(
) -> Result<Felt, StarknetApiError> {
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)?;
10 changes: 10 additions & 0 deletions crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
@@ -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<Felt>);
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<Felt>);
pub struct TransactionVersion(pub Felt);
pub enum ValidResourceBounds {
L1Gas(ResourceBounds) = 0,
AllResources(AllResourcesBounds) = 1,
}
pub struct Version{
pub major: u32,
pub minor: u32,
8 changes: 7 additions & 1 deletion crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Felt>);
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 {
25 changes: 16 additions & 9 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -955,7 +955,14 @@ impl TryFrom<Vec<(Resource, ResourceBounds)>> 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<BTreeMap<Resource, ResourceBounds>> 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(),
})
}))
}
}
}
4 changes: 3 additions & 1 deletion crates/starknet_api/src/transaction_hash.rs
Original file line number Diff line number Diff line change
@@ -179,7 +179,9 @@ fn get_tip_resource_bounds_hash(
) -> Result<Felt, StarknetApiError> {
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)?;

0 comments on commit e35af6e

Please sign in to comment.