Skip to content

Commit 4d045e8

Browse files
starknet_api: add fee_proposal_fri to block hash calculator
1 parent add4493 commit 4d045e8

9 files changed

Lines changed: 46 additions & 20 deletions

File tree

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/block_hash.cairo

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from starkware.cairo.common.hash_state_poseidon import hash_finalize, hash_init,
33
from starkware.starknet.common.new_syscalls import BlockInfo
44

55
// The latest block hash version.
6-
const BLOCK_HASH_VERSION = 'STARKNET_BLOCK_HASH1';
6+
const BLOCK_HASH_VERSION = 'STARKNET_BLOCK_HASH2';
77

88
struct BlockHeaderCommitments {
99
transaction_commitment: felt,
@@ -20,6 +20,7 @@ func calculate_block_hash{poseidon_ptr: PoseidonBuiltin*}(
2020
block_info: BlockInfo*,
2121
header_commitments: BlockHeaderCommitments*,
2222
gas_prices_hash: felt,
23+
fee_proposal_fri: felt,
2324
state_root: felt,
2425
previous_block_hash: felt,
2526
starknet_version: felt,
@@ -40,6 +41,8 @@ func calculate_block_hash{poseidon_ptr: PoseidonBuiltin*}(
4041
hash_update_single(header_commitments.event_commitment);
4142
hash_update_single(header_commitments.receipt_commitment);
4243
hash_update_single(gas_prices_hash);
44+
// SNIP-35: include the proposer's recommended fee.
45+
hash_update_single(fee_proposal_fri);
4346
hash_update_single(starknet_version);
4447
hash_update_single(0);
4548
hash_update_single(previous_block_hash);
@@ -67,10 +70,12 @@ func get_block_hashes{poseidon_ptr: PoseidonBuiltin*}(block_info: BlockInfo*, st
6770

6871
%{ GetBlockHashes %}
6972

73+
// TODO(SNIP-35): plumb the proposer's fee_proposal_fri through the OS input.
7074
let block_hash = calculate_block_hash(
7175
block_info=block_info,
7276
header_commitments=header_commitments,
7377
gas_prices_hash=gas_prices_hash,
78+
fee_proposal_fri=0,
7479
state_root=state_root,
7580
previous_block_hash=previous_block_hash,
7681
starknet_version=starknet_version,

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/constants.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const STORED_BLOCK_HASH_BUFFER = 10;
6666

6767
// Allowed virtual OS program hashes for client-side proving.
6868
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_0 = (
69-
0x03e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473
69+
0x0576598b6f9b53f0c0b5703eec1c9da47c4717aca88acfe88133c626a33ce3c2
7070
);
7171
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 1;
7272

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"os": "0xb0134eed363da4094afca019a74939b4c17f238a4f7411798813f55905cd7",
3-
"virtual_os": "0x3e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473",
2+
"os": "0x1c8fb6fff3a785dafb6567f1b3b22a2512ac7095bcb00e531c02ced345febe4",
3+
"virtual_os": "0x576598b6f9b53f0c0b5703eec1c9da47c4717aca88acfe88133c626a33ce3c2",
44
"aggregator": "0x43666b81f964bcdedf0098d2791b061d61f3098ff1429a754d0b97eeeae9489",
55
"aggregator_with_prefix": "0x68072c8f5ff5ae133060e12cfad3a38362dbb6d667abd4f7e1fac6f37febe46"
66
}

crates/apollo_starknet_os_program/src/virtual_os_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ fn test_virtual_os_swapped_files() {
1919
#[test]
2020
fn test_program_bytecode_lengths() {
2121
expect![[r#"
22-
15610
22+
15615
2323
"#]]
2424
.assert_debug_eq(&OS_PROGRAM.data_len());
2525
expect![[r#"
26-
10214
26+
10219
2727
"#]]
2828
.assert_debug_eq(&VIRTUAL_OS_PROGRAM.data_len());
2929
}

crates/blockifier/resources/blockifier_versioned_constants_0_14_3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"segment_arena_cells": false,
6868
"os_constants": {
6969
"allowed_virtual_os_program_hashes": [
70-
"0x3e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473"
70+
"0x576598b6f9b53f0c0b5703eec1c9da47c4717aca88acfe88133c626a33ce3c2"
7171
],
7272
"constructor_entry_point_selector": "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
7373
"default_entry_point_selector": "0x0",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
~ /os_constants/allowed_virtual_os_program_hashes/0: "0x576598b6f9b53f0c0b5703eec1c9da47c4717aca88acfe88133c626a33ce3c2"

crates/starknet_api/src/block_hash/block_hash_calculator.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ use crate::{StarknetApiError, StarknetApiResult};
4242
#[path = "block_hash_calculator_test.rs"]
4343
mod block_hash_calculator_test;
4444

45-
static STARKNET_BLOCK_HASH0: LazyLock<Felt> = LazyLock::new(|| {
45+
// The prefix constant for the block hash calculation.
46+
type BlockHashConstant = Felt;
47+
48+
static STARKNET_BLOCK_HASH0: LazyLock<BlockHashConstant> = LazyLock::new(|| {
4649
ascii_as_felt("STARKNET_BLOCK_HASH0").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH0'")
4750
});
48-
pub static STARKNET_BLOCK_HASH1: LazyLock<Felt> = LazyLock::new(|| {
51+
pub static STARKNET_BLOCK_HASH1: LazyLock<BlockHashConstant> = LazyLock::new(|| {
4952
ascii_as_felt("STARKNET_BLOCK_HASH1").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH1'")
5053
});
54+
pub static STARKNET_BLOCK_HASH2: LazyLock<BlockHashConstant> = LazyLock::new(|| {
55+
ascii_as_felt("STARKNET_BLOCK_HASH2").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH2'")
56+
});
5157
pub static STARKNET_GAS_PRICES0: LazyLock<Felt> = LazyLock::new(|| {
5258
ascii_as_felt("STARKNET_GAS_PRICES0").expect("ascii_as_felt failed for 'STARKNET_GAS_PRICES0'")
5359
});
@@ -57,13 +63,15 @@ pub static STARKNET_GAS_PRICES0: LazyLock<Felt> = LazyLock::new(|| {
5763
pub enum BlockHashVersion {
5864
V0_13_2,
5965
V0_13_4,
66+
V0_14_3,
6067
}
6168

6269
impl From<BlockHashVersion> for StarknetVersion {
6370
fn from(value: BlockHashVersion) -> Self {
6471
match value {
6572
BlockHashVersion::V0_13_2 => StarknetVersion::V0_13_2,
6673
BlockHashVersion::V0_13_4 => StarknetVersion::V0_13_4,
74+
BlockHashVersion::V0_14_3 => StarknetVersion::V0_14_3,
6775
}
6876
}
6977
}
@@ -77,20 +85,20 @@ impl TryFrom<StarknetVersion> for BlockHashVersion {
7785
} else if value < Self::V0_13_4.into() {
7886
// Starknet versions 0.13.2 and 0.13.3 both have the same block hash mechanism.
7987
Ok(Self::V0_13_2)
80-
} else {
88+
} else if value < Self::V0_14_3.into() {
8189
Ok(Self::V0_13_4)
90+
} else {
91+
Ok(Self::V0_14_3)
8292
}
8393
}
8494
}
8595

86-
// The prefix constant for the block hash calculation.
87-
type BlockHashConstant = Felt;
88-
8996
impl From<BlockHashVersion> for BlockHashConstant {
9097
fn from(block_hash_version: BlockHashVersion) -> Self {
9198
match block_hash_version {
9299
BlockHashVersion::V0_13_2 => *STARKNET_BLOCK_HASH0,
93100
BlockHashVersion::V0_13_4 => *STARKNET_BLOCK_HASH1,
101+
BlockHashVersion::V0_14_3 => *STARKNET_BLOCK_HASH2,
94102
}
95103
}
96104
}
@@ -275,6 +283,14 @@ pub fn calculate_block_hash(
275283
)
276284
.iter(),
277285
)
286+
// SNIP-35: include fee_proposal_fri in the hash chain only for V0_14_3 and later.
287+
.chain_if_fn(|| {
288+
if block_hash_version >= BlockHashVersion::V0_14_3 {
289+
Some(partial_block_hash_components.fee_proposal_fri.0.into())
290+
} else {
291+
None
292+
}
293+
})
278294
.chain(
279295
&Felt::try_from(&partial_block_hash_components.starknet_version)
280296
.expect("Expect ASCII version"),

crates/starknet_api/src/block_hash/block_hash_calculator_test.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ macro_rules! test_hash_changes {
4646
) => {
4747
{
4848
let partial_block_hash_components = PartialBlockHashComponents {
49-
starknet_version: BlockHashVersion::V0_13_4.into(),
49+
starknet_version: BlockHashVersion::V0_14_3.into(),
5050
$(
5151
$partial_block_hash_components_field: $partial_block_hash_components_value,
5252
)*
53-
..Default::default()
5453
};
5554
let state_root = $state_root;
5655
let previous_block_hash = $previous_block_hash;
@@ -76,7 +75,7 @@ macro_rules! test_hash_changes {
7675
#[rstest]
7776
#[tokio::test]
7877
async fn test_block_hash_regression(
79-
#[values(BlockHashVersion::V0_13_2, BlockHashVersion::V0_13_4)]
78+
#[values(BlockHashVersion::V0_13_2, BlockHashVersion::V0_13_4, BlockHashVersion::V0_14_3)]
8079
block_hash_version: BlockHashVersion,
8180
) {
8281
let state_root = GlobalRoot(Felt::from(2_u8));
@@ -119,6 +118,9 @@ async fn test_block_hash_regression(
119118
BlockHashVersion::V0_13_4 => {
120119
felt!("0x3d6174623c812f5dc03fa3faa07c42c06fd90ad425629ee5f39e149df65c3ca")
121120
}
121+
BlockHashVersion::V0_14_3 => {
122+
felt!("0x79a7eea33732ee03654abf06b7cc3e6541dcfdf8e956f32bc3e4140d90fe9ce")
123+
}
122124
};
123125

124126
assert_eq!(
@@ -231,6 +233,7 @@ fn extract_event_count_from_concatenated_counts_test(
231233
}
232234

233235
/// Test that if one of the input to block hash changes, the hash changes.
236+
// TODO(AndrewL): add fee_proposal to the test.
234237
#[test]
235238
fn change_field_of_hash_input() {
236239
// Set non-default values for the header and the commitments fields. Test that changing any of
@@ -252,7 +255,8 @@ fn change_field_of_hash_input() {
252255
},
253256
l2_gas_price: GasPricePerToken { price_in_fri: 1_u8.into(), price_in_wei: 1_u8.into() },
254257
sequencer: SequencerContractAddress(ContractAddress::from(1_u128)),
255-
timestamp: BlockTimestamp(1)
258+
timestamp: BlockTimestamp(1),
259+
fee_proposal_fri: GasPrice(1)
256260
},
257261
state_root: GlobalRoot(Felt::ONE),
258262
previous_block_hash: BlockHash(Felt::ONE)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use starknet_api::block_hash::block_hash_calculator::{
1919
BlockHashVersion,
2020
BlockHeaderCommitments,
2121
PartialBlockHashComponents,
22-
STARKNET_BLOCK_HASH1,
22+
STARKNET_BLOCK_HASH2,
2323
};
2424
use starknet_api::core::{
2525
EventCommitment,
@@ -90,6 +90,7 @@ fn cairo_calculate_block_hash(
9090
block_info_arg,
9191
header_commitments_arg,
9292
EndpointArg::from(gas_prices_hash),
93+
EndpointArg::from(Felt::from(components.fee_proposal_fri.0)),
9394
EndpointArg::from(state_root),
9495
EndpointArg::from(previous_block_hash),
9596
EndpointArg::from(Felt::try_from(&components.starknet_version).unwrap()),
@@ -162,7 +163,7 @@ fn test_block_hash_version() {
162163
// NOTE: if these checks fail, it means the block hash version in the OS program is not the
163164
// latest, and a backward-compatibility flow must be added for the transition.
164165
assert_eq!(
165-
*STARKNET_BLOCK_HASH1, latest_block_hash_version,
166+
*STARKNET_BLOCK_HASH2, latest_block_hash_version,
166167
"Latest block hash version constant mismatch"
167168
);
168169
assert_eq!(

0 commit comments

Comments
 (0)