Skip to content

Commit

Permalink
feat(storage)!: compress thin state diff (#1342)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-starkware authored Oct 30, 2023
1 parent 2f81afa commit 46bff2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/papyrus_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ use crate::version::{VersionStorageReader, VersionStorageWriter};
/// The current version of the storage code.
/// Whenever a breaking change is introduced, the version is incremented and a storage
/// migration is required for existing storages.
pub const STORAGE_VERSION: Version = Version(6);
pub const STORAGE_VERSION: Version = Version(7);

/// Opens a storage and returns a [`StorageReader`] and a [`StorageWriter`].
pub fn open_storage(
Expand Down
40 changes: 32 additions & 8 deletions crates/papyrus_storage/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,6 @@ auto_storage_serde! {
pub events_contract_addresses: Vec<ContractAddress>,
pub execution_status: TransactionExecutionStatus,
}
pub struct ThinStateDiff {
pub deployed_contracts: IndexMap<ContractAddress, ClassHash>,
pub storage_diffs: IndexMap<ContractAddress, IndexMap<StorageKey, StarkFelt>>,
pub declared_classes: IndexMap<ClassHash, CompiledClassHash>,
pub deprecated_declared_classes: Vec<ClassHash>,
pub nonces: IndexMap<ContractAddress, Nonce>,
pub replaced_classes: IndexMap<ContractAddress, ClassHash>,
}
pub enum ThinTransactionOutput {
Declare(ThinDeclareTransactionOutput) = 0,
Deploy(ThinDeployTransactionOutput) = 1,
Expand Down Expand Up @@ -980,3 +972,35 @@ impl StorageSerde for CasmContractClass {

#[cfg(test)]
create_storage_serde_test!(CasmContractClass);

impl StorageSerde for ThinStateDiff {
fn serialize_into(&self, res: &mut impl std::io::Write) -> Result<(), StorageSerdeError> {
let mut to_compress: Vec<u8> = Vec::new();
self.deployed_contracts.serialize_into(&mut to_compress)?;
self.storage_diffs.serialize_into(&mut to_compress)?;
self.declared_classes.serialize_into(&mut to_compress)?;
self.deprecated_declared_classes.serialize_into(&mut to_compress)?;
self.nonces.serialize_into(&mut to_compress)?;
self.replaced_classes.serialize_into(&mut to_compress)?;
let compressed = compress(to_compress.as_slice())?;
compressed.serialize_into(res)?;
Ok(())
}

fn deserialize_from(bytes: &mut impl std::io::Read) -> Option<Self> {
let compressed_data = Vec::<u8>::deserialize_from(bytes)?;
let data = decompress(compressed_data.as_slice()).ok()?;
let data = &mut data.as_slice();
Some(Self {
deployed_contracts: IndexMap::deserialize_from(data)?,
storage_diffs: IndexMap::deserialize_from(data)?,
declared_classes: IndexMap::deserialize_from(data)?,
deprecated_declared_classes: Vec::deserialize_from(data)?,
nonces: IndexMap::deserialize_from(data)?,
replaced_classes: IndexMap::deserialize_from(data)?,
})
}
}

#[cfg(test)]
create_storage_serde_test!(ThinStateDiff);

0 comments on commit 46bff2f

Please sign in to comment.