Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dumping and loading of state #555

Merged
merged 21 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion crates/sequencer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ license.workspace = true
# Starknet
# TODO: remove the blockifier patch on the workspace once we can remove Katana.
blockifier = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
starknet_api = { workspace = true }
starknet = { workspace = true }

# Other
eyre = { workspace = true }
tracing = { workspace = true }
rustc-hash = "1.1.0"
thiserror = { workspace = true }

[dev-dependencies]
lazy_static = { workspace = true }
serde_json = { workspace = true }
tempfile = "3.8.0"
5 changes: 4 additions & 1 deletion crates/sequencer/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ pub mod test_constants {
use starknet::core::types::FieldElement;
use starknet_api::{
block::{BlockNumber, BlockTimestamp},
core::{ClassHash, CompiledClassHash, ContractAddress, PatriciaKey},
core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey},
hash::StarkFelt,
state::StorageKey,
};

lazy_static::lazy_static! {
pub static ref TEST_CONTRACT: ContractAddress = ContractAddress(*ONE_PATRICIA);
pub static ref TEST_ACCOUNT: ContractAddress = ContractAddress(*TWO_PATRICIA);
pub static ref TEST_STORAGE_KEY: StorageKey = StorageKey(*ONE_PATRICIA);
pub static ref TEST_NONCE: Nonce = Nonce(*ONE_FELT);
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 ETH_FEE_TOKEN_ADDRESS: ContractAddress = ContractAddress(TryInto::<PatriciaKey>::try_into(StarkFelt::from(12345u16)).unwrap());
Expand Down
1 change: 1 addition & 0 deletions crates/sequencer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ pub mod commit;
pub mod constants;
pub mod execution;
pub mod sequencer;
pub mod serde;
pub mod state;
pub mod transaction;
26 changes: 25 additions & 1 deletion crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::{commit::Committer, execution::Execution};
use std::path::Path;

use crate::{
commit::Committer,
execution::Execution,
serde::{DumpLoad, SerializationError},
};
use blockifier::{
block_context::BlockContext,
state::{
Expand Down Expand Up @@ -43,6 +49,24 @@ where
}
}

impl<S> Sequencer<S>
where
S: DumpLoad + Clone,
for<'any> &'any mut S: State + StateReader,
{
Eikix marked this conversation as resolved.
Show resolved Hide resolved
pub fn dump_state_to_file(&self, file_path: &Path) -> Result<(), SerializationError> {
self.state.clone().dump_state_to_file(file_path)
}
greged93 marked this conversation as resolved.
Show resolved Hide resolved

pub fn load_state_from_file(
block_context: BlockContext,
file_path: &Path,
) -> Result<Self, SerializationError> {
let state = S::load_state_from_file(file_path)?;
Ok(Self::new(block_context, state))
}
}

impl<S> Execution for Sequencer<S>
where
for<'any> &'any mut S: State + StateReader + Committer<S>,
Expand Down
222 changes: 222 additions & 0 deletions crates/sequencer/src/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
use std::{fs, io, path::Path};

use crate::state::State;
use blockifier::{
execution::contract_class::ContractClass, state::cached_state::ContractStorageKey,
};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use starknet_api::{
core::{ClassHash, CompiledClassHash, ContractAddress, Nonce},
hash::StarkFelt,
};

use thiserror::Error;

pub trait DumpLoad {
fn dump_state_to_file(self, file_path: &Path) -> Result<(), SerializationError>;

fn load_state_from_file(file_path: &Path) -> Result<Self, SerializationError>
where
Self: Sized;
}

impl DumpLoad for State {
/// This will serialize the current state, and will save it to a path
fn dump_state_to_file(self, path: &Path) -> Result<(), SerializationError> {
let serializable_state: SerializableState = self.into();

let dump = serde_json::to_string(&serializable_state)
.map_err(SerializationError::SerdeJsonError)?;
greged93 marked this conversation as resolved.
Show resolved Hide resolved

fs::write(path, dump).map_err(SerializationError::IoError)?;
greged93 marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

/// This will read a dump from a file and initialize the state from it
fn load_state_from_file(path: &Path) -> Result<Self, SerializationError> {
let dump = fs::read(path).unwrap();
greged93 marked this conversation as resolved.
Show resolved Hide resolved
let serializable_state: SerializableState =
serde_json::from_slice(&dump).map_err(SerializationError::SerdeJsonError)?;
greged93 marked this conversation as resolved.
Show resolved Hide resolved

Ok(serializable_state.into())
}
}

#[derive(Error, Debug)]
pub enum SerializationError {
#[error(transparent)]
IoError(#[from] io::Error),
#[error(transparent)]
SerdeJsonError(#[from] serde_json::Error),
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct SerializableState {
pub classes: FxHashMap<ClassHash, ContractClass>,
pub compiled_classes_hash: FxHashMap<ClassHash, CompiledClassHash>,
pub contracts: FxHashMap<ContractAddress, ClassHash>,
#[serde(with = "serialize_contract_storage")]
pub storage: FxHashMap<ContractStorageKey, StarkFelt>,
pub nonces: FxHashMap<ContractAddress, Nonce>,
}

impl From<State> for SerializableState {
fn from(state: State) -> Self {
Self {
classes: state.classes,
compiled_classes_hash: state.compiled_class_hashes,
contracts: state.contracts,
storage: state.storage,
nonces: state.nonces,
}
}
}

impl From<SerializableState> for State {
fn from(serializable_state: SerializableState) -> Self {
Self {
classes: serializable_state.classes,
compiled_class_hashes: serializable_state.compiled_classes_hash,
contracts: serializable_state.contracts,
storage: serializable_state.storage,
nonces: serializable_state.nonces,
}
}
}

mod serialize_contract_storage {
use blockifier::state::cached_state::ContractStorageKey;
use rustc_hash::{FxHashMap, FxHasher};
Eikix marked this conversation as resolved.
Show resolved Hide resolved
use serde::de::{Deserializer, MapAccess, Visitor};
use serde::ser::{SerializeMap, Serializer};
use starknet_api::hash::StarkFelt;
use std::fmt;
use std::hash::BuildHasherDefault;

pub fn serialize<S>(
map: &FxHashMap<ContractStorageKey, StarkFelt>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut serialized_map = serializer.serialize_map(Some(map.len()))?;
for (k, v) in map {
let key = serde_json::to_string(k).map_err(|error| {
serde::ser::Error::custom(format!(
"failed to deserialize contract_storage_key {:?},\n error {}",
k, error
))
})?;

serialized_map.serialize_entry(&key, &v)?;
}
serialized_map.end()
}

pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<FxHashMap<ContractStorageKey, StarkFelt>, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_map(MapContractStorageKeyVisitor)
}

struct MapContractStorageKeyVisitor;

impl<'de> Visitor<'de> for MapContractStorageKeyVisitor {
// The type that our Visitor is going to produce.
type Value = FxHashMap<ContractStorageKey, StarkFelt>;

// Format a message stating what data this Visitor expects to receive.
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("ContractStorageKey to Value map")
}

// Deserialize Map from an abstract "map" provided by the
// Deserializer. The MapAccess input is a callback provided by
// the Deserializer to let us see each entry in the map.
fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let mut map = FxHashMap::with_capacity_and_hasher(
access.size_hint().unwrap_or(0),
BuildHasherDefault::<FxHasher>::default(),
);

// While there are entries remaining in the input, add them
// into our map.
while let Some((key, value)) = access.next_entry::<String, StarkFelt>()? {
let key: ContractStorageKey = serde_json::from_str(&key).map_err(|error| {
serde::de::Error::custom(format!(
"failed to deserialize contract_storage_key {:?},\n error {}",
key, error
))
})?;
map.insert(key, value);
}

Ok(map)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use blockifier::{
execution::contract_class::{ContractClass, ContractClassV0},
state::cached_state::ContractStorageKey,
};

use crate::{
constants::test_constants::{
ONE_CLASS_HASH, ONE_COMPILED_CLASS_HASH, ONE_FELT, TEST_CONTRACT, TEST_NONCE,
TEST_STORAGE_KEY,
},
state::State,
};

#[test]
pub fn dump_and_load_state() {
let mut state = State::default();

// setting up entry for state.classes
let class_hash = *ONE_CLASS_HASH;
let contract_class = include_str!("./test_data/cairo_0/compiled_classes/counter.json");
let contract_class: ContractClassV0 = serde_json::from_str(contract_class).expect("failed to deserialize ContractClass from ./crates/sequencer/test_data/cairo_1/compiled_classes/account.json");
let contract_class = ContractClass::V0(contract_class);

let compiled_class_hash = *ONE_COMPILED_CLASS_HASH;
let contract_address = *TEST_CONTRACT;
let contract_storage_key: ContractStorageKey = (contract_address, *TEST_STORAGE_KEY);
let storage_value = *ONE_FELT;
let nonce = *TEST_NONCE;

state.classes.insert(class_hash, contract_class);
state
.compiled_class_hashes
.insert(class_hash, compiled_class_hash);
state.contracts.insert(contract_address, class_hash);
state.storage.insert(contract_storage_key, storage_value);
state.nonces.insert(contract_address, nonce);

let temp_file = tempfile::NamedTempFile::new().expect("failed open named temp file");
let dump_file_path = temp_file.into_temp_path();

state
.clone()
.dump_state_to_file(&dump_file_path)
.expect("failed to save dump to file");

let loaded_state =
State::load_state_from_file(&dump_file_path).expect("failed to load state from file");
assert_eq!(state, loaded_state);

dump_file_path.close().expect("failed to close temp file");
}
}
14 changes: 8 additions & 6 deletions crates/sequencer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use starknet_api::{
hash::StarkFelt,
};

use serde::{Deserialize, Serialize};

use crate::commit::Committer;

/// Generic state structure for the sequencer.
Expand All @@ -22,13 +24,13 @@ use crate::commit::Committer;
/// 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)]
#[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct State {
classes: FxHashMap<ClassHash, ContractClass>,
compiled_class_hashes: FxHashMap<ClassHash, CompiledClassHash>,
contracts: FxHashMap<ContractAddress, ClassHash>,
storage: FxHashMap<ContractStorageKey, StarkFelt>,
nonces: FxHashMap<ContractAddress, Nonce>,
pub(crate) classes: FxHashMap<ClassHash, ContractClass>,
Eikix marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) compiled_class_hashes: FxHashMap<ClassHash, CompiledClassHash>,
pub(crate) contracts: FxHashMap<ContractAddress, ClassHash>,
pub(crate) storage: FxHashMap<ContractStorageKey, StarkFelt>,
pub(crate) nonces: FxHashMap<ContractAddress, Nonce>,
}

impl State {
Expand Down
Loading