Skip to content

Commit

Permalink
move blockifier from workspace to maintain two versions
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 committed Sep 20, 2023
1 parent 33f68b2 commit 02d761f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 40 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ reqwest = { version = "0.11.20", features = ["gzip"] }
rstest = "0.18.1"
thiserror = "1.0.47"
tokio = { version = "1.21.2", features = ["macros"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
walkdir = "2.3.3"
zip = "0.6.6"

# Serde
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0"

[patch."https://github.com/starkware-libs/blockifier"]
blockifier = { git = "https://github.com/dojoengine/blockifier", rev = "c794d1b" }

[patch."https://github.com/ethereum/c-kzg-4844"]
c-kzg = { git = "https://github.com/rjected/c-kzg-4844", branch = "dan/add-serde-feature" }

Expand Down
7 changes: 5 additions & 2 deletions crates/ef-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ regex = { workspace = true }
reqwest = { workspace = true, optional = true }
rstest = { workspace = true }
tokio = { workspace = true }
tracing = "0.1.37"
tracing = { workspace = true }
walkdir = { workspace = true }
zip = { workspace = true, optional = true }

[dev-dependencies]
tracing-subscriber = "0.3.17"
tracing-subscriber = { workspace = true }

[features]
ef-tests = []
Expand All @@ -61,3 +61,6 @@ required-features = ["dump"]
name = "fetch-kakarot-submodule-commit"
path = "src/bin/fetch_kakarot_submodule_commit.rs"
required-features = ["fetch-commit"]

[patch."https://github.com/starkware-libs/blockifier"]
blockifier = { git = "https://github.com/dojoengine/blockifier", rev = "c794d1b" }
2 changes: 1 addition & 1 deletion crates/sequencer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ license.workspace = true
[dependencies]
# Starknet
blockifier = { git = "https://github.com/starkware-libs/blockifier.git", tag = "v0.3.0-rc0" }
starknet_api = { workspace = true }
starknet_api = "0.5.0-rc1"

# Other
rustc-hash = "1.1.0"
Expand Down
1 change: 0 additions & 1 deletion crates/sequencer/src/config.rs

This file was deleted.

27 changes: 27 additions & 0 deletions crates/sequencer/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#[cfg(test)]
pub mod test_constants {
use starknet::core::types::FieldElement;
use starknet_api::{
block::{BlockNumber, BlockTimestamp},
core::{ClassHash, CompiledClassHash, ContractAddress, PatriciaKey},
hash::StarkFelt,
};

lazy_static::lazy_static! {
pub static ref SENDER_ADDRESS: FieldElement = FieldElement::from(2u8);
pub static ref SEQUENCER_ADDRESS: ContractAddress = ContractAddress(TryInto::<PatriciaKey>::try_into(StarkFelt::from(1234u16)).unwrap());
pub static ref FEE_TOKEN_ADDRESS: ContractAddress = ContractAddress(TryInto::<PatriciaKey>::try_into(StarkFelt::from(12345u16)).unwrap());

pub static ref ONE_FELT: StarkFelt = StarkFelt::from(1u8);
pub static ref TWO_FELT: StarkFelt = StarkFelt::from(2u8);
pub static ref ONE_PATRICIA: PatriciaKey = TryInto::<PatriciaKey>::try_into(*ONE_FELT).unwrap();
pub static ref TWO_PATRICIA: PatriciaKey = TryInto::<PatriciaKey>::try_into(*TWO_FELT).unwrap();
pub static ref ONE_HASH: ClassHash = ClassHash(*ONE_FELT);
pub static ref TWO_HASH: ClassHash = ClassHash(*TWO_FELT);
pub static ref ONE_COMPILED_HASH: CompiledClassHash = CompiledClassHash(*ONE_FELT);
pub static ref ONE_BLOCK_NUMBER: BlockNumber = BlockNumber(1);
pub static ref ONE_BLOCK_TIMESTAMP: BlockTimestamp = BlockTimestamp(1);
pub static ref TEST_ADDRESS: ContractAddress = ContractAddress(*ONE_PATRICIA);
pub static ref TEST_ACCOUNT: ContractAddress = ContractAddress(*TWO_PATRICIA);
}
}
2 changes: 1 addition & 1 deletion crates/sequencer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod config;
pub mod constants;
pub mod sequencer;
pub mod state;
18 changes: 13 additions & 5 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ use blockifier::state::state_api::{State, StateReader};
/// Sequencer is the main struct of the sequencer crate.
/// Using a trait bound for the state allows for better
/// speed, as the type of the state is known at compile time.
pub struct Sequencer<S: State + StateReader> {
pub config: SequencerConfig,
pub struct Sequencer<S>
where
for<'a> &'a mut S: State + StateReader,
{
pub context: BlockContext,
pub state: S,
}

impl<S: State + StateReader> Sequencer<S> {
impl<S> Sequencer<S>
where
for<'a> &'a mut S: State + StateReader,
{
/// Creates a new Sequencer instance.
pub fn new(config: SequencerConfig, state: S) -> Self {
Self { config, state }
pub fn new(context: BlockContext, state: S) -> Self {
Self { context, state }
}
}
}
}
50 changes: 23 additions & 27 deletions crates/sequencer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use starknet_api::{
/// See [rustc-hash](https://crates.io/crates/rustc-hash) for more information.
#[derive(Default)]
pub struct State {
pub classes: FxHashMap<ClassHash, ContractClass>,
pub compiled_classes: FxHashMap<ClassHash, CompiledClassHash>,
pub contracts: FxHashMap<ContractAddress, ClassHash>,
pub storage: FxHashMap<ContractStorageKey, StarkFelt>,
pub nonces: FxHashMap<ContractAddress, Nonce>,
classes: FxHashMap<ClassHash, ContractClass>,
compiled_class_hashes: FxHashMap<ClassHash, CompiledClassHash>,
contracts: FxHashMap<ContractAddress, ClassHash>,
storage: FxHashMap<ContractStorageKey, StarkFelt>,
nonces: FxHashMap<ContractAddress, Nonce>,
}

impl BlockifierState for State {
impl<'a> BlockifierState for &'a mut State {
fn set_storage_at(
&mut self,
contract_address: ContractAddress,
Expand Down Expand Up @@ -78,17 +78,17 @@ impl BlockifierState for State {
class_hash: ClassHash,
compiled_class_hash: CompiledClassHash,
) -> StateResult<()> {
self.compiled_classes
self.compiled_class_hashes
.insert(class_hash, compiled_class_hash);
Ok(())
}

fn to_state_diff(&self) -> CommitmentStateDiff {
fn to_state_diff(&mut self) -> CommitmentStateDiff {
unreachable!("to_state_diff should not be called in the sequencer")
}
}

impl BlockifierStateReader for State {
impl<'a> BlockifierStateReader for &'a mut State {
/// Default: 0 for an uninitialized contract address.
fn get_storage_at(
&mut self,
Expand Down Expand Up @@ -120,7 +120,7 @@ impl BlockifierStateReader for State {
.unwrap_or_default())
}

/// Errors if the compiled class hash is not declared.
/// Errors if the compiled class is not declared.
fn get_compiled_contract_class(
&mut self,
class_hash: &ClassHash,
Expand All @@ -131,9 +131,9 @@ impl BlockifierStateReader for State {
.ok_or_else(|| StateError::UndeclaredClassHash(class_hash.to_owned()))
}

/// Errors if the class hash is not declared.
/// Errors if the compiled class hash is not declared.
fn get_compiled_class_hash(&mut self, class_hash: ClassHash) -> StateResult<CompiledClassHash> {
self.compiled_classes
self.compiled_class_hashes
.get(&class_hash)
.cloned()
.ok_or_else(|| StateError::UndeclaredClassHash(class_hash))
Expand All @@ -143,21 +143,17 @@ impl BlockifierStateReader for State {
#[cfg(test)]
mod tests {
use blockifier::execution::contract_class::ContractClassV0;
use starknet_api::core::PatriciaKey;

use crate::constants::test_constants::{
ONE_COMPILED_HASH, ONE_FELT, ONE_HASH, ONE_PATRICIA, TEST_ADDRESS,
};

use super::*;
lazy_static::lazy_static! {
static ref ONE_FELT: StarkFelt = StarkFelt::from(1u8);
static ref ONE_PATRICIA: PatriciaKey = TryInto::<PatriciaKey>::try_into(*ONE_FELT).unwrap();
static ref ONE_HASH: ClassHash = ClassHash(*ONE_FELT);
static ref ONE_COMPILED_HASH: CompiledClassHash = CompiledClassHash(*ONE_FELT);
static ref TEST_ADDRESS: ContractAddress = ContractAddress(*ONE_PATRICIA);
}

#[test]
fn test_storage() {
// Given
let mut state = State::default();
let mut state = &mut State::default();

// When
state.set_storage_at(*TEST_ADDRESS, StorageKey(*ONE_PATRICIA), *ONE_FELT);
Expand All @@ -173,7 +169,7 @@ mod tests {
#[test]
fn test_nonce() {
// Given
let mut state = State::default();
let mut state = &mut State::default();

// When
state.increment_nonce(*TEST_ADDRESS).unwrap();
Expand All @@ -187,7 +183,7 @@ mod tests {
#[test]
fn test_class_hash() {
// Given
let mut state = State::default();
let mut state = &mut State::default();

// When
state.set_class_hash_at(*TEST_ADDRESS, *ONE_HASH).unwrap();
Expand All @@ -201,7 +197,7 @@ mod tests {
#[test]
fn test_contract_class() {
// Given
let mut state = State::default();
let mut state = &mut State::default();

// When
state
Expand All @@ -220,7 +216,7 @@ mod tests {
)]
fn test_uninitialized_contract_class() {
// Given
let mut state = State::default();
let mut state = &mut State::default();

// When
state.get_compiled_contract_class(&ONE_HASH).unwrap();
Expand All @@ -229,7 +225,7 @@ mod tests {
#[test]
fn test_compiled_class_hash() {
// Given
let mut state = State::default();
let mut state = &mut State::default();

// When
state
Expand All @@ -248,7 +244,7 @@ mod tests {
)]
fn test_uninitialized_compiled_class_hash() {
// Given
let mut state = State::default();
let mut state = &mut State::default();

// When
state.get_compiled_class_hash(*ONE_HASH).unwrap();
Expand Down

0 comments on commit 02d761f

Please sign in to comment.