Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Add Storage mock and test for get_block_id (#1155)
Browse files Browse the repository at this point in the history
Specifically, when retrieving block_id from a Papyrus snapshot
it can be a large hash, which we had a bug on back when we assumed it
was a u64.

commit-id:979a9b19

Co-authored-by: Gilad Chase <gilad@starkware.com>
  • Loading branch information
giladchase and Gilad Chase authored Nov 22, 2023
1 parent e3857bb commit 5037f35
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/native_blockifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod py_utils;
pub mod py_validator;
pub mod state_readers;
pub mod storage;
pub mod test_utils;
pub mod transaction_executor;

use errors::{add_py_exceptions, UndeclaredClassHashError};
Expand Down
27 changes: 26 additions & 1 deletion crates/native_blockifier/src/py_block_executor_test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use std::collections::HashMap;

use blockifier::state::state_api::State;
use blockifier::test_utils::{get_test_contract_class, TEST_CLASS_HASH};
use cached::Cached;
use pretty_assertions::assert_eq;
use starknet_api::class_hash;
use starknet_api::core::ClassHash;
use starknet_api::hash::StarkHash;
use starknet_api::hash::{StarkFelt, StarkHash};

use crate::py_block_executor::{PyBlockExecutor, PyGeneralConfig};
use crate::py_state_diff::PyBlockInfo;
use crate::py_utils::PyFelt;
use crate::test_utils::MockStorage;

#[test]
fn global_contract_cache_update() {
// Initialize executor and set a contract class on the state.
Expand Down Expand Up @@ -37,3 +43,22 @@ fn global_contract_cache_update() {
assert_eq!(block_executor.global_contract_cache.lock().unwrap().cache_size(), 1);
block_executor.teardown_block_execution();
}

#[test]
fn get_block_id() {
let max_class_hash = [
0x9, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
];
let max_class_hash_vec = Vec::from(max_class_hash);
let expected_max_class_hash_as_py_felt = PyFelt(StarkFelt::new(max_class_hash).unwrap());

let storage =
MockStorage { block_number_to_class_hash: HashMap::from([(1138, max_class_hash_vec)]) };
let block_executor = PyBlockExecutor::create_for_testing_with_storage(storage);

assert_eq!(
block_executor.get_block_id_at_target(1138).unwrap().unwrap(),
expected_max_class_hash_as_py_felt
);
}
58 changes: 58 additions & 0 deletions crates/native_blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::collections::HashMap;

use crate::errors::NativeBlockifierResult;
use crate::storage::Storage;

pub struct MockStorage {
pub block_number_to_class_hash: HashMap<u64, Vec<u8>>,
// .. Add more as needed.
}

impl Storage for MockStorage {
fn get_block_id(&self, block_number: u64) -> NativeBlockifierResult<Option<Vec<u8>>> {
Ok(self.block_number_to_class_hash.get(&block_number).cloned())
}

fn get_state_marker(&self) -> NativeBlockifierResult<u64> {
todo!()
}

fn get_header_marker(&self) -> NativeBlockifierResult<u64> {
todo!()
}

fn revert_block(&mut self, _block_number: u64) -> NativeBlockifierResult<()> {
todo!()
}

fn append_block(
&mut self,
_block_id: u64,
_previous_block_id: Option<crate::py_utils::PyFelt>,
_py_block_info: crate::py_state_diff::PyBlockInfo,
_py_state_diff: crate::py_state_diff::PyStateDiff,
_declared_class_hash_to_class: HashMap<
crate::py_utils::PyFelt,
(crate::py_utils::PyFelt, String),
>,
_deprecated_declared_class_hash_to_class: HashMap<crate::py_utils::PyFelt, String>,
) -> NativeBlockifierResult<()> {
todo!()
}

fn validate_aligned(&self, _source_block_number: u64) {
todo!()
}

fn reader(&self) -> &papyrus_storage::StorageReader {
todo!()
}

fn writer(&mut self) -> &mut papyrus_storage::StorageWriter {
todo!()
}

fn close(&mut self) {
todo!()
}
}

0 comments on commit 5037f35

Please sign in to comment.