Skip to content

Commit

Permalink
fix(katana): include full fee info in primitive receipts (#1948)
Browse files Browse the repository at this point in the history
include more verbose fee info in primitive receipt
  • Loading branch information
kariy authored May 13, 2024
1 parent 6dca944 commit d21163e
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 59 deletions.
15 changes: 14 additions & 1 deletion crates/katana/core/src/backend/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ mod tests {
use katana_primitives::block::{
Block, FinalityStatus, GasPrices, Header, SealedBlockWithStatus,
};
use katana_primitives::fee::TxFeeInfo;
use katana_primitives::genesis::constant::{
DEFAULT_FEE_TOKEN_ADDRESS, DEFAULT_LEGACY_ERC20_CONTRACT_CASM,
DEFAULT_LEGACY_ERC20_CONTRACT_CLASS_HASH, DEFAULT_LEGACY_UDC_CASM,
Expand All @@ -156,6 +157,7 @@ mod tests {
};
use katana_provider::traits::state::StateFactoryProvider;
use katana_provider::traits::transaction::{TransactionProvider, TransactionTraceProvider};
use starknet::core::types::PriceUnit;
use starknet::macros::felt;

use super::Blockchain;
Expand Down Expand Up @@ -254,7 +256,18 @@ mod tests {
.insert_block_with_states_and_receipts(
dummy_block.clone(),
StateUpdatesWithDeclaredClasses::default(),
vec![Receipt::Invoke(InvokeTxReceipt::default())],
vec![Receipt::Invoke(InvokeTxReceipt {
revert_error: None,
events: Vec::new(),
messages_sent: Vec::new(),
execution_resources: Default::default(),
fee: TxFeeInfo {
gas_price: 0,
overall_fee: 0,
gas_consumed: 0,
unit: PriceUnit::Wei,
},
})],
vec![TxExecInfo::default()],
)
.unwrap();
Expand Down
14 changes: 3 additions & 11 deletions crates/katana/executor/src/abstraction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub use error::*;
pub use executor::*;
use katana_primitives::class::{ClassHash, CompiledClass, CompiledClassHash, FlattenedSierraClass};
use katana_primitives::contract::{ContractAddress, Nonce, StorageKey, StorageValue};
use katana_primitives::fee::TxFeeInfo;
use katana_primitives::receipt::Receipt;
use katana_primitives::state::{StateUpdates, StateUpdatesWithDeclaredClasses};
use katana_primitives::trace::TxExecInfo;
Expand Down Expand Up @@ -105,13 +104,13 @@ pub struct EntryPointCall {
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum ExecutionResult {
Success { receipt: Receipt, trace: TxExecInfo, fee: TxFeeInfo },
Success { receipt: Receipt, trace: TxExecInfo },
Failed { error: ExecutionError },
}

impl ExecutionResult {
pub fn new_success(receipt: Receipt, trace: TxExecInfo, fee: TxFeeInfo) -> Self {
ExecutionResult::Success { receipt, trace, fee }
pub fn new_success(receipt: Receipt, trace: TxExecInfo) -> Self {
ExecutionResult::Success { receipt, trace }
}

pub fn new_failed(error: impl Into<ExecutionError>) -> Self {
Expand Down Expand Up @@ -139,13 +138,6 @@ impl ExecutionResult {
_ => None,
}
}

pub fn fee(&self) -> Option<&TxFeeInfo> {
match self {
ExecutionResult::Success { fee, .. } => Some(fee),
_ => None,
}
}
}

#[derive(Debug, Clone)]
Expand Down
12 changes: 6 additions & 6 deletions crates/katana/executor/src/implementation/blockifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use starknet_api::block::{BlockNumber, BlockTimestamp};
use tracing::info;

use self::state::CachedState;
use crate::utils::receipt_from_exec_info;
use crate::utils::build_receipt;
use crate::{
BlockExecutor, EntryPointCall, ExecutionError, ExecutionOutput, ExecutionResult,
ExecutionStats, ExecutorExt, ExecutorFactory, ExecutorResult, ResultAndStates, SimulationFlag,
Expand Down Expand Up @@ -188,19 +188,19 @@ impl<'a> BlockExecutor<'a> for StarknetVMProcessor<'a> {
Ok((info, fee)) => {
// get the trace and receipt from the execution info
let trace = utils::to_exec_info(info);
let receipt = receipt_from_exec_info(&tx, &trace);
let receipt = build_receipt(&tx, fee, &trace);

crate::utils::log_resources(&trace.actual_resources);
crate::utils::log_events(receipt.events());

self.stats.l1_gas_used += fee.gas_consumed;
self.stats.l1_gas_used += receipt.fee().gas_consumed;
self.stats.cairo_steps_used += receipt.resources_used().steps as u128;

if let Some(reason) = receipt.revert_reason() {
info!(target: LOG_TARGET, reason = %reason, "Transaction reverted.");
}

ExecutionResult::new_success(receipt, trace, fee)
ExecutionResult::new_success(receipt, trace)
}
Err(e) => {
info!(target: LOG_TARGET, error = %e, "Executing transaction.");
Expand Down Expand Up @@ -262,8 +262,8 @@ impl ExecutorExt for StarknetVMProcessor<'_> {
let result = match res {
Ok((info, fee)) => {
let trace = utils::to_exec_info(info);
let receipt = receipt_from_exec_info(&tx, &trace);
ExecutionResult::new_success(receipt, trace, fee)
let receipt = build_receipt(&tx, fee, &trace);
ExecutionResult::new_success(receipt, trace)
}
Err(e) => ExecutionResult::new_failed(e),
};
Expand Down
12 changes: 6 additions & 6 deletions crates/katana/executor/src/implementation/sir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::abstraction::{
BlockExecutor, ExecutionOutput, ExecutorExt, ExecutorFactory, ExecutorResult, SimulationFlag,
StateProviderDb,
};
use crate::utils::receipt_from_exec_info;
use crate::utils::build_receipt;
use crate::{EntryPointCall, ExecutionError, ExecutionResult, ExecutionStats, ResultAndStates};

pub(crate) const LOG_TARGET: &str = "katana::executor::sir";
Expand Down Expand Up @@ -155,19 +155,19 @@ impl<'a> BlockExecutor<'a> for StarknetVMProcessor<'a> {
Ok((info, fee)) => {
// get the trace and receipt from the execution info
let trace = utils::to_exec_info(&info);
let receipt = receipt_from_exec_info(&tx, &trace);
let receipt = build_receipt(&tx, fee, &trace);

crate::utils::log_resources(&trace.actual_resources);
crate::utils::log_events(receipt.events());

self.stats.l1_gas_used += fee.gas_consumed;
self.stats.l1_gas_used += receipt.fee().gas_consumed;
self.stats.cairo_steps_used += receipt.resources_used().steps as u128;

if let Some(reason) = receipt.revert_reason() {
info!(target: LOG_TARGET, reason = %reason, "Transaction reverted.");
}

ExecutionResult::new_success(receipt, trace, fee)
ExecutionResult::new_success(receipt, trace)
}
Err(e) => {
info!(target: LOG_TARGET, error = %e, "Executing transaction.");
Expand Down Expand Up @@ -235,8 +235,8 @@ impl<'a> ExecutorExt for StarknetVMProcessor<'a> {
Ok((info, fee)) => {
// get the trace and receipt from the execution info
let trace = utils::to_exec_info(&info);
let receipt = receipt_from_exec_info(&tx, &trace);
ExecutionResult::new_success(receipt, trace, fee)
let receipt = build_receipt(&tx, fee, &trace);
ExecutionResult::new_success(receipt, trace)
}
Err(e) => ExecutionResult::new_failed(e),
};
Expand Down
12 changes: 6 additions & 6 deletions crates/katana/executor/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use convert_case::{Case, Casing};
use katana_primitives::fee::TxFeeInfo;
use katana_primitives::receipt::{
DeclareTxReceipt, DeployAccountTxReceipt, Event, InvokeTxReceipt, L1HandlerTxReceipt,
MessageToL1, Receipt, TxExecutionResources,
Expand Down Expand Up @@ -47,8 +48,7 @@ pub fn log_events(events: &[Event]) {
}
}

pub fn receipt_from_exec_info(tx: &Tx, info: &TxExecInfo) -> Receipt {
let actual_fee = info.actual_fee;
pub fn build_receipt(tx: &Tx, fee: TxFeeInfo, info: &TxExecInfo) -> Receipt {
let events = events_from_exec_info(info);
let revert_error = info.revert_error.clone();
let messages_sent = l2_to_l1_messages_from_exec_info(info);
Expand All @@ -57,23 +57,23 @@ pub fn receipt_from_exec_info(tx: &Tx, info: &TxExecInfo) -> Receipt {
match tx {
Tx::Invoke(_) => Receipt::Invoke(InvokeTxReceipt {
events,
actual_fee,
fee,
revert_error,
messages_sent,
execution_resources: actual_resources,
}),

Tx::Declare(_) => Receipt::Declare(DeclareTxReceipt {
events,
actual_fee,
fee,
revert_error,
messages_sent,
execution_resources: actual_resources,
}),

Tx::L1Handler(tx) => Receipt::L1Handler(L1HandlerTxReceipt {
events,
actual_fee,
fee,
revert_error,
messages_sent,
message_hash: tx.message_hash,
Expand All @@ -82,7 +82,7 @@ pub fn receipt_from_exec_info(tx: &Tx, info: &TxExecInfo) -> Receipt {

Tx::DeployAccount(tx) => Receipt::DeployAccount(DeployAccountTxReceipt {
events,
actual_fee,
fee,
revert_error,
messages_sent,
execution_resources: actual_resources,
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/executor/tests/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn test_executor_with_valid_blocks_impl<EF: ExecutorFactory>(
let actual_txs: Vec<TxWithHash> = transactions
.iter()
.map(|(tx, res)| {
if let Some(fee) = res.fee() {
if let Some(fee) = res.receipt().map(|r| r.fee()) {
actual_total_gas += fee.gas_consumed;
}
if let Some(rec) = res.receipt() {
Expand Down
3 changes: 2 additions & 1 deletion crates/katana/primitives/src/fee.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use starknet::core::types::PriceUnit;

/// Information regarding the fee and gas usages of a transaction.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TxFeeInfo {
/// The total amount of L1 gas consumed by the transaction.
pub gas_consumed: u128,
Expand Down
28 changes: 19 additions & 9 deletions crates/katana/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloy_primitives::B256;

use crate::contract::ContractAddress;
use crate::fee::TxFeeInfo;
use crate::FieldElement;

#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -27,11 +28,11 @@ pub struct MessageToL1 {
}

/// Receipt for a `Invoke` transaction.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct InvokeTxReceipt {
/// Actual fee paid for the transaction.
pub actual_fee: u128,
/// Information about the transaction fee.
pub fee: TxFeeInfo,
/// Events emitted by contracts.
pub events: Vec<Event>,
/// Messages sent to L1.
Expand All @@ -46,8 +47,8 @@ pub struct InvokeTxReceipt {
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DeclareTxReceipt {
/// Actual fee paid for the transaction.
pub actual_fee: u128,
/// Information about the transaction fee.
pub fee: TxFeeInfo,
/// Events emitted by contracts.
pub events: Vec<Event>,
/// Messages sent to L1.
Expand All @@ -62,8 +63,8 @@ pub struct DeclareTxReceipt {
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct L1HandlerTxReceipt {
/// Actual fee paid for the transaction.
pub actual_fee: u128,
/// Information about the transaction fee.
pub fee: TxFeeInfo,
/// Events emitted by contracts.
pub events: Vec<Event>,
/// The hash of the L1 message
Expand All @@ -80,8 +81,8 @@ pub struct L1HandlerTxReceipt {
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DeployAccountTxReceipt {
/// Actual fee paid for the transaction.
pub actual_fee: u128,
/// Information about the transaction fee.
pub fee: TxFeeInfo,
/// Events emitted by contracts.
pub events: Vec<Event>,
/// Messages sent to L1.
Expand Down Expand Up @@ -151,6 +152,15 @@ impl Receipt {
Receipt::DeployAccount(rct) => &rct.execution_resources,
}
}

pub fn fee(&self) -> &TxFeeInfo {
match self {
Receipt::Invoke(rct) => &rct.fee,
Receipt::Declare(rct) => &rct.fee,
Receipt::L1Handler(rct) => &rct.fee,
Receipt::DeployAccount(rct) => &rct.fee,
}
}
}

/// Transaction execution resources.
Expand Down
Loading

0 comments on commit d21163e

Please sign in to comment.