Skip to content

Commit 59d2ec9

Browse files
starknet_api: include fee_proposal_fri in proposal commitment hash
1 parent d961488 commit 59d2ec9

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

crates/starknet_api/src/block_hash/block_hash_calculator.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ impl PartialBlockHash {
194194

195195
/// Hash of [`PartialBlockHashComponents`].
196196
/// Uses the same formula as [`calculate_block_hash`] with the fixed constants above for the
197-
/// state root and parent hash.
197+
/// state root and parent hash, then SNIP-35 chains `fee_proposal_fri` so that the proposal
198+
/// commitment binds to the proposer's fee_proposal without changing the block hash.
198199
pub fn from_partial_block_hash_components(
199200
partial_block_hash_components: &PartialBlockHashComponents,
200201
) -> StarknetApiResult<Self> {
@@ -203,7 +204,11 @@ impl PartialBlockHash {
203204
Self::GLOBAL_ROOT_FOR_PARTIAL_BLOCK_HASH,
204205
Self::PARENT_HASH_FOR_PARTIAL_BLOCK_HASH,
205206
)?;
206-
Ok(Self(block_hash.0))
207+
let proposal_commitment = HashChain::new()
208+
.chain(&block_hash.0)
209+
.chain(&partial_block_hash_components.fee_proposal_fri.0.into())
210+
.get_poseidon_hash();
211+
Ok(Self(proposal_commitment))
207212
}
208213
}
209214

crates/starknet_api/src/block_hash/block_hash_calculator_test.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::block_hash::block_hash_calculator::{
1515
calculate_block_hash,
1616
BlockHashVersion,
1717
BlockHeaderCommitments,
18+
PartialBlockHash,
1819
PartialBlockHashComponents,
1920
TransactionHashingData,
2021
};
@@ -252,10 +253,38 @@ fn change_field_of_hash_input() {
252253
},
253254
l2_gas_price: GasPricePerToken { price_in_fri: 1_u8.into(), price_in_wei: 1_u8.into() },
254255
sequencer: SequencerContractAddress(ContractAddress::from(1_u128)),
255-
timestamp: BlockTimestamp(1)
256+
timestamp: BlockTimestamp(1),
256257
},
257258
state_root: GlobalRoot(Felt::ONE),
258259
previous_block_hash: BlockHash(Felt::ONE)
259260
)
260261
// TODO(Aviv, 10/06/2024): add tests that changes the first hash input, and the const zero.
261262
}
263+
264+
/// SNIP-35: `fee_proposal_fri` must change the proposal commitment hash (`PartialBlockHash`) but
265+
/// must not change the block hash (`BlockHash`).
266+
#[test]
267+
fn fee_proposal_fri_affects_partial_block_hash_but_not_block_hash() {
268+
let base = PartialBlockHashComponents {
269+
starknet_version: BlockHashVersion::V0_13_4.into(),
270+
fee_proposal_fri: GasPrice(0),
271+
..Default::default()
272+
};
273+
let modified = PartialBlockHashComponents { fee_proposal_fri: GasPrice(1), ..base.clone() };
274+
275+
let base_block_hash =
276+
calculate_block_hash(&base, GlobalRoot::default(), BlockHash::default()).unwrap();
277+
let modified_block_hash =
278+
calculate_block_hash(&modified, GlobalRoot::default(), BlockHash::default()).unwrap();
279+
assert_eq!(
280+
base_block_hash, modified_block_hash,
281+
"BlockHash must not depend on fee_proposal_fri"
282+
);
283+
284+
let base_partial = PartialBlockHash::from_partial_block_hash_components(&base).unwrap();
285+
let modified_partial = PartialBlockHash::from_partial_block_hash_components(&modified).unwrap();
286+
assert_ne!(
287+
base_partial, modified_partial,
288+
"PartialBlockHash (proposal commitment) must depend on fee_proposal_fri"
289+
);
290+
}

0 commit comments

Comments
 (0)