Skip to content

Commit

Permalink
refactor(starknet_api): gas vector to contain fields of type GasAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
dorimedini-starkware committed Oct 8, 2024
1 parent 4ce272b commit e512f78
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 30 deletions.
8 changes: 6 additions & 2 deletions crates/committer_cli/src/tests/utils/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use starknet_api::core::{
Nonce,
PatriciaKey,
};
use starknet_api::execution_resources::GasVector;
use starknet_api::execution_resources::{GasAmount, GasVector};
use starknet_api::state::{StorageKey, ThinStateDiff};
use starknet_api::transaction::{
Event,
Expand Down Expand Up @@ -49,7 +49,11 @@ pub(crate) fn get_transaction_output_for_hash(
},
}],
execution_status: expected_execution_status,
gas_consumed: GasVector { l1_gas: 0, l2_gas: 0, l1_data_gas: 64 },
gas_consumed: GasVector {
l1_gas: GasAmount(0),
l2_gas: GasAmount(0),
l1_data_gas: GasAmount(64),
},
messages_sent: vec![MessageToL1 {
from_address: ContractAddress(PatriciaKey::from(2_u128)),
to_address: EthAddress::try_from(Felt::from_bytes_be_slice(&[1_u8]))
Expand Down
9 changes: 1 addition & 8 deletions crates/papyrus_execution/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,7 @@ fn vm_resources_to_execution_resources(
steps: vm_resources.n_steps as u64,
builtin_instance_counter,
memory_holes: vm_resources.n_memory_holes as u64,
da_gas_consumed: StarknetApiGasVector {
l1_gas: l1_gas.0.try_into().map_err(|_| ExecutionError::GasConsumedOutOfRange)?,
l2_gas: l2_gas.0.try_into().map_err(|_| ExecutionError::GasConsumedOutOfRange)?,
l1_data_gas: l1_data_gas
.0
.try_into()
.map_err(|_| ExecutionError::GasConsumedOutOfRange)?,
},
da_gas_consumed: StarknetApiGasVector { l1_gas, l2_gas, l1_data_gas },
gas_consumed: StarknetApiGasVector::default(),
})
}
Expand Down
12 changes: 8 additions & 4 deletions crates/papyrus_protobuf/src/converters/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ impl TryFrom<protobuf::receipt::ExecutionResources> for ExecutionResources {

impl From<protobuf::receipt::execution_resources::GasVector> for GasVector {
fn from(value: protobuf::receipt::execution_resources::GasVector) -> Self {
GasVector { l1_gas: value.l1_gas, l1_data_gas: value.l1_data_gas, l2_gas: value.l2_gas }
GasVector {
l1_gas: starknet_api::execution_resources::GasAmount(value.l1_gas),
l1_data_gas: starknet_api::execution_resources::GasAmount(value.l1_data_gas),
l2_gas: starknet_api::execution_resources::GasAmount(value.l2_gas),
}
}
}

Expand All @@ -359,9 +363,9 @@ impl From<ExecutionResources> for protobuf::receipt::ExecutionResources {
impl From<GasVector> for protobuf::receipt::execution_resources::GasVector {
fn from(value: GasVector) -> Self {
protobuf::receipt::execution_resources::GasVector {
l1_gas: value.l1_gas,
l1_data_gas: value.l1_data_gas,
l2_gas: value.l2_gas,
l1_gas: value.l1_gas.0,
l1_data_gas: value.l1_data_gas.0,
l2_gas: value.l2_gas.0,
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/papyrus_rpc/src/v0_8/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use starknet_api::core::{
Nonce,
};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::execution_resources::GasAmount;
use starknet_api::serde_utils::bytes_from_hex_str;
use starknet_api::transaction::{
AccountDeploymentData,
Expand Down Expand Up @@ -902,8 +903,8 @@ pub struct ComputationResources {

#[derive(Debug, Default, Clone, Eq, Hash, PartialEq, Deserialize, Serialize, PartialOrd, Ord)]
pub struct DataAvailabilityResources {
pub l1_gas: u64,
pub l1_data_gas: u64,
pub l1_gas: GasAmount,
pub l1_data_gas: GasAmount,
}

impl Add for ExecutionResources {
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 @@ -64,7 +64,7 @@ use starknet_api::deprecated_contract_class::{
StructType,
TypedParameter,
};
use starknet_api::execution_resources::{Builtin, ExecutionResources, GasVector};
use starknet_api::execution_resources::{Builtin, ExecutionResources, GasAmount, GasVector};
use starknet_api::hash::{PoseidonHash, StarkHash};
use starknet_api::state::{
ContractClass,
Expand Down Expand Up @@ -284,14 +284,15 @@ auto_storage_serde! {
View = 0,
}
pub struct GasPrice(pub u128);
pub struct GasAmount(pub u64);
pub struct GasPricePerToken {
pub price_in_fri: GasPrice,
pub price_in_wei: GasPrice,
}
pub struct GasVector {
pub l1_gas: u64,
pub l1_data_gas: u64,
pub l2_gas: u64,
pub l1_gas: GasAmount,
pub l1_data_gas: GasAmount,
pub l2_gas: GasAmount,
}
pub struct GlobalRoot(pub StarkHash);
pub struct H160(pub [u8; 20]);
Expand Down
9 changes: 7 additions & 2 deletions crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ use starknet_api::deprecated_contract_class::{
StructType,
TypedParameter,
};
use starknet_api::execution_resources::{Builtin, ExecutionResources, GasVector};
use starknet_api::execution_resources::{Builtin, ExecutionResources, GasAmount, GasVector};
use starknet_api::felt;
use starknet_api::hash::{PoseidonHash, StarkHash};
use starknet_api::rpc_transaction::{
Expand Down Expand Up @@ -637,6 +637,7 @@ auto_impl_get_test_instance! {
pub enum FunctionType {
Function = 0,
}
pub struct GasAmount(pub u64);
pub struct GasPrice(pub u128);
pub struct GasPricePerToken {
pub price_in_fri: GasPrice,
Expand Down Expand Up @@ -1130,7 +1131,11 @@ impl GetTestInstance for ExecutionResources {

impl GetTestInstance for GasVector {
fn get_test_instance(rng: &mut ChaCha8Rng) -> Self {
Self { l1_gas: rng.next_u64(), l2_gas: rng.next_u64(), l1_data_gas: rng.next_u64() }
Self {
l1_gas: GasAmount(rng.next_u64()),
l2_gas: GasAmount(rng.next_u64()),
l1_data_gas: GasAmount(rng.next_u64()),
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions crates/starknet_api/src/block_hash/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use starknet_types_core::felt::Felt;

use super::block_hash_calculator::TransactionOutputForHash;
use crate::core::{ClassHash, CompiledClassHash, ContractAddress, EthAddress, Nonce};
use crate::execution_resources::GasVector;
use crate::execution_resources::{GasAmount, GasVector};
use crate::state::ThinStateDiff;
use crate::transaction::{
Fee,
Expand All @@ -24,7 +24,11 @@ pub(crate) fn get_transaction_output() -> TransactionOutputForHash {
messages_sent: vec![generate_message_to_l1(34), generate_message_to_l1(56)],
events: vec![],
execution_status,
gas_consumed: GasVector { l1_gas: 16580, l2_gas: 0, l1_data_gas: 32 },
gas_consumed: GasVector {
l1_gas: GasAmount(16580),
l2_gas: GasAmount(0),
l1_data_gas: GasAmount(32),
},
}
}

Expand Down
15 changes: 12 additions & 3 deletions crates/starknet_api/src/execution_resources.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;
use strum_macros::EnumIter;

use crate::block::{GasPrice, NonzeroGasPrice};
Expand All @@ -18,11 +19,19 @@ use crate::transaction::Fee;
Eq,
PartialEq,
PartialOrd,
Ord,
Serialize,
Deserialize,
Hash,
)]
pub struct GasAmount(pub u64);

impl From<GasAmount> for Felt {
fn from(gas_amount: GasAmount) -> Self {
Self::from(gas_amount.0)
}
}

macro_rules! impl_from_uint_for_gas_amount {
($($uint:ty),*) => {
$(
Expand Down Expand Up @@ -59,10 +68,10 @@ impl GasAmount {

#[derive(Debug, Default, Deserialize, Serialize, Clone, Eq, PartialEq)]
pub struct GasVector {
pub l1_gas: u64,
pub l1_data_gas: u64,
pub l1_gas: GasAmount,
pub l1_data_gas: GasAmount,
#[serde(default)]
pub l2_gas: u64,
pub l2_gas: GasAmount,
}

/// The execution resources used by a transaction.
Expand Down
6 changes: 3 additions & 3 deletions crates/starknet_client/src/reader/objects/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use starknet_api::core::{
EthAddress,
Nonce,
};
use starknet_api::execution_resources::GasVector;
use starknet_api::execution_resources::{GasAmount, GasVector};
use starknet_api::hash::StarkHash;
use starknet_api::transaction::{
AccountDeploymentData,
Expand Down Expand Up @@ -716,8 +716,8 @@ impl From<ExecutionResources> for starknet_api::execution_resources::ExecutionRe
None => GasVector {
// It's hardcoded that this field is 0 for pre-v0.13.2 blocks (this field is
// only used in calculating the receipt hash)
l1_gas: 0,
l2_gas: 0,
l1_gas: GasAmount(0),
l2_gas: GasAmount(0),
l1_data_gas: da_gas_consumed.l1_data_gas,
},
},
Expand Down

0 comments on commit e512f78

Please sign in to comment.