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 0b8e76a commit 2b80b29
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 20 deletions.
4 changes: 2 additions & 2 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cairo_vm::vm::vm_core::VirtualMachine;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::state::StorageKey;
use starknet_api::transaction::{AllResourceBounds, Calldata};
use starknet_api::transaction::{AllResourcesBounds, Calldata};
use starknet_api::StarknetApiError;
use starknet_types_core::felt::{Felt, FromStrError};
use thiserror::Error;
Expand Down Expand Up @@ -482,7 +482,7 @@ impl<'a> SyscallHintProcessor<'a> {
]
}
starknet_api::transaction::ValidResourceBounds::AllResources(
AllResourceBounds { l1_gas, l2_gas, l1_data_gas },
AllResourcesBounds { l1_gas, l2_gas, l1_data_gas },
) => {
vec![
l1_gas_as_felt,
Expand Down
3 changes: 1 addition & 2 deletions crates/native_blockifier/src/py_declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use starknet_api::transaction::{
DeclareTransactionV3,
Fee,
PaymasterData,
DeprecatedResourceBoundsMapping,
Tip,
TransactionHash,
TransactionSignature,
Expand Down Expand Up @@ -88,7 +87,7 @@ impl TryFrom<PyDeclareTransactionV3> for DeclareTransactionV3 {
type Error = NativeBlockifierInputError;
fn try_from(tx: PyDeclareTransactionV3) -> Result<Self, Self::Error> {
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),
Expand Down
3 changes: 1 addition & 2 deletions crates/native_blockifier/src/py_deploy_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use starknet_api::transaction::{
DeployAccountTransactionV3,
Fee,
PaymasterData,
DeprecatedResourceBoundsMapping,
Tip,
TransactionHash,
TransactionSignature,
Expand Down Expand Up @@ -64,7 +63,7 @@ impl TryFrom<PyDeployAccountTransactionV3> for DeployAccountTransactionV3 {
type Error = NativeBlockifierInputError;
fn try_from(tx: PyDeployAccountTransactionV3) -> Result<Self, Self::Error> {
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),
Expand Down
3 changes: 1 addition & 2 deletions crates/native_blockifier/src/py_invoke_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use starknet_api::transaction::{
InvokeTransactionV1,
InvokeTransactionV3,
PaymasterData,
DeprecatedResourceBoundsMapping,
Tip,
TransactionHash,
TransactionSignature,
Expand Down Expand Up @@ -87,7 +86,7 @@ impl TryFrom<PyInvokeTransactionV3> for InvokeTransactionV3 {
type Error = NativeBlockifierInputError;
fn try_from(tx: PyInvokeTransactionV3) -> Result<Self, Self::Error> {
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),
Expand Down
18 changes: 16 additions & 2 deletions crates/native_blockifier/src/py_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -68,7 +73,9 @@ 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 starknet_api::transaction::DeprecatedResourceBoundsMapping
{
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
Expand All @@ -82,6 +89,13 @@ impl TryFrom<PyResourceBoundsMapping> for starknet_api::transaction::DeprecatedR
}
}

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)?.0.try_into()
}
}

#[derive(Clone)]
pub enum PyDataAvailabilityMode {
L1 = 0,
Expand Down
4 changes: 3 additions & 1 deletion crates/papyrus_common/src/transaction_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
10 changes: 10 additions & 0 deletions crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ use starknet_api::state::{
};
use starknet_api::transaction::{
AccountDeploymentData,
AllResourcesBounds,
Calldata,
ContractAddressSalt,
DeclareTransaction,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ use starknet_api::state::{
};
use starknet_api::transaction::{
AccountDeploymentData,
AllResourcesBounds,
Calldata,
ContractAddressSalt,
DeclareTransaction,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -785,6 +791,11 @@ auto_impl_get_test_instance! {
pub r#type: String,
}

pub enum ValidResourceBounds {
L1Gas(ResourceBounds) = 0,
AllResources(AllResourcesBounds) = 1,
}

pub struct CasmContractClass {
pub prime: BigUint,
pub compiler_version: String,
Expand Down
16 changes: 8 additions & 8 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,11 +955,11 @@ 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(AllResourceBounds),
AllResources(AllResourcesBounds),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AllResourceBounds {
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct AllResourcesBounds {
pub l1_gas: ResourceBounds,
pub l2_gas: ResourceBounds,
pub l1_data_gas: ResourceBounds,
Expand All @@ -986,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)
Expand All @@ -1005,7 +1005,7 @@ 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(AllResourceBounds {
Some(data_bounds) => Ok(Self::AllResources(AllResourcesBounds {
l1_gas: *l1_bounds,
l1_data_gas: *data_bounds,
l2_gas: *l2_bounds,
Expand Down
4 changes: 3 additions & 1 deletion crates/starknet_api/src/transaction_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down

0 comments on commit 2b80b29

Please sign in to comment.