Skip to content

Commit

Permalink
feat(starknet_api): chain gas prices hash (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs authored Sep 23, 2024
1 parent a9beaf5 commit 395fec7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
58 changes: 45 additions & 13 deletions 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, StarknetVersion};
use crate::block::{BlockHash, BlockHeaderWithoutHash, GasPricePerToken, StarknetVersion};
use crate::core::{EventCommitment, ReceiptCommitment, StateDiffCommitment, TransactionCommitment};
use crate::crypto::utils::HashChain;
use crate::data_availability::L1DataAvailabilityMode;
Expand Down Expand Up @@ -78,15 +78,12 @@ pub struct BlockHeaderCommitments {
/// Poseidon (
/// “STARKNET_BLOCK_HASH0”, block_number, global_state_root, sequencer_address,
/// block_timestamp, concat_counts, state_diff_hash, transaction_commitment,
/// event_commitment, receipt_commitment, gas_price_wei, gas_price_fri,
/// data_gas_price_wei, data_gas_price_fri, starknet_version, 0, parent_block_hash
/// event_commitment, receipt_commitment, gas_prices, starknet_version, 0, parent_block_hash
/// ).
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 @@ -99,14 +96,15 @@ pub fn calculate_block_hash(
.chain(&block_commitments.transaction_commitment.0)
.chain(&block_commitments.event_commitment.0)
.chain(&block_commitments.receipt_commitment.0)
.chain(&header.l1_gas_price.price_in_wei.0.into())
.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 >= BlockHashVersion::VO_13_3.into())
.then_some(l2_gas_prices.into_iter())
})
.chain_iter(
gas_prices_to_hash(
&header.l1_gas_price,
&header.l1_data_gas_price,
&header.l2_gas_price,
&header.starknet_version,
)
.iter(),
)
.chain(
&ascii_as_felt(&header.starknet_version.to_string()).expect("Expect ASCII version"),
)
Expand Down Expand Up @@ -186,3 +184,37 @@ fn to_64_bits(num: usize) -> [u8; 8] {
let sized_transaction_count: u64 = num.try_into().expect("Expect usize is at most 8 bytes");
sized_transaction_count.to_be_bytes()
}

// For starknet version >= 0.13.3, returns:
// [Poseidon (
// gas_price_wei, gas_price_fri, data_gas_price_wei, data_gas_price_fri,
// l2_gas_price_wei, l2_gas_price_fri
// )].
// Otherwise, returns:
// [gas_price_wei, gas_price_fri, data_gas_price_wei, data_gas_price_fri].
fn gas_prices_to_hash(
l1_gas_price: &GasPricePerToken,
l1_data_gas_price: &GasPricePerToken,
l2_gas_price: &GasPricePerToken,
starknet_version: &StarknetVersion,
) -> Vec<Felt> {
if *starknet_version >= BlockHashVersion::VO_13_3.into() {
vec![
HashChain::new()
.chain(&l1_gas_price.price_in_wei.0.into())
.chain(&l1_gas_price.price_in_fri.0.into())
.chain(&l1_data_gas_price.price_in_wei.0.into())
.chain(&l1_data_gas_price.price_in_fri.0.into())
.chain(&l2_gas_price.price_in_wei.0.into())
.chain(&l2_gas_price.price_in_fri.0.into())
.get_poseidon_hash(),
]
} else {
vec![
l1_gas_price.price_in_wei.0.into(),
l1_gas_price.price_in_fri.0.into(),
l1_data_gas_price.price_in_wei.0.into(),
l1_data_gas_price.price_in_fri.0.into(),
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn test_block_hash_regression(
felt!("0xe248d6ce583f8fa48d1d401d4beb9ceced3733e38d8eacb0d8d3669a7d901c")
}
BlockHashVersion::VO_13_3 => {
felt!("0x151e476191e08eb205e879d4fc5bce4b48d039a2dc18b94af52b37da749f770")
felt!("0x17c0dc0b67fa9bcf74197b758a6b48eb412cae14397a51a6393f1e0305fe585")
}
};

Expand Down
12 changes: 0 additions & 12 deletions crates/starknet_api/src/crypto/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,6 @@ 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 395fec7

Please sign in to comment.