Skip to content

Commit 6902537

Browse files
starknet_api: add fee_proposal to block hash calculator
1 parent 839f28c commit 6902537

7 files changed

Lines changed: 69 additions & 29 deletions

File tree

crates/apollo_batcher/src/batcher.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ impl Batcher {
831831
sequencer: block_header_without_hash.sequencer,
832832
timestamp: block_header_without_hash.timestamp,
833833
starknet_version: block_header_without_hash.starknet_version,
834+
fee_proposal: block_header_without_hash.fee_proposal,
834835
})
835836
}
836837
None => return Err(BatcherError::MissingHeaderCommitments { block_number }),

crates/apollo_storage/src/serialization/serializers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ auto_storage_serde! {
381381
pub sequencer: SequencerContractAddress,
382382
pub timestamp: BlockTimestamp,
383383
pub starknet_version: StarknetVersion,
384+
pub fee_proposal: GasPrice,
384385
}
385386
pub struct PaymasterData(pub Vec<Felt>);
386387
pub struct PoseidonHash(pub Felt);

crates/apollo_test_utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ auto_impl_get_test_instance! {
748748
pub sequencer: SequencerContractAddress,
749749
pub timestamp: BlockTimestamp,
750750
pub starknet_version: StarknetVersion,
751+
pub fee_proposal: GasPrice,
751752
}
752753
pub struct PaymasterData(pub Vec<Felt>);
753754
pub struct PoseidonHash(pub Felt);

crates/starknet_api/src/block_hash/block_hash_calculator.rs

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::block::{
1616
BlockInfo,
1717
BlockNumber,
1818
BlockTimestamp,
19+
GasPrice,
1920
GasPricePerToken,
2021
StarknetVersion,
2122
};
@@ -47,6 +48,11 @@ static STARKNET_BLOCK_HASH0: LazyLock<Felt> = LazyLock::new(|| {
4748
pub static STARKNET_BLOCK_HASH1: LazyLock<Felt> = LazyLock::new(|| {
4849
ascii_as_felt("STARKNET_BLOCK_HASH1").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH1'")
4950
});
51+
// TODO(SNIP-35): Consult protocol team on the correct block hash constant name and version
52+
// number for SNIP-35. Using "STARKNET_BLOCK_HASH2" and V0_14_4 as placeholders.
53+
pub static STARKNET_BLOCK_HASH2: LazyLock<Felt> = LazyLock::new(|| {
54+
ascii_as_felt("STARKNET_BLOCK_HASH2").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH2'")
55+
});
5056
pub static STARKNET_GAS_PRICES0: LazyLock<Felt> = LazyLock::new(|| {
5157
ascii_as_felt("STARKNET_GAS_PRICES0").expect("ascii_as_felt failed for 'STARKNET_GAS_PRICES0'")
5258
});
@@ -56,13 +62,21 @@ pub static STARKNET_GAS_PRICES0: LazyLock<Felt> = LazyLock::new(|| {
5662
pub enum BlockHashVersion {
5763
V0_13_2,
5864
V0_13_4,
65+
// TODO(SNIP-35): Consult protocol team on the correct version for SNIP-35 block hash.
66+
// V0_14_4 is a placeholder — the actual version depends on the release schedule.
67+
V0_14_4,
5968
}
6069

6170
impl From<BlockHashVersion> for StarknetVersion {
6271
fn from(value: BlockHashVersion) -> Self {
6372
match value {
6473
BlockHashVersion::V0_13_2 => StarknetVersion::V0_13_2,
6574
BlockHashVersion::V0_13_4 => StarknetVersion::V0_13_4,
75+
// TODO(SNIP-35): Map to the real StarknetVersion once the protocol team assigns one.
76+
// For now, V0_14_4 is only used internally and never converted to StarknetVersion.
77+
BlockHashVersion::V0_14_4 => {
78+
panic!("V0_14_4 BlockHashVersion cannot be converted to StarknetVersion yet")
79+
}
6680
}
6781
}
6882
}
@@ -77,6 +91,8 @@ impl TryFrom<StarknetVersion> for BlockHashVersion {
7791
// Starknet versions 0.13.2 and 0.13.3 both have the same block hash mechanism.
7892
Ok(Self::V0_13_2)
7993
} else {
94+
// TODO(SNIP-35): When the protocol team assigns a StarknetVersion for SNIP-35,
95+
// add a branch here: `else if value < V_SNIP35.into() { V0_13_4 } else { V0_14_4 }`.
8096
Ok(Self::V0_13_4)
8197
}
8298
}
@@ -90,6 +106,7 @@ impl From<BlockHashVersion> for BlockHashConstant {
90106
match block_hash_version {
91107
BlockHashVersion::V0_13_2 => *STARKNET_BLOCK_HASH0,
92108
BlockHashVersion::V0_13_4 => *STARKNET_BLOCK_HASH1,
109+
BlockHashVersion::V0_14_4 => *STARKNET_BLOCK_HASH2,
93110
}
94111
}
95112
}
@@ -218,6 +235,8 @@ pub struct PartialBlockHashComponents {
218235
pub sequencer: SequencerContractAddress,
219236
pub timestamp: BlockTimestamp,
220237
pub starknet_version: StarknetVersion,
238+
/// SNIP-35: proposer's recommended fee for future blocks.
239+
pub fee_proposal: GasPrice,
221240
}
222241

223242
impl PartialBlockHashComponents {
@@ -231,6 +250,7 @@ impl PartialBlockHashComponents {
231250
sequencer: SequencerContractAddress(block_info.sequencer_address),
232251
timestamp: block_info.block_timestamp,
233252
starknet_version: block_info.starknet_version,
253+
fee_proposal: GasPrice::default(),
234254
}
235255
}
236256
}
@@ -250,35 +270,39 @@ pub fn calculate_block_hash(
250270
let block_hash_version: BlockHashVersion =
251271
partial_block_hash_components.starknet_version.try_into()?;
252272
let block_commitments = &partial_block_hash_components.header_commitments;
253-
Ok(BlockHash(
254-
HashChain::new()
255-
.chain(&block_hash_version.clone().into())
256-
.chain(&partial_block_hash_components.block_number.0.into())
257-
.chain(&state_root.0)
258-
.chain(&partial_block_hash_components.sequencer.0)
259-
.chain(&partial_block_hash_components.timestamp.0.into())
260-
.chain(&block_commitments.concatenated_counts)
261-
.chain(&block_commitments.state_diff_commitment.0.0)
262-
.chain(&block_commitments.transaction_commitment.0)
263-
.chain(&block_commitments.event_commitment.0)
264-
.chain(&block_commitments.receipt_commitment.0)
265-
.chain_iter(
266-
gas_prices_to_hash(
267-
&partial_block_hash_components.l1_gas_price,
268-
&partial_block_hash_components.l1_data_gas_price,
269-
&partial_block_hash_components.l2_gas_price,
270-
&block_hash_version,
271-
)
272-
.iter(),
273-
)
274-
.chain(
275-
&Felt::try_from(&partial_block_hash_components.starknet_version)
276-
.expect("Expect ASCII version"),
273+
let mut hash_chain = HashChain::new()
274+
.chain(&block_hash_version.clone().into())
275+
.chain(&partial_block_hash_components.block_number.0.into())
276+
.chain(&state_root.0)
277+
.chain(&partial_block_hash_components.sequencer.0)
278+
.chain(&partial_block_hash_components.timestamp.0.into())
279+
.chain(&block_commitments.concatenated_counts)
280+
.chain(&block_commitments.state_diff_commitment.0.0)
281+
.chain(&block_commitments.transaction_commitment.0)
282+
.chain(&block_commitments.event_commitment.0)
283+
.chain(&block_commitments.receipt_commitment.0)
284+
.chain_iter(
285+
gas_prices_to_hash(
286+
&partial_block_hash_components.l1_gas_price,
287+
&partial_block_hash_components.l1_data_gas_price,
288+
&partial_block_hash_components.l2_gas_price,
289+
&block_hash_version,
277290
)
278-
.chain(&Felt::ZERO)
279-
.chain(&previous_block_hash.0)
280-
.get_poseidon_hash(),
281-
))
291+
.iter(),
292+
)
293+
.chain(
294+
&Felt::try_from(&partial_block_hash_components.starknet_version)
295+
.expect("Expect ASCII version"),
296+
)
297+
.chain(&Felt::ZERO)
298+
.chain(&previous_block_hash.0);
299+
300+
// SNIP-35: include fee_proposal in block hash for V0_14_4+.
301+
if block_hash_version >= BlockHashVersion::V0_14_4 {
302+
hash_chain = hash_chain.chain(&partial_block_hash_components.fee_proposal.0.into());
303+
}
304+
305+
Ok(BlockHash(hash_chain.get_poseidon_hash()))
282306
}
283307

284308
/// Calculates the commitments of the transactions data for the block hash.

crates/starknet_api/src/block_hash/block_hash_calculator_test.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ use rstest::rstest;
22
use starknet_types_core::felt::Felt;
33

44
use super::{concat_counts, extract_event_count_from_concatenated_counts};
5-
use crate::block::{BlockHash, BlockNumber, BlockTimestamp, GasPricePerToken, StarknetVersion};
5+
use crate::block::{
6+
BlockHash,
7+
BlockNumber,
8+
BlockTimestamp,
9+
GasPrice,
10+
GasPricePerToken,
11+
StarknetVersion,
12+
};
613
use crate::block_hash::block_hash_calculator::{
714
calculate_block_commitments,
815
calculate_block_hash,
@@ -43,6 +50,7 @@ macro_rules! test_hash_changes {
4350
$(
4451
$partial_block_hash_components_field: $partial_block_hash_components_value,
4552
)*
53+
..Default::default()
4654
};
4755
let state_root = $state_root;
4856
let previous_block_hash = $previous_block_hash;
@@ -101,6 +109,7 @@ async fn test_block_hash_regression(
101109
l2_gas_price: GasPricePerToken { price_in_fri: 11_u8.into(), price_in_wei: 12_u8.into() },
102110
starknet_version: block_hash_version.clone().into(),
103111
header_commitments: block_commitments,
112+
fee_proposal: GasPrice::default(),
104113
};
105114

106115
let expected_hash = match block_hash_version {
@@ -110,6 +119,8 @@ async fn test_block_hash_regression(
110119
BlockHashVersion::V0_13_4 => {
111120
felt!("0x3d6174623c812f5dc03fa3faa07c42c06fd90ad425629ee5f39e149df65c3ca")
112121
}
122+
// V0_14_4 is not yet reachable (no StarknetVersion maps to it).
123+
BlockHashVersion::V0_14_4 => unreachable!(),
113124
};
114125

115126
assert_eq!(

crates/starknet_committer_and_os_cli/src/committer_cli/block_hash.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl BlockHashInput {
3737
l2_gas_price: self.header.l2_gas_price,
3838
sequencer: self.header.sequencer,
3939
timestamp: self.header.timestamp,
40+
fee_proposal: self.header.fee_proposal,
4041
},
4142
self.header.state_root,
4243
self.header.parent_hash,

crates/starknet_os/src/hints/hint_implementation/block_hash/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ fn test_block_hash_cairo() {
136136
},
137137
l2_gas_price: GasPricePerToken { price_in_wei: GasPrice(14), price_in_fri: GasPrice(15) },
138138
starknet_version: StarknetVersion::LATEST,
139+
fee_proposal: GasPrice::default(),
139140
};
140141
let state_root = Felt::from(16);
141142
let previous_block_hash = Felt::from(17);

0 commit comments

Comments
 (0)