Skip to content

Commit

Permalink
build(fee): tx_args holds valid resource bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Aug 22, 2024
1 parent 19af765 commit 6f28dcd
Show file tree
Hide file tree
Showing 24 changed files with 256 additions and 100 deletions.
48 changes: 19 additions & 29 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,35 +469,25 @@ 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 {
ValidResourceBounds::L1Gas(l1_bounds) => {
vec![
l1_gas_as_felt,
Felt::from(l1_bounds.max_amount),
Felt::from(l1_bounds.max_price_per_unit),
l2_gas_as_felt,
Felt::ZERO,
Felt::ZERO,
]
}
ValidResourceBounds::AllResources(AllResourceBounds {
l1_gas,
l2_gas,
l1_data_gas,
}) => {
vec![
l1_gas_as_felt,
Felt::from(l1_gas.max_amount),
Felt::from(l1_gas.max_price_per_unit),
l2_gas_as_felt,
Felt::from(l2_gas.max_amount),
Felt::from(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),
]
}
};
let l1_gas_bounds = tx_info.resource_bounds.get_l1_bounds();
let l2_gas_bounds = tx_info.resource_bounds.get_l2_bounds();
let mut flat_resource_bounds = vec![
l1_gas_as_felt,
Felt::from(l1_gas_bounds.max_amount),
Felt::from(l1_gas_bounds.max_price_per_unit),
l2_gas_as_felt,
Felt::from(l2_gas_bounds.max_amount),
Felt::from(l2_gas_bounds.max_price_per_unit),
];
if let ValidResourceBounds::AllResources(AllResourceBounds { l1_data_gas, .. }) =
tx_info.resource_bounds
{
flat_resource_bounds.extend(vec![
l1_data_gas_as_felt,
Felt::from(l1_data_gas.max_amount),
Felt::from(l1_data_gas.max_price_per_unit),
])
}

self.allocate_data_segment(vm, &flat_resource_bounds)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn declare_tx(declare_tx_args: DeclareTxArgs, class_info: ClassInfo) -> Acco
starknet_api::transaction::DeclareTransaction::V3(DeclareTransactionV3 {
signature: declare_tx_args.signature,
sender_address: declare_tx_args.sender_address,
resource_bounds: declare_tx_args.resource_bounds,
resource_bounds: declare_tx_args.resource_bounds.try_into().expect("todo"),
tip: declare_tx_args.tip,
nonce_data_availability_mode: declare_tx_args.nonce_data_availability_mode,
fee_data_availability_mode: declare_tx_args.fee_data_availability_mode,
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/deploy_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn deploy_account_tx(
} else if deploy_tx_args.version == TransactionVersion::THREE {
starknet_api::transaction::DeployAccountTransaction::V3(DeployAccountTransactionV3 {
signature: deploy_tx_args.signature,
resource_bounds: deploy_tx_args.resource_bounds,
resource_bounds: deploy_tx_args.resource_bounds.try_into().expect("todo"),
tip: deploy_tx_args.tip,
nonce_data_availability_mode: deploy_tx_args.nonce_data_availability_mode,
fee_data_availability_mode: deploy_tx_args.fee_data_availability_mode,
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn invoke_tx(invoke_args: InvokeTxArgs) -> InvokeTransaction {
})
} else if invoke_args.version == TransactionVersion::THREE {
starknet_api::transaction::InvokeTransaction::V3(InvokeTransactionV3 {
resource_bounds: invoke_args.resource_bounds,
resource_bounds: invoke_args.resource_bounds.try_into().expect("todo"),
calldata: invoke_args.calldata,
sender_address: invoke_args.sender_address,
nonce: invoke_args.nonce,
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl TransactionInfoCreator for DeclareTransaction {
starknet_api::transaction::DeclareTransaction::V3(tx) => {
Ok(TransactionInfo::Current(CurrentTransactionInfo {
common_fields,
resource_bounds: tx.resource_bounds.clone().try_into()?,
resource_bounds: tx.resource_bounds.clone(),
tip: tx.tip,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
Expand Down Expand Up @@ -411,7 +411,7 @@ impl TransactionInfoCreator for DeployAccountTransaction {
starknet_api::transaction::DeployAccountTransaction::V3(tx) => {
Ok(TransactionInfo::Current(CurrentTransactionInfo {
common_fields,
resource_bounds: tx.resource_bounds.clone().try_into()?,
resource_bounds: tx.resource_bounds.clone(),
tip: tx.tip,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
Expand Down Expand Up @@ -535,7 +535,7 @@ impl TransactionInfoCreator for InvokeTransaction {
starknet_api::transaction::InvokeTransaction::V3(tx) => {
Ok(TransactionInfo::Current(CurrentTransactionInfo {
common_fields,
resource_bounds: tx.resource_bounds.clone().try_into()?,
resource_bounds: tx.resource_bounds.clone(),
tip: tx.tip,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
Expand Down
12 changes: 4 additions & 8 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::executable_transaction::Transaction;
use starknet_api::transaction::{DeprecatedResourceBoundsMapping, Resource, Tip, TransactionHash};
use starknet_api::transaction::{Tip, TransactionHash, ValidResourceBounds};
use starknet_mempool_types::errors::MempoolError;
use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput, MempoolResult};

Expand Down Expand Up @@ -194,13 +194,13 @@ impl Mempool {
/// TODO(Mohammad): rename this struct to `ThinTransaction` once that name
/// becomes available, to better reflect its purpose and usage.
/// TODO(Mohammad): restore the Copy once ResourceBoundsMapping implements it.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TransactionReference {
pub sender_address: ContractAddress,
pub nonce: Nonce,
pub tx_hash: TransactionHash,
pub tip: Tip,
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
}

impl TransactionReference {
Expand All @@ -218,10 +218,6 @@ impl TransactionReference {
}

pub fn get_l2_gas_price(&self) -> u128 {
self.resource_bounds
.0
.get(&Resource::L2Gas)
.map(|bounds| bounds.max_price_per_unit)
.expect("Expected a valid L2 gas resource bounds.")
self.resource_bounds.get_l2_bounds().max_price_per_unit
}
}
8 changes: 5 additions & 3 deletions crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use starknet_api::transaction::{
AccountDeploymentData,
Calldata,
ContractAddressSalt,
DeprecatedResourceBoundsMapping as ExecutableResourceBoundsMapping,
DeprecatedResourceBoundsMapping,
PaymasterData,
ResourceBounds,
Tip,
Expand Down Expand Up @@ -552,15 +552,17 @@ pub fn create_executable_tx(
tx_hash: TransactionHash,
tip: Tip,
nonce: Nonce,
resource_bounds: ExecutableResourceBoundsMapping,
resource_bounds: DeprecatedResourceBoundsMapping,
) -> Transaction {
Transaction::Invoke(InvokeTransaction {
tx: starknet_api::transaction::InvokeTransaction::V3(
starknet_api::transaction::InvokeTransactionV3 {
sender_address,
tip,
nonce,
resource_bounds,
resource_bounds: resource_bounds
.try_into()
.expect("Invalid resource bounds were given."),
signature: TransactionSignature::default(),
calldata: Calldata::default(),
nonce_data_availability_mode: DataAvailabilityMode::L1,
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 @@ -10,7 +10,6 @@ use starknet_api::transaction::{
DeclareTransactionV0V1,
DeclareTransactionV2,
DeclareTransactionV3,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
Tip,
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 @@ -10,7 +10,6 @@ use starknet_api::transaction::{
ContractAddressSalt,
DeployAccountTransactionV1,
DeployAccountTransactionV3,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
Tip,
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 @@ -9,7 +9,6 @@ use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::transaction::{
AccountDeploymentData,
Calldata,
DeprecatedResourceBoundsMapping,
Fee,
InvokeTransactionV0,
InvokeTransactionV1,
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
14 changes: 13 additions & 1 deletion 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 @@ -84,6 +89,13 @@ impl TryFrom<PyResourceBoundsMapping>
}
}

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()
}
}

#[derive(Clone)]
pub enum PyDataAvailabilityMode {
L1 = 0,
Expand Down
15 changes: 6 additions & 9 deletions crates/papyrus_common/src/transaction_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ use starknet_api::transaction::{
DeployAccountTransactionV1,
DeployAccountTransactionV3,
DeployTransaction,
DeprecatedResourceBoundsMapping,
InvokeTransaction,
InvokeTransactionV0,
InvokeTransactionV1,
InvokeTransactionV3,
L1HandlerTransaction,
Resource,
ResourceBounds,
Tip,
Transaction,
TransactionHash,
TransactionVersion,
ValidResourceBounds,
};
use starknet_api::StarknetApiError;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -220,16 +219,14 @@ pub(crate) fn ascii_as_felt(ascii_str: &str) -> Result<Felt, StarknetApiError> {

// An implementation of the SNIP: https://github.com/EvyatarO/SNIPs/blob/snip-8/SNIPS/snip-8.md
fn get_tip_resource_bounds_hash(
resource_bounds_mapping: &DeprecatedResourceBoundsMapping,
resource_bounds: &ValidResourceBounds,
tip: &Tip,
) -> Result<Felt, StarknetApiError> {
let l1_resource_bounds =
resource_bounds_mapping.0.get(&Resource::L1Gas).expect("Missing l1 resource");
let l1_resource = get_concat_resource(l1_resource_bounds, L1_GAS)?;
let l1_resource_bounds = resource_bounds.get_l1_bounds();
let l2_resource_bounds = resource_bounds.get_l2_bounds();

let l2_resource_bounds =
resource_bounds_mapping.0.get(&Resource::L2Gas).expect("Missing l2 resource");
let l2_resource = get_concat_resource(l2_resource_bounds, L2_GAS)?;
let l1_resource = get_concat_resource(&l1_resource_bounds, L1_GAS)?;
let l2_resource = get_concat_resource(&l2_resource_bounds, L2_GAS)?;

Ok(HashChain::new()
.chain(&tip.0.into())
Expand Down
34 changes: 31 additions & 3 deletions crates/papyrus_protobuf/src/converters/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use starknet_api::transaction::{
TransactionOutput,
TransactionSignature,
TransactionVersion,
ValidResourceBounds,
};
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -444,7 +445,8 @@ impl TryFrom<protobuf::transaction::DeployAccountV3> for DeployAccountTransactio
value.resource_bounds.ok_or(ProtobufConversionError::MissingField {
field_description: "DeployAccountV3::resource_bounds",
})?,
)?;
)?
.try_into()?;

let tip = Tip(value.tip);

Expand Down Expand Up @@ -627,6 +629,30 @@ impl From<DeprecatedResourceBoundsMapping> for protobuf::ResourceBounds {
}
}

impl From<ValidResourceBounds> for protobuf::ResourceBounds {
fn from(value: ValidResourceBounds) -> Self {
let mut res = protobuf::ResourceBounds::default();

let resource_bounds_l1 = value.get_l1_bounds();

let resource_limits_l1 = protobuf::ResourceLimits {
max_amount: resource_bounds_l1.max_amount,
max_price_per_unit: Some(Felt::from(resource_bounds_l1.max_price_per_unit).into()),
};
res.l1_gas = Some(resource_limits_l1);

let resource_bounds_l2 = value.get_l2_bounds();

let resource_limits_l2 = protobuf::ResourceLimits {
max_amount: resource_bounds_l2.max_amount,
max_price_per_unit: Some(Felt::from(resource_bounds_l2.max_price_per_unit).into()),
};
res.l2_gas = Some(resource_limits_l2);

res
}
}

impl TryFrom<protobuf::transaction::InvokeV0> for InvokeTransactionV0 {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::transaction::InvokeV0) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -760,7 +786,8 @@ impl TryFrom<protobuf::transaction::InvokeV3> for InvokeTransactionV3 {
value.resource_bounds.ok_or(ProtobufConversionError::MissingField {
field_description: "InvokeV3::resource_bounds",
})?,
)?;
)?
.try_into()?;

let tip = Tip(value.tip);

Expand Down Expand Up @@ -1078,7 +1105,8 @@ impl TryFrom<protobuf::transaction::DeclareV3> for DeclareTransactionV3 {
value.resource_bounds.ok_or(ProtobufConversionError::MissingField {
field_description: "DeclareV3::resource_bounds",
})?,
)?;
)?
.try_into()?;

let tip = Tip(value.tip);

Expand Down
Loading

0 comments on commit 6f28dcd

Please sign in to comment.