Skip to content

Commit

Permalink
Merge pull request #2415 from eqlabs/vbar/execution-resources-with-l2…
Browse files Browse the repository at this point in the history
…-gas

feat: added l2_gas to P2P & DTO ExecutionResources
  • Loading branch information
vbar authored Nov 28, 2024
2 parents 28f5926 + c5f195a commit 2650271
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 16 deletions.
8 changes: 6 additions & 2 deletions crates/p2p/src/client/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use pathfinder_common::receipt::{
ExecutionResources,
ExecutionStatus,
L1Gas,
L2Gas,
L2ToL1Message,
Receipt,
};
Expand Down Expand Up @@ -340,6 +341,7 @@ impl ToDto<p2p_proto::receipt::Receipt> for (&TransactionVariant, Receipt) {
l1_data_gas: Some(da.l1_data_gas.into()),
total_l1_gas: Some(total.l1_gas.into()),
total_l1_data_gas: Some(total.l1_data_gas.into()),
l2_gas: Some(e.l2_gas.0.into()),
}
},
revert_reason,
Expand Down Expand Up @@ -760,8 +762,10 @@ impl TryFrom<(p2p_proto::receipt::Receipt, TransactionIndex)> for crate::client:
)?
.0,
},
// TODO: Fix this when we have a way to get L2 gas from the gateway
l2_gas: Default::default(),
l2_gas: L2Gas(
GasPrice::try_from(common.execution_resources.l2_gas.unwrap_or_default())?
.0,
),
},
l2_to_l1_messages: common
.messages_sent
Expand Down
1 change: 1 addition & 0 deletions crates/p2p_proto/proto/receipt.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ message Receipt {
starknet.common.Felt252 l1_data_gas = 5;
starknet.common.Felt252 total_l1_gas = 6;
starknet.common.Felt252 total_l1_data_gas = 7;
starknet.common.Felt252 l2_gas = 8;
}

message Common {
Expand Down
2 changes: 2 additions & 0 deletions crates/p2p_proto/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub struct ExecutionResources {
pub total_l1_gas: Option<Felt>,
#[optional]
pub total_l1_data_gas: Option<Felt>,
#[optional]
pub l2_gas: Option<Felt>,
}

pub mod execution_resources {
Expand Down
111 changes: 98 additions & 13 deletions crates/storage/src/connection/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,6 @@ pub(crate) mod dto {
pub data_availability: L1Gas,
}

/// Represents execution resources for L2 transaction.
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ExecutionResourcesV1 {
Expand All @@ -817,6 +816,18 @@ pub(crate) mod dto {
pub total_gas_consumed: L1Gas,
}

/// Represents execution resources for L2 transaction.
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ExecutionResourcesV2 {
pub builtins: BuiltinCountersV1,
pub n_steps: u64,
pub n_memory_holes: u64,
pub data_availability: L1Gas,
pub total_gas_consumed: L1Gas,
pub l2_gas_consumed: L2Gas,
}

#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct L1Gas {
Expand All @@ -825,20 +836,40 @@ pub(crate) mod dto {
pub l1_data_gas: Option<u128>,
}

impl From<ExecutionResourcesV0> for ExecutionResourcesV1 {
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct L2Gas {
pub l2_gas: u128,
}

impl From<ExecutionResourcesV0> for ExecutionResourcesV2 {
fn from(value: ExecutionResourcesV0) -> Self {
Self {
builtins: value.builtins.into(),
n_steps: value.n_steps,
n_memory_holes: value.n_memory_holes,
data_availability: value.data_availability,
total_gas_consumed: Default::default(),
l2_gas_consumed: Default::default(),
}
}
}

impl From<ExecutionResourcesV1> for pathfinder_common::receipt::ExecutionResources {
impl From<ExecutionResourcesV1> for ExecutionResourcesV2 {
fn from(value: ExecutionResourcesV1) -> Self {
Self {
builtins: value.builtins,
n_steps: value.n_steps,
n_memory_holes: value.n_memory_holes,
data_availability: value.data_availability,
total_gas_consumed: Default::default(),
l2_gas_consumed: Default::default(),
}
}
}

impl From<ExecutionResourcesV2> for pathfinder_common::receipt::ExecutionResources {
fn from(value: ExecutionResourcesV2) -> Self {
Self {
builtins: value.builtins.into(),
n_steps: value.n_steps,
Expand All @@ -863,12 +894,12 @@ pub(crate) mod dto {
},
_ => Default::default(),
},
l2_gas: Default::default(),
l2_gas: pathfinder_common::receipt::L2Gas(value.l2_gas_consumed.l2_gas),
}
}
}

impl From<&pathfinder_common::receipt::ExecutionResources> for ExecutionResourcesV1 {
impl From<&pathfinder_common::receipt::ExecutionResources> for ExecutionResourcesV2 {
fn from(value: &pathfinder_common::receipt::ExecutionResources) -> Self {
Self {
builtins: (&value.builtins).into(),
Expand All @@ -882,6 +913,9 @@ pub(crate) mod dto {
l1_gas: Some(value.total_gas_consumed.l1_gas),
l1_data_gas: Some(value.total_gas_consumed.l1_data_gas),
},
l2_gas_consumed: L2Gas {
l2_gas: value.l2_gas.0,
},
}
}
}
Expand Down Expand Up @@ -930,6 +964,33 @@ pub(crate) mod dto {
}
}

impl<T> Dummy<T> for ExecutionResourcesV2 {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &T, rng: &mut R) -> Self {
let (l1_gas, l1_data_gas) = if rng.gen() {
(Some(rng.next_u32() as u128), Some(rng.next_u32() as u128))
} else {
(None, None)
};

Self {
builtins: Faker.fake_with_rng(rng),
n_steps: rng.next_u32() as u64,
n_memory_holes: rng.next_u32() as u64,
data_availability: L1Gas {
l1_gas,
l1_data_gas,
},
total_gas_consumed: L1Gas {
l1_gas: l1_gas.map(|x| x + rng.next_u32() as u128),
l1_data_gas: l1_data_gas.map(|x| x + rng.next_u32() as u128),
},
l2_gas_consumed: L2Gas {
l2_gas: rng.next_u32() as u128,
},
}
}
}

#[derive(Copy, Clone, Default, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct BuiltinCountersV0 {
Expand Down Expand Up @@ -1201,7 +1262,6 @@ pub(crate) mod dto {
pub execution_status: ExecutionStatus,
}

/// Represents deserialized L2 transaction receipt data.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Dummy)]
#[serde(deny_unknown_fields)]
pub struct ReceiptV2 {
Expand All @@ -1213,7 +1273,19 @@ pub(crate) mod dto {
pub execution_status: ExecutionStatus,
}

impl From<ReceiptV0> for ReceiptV2 {
/// Represents deserialized L2 transaction receipt data.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Dummy)]
#[serde(deny_unknown_fields)]
pub struct ReceiptV3 {
pub actual_fee: MinimalFelt,
pub execution_resources: Option<ExecutionResourcesV2>,
pub l2_to_l1_messages: Vec<L2ToL1MessageV1>,
pub transaction_hash: MinimalFelt,
pub transaction_index: TransactionIndex,
pub execution_status: ExecutionStatus,
}

impl From<ReceiptV0> for ReceiptV3 {
fn from(value: ReceiptV0) -> Self {
Self {
actual_fee: value.actual_fee,
Expand All @@ -1230,7 +1302,7 @@ pub(crate) mod dto {
}
}

impl From<ReceiptV1> for ReceiptV2 {
impl From<ReceiptV1> for ReceiptV3 {
fn from(value: ReceiptV1) -> Self {
Self {
actual_fee: value.actual_fee,
Expand All @@ -1247,11 +1319,24 @@ pub(crate) mod dto {
}
}

impl From<ReceiptV2> for pathfinder_common::receipt::Receipt {
impl From<ReceiptV2> for ReceiptV3 {
fn from(value: ReceiptV2) -> Self {
Self {
actual_fee: value.actual_fee,
execution_resources: value.execution_resources.map(Into::into),
l2_to_l1_messages: value.l2_to_l1_messages,
transaction_hash: value.transaction_hash,
transaction_index: value.transaction_index,
execution_status: value.execution_status,
}
}
}

impl From<ReceiptV3> for pathfinder_common::receipt::Receipt {
fn from(value: ReceiptV3) -> Self {
use pathfinder_common::receipt as common;

let ReceiptV2 {
let ReceiptV3 {
actual_fee,
execution_resources,
// This information is redundant as it is already in the transaction itself.
Expand All @@ -1277,7 +1362,7 @@ pub(crate) mod dto {
}
}

impl From<&pathfinder_common::receipt::Receipt> for ReceiptV2 {
impl From<&pathfinder_common::receipt::Receipt> for ReceiptV3 {
fn from(value: &pathfinder_common::receipt::Receipt) -> Self {
Self {
actual_fee: value.actual_fee.as_inner().to_owned().into(),
Expand Down Expand Up @@ -1443,7 +1528,7 @@ pub(crate) mod dto {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct TransactionWithReceiptV3 {
pub transaction: TransactionV2,
pub receipt: ReceiptV2,
pub receipt: ReceiptV3,
}

impl From<TransactionWithReceiptV0> for TransactionWithReceiptV3 {
Expand All @@ -1468,7 +1553,7 @@ pub(crate) mod dto {
fn from(v2: TransactionWithReceiptV2) -> Self {
Self {
transaction: v2.transaction.into(),
receipt: v2.receipt,
receipt: v2.receipt.into(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/src/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub mod generate {
let transaction_hash = t.variant.calculate_hash(ChainId::SEPOLIA_TESTNET, false);
t.hash = transaction_hash;

let r: Receipt = crate::connection::transaction::dto::ReceiptV2 {
let r: Receipt = crate::connection::transaction::dto::ReceiptV3 {
transaction_hash: transaction_hash.as_inner().to_owned().into(),
transaction_index: TransactionIndex::new_or_panic(
i.try_into().expect("u64 is at least as wide as usize"),
Expand Down

0 comments on commit 2650271

Please sign in to comment.