Skip to content

Commit

Permalink
feat(block_hash): add l2 gas to the block hash (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs committed Sep 18, 2024
1 parent 3e0186d commit 50e6493
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/starknet_api/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion crates/starknet_api/src/block_hash/block_hash_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +31,8 @@ mod block_hash_calculator_test;
static STARKNET_BLOCK_HASH0: LazyLock<Felt> = LazyLock::new(|| {
ascii_as_felt("STARKNET_BLOCK_HASH0").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH0'")
});
static STARKNET_VERSION_V0_13_3: LazyLock<StarknetVersion> =
LazyLock::new(|| StarknetVersion("0.13.3".to_owned()));

/// The common fields of transaction output types.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
};
Expand All @@ -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),);
}
Expand All @@ -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),
Expand Down
12 changes: 12 additions & 0 deletions crates/starknet_api/src/crypto/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
I: Iterator<Item = &'a Felt>,
{
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())
Expand Down

0 comments on commit 50e6493

Please sign in to comment.