Skip to content

Commit

Permalink
fix: remove execution resources from block hash (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs authored Jun 4, 2024
1 parent f62b1aa commit 4bc26ac
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 51 deletions.
17 changes: 15 additions & 2 deletions src/block_hash/block_hash_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use crate::core::{EventCommitment, ReceiptCommitment, StateDiffCommitment, Trans
use crate::crypto::utils::HashChain;
use crate::data_availability::L1DataAvailabilityMode;
use crate::state::ThinStateDiff;
use crate::transaction::{TransactionHash, TransactionOutputCommon, TransactionSignature};
use crate::transaction::{
Event, Fee, GasVector, MessageToL1, TransactionExecutionStatus, TransactionHash,
TransactionSignature,
};
use crate::transaction_hash::ascii_as_felt;

#[cfg(test)]
Expand All @@ -22,9 +25,19 @@ static STARKNET_BLOCK_HASH0: Lazy<Felt> = Lazy::new(|| {
ascii_as_felt("STARKNET_BLOCK_HASH0").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH0'")
});

/// The common fields of transaction output types.
#[derive(Clone)]
pub struct TransactionOutputForHash {
pub actual_fee: Fee,
pub events: Vec<Event>,
pub execution_status: TransactionExecutionStatus,
pub gas_consumed: GasVector,
pub messages_sent: Vec<MessageToL1>,
}

pub struct TransactionHashingData {
pub transaction_signature: Option<TransactionSignature>,
pub transaction_output: TransactionOutputCommon,
pub transaction_output: TransactionOutputForHash,
pub transaction_hash: TransactionHash,
}

Expand Down
20 changes: 7 additions & 13 deletions src/block_hash/receipt_commitment.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::StarkHash;

use super::block_hash_calculator::TransactionHashingData;
use super::block_hash_calculator::{TransactionHashingData, TransactionOutputForHash};
use crate::core::ReceiptCommitment;
use crate::crypto::patricia_hash::calculate_root;
use crate::crypto::utils::HashChain;
use crate::hash::starknet_keccak_hash;
use crate::transaction::{
ExecutionResources, MessageToL1, TransactionExecutionStatus, TransactionHash,
TransactionOutputCommon,
};
use crate::transaction::{GasVector, MessageToL1, TransactionExecutionStatus, TransactionHash};

#[cfg(test)]
#[path = "receipt_commitment_test.rs"]
Expand All @@ -19,7 +16,7 @@ mod receipt_commitment_test;
#[derive(Clone)]
pub struct ReceiptElement {
pub transaction_hash: TransactionHash,
pub transaction_output: TransactionOutputCommon,
pub transaction_output: TransactionOutputForHash,
}

impl From<&TransactionHashingData> for ReceiptElement {
Expand Down Expand Up @@ -50,7 +47,7 @@ fn calculate_receipt_hash(receipt_element: &ReceiptElement) -> Felt {
.chain(&receipt_element.transaction_output.actual_fee.0.into())
.chain(&calculate_messages_sent_hash(&receipt_element.transaction_output.messages_sent))
.chain(&get_revert_reason_hash(&receipt_element.transaction_output.execution_status));
chain_execution_resources(hash_chain, &receipt_element.transaction_output.execution_resources)
chain_gas_consumed(hash_chain, &receipt_element.transaction_output.gas_consumed)
.get_poseidon_hash()
}

Expand Down Expand Up @@ -85,12 +82,9 @@ fn get_revert_reason_hash(execution_status: &TransactionExecutionStatus) -> Felt
// L1 gas consumed (In the current RPC:
// L1 gas consumed for calldata + L1 gas consumed for steps and builtins.
// L1 data gas consumed (In the current RPC: L1 data gas consumed for blob).
fn chain_execution_resources(
hash_chain: HashChain,
execution_resources: &ExecutionResources,
) -> HashChain {
fn chain_gas_consumed(hash_chain: HashChain, gas_consumed: &GasVector) -> HashChain {
hash_chain
.chain(&Felt::ZERO) // L2 gas consumed
.chain(&execution_resources.gas_consumed.l1_gas.into())
.chain(&execution_resources.gas_consumed.l1_data_gas.into())
.chain(&gas_consumed.l1_gas.into())
.chain(&gas_consumed.l1_data_gas.into())
}
20 changes: 6 additions & 14 deletions src/block_hash/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
use std::collections::HashMap;

use indexmap::indexmap;
use primitive_types::H160;
use starknet_types_core::felt::Felt;

use super::block_hash_calculator::TransactionOutputForHash;
use crate::core::{ClassHash, CompiledClassHash, ContractAddress, EthAddress, Nonce};
use crate::state::ThinStateDiff;
use crate::transaction::{
Builtin, ExecutionResources, Fee, GasVector, L2ToL1Payload, MessageToL1,
RevertedTransactionExecutionStatus, TransactionExecutionStatus, TransactionOutputCommon,
Fee, GasVector, L2ToL1Payload, MessageToL1, RevertedTransactionExecutionStatus,
TransactionExecutionStatus,
};

pub(crate) fn get_transaction_output() -> TransactionOutputCommon {
pub(crate) fn get_transaction_output() -> TransactionOutputForHash {
let execution_status =
TransactionExecutionStatus::Reverted(RevertedTransactionExecutionStatus {
revert_reason: "aborted".to_string(),
});
let execution_resources = ExecutionResources {
steps: 98,
builtin_instance_counter: HashMap::from([(Builtin::Bitwise, 11), (Builtin::EcOp, 22)]),
memory_holes: 76,
da_gas_consumed: GasVector { l1_gas: 54, l1_data_gas: 10 },
gas_consumed: GasVector { l1_gas: 16580, l1_data_gas: 32 },
};
TransactionOutputCommon {
TransactionOutputForHash {
actual_fee: Fee(99804),
messages_sent: vec![generate_message_to_l1(34), generate_message_to_l1(56)],
events: vec![],
execution_status,
execution_resources,
gas_consumed: GasVector { l1_gas: 16580, l1_data_gas: 32 },
}
}

Expand Down
22 changes: 0 additions & 22 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,28 +148,6 @@ impl TransactionOutput {
}
}

/// The common fields of transaction output types.
#[derive(Clone)]
pub struct TransactionOutputCommon {
pub actual_fee: Fee,
pub events: Vec<Event>,
pub execution_status: TransactionExecutionStatus,
pub execution_resources: ExecutionResources,
pub messages_sent: Vec<MessageToL1>,
}

impl From<TransactionOutput> for TransactionOutputCommon {
fn from(transaction_output: TransactionOutput) -> Self {
Self {
actual_fee: transaction_output.actual_fee(),
events: transaction_output.events().to_vec(),
execution_status: transaction_output.execution_status().to_owned(),
execution_resources: transaction_output.execution_resources().to_owned(),
messages_sent: transaction_output.messages_sent().to_owned(),
}
}
}

/// A declare V0 or V1 transaction (same schema but different version).
#[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)]
pub struct DeclareTransactionV0V1 {
Expand Down

0 comments on commit 4bc26ac

Please sign in to comment.