Skip to content

Commit

Permalink
build(fee): define valid resources bounds enum
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Aug 20, 2024
1 parent c34b81b commit b64b716
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ impl<'a> SyscallHintProcessor<'a> {
let resource = match resource {
Resource::L1Gas => l1_gas,
Resource::L2Gas => l2_gas,
Resource::L1DataGas => todo!(),
};

vec![
Expand Down
1 change: 1 addition & 0 deletions crates/gateway/src/stateless_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ fn validate_resource_is_non_zero(
let resource_bounds = match resource {
Resource::L1Gas => resource_bounds_mapping.l1_gas,
Resource::L2Gas => resource_bounds_mapping.l2_gas,
Resource::L1DataGas => todo!(),
};
if resource_bounds.max_amount == 0 || resource_bounds.max_price_per_unit == 0 {
return Err(StatelessTransactionValidatorError::ZeroResourceBounds {
Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ auto_storage_serde! {
pub enum Resource {
L1Gas = 0,
L2Gas = 1,
L1DataGas = 2,
}
pub struct ResourceBounds {
pub max_amount: u64,
Expand Down
73 changes: 70 additions & 3 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashSet};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::Display;
use std::sync::Arc;

Expand Down Expand Up @@ -867,6 +867,8 @@ pub enum Resource {
L1Gas,
#[serde(rename = "L2_GAS")]
L2Gas,
#[serde(rename = "L1_DATA_GAS")]
L1DataGas,
}

/// Fee bounds for an execution resource.
Expand All @@ -884,6 +886,13 @@ pub struct ResourceBounds {
pub max_price_per_unit: u128,
}

impl ResourceBounds {
/// Returns true iff both the max amount and the max amount per unit is zero.
pub fn is_zero(&self) -> bool {
self.max_amount == 0 && self.max_price_per_unit == 0
}
}

fn u64_to_hex<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down Expand Up @@ -916,6 +925,7 @@ where

/// A mapping from execution resources to their corresponding fee bounds..
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
// TODO(Nimrod): Remove this struct definition.
pub struct ResourceBoundsMapping(pub BTreeMap<Resource, ResourceBounds>);

impl TryFrom<Vec<(Resource, ResourceBounds)>> for ResourceBoundsMapping {
Expand All @@ -924,11 +934,13 @@ impl TryFrom<Vec<(Resource, ResourceBounds)>> for ResourceBoundsMapping {
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 unique_resources.len() != n_variants
|| resource_resource_bounds_pairs.len() != n_variants
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
Expand All @@ -939,6 +951,61 @@ impl TryFrom<Vec<(Resource, ResourceBounds)>> for ResourceBoundsMapping {
}
}

pub enum ValidResourceBounds {
L1Gas(ResourceBounds), // Pre 0.13.3. Only L1 gas. L2 bounds are signed but never used.
AllResources(AllResourceBounds),
}

pub struct AllResourceBounds {
pub l1_gas: ResourceBounds,
pub l2_gas: ResourceBounds,
pub l1_data_gas: ResourceBounds,
}

impl AllResourceBounds {
#[allow(dead_code)]
fn get_bound(&self, resource: Resource) -> ResourceBounds {
match resource {
Resource::L1Gas => self.l1_gas,
Resource::L2Gas => self.l2_gas,
Resource::L1DataGas => self.l1_data_gas,
}
}
}

impl TryFrom<HashMap<Resource, ResourceBounds>> for ValidResourceBounds {
type Error = StarknetApiError;
fn try_from(
raw_resource_bounds: HashMap<Resource, ResourceBounds>,
) -> Result<Self, Self::Error> {
if let (Some(l1_bounds), Some(l2_bounds)) =
(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 {
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 {
Err(StarknetApiError::InvalidResourceMappingInitializer(format!(
"Missing data gas bounds but L2 gas bound is not zero: \
{raw_resource_bounds:?}",
)))
}
}
}
} else {
Err(StarknetApiError::InvalidResourceMappingInitializer(format!(
"{raw_resource_bounds:?}",
)))
}
}
}

/// Paymaster-related data.
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct PaymasterData(pub Vec<Felt>);
Expand Down

0 comments on commit b64b716

Please sign in to comment.