From f36f9f88cb4b2584f6d08767eccd7b894128a809 Mon Sep 17 00:00:00 2001 From: greged93 <82421016+greged93@users.noreply.github.com> Date: Tue, 19 Sep 2023 16:56:28 +0700 Subject: [PATCH] feat: first draft for sequencer (#104) * first draft for sequencer * add rust doc * comments + linting --- Cargo.lock | 9 +++++++++ Cargo.toml | 2 +- crates/sequencer/Cargo.toml | 20 ++++++++++++++++++++ crates/sequencer/src/config.rs | 1 + crates/sequencer/src/lib.rs | 3 +++ crates/sequencer/src/sequencer.rs | 17 +++++++++++++++++ crates/sequencer/src/state.rs | 24 ++++++++++++++++++++++++ 7 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 crates/sequencer/Cargo.toml create mode 100644 crates/sequencer/src/config.rs create mode 100644 crates/sequencer/src/lib.rs create mode 100644 crates/sequencer/src/sequencer.rs create mode 100644 crates/sequencer/src/state.rs diff --git a/Cargo.lock b/Cargo.lock index 0fa1fd10..b94b1a5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7913,6 +7913,15 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73" +[[package]] +name = "sequencer" +version = "0.1.0" +dependencies = [ + "blockifier", + "rustc-hash", + "starknet_api", +] + [[package]] name = "serde" version = "1.0.188" diff --git a/Cargo.toml b/Cargo.toml index 7b30a731..5a4dbb49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["crates/ef-testing"] +members = ["crates/ef-testing", "crates/sequencer"] resolver = "2" [workspace.package] diff --git a/crates/sequencer/Cargo.toml b/crates/sequencer/Cargo.toml new file mode 100644 index 00000000..9abd6584 --- /dev/null +++ b/crates/sequencer/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "sequencer" +version.workspace = true +edition.workspace = true +authors.workspace = true +description.workspace = true +homepage.workspace = true +repository.workspace = true +readme.workspace = true +license.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +# Starknet +blockifier = { git = "https://github.com/starkware-libs/blockifier.git", tag = "v0.3.0-rc0" } +starknet_api = { workspace = true } + +# Other +rustc-hash = "1.1.0" diff --git a/crates/sequencer/src/config.rs b/crates/sequencer/src/config.rs new file mode 100644 index 00000000..989c9ba7 --- /dev/null +++ b/crates/sequencer/src/config.rs @@ -0,0 +1 @@ +pub struct SequencerConfig {} diff --git a/crates/sequencer/src/lib.rs b/crates/sequencer/src/lib.rs new file mode 100644 index 00000000..4f4593df --- /dev/null +++ b/crates/sequencer/src/lib.rs @@ -0,0 +1,3 @@ +pub mod config; +pub mod sequencer; +pub mod state; diff --git a/crates/sequencer/src/sequencer.rs b/crates/sequencer/src/sequencer.rs new file mode 100644 index 00000000..e4d845b8 --- /dev/null +++ b/crates/sequencer/src/sequencer.rs @@ -0,0 +1,17 @@ +use crate::config::SequencerConfig; +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 { + pub config: SequencerConfig, + pub state: S, +} + +impl Sequencer { + /// Creates a new Sequencer instance. + pub fn new(config: SequencerConfig, state: S) -> Self { + Self { config, state } + } +} diff --git a/crates/sequencer/src/state.rs b/crates/sequencer/src/state.rs new file mode 100644 index 00000000..1a375d97 --- /dev/null +++ b/crates/sequencer/src/state.rs @@ -0,0 +1,24 @@ +use blockifier::{ + execution::contract_class::ContractClass, state::cached_state::ContractStorageKey, +}; +use rustc_hash::FxHashMap; +use starknet_api::core::CompiledClassHash; +use starknet_api::{ + core::{ClassHash, ContractAddress, Nonce}, + hash::StarkFelt, +}; + +/// Generic state structure for the sequencer. +/// The use of FxHashMap allows for a better performance. +/// This hash map is used by rustc. It uses a non cryptographic hash function +/// which is faster than the default hash function. Think about changing +/// if the test sequencer is used for tests outside of ef-tests. +/// See [rustc-hash](https://crates.io/crates/rustc-hash) for more information. +#[derive(Default)] +pub struct State { + pub classes: FxHashMap, + pub compiled_classes: FxHashMap, + pub contracts: FxHashMap, + pub storage: FxHashMap, + pub nonces: FxHashMap, +}