Skip to content

Commit e8a6182

Browse files
starknet_api: add BlockHashVersion::V0_14_3 variant
1 parent 7dbd196 commit e8a6182

5 files changed

Lines changed: 31 additions & 15 deletions

File tree

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

Lines changed: 1 addition & 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,
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": "0xc2ef94f4f0ad828490194c4a652dbbb71dbf1c6bccd122135383b3c9c5fbd",
3+
"virtual_os": "0x46328da2901e5ccd1585a82de99fd97d0e757a876d6b66f29d8ca94bb5ba27b",
44
"aggregator": "0x43666b81f964bcdedf0098d2791b061d61f3098ff1429a754d0b97eeeae9489",
55
"aggregator_with_prefix": "0x68072c8f5ff5ae133060e12cfad3a38362dbb6d667abd4f7e1fac6f37febe46"
66
}

crates/starknet_api/src/block_hash/block_hash_calculator.rs

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

44-
static STARKNET_BLOCK_HASH0: LazyLock<Felt> = LazyLock::new(|| {
44+
// The prefix constant for the block hash calculation.
45+
type BlockHashConstant = Felt;
46+
47+
static STARKNET_BLOCK_HASH0: LazyLock<BlockHashConstant> = LazyLock::new(|| {
4548
ascii_as_felt("STARKNET_BLOCK_HASH0").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH0'")
4649
});
47-
pub static STARKNET_BLOCK_HASH1: LazyLock<Felt> = LazyLock::new(|| {
50+
pub static STARKNET_BLOCK_HASH1: LazyLock<BlockHashConstant> = LazyLock::new(|| {
4851
ascii_as_felt("STARKNET_BLOCK_HASH1").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH1'")
4952
});
53+
pub static STARKNET_BLOCK_HASH2: LazyLock<BlockHashConstant> = 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,15 @@ pub static STARKNET_GAS_PRICES0: LazyLock<Felt> = LazyLock::new(|| {
5662
pub enum BlockHashVersion {
5763
V0_13_2,
5864
V0_13_4,
65+
V0_14_3,
5966
}
6067

6168
impl From<BlockHashVersion> for StarknetVersion {
6269
fn from(value: BlockHashVersion) -> Self {
6370
match value {
6471
BlockHashVersion::V0_13_2 => StarknetVersion::V0_13_2,
6572
BlockHashVersion::V0_13_4 => StarknetVersion::V0_13_4,
73+
BlockHashVersion::V0_14_3 => StarknetVersion::V0_14_3,
6674
}
6775
}
6876
}
@@ -76,20 +84,20 @@ impl TryFrom<StarknetVersion> for BlockHashVersion {
7684
} else if value < Self::V0_13_4.into() {
7785
// Starknet versions 0.13.2 and 0.13.3 both have the same block hash mechanism.
7886
Ok(Self::V0_13_2)
79-
} else {
87+
} else if value < Self::V0_14_3.into() {
8088
Ok(Self::V0_13_4)
89+
} else {
90+
Ok(Self::V0_14_3)
8191
}
8292
}
8393
}
8494

85-
// The prefix constant for the block hash calculation.
86-
type BlockHashConstant = Felt;
87-
8895
impl From<BlockHashVersion> for BlockHashConstant {
8996
fn from(block_hash_version: BlockHashVersion) -> Self {
9097
match block_hash_version {
9198
BlockHashVersion::V0_13_2 => *STARKNET_BLOCK_HASH0,
9299
BlockHashVersion::V0_13_4 => *STARKNET_BLOCK_HASH1,
100+
BlockHashVersion::V0_14_3 => *STARKNET_BLOCK_HASH2,
93101
}
94102
}
95103
}

crates/starknet_api/src/block_hash/block_hash_calculator_test.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ use crate::{felt, tx_hash};
3333
/// and asserts that the block hash changes as a result.
3434
macro_rules! test_hash_changes {
3535
(
36+
block_hash_version: $block_hash_version:expr,
3637
PartialBlockHashComponents { $($partial_block_hash_components_field:ident: $partial_block_hash_components_value:expr),* $(,)? },
3738
state_root: $state_root:expr,
3839
previous_block_hash: $previous_block_hash:expr
3940
) => {
4041
{
4142
let partial_block_hash_components = PartialBlockHashComponents {
42-
starknet_version: BlockHashVersion::V0_13_4.into(),
43+
starknet_version: $block_hash_version.into(),
4344
$(
4445
$partial_block_hash_components_field: $partial_block_hash_components_value,
4546
)*
@@ -68,7 +69,7 @@ macro_rules! test_hash_changes {
6869
#[rstest]
6970
#[tokio::test]
7071
async fn test_block_hash_regression(
71-
#[values(BlockHashVersion::V0_13_2, BlockHashVersion::V0_13_4)]
72+
#[values(BlockHashVersion::V0_13_2, BlockHashVersion::V0_13_4, BlockHashVersion::V0_14_3)]
7273
block_hash_version: BlockHashVersion,
7374
) {
7475
let state_root = GlobalRoot(Felt::from(2_u8));
@@ -110,6 +111,9 @@ async fn test_block_hash_regression(
110111
BlockHashVersion::V0_13_4 => {
111112
felt!("0x3d6174623c812f5dc03fa3faa07c42c06fd90ad425629ee5f39e149df65c3ca")
112113
}
114+
BlockHashVersion::V0_14_3 => {
115+
felt!("0x477e98ed084a0274e4510ab27327f08235d8e4fdb7506e46e92e9ab0c5ea459")
116+
}
113117
};
114118

115119
assert_eq!(
@@ -222,11 +226,15 @@ fn extract_event_count_from_concatenated_counts_test(
222226
}
223227

224228
/// Test that if one of the input to block hash changes, the hash changes.
225-
#[test]
226-
fn change_field_of_hash_input() {
229+
#[rstest]
230+
fn change_field_of_hash_input(
231+
#[values(BlockHashVersion::V0_13_4, BlockHashVersion::V0_14_3)]
232+
block_hash_version: BlockHashVersion,
233+
) {
227234
// Set non-default values for the header and the commitments fields. Test that changing any of
228235
// these fields changes the hash.
229236
test_hash_changes!(
237+
block_hash_version: block_hash_version,
230238
PartialBlockHashComponents {
231239
header_commitments: BlockHeaderCommitments {
232240
transaction_commitment: TransactionCommitment(Felt::ONE),

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

Lines changed: 2 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,
@@ -161,7 +161,7 @@ fn test_block_hash_version() {
161161
// NOTE: if these checks fail, it means the block hash version in the OS program is not the
162162
// latest, and a backward-compatibility flow must be added for the transition.
163163
assert_eq!(
164-
*STARKNET_BLOCK_HASH1, latest_block_hash_version,
164+
*STARKNET_BLOCK_HASH2, latest_block_hash_version,
165165
"Latest block hash version constant mismatch"
166166
);
167167
assert_eq!(

0 commit comments

Comments
 (0)