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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from starkware.cairo.common.hash_state_poseidon import hash_finalize, hash_init,
from starkware.starknet.common.new_syscalls import BlockInfo

// The latest block hash version.
const BLOCK_HASH_VERSION = 'STARKNET_BLOCK_HASH1';
const BLOCK_HASH_VERSION = 'STARKNET_BLOCK_HASH2';

struct BlockHeaderCommitments {
transaction_commitment: felt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ const STORED_BLOCK_HASH_BUFFER = 10;
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_0 = (
0x03e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473
);
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 1;
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_1 = (
0x046328da2901e5ccd1585a82de99fd97d0e757a876d6b66f29d8ca94bb5ba27b
);
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 2;

// Gas constants.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ func check_is_reverted(is_reverted: felt) {

// Returns TRUE if the given virtual OS program hash is allowed, FALSE otherwise.
func is_program_hash_allowed(program_hash: felt) -> felt {
static_assert ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN == 1;
static_assert ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN == 2;
if (program_hash == ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_0) {
return TRUE;
}
if (program_hash == ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_1) {
return TRUE;
}
return FALSE;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/apollo_starknet_os_program/src/program_hash.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"os": "0xb0134eed363da4094afca019a74939b4c17f238a4f7411798813f55905cd7",
"virtual_os": "0x3e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473",
"os": "0xc2ef94f4f0ad828490194c4a652dbbb71dbf1c6bccd122135383b3c9c5fbd",
"virtual_os": "0x46328da2901e5ccd1585a82de99fd97d0e757a876d6b66f29d8ca94bb5ba27b",
"aggregator": "0x43666b81f964bcdedf0098d2791b061d61f3098ff1429a754d0b97eeeae9489",
"aggregator_with_prefix": "0x68072c8f5ff5ae133060e12cfad3a38362dbb6d667abd4f7e1fac6f37febe46"
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"segment_arena_cells": false,
"os_constants": {
"allowed_virtual_os_program_hashes": [
"0x3e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473"
"0x3e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473",
"0x46328da2901e5ccd1585a82de99fd97d0e757a876d6b66f29d8ca94bb5ba27b"
],
"constructor_entry_point_selector": "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
"default_entry_point_selector": "0x0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@

+ /os_constants/allowed_virtual_os_program_hashes/1: "0x46328da2901e5ccd1585a82de99fd97d0e757a876d6b66f29d8ca94bb5ba27b"
20 changes: 14 additions & 6 deletions crates/starknet_api/src/block_hash/block_hash_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ use crate::{StarknetApiError, StarknetApiResult};
#[path = "block_hash_calculator_test.rs"]
mod block_hash_calculator_test;

static STARKNET_BLOCK_HASH0: LazyLock<Felt> = LazyLock::new(|| {
// The prefix constant for the block hash calculation.
type BlockHashConstant = Felt;

static STARKNET_BLOCK_HASH0: LazyLock<BlockHashConstant> = LazyLock::new(|| {
ascii_as_felt("STARKNET_BLOCK_HASH0").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH0'")
});
pub static STARKNET_BLOCK_HASH1: LazyLock<Felt> = LazyLock::new(|| {
pub static STARKNET_BLOCK_HASH1: LazyLock<BlockHashConstant> = LazyLock::new(|| {
ascii_as_felt("STARKNET_BLOCK_HASH1").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH1'")
});
pub static STARKNET_BLOCK_HASH2: LazyLock<BlockHashConstant> = LazyLock::new(|| {
ascii_as_felt("STARKNET_BLOCK_HASH2").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH2'")
});
pub static STARKNET_GAS_PRICES0: LazyLock<Felt> = LazyLock::new(|| {
ascii_as_felt("STARKNET_GAS_PRICES0").expect("ascii_as_felt failed for 'STARKNET_GAS_PRICES0'")
});
Expand All @@ -56,13 +62,15 @@ pub static STARKNET_GAS_PRICES0: LazyLock<Felt> = LazyLock::new(|| {
pub enum BlockHashVersion {
V0_13_2,
V0_13_4,
V0_14_3,
}

impl From<BlockHashVersion> for StarknetVersion {
fn from(value: BlockHashVersion) -> Self {
match value {
BlockHashVersion::V0_13_2 => StarknetVersion::V0_13_2,
BlockHashVersion::V0_13_4 => StarknetVersion::V0_13_4,
BlockHashVersion::V0_14_3 => StarknetVersion::V0_14_3,
}
}
}
Expand All @@ -76,20 +84,20 @@ impl TryFrom<StarknetVersion> for BlockHashVersion {
} else if value < Self::V0_13_4.into() {
// Starknet versions 0.13.2 and 0.13.3 both have the same block hash mechanism.
Ok(Self::V0_13_2)
} else {
} else if value < Self::V0_14_3.into() {
Ok(Self::V0_13_4)
} else {
Ok(Self::V0_14_3)
}
}
}

// The prefix constant for the block hash calculation.
type BlockHashConstant = Felt;

impl From<BlockHashVersion> for BlockHashConstant {
fn from(block_hash_version: BlockHashVersion) -> Self {
match block_hash_version {
BlockHashVersion::V0_13_2 => *STARKNET_BLOCK_HASH0,
BlockHashVersion::V0_13_4 => *STARKNET_BLOCK_HASH1,
BlockHashVersion::V0_14_3 => *STARKNET_BLOCK_HASH2,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ macro_rules! test_hash_changes {
) => {
{
let partial_block_hash_components = PartialBlockHashComponents {
starknet_version: BlockHashVersion::V0_13_4.into(),
starknet_version: BlockHashVersion::V0_14_3.into(),
$(
$partial_block_hash_components_field: $partial_block_hash_components_value,
)*
Expand Down Expand Up @@ -68,7 +68,7 @@ macro_rules! test_hash_changes {
#[rstest]
#[tokio::test]
async fn test_block_hash_regression(
#[values(BlockHashVersion::V0_13_2, BlockHashVersion::V0_13_4)]
#[values(BlockHashVersion::V0_13_2, BlockHashVersion::V0_13_4, BlockHashVersion::V0_14_3)]
block_hash_version: BlockHashVersion,
) {
let state_root = GlobalRoot(Felt::from(2_u8));
Expand Down Expand Up @@ -110,6 +110,9 @@ async fn test_block_hash_regression(
BlockHashVersion::V0_13_4 => {
felt!("0x3d6174623c812f5dc03fa3faa07c42c06fd90ad425629ee5f39e149df65c3ca")
}
BlockHashVersion::V0_14_3 => {
felt!("0x477e98ed084a0274e4510ab27327f08235d8e4fdb7506e46e92e9ab0c5ea459")
}
};

assert_eq!(
Expand Down Expand Up @@ -222,6 +225,7 @@ fn extract_event_count_from_concatenated_counts_test(
}

/// Test that if one of the input to block hash changes, the hash changes.
// TODO(AndrewL): add fee_proposal to the test.
#[test]
fn change_field_of_hash_input() {
// Set non-default values for the header and the commitments fields. Test that changing any of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use starknet_api::block_hash::block_hash_calculator::{
BlockHashVersion,
BlockHeaderCommitments,
PartialBlockHashComponents,
STARKNET_BLOCK_HASH1,
STARKNET_BLOCK_HASH2,
};
use starknet_api::core::{
EventCommitment,
Expand Down Expand Up @@ -161,7 +161,7 @@ fn test_block_hash_version() {
// NOTE: if these checks fail, it means the block hash version in the OS program is not the
// latest, and a backward-compatibility flow must be added for the transition.
assert_eq!(
*STARKNET_BLOCK_HASH1, latest_block_hash_version,
*STARKNET_BLOCK_HASH2, latest_block_hash_version,
"Latest block hash version constant mismatch"
);
assert_eq!(
Expand Down
Loading