Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions crates/starknet_api/src/block_hash/block_hash_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ impl PartialBlockHash {

/// Hash of [`PartialBlockHashComponents`].
/// Uses the same formula as [`calculate_block_hash`] with the fixed constants above for the
/// state root and parent hash.
/// state root and parent hash, then SNIP-35 chains `fee_proposal_fri` so that the proposal
/// commitment binds to the proposer's fee_proposal without changing the block hash.
pub fn from_partial_block_hash_components(
partial_block_hash_components: &PartialBlockHashComponents,
) -> StarknetApiResult<Self> {
Expand All @@ -203,7 +204,11 @@ impl PartialBlockHash {
Self::GLOBAL_ROOT_FOR_PARTIAL_BLOCK_HASH,
Self::PARENT_HASH_FOR_PARTIAL_BLOCK_HASH,
)?;
Ok(Self(block_hash.0))
let proposal_commitment = HashChain::new()
.chain(&block_hash.0)
.chain(&partial_block_hash_components.fee_proposal_fri.0.into())
.get_poseidon_hash();
Ok(Self(proposal_commitment))
}
}

Expand Down
31 changes: 30 additions & 1 deletion crates/starknet_api/src/block_hash/block_hash_calculator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::block_hash::block_hash_calculator::{
calculate_block_hash,
BlockHashVersion,
BlockHeaderCommitments,
PartialBlockHash,
PartialBlockHashComponents,
TransactionHashingData,
};
Expand Down Expand Up @@ -252,10 +253,38 @@ fn change_field_of_hash_input() {
},
l2_gas_price: GasPricePerToken { price_in_fri: 1_u8.into(), price_in_wei: 1_u8.into() },
sequencer: SequencerContractAddress(ContractAddress::from(1_u128)),
timestamp: BlockTimestamp(1)
timestamp: BlockTimestamp(1),
},
state_root: GlobalRoot(Felt::ONE),
previous_block_hash: BlockHash(Felt::ONE)
)
// TODO(Aviv, 10/06/2024): add tests that changes the first hash input, and the const zero.
}

/// SNIP-35: `fee_proposal_fri` must change the proposal commitment hash (`PartialBlockHash`) but
/// must not change the block hash (`BlockHash`).
#[test]
fn fee_proposal_fri_affects_partial_block_hash_but_not_block_hash() {
let base = PartialBlockHashComponents {
starknet_version: BlockHashVersion::V0_13_4.into(),
fee_proposal_fri: GasPrice(0),
..Default::default()
};
let modified = PartialBlockHashComponents { fee_proposal_fri: GasPrice(1), ..base.clone() };

let base_block_hash =
calculate_block_hash(&base, GlobalRoot::default(), BlockHash::default()).unwrap();
let modified_block_hash =
calculate_block_hash(&modified, GlobalRoot::default(), BlockHash::default()).unwrap();
assert_eq!(
base_block_hash, modified_block_hash,
"BlockHash must not depend on fee_proposal_fri"
);

let base_partial = PartialBlockHash::from_partial_block_hash_components(&base).unwrap();
let modified_partial = PartialBlockHash::from_partial_block_hash_components(&modified).unwrap();
assert_ne!(
base_partial, modified_partial,
"PartialBlockHash (proposal commitment) must depend on fee_proposal_fri"
);
}
Loading