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 19, 2024
1 parent 3f4a277 commit fdb5979
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 40 deletions.
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata,
ContractAddressSalt,
DeprecatedResourceBoundsMapping,
Resource,
ResourceBounds,
DeprecatedResourceBoundsMapping,
TransactionVersion,
};
use starknet_api::{contract_address, felt, patricia_key};
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/test_utils/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use starknet_api::transaction::{
DeclareTransactionV0V1,
DeclareTransactionV2,
DeclareTransactionV3,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
DeprecatedResourceBoundsMapping,
Tip,
TransactionHash,
TransactionSignature,
Expand Down 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.0.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
4 changes: 2 additions & 2 deletions crates/blockifier/src/test_utils/deploy_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use starknet_api::transaction::{
ContractAddressSalt,
DeployAccountTransactionV1,
DeployAccountTransactionV3,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
DeprecatedResourceBoundsMapping,
Tip,
TransactionHash,
TransactionSignature,
Expand Down 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.0.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
4 changes: 2 additions & 2 deletions crates/blockifier/src/test_utils/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::transaction::{
AccountDeploymentData,
Calldata,
DeprecatedResourceBoundsMapping,
Fee,
InvokeTransactionV0,
InvokeTransactionV1,
InvokeTransactionV3,
PaymasterData,
DeprecatedResourceBoundsMapping,
Tip,
TransactionHash,
TransactionSignature,
Expand Down 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.0.try_into().expect("todo"),
calldata: invoke_args.calldata,
sender_address: invoke_args.sender_address,
nonce: invoke_args.nonce,
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::state::cached_state::TransactionalState;
use crate::state::errors::StateError;
use crate::state::state_api::{State, UpdatableState};
use crate::transaction::constants;
use crate::transaction::errors::{TransactionInfoCreationError, TransactionExecutionError};
use crate::transaction::errors::{TransactionExecutionError, TransactionInfoCreationError};
use crate::transaction::objects::{
CommonAccountFields,
CurrentTransactionInfo,
Expand Down 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.0.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 @@ -401,7 +401,7 @@ impl TransactionInfoCreator for DeployAccountTransaction {
starknet_api::transaction::DeployAccountTransaction::V3(tx) => {
Ok(TransactionInfo::Current(CurrentTransactionInfo {
common_fields,
resource_bounds: tx.resource_bounds.0.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 @@ -519,7 +519,7 @@ impl TransactionInfoCreator for InvokeTransaction {
starknet_api::transaction::InvokeTransaction::V3(tx) => {
Ok(TransactionInfo::Current(CurrentTransactionInfo {
common_fields,
resource_bounds: tx.resource_bounds.0.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
15 changes: 7 additions & 8 deletions crates/papyrus_common/src/transaction_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ use starknet_api::transaction::{
InvokeTransactionV1,
InvokeTransactionV3,
L1HandlerTransaction,
Resource,
ResourceBounds,
DeprecatedResourceBoundsMapping,
Tip,
Transaction,
TransactionHash,
TransactionVersion,
ValidResourceBounds,
};
use starknet_api::StarknetApiError;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -220,15 +219,15 @@ 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, 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),
};

let l2_resource_bounds =
resource_bounds_mapping.0.get(&Resource::L2Gas).expect("Missing l2 resource");
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()
Expand Down
9 changes: 5 additions & 4 deletions crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ use starknet_api::transaction::{
DeployAccountTransactionV3,
DeployTransaction,
DeployTransactionOutput,
DeprecatedResourceBoundsMapping,
Event,
EventContent,
EventData,
Expand All @@ -109,7 +110,6 @@ use starknet_api::transaction::{
PaymasterData,
Resource,
ResourceBounds,
DeprecatedResourceBoundsMapping,
RevertedTransactionExecutionStatus,
Tip,
Transaction,
Expand All @@ -119,6 +119,7 @@ use starknet_api::transaction::{
TransactionOutput,
TransactionSignature,
TransactionVersion,
ValidResourceBounds,
};
use starknet_types_core::felt::Felt;
use tracing::warn;
Expand Down Expand Up @@ -211,7 +212,7 @@ auto_storage_serde! {
pub sender_address: ContractAddress,
}
pub struct DeclareTransactionV3 {
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down Expand Up @@ -1162,7 +1163,7 @@ auto_storage_serde_conditionally_compressed! {
}

pub struct DeployAccountTransactionV3 {
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down Expand Up @@ -1198,7 +1199,7 @@ auto_storage_serde_conditionally_compressed! {
}

pub struct InvokeTransactionV3 {
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down
19 changes: 14 additions & 5 deletions crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ use starknet_api::transaction::{
DeployAccountTransactionV3,
DeployTransaction,
DeployTransactionOutput,
DeprecatedResourceBoundsMapping,
Event,
EventContent,
EventData,
Expand All @@ -132,7 +133,6 @@ use starknet_api::transaction::{
PaymasterData,
Resource,
ResourceBounds,
DeprecatedResourceBoundsMapping,
RevertedTransactionExecutionStatus,
Tip,
Transaction,
Expand All @@ -142,6 +142,7 @@ use starknet_api::transaction::{
TransactionOutput,
TransactionSignature,
TransactionVersion,
ValidResourceBounds,
};
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -517,7 +518,7 @@ auto_impl_get_test_instance! {
pub sender_address: ContractAddress,
}
pub struct DeclareTransactionV3 {
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down Expand Up @@ -550,7 +551,7 @@ auto_impl_get_test_instance! {
pub constructor_calldata: Calldata,
}
pub struct DeployAccountTransactionV3 {
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down Expand Up @@ -657,7 +658,7 @@ auto_impl_get_test_instance! {
pub calldata: Calldata,
}
pub struct InvokeTransactionV3 {
pub resource_bounds: DeprecatedResourceBoundsMapping,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down Expand Up @@ -783,6 +784,10 @@ auto_impl_get_test_instance! {
pub name: String,
pub r#type: String,
}
pub enum ValidResourceBounds {
L1Gas(ResourceBounds) = 0,
AllResources { l1_gas: ResourceBounds, l2_gas: ResourceBounds, l1_data_gas: ResourceBounds } = 1,
}

pub struct CasmContractClass {
pub prime: BigUint,
Expand Down Expand Up @@ -894,7 +899,7 @@ macro_rules! auto_impl_get_test_instance {
auto_impl_get_test_instance!($($rest)*);
};
// enums.
($(pub)? enum $name:ident { $($variant:ident $( ($ty:ty) )? = $num:expr ,)* } $($rest:tt)*) => {
($(pub)? enum $name:ident { $($variant:ident $( ($ty:ty) )? $( { $($field:ident : $field_ty:ty),* } )? = $num:expr ,)* } $($rest:tt)*) => {
impl GetTestInstance for $name {
fn get_test_instance(rng: &mut rand_chacha::ChaCha8Rng) -> Self {
use rand::Rng;
Expand All @@ -903,6 +908,10 @@ macro_rules! auto_impl_get_test_instance {
$(
$num => {
Self::$variant$((<$ty>::get_test_instance(rng)))?

$(Self::$variant {
$($field: <$field_ty>::get_test_instance(rng)),*
})?
}
)*
_ => {
Expand Down
42 changes: 38 additions & 4 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ impl TransactionHasher for DeclareTransactionV2 {
/// A declare V3 transaction.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct DeclareTransactionV3 {
pub resource_bounds: DeprecatedResourceBoundsMapping,
#[serde(deserialize_with = "deserialize_resource_bounds")]
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down Expand Up @@ -318,7 +319,8 @@ impl TransactionHasher for DeployAccountTransactionV1 {
/// A deploy account V3 transaction.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)]
pub struct DeployAccountTransactionV3 {
pub resource_bounds: DeprecatedResourceBoundsMapping,
#[serde(deserialize_with = "deserialize_resource_bounds")]
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down Expand Up @@ -455,7 +457,8 @@ impl TransactionHasher for InvokeTransactionV1 {
/// An invoke V3 transaction.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)]
pub struct InvokeTransactionV3 {
pub resource_bounds: DeprecatedResourceBoundsMapping,
#[serde(deserialize_with = "deserialize_resource_bounds")]
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
Expand Down Expand Up @@ -949,12 +952,43 @@ impl TryFrom<Vec<(Resource, ResourceBounds)>> for DeprecatedResourceBoundsMappin
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[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 },
}

/// Deserializes raw resource bounds, given as map, into valid resource bounds.
pub fn deserialize_resource_bounds<'de, D>(de: D) -> Result<ValidResourceBounds, D::Error>
where
D: Deserializer<'de>,
{
let raw_resource_bounds: BTreeMap<Resource, ResourceBounds> = Deserialize::deserialize(de)?;
ValidResourceBounds::try_from(raw_resource_bounds).map_err(serde::de::Error::custom)
}

/// Serializes ValidResourceBounds as map for BC.
// TODO(Nimrod): Can this be removed?
impl Serialize for ValidResourceBounds {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let map = match self {
ValidResourceBounds::L1Gas(l1_gas) => BTreeMap::from([
(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),
]),
};
DeprecatedResourceBoundsMapping(map).serialize(s)
}
}

impl TryFrom<BTreeMap<Resource, ResourceBounds>> for ValidResourceBounds {
type Error = StarknetApiError;
fn try_from(
Expand Down
15 changes: 7 additions & 8 deletions crates/starknet_api/src/transaction_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ use crate::transaction::{
DeployAccountTransactionV1,
DeployAccountTransactionV3,
DeployTransaction,
DeprecatedResourceBoundsMapping,
InvokeTransaction,
InvokeTransactionV0,
InvokeTransactionV1,
InvokeTransactionV3,
L1HandlerTransaction,
Resource,
ResourceBounds,
Tip,
Transaction,
TransactionHash,
TransactionVersion,
ValidResourceBounds,
};
use crate::StarknetApiError;

Expand Down Expand Up @@ -175,15 +174,15 @@ 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, 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),
};

let l2_resource_bounds =
resource_bounds_mapping.0.get(&Resource::L2Gas).expect("Missing l2 resource");
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()
Expand Down

0 comments on commit fdb5979

Please sign in to comment.