From 50e64938068d48457a0f618c96dfd0b6b535f87c Mon Sep 17 00:00:00 2001 From: yoavGrs <97383386+yoavGrs@users.noreply.github.com> Date: Wed, 18 Sep 2024 10:15:49 +0300 Subject: [PATCH] feat(block_hash): add l2 gas to the block hash (#683) --- crates/starknet_api/src/block.rs | 1 + .../src/block_hash/block_hash_calculator.rs | 10 +++++++++- .../src/block_hash/block_hash_calculator_test.rs | 4 +++- crates/starknet_api/src/crypto/utils.rs | 12 ++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/starknet_api/src/block.rs b/crates/starknet_api/src/block.rs index 26729420fc..dd0737f23b 100644 --- a/crates/starknet_api/src/block.rs +++ b/crates/starknet_api/src/block.rs @@ -90,6 +90,7 @@ pub struct BlockHeaderWithoutHash { pub block_number: BlockNumber, pub l1_gas_price: GasPricePerToken, pub l1_data_gas_price: GasPricePerToken, + pub l2_gas_price: GasPricePerToken, pub state_root: GlobalRoot, pub sequencer: SequencerContractAddress, pub timestamp: BlockTimestamp, diff --git a/crates/starknet_api/src/block_hash/block_hash_calculator.rs b/crates/starknet_api/src/block_hash/block_hash_calculator.rs index a4dcdf3c40..56e2075736 100644 --- a/crates/starknet_api/src/block_hash/block_hash_calculator.rs +++ b/crates/starknet_api/src/block_hash/block_hash_calculator.rs @@ -8,7 +8,7 @@ use super::event_commitment::{calculate_event_commitment, EventLeafElement}; use super::receipt_commitment::{calculate_receipt_commitment, ReceiptElement}; use super::state_diff_hash::calculate_state_diff_hash; use super::transaction_commitment::{calculate_transaction_commitment, TransactionLeafElement}; -use crate::block::{BlockHash, BlockHeaderWithoutHash}; +use crate::block::{BlockHash, BlockHeaderWithoutHash, StarknetVersion}; use crate::core::{EventCommitment, ReceiptCommitment, StateDiffCommitment, TransactionCommitment}; use crate::crypto::utils::HashChain; use crate::data_availability::L1DataAvailabilityMode; @@ -31,6 +31,8 @@ mod block_hash_calculator_test; static STARKNET_BLOCK_HASH0: LazyLock = LazyLock::new(|| { ascii_as_felt("STARKNET_BLOCK_HASH0").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH0'") }); +static STARKNET_VERSION_V0_13_3: LazyLock = + LazyLock::new(|| StarknetVersion("0.13.3".to_owned())); /// The common fields of transaction output types. #[derive(Clone, Debug, Deserialize, PartialEq, Eq)] @@ -69,6 +71,8 @@ pub fn calculate_block_hash( header: BlockHeaderWithoutHash, block_commitments: BlockHeaderCommitments, ) -> BlockHash { + let l2_gas_prices = + [&header.l2_gas_price.price_in_wei.0.into(), &header.l2_gas_price.price_in_fri.0.into()]; BlockHash( HashChain::new() .chain(&STARKNET_BLOCK_HASH0) @@ -85,6 +89,10 @@ pub fn calculate_block_hash( .chain(&header.l1_gas_price.price_in_fri.0.into()) .chain(&header.l1_data_gas_price.price_in_wei.0.into()) .chain(&header.l1_data_gas_price.price_in_fri.0.into()) + .chain_iter_if_fn(|| { + (header.starknet_version >= *STARKNET_VERSION_V0_13_3) + .then_some(l2_gas_prices.into_iter()) + }) .chain(&ascii_as_felt(&header.starknet_version.0).expect("Expect ASCII version")) .chain(&Felt::ZERO) .chain(&header.parent_hash.0) diff --git a/crates/starknet_api/src/block_hash/block_hash_calculator_test.rs b/crates/starknet_api/src/block_hash/block_hash_calculator_test.rs index 258c793b19..45b28acff4 100644 --- a/crates/starknet_api/src/block_hash/block_hash_calculator_test.rs +++ b/crates/starknet_api/src/block_hash/block_hash_calculator_test.rs @@ -73,6 +73,7 @@ fn test_block_hash_regression() { price_in_fri: GasPrice(10), price_in_wei: GasPrice(9), }, + l2_gas_price: GasPricePerToken { price_in_fri: GasPrice(11), price_in_wei: GasPrice(12) }, starknet_version: StarknetVersion("10".to_owned()), parent_hash: BlockHash(Felt::from(11_u8)), }; @@ -86,7 +87,7 @@ fn test_block_hash_regression() { let block_commitments = calculate_block_commitments(&transactions_data, &state_diff, block_header.l1_da_mode); - let expected_hash = felt!("0x061e4998d51a248f1d0288d7e17f6287757b0e5e6c5e1e58ddf740616e312134"); + let expected_hash = felt!("0x75ebad05e0b18dbfbabec32edffed5992b24f8d2d9666d04982971eac0ab06f"); assert_eq!(BlockHash(expected_hash), calculate_block_hash(block_header, block_commitments),); } @@ -109,6 +110,7 @@ fn change_field_of_hash_input() { price_in_fri: GasPrice(1), price_in_wei: GasPrice(1), }, + l2_gas_price: GasPricePerToken { price_in_fri: GasPrice(1), price_in_wei: GasPrice(1) }, state_root: GlobalRoot(Felt::ONE), sequencer: SequencerContractAddress(ContractAddress::from(1_u128)), timestamp: BlockTimestamp(1), diff --git a/crates/starknet_api/src/crypto/utils.rs b/crates/starknet_api/src/crypto/utils.rs index 0b9fc7d847..28451fddaa 100644 --- a/crates/starknet_api/src/crypto/utils.rs +++ b/crates/starknet_api/src/crypto/utils.rs @@ -98,6 +98,18 @@ impl HashChain { felts.fold(self, |current, felt| current.chain(felt)) } + // Chains many felts to the hash chain according the result of a function. + pub fn chain_iter_if_fn<'a, F, I>(self, f: F) -> Self + where + F: Fn() -> Option, + I: Iterator, + { + match f() { + Some(felts) => self.chain_iter(felts), + None => self, + } + } + // Chains the number of felts followed by the felts themselves to the hash chain. pub fn chain_size_and_elements(self, felts: &[Felt]) -> Self { self.chain(&felts.len().into()).chain_iter(felts.iter())