Skip to content

Commit

Permalink
style(storage): style improvement of transaction serialization (#1361)
Browse files Browse the repository at this point in the history
style(storage): style improvement of transaction serialisation
  • Loading branch information
dan-starkware authored Nov 10, 2023
1 parent 6130f35 commit 568887a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
6 changes: 6 additions & 0 deletions crates/papyrus_storage/src/compression_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ pub fn decompress_from_reader(bytes: &mut impl std::io::Read) -> Option<Vec<u8>>
let compressed_data = Vec::<u8>::deserialize_from(bytes)?;
decompress(compressed_data.as_slice()).ok()
}

#[derive(Debug, Eq, PartialEq)]
pub(crate) enum IsCompressed {
No = 0,
Yes = 1,
}
34 changes: 19 additions & 15 deletions crates/papyrus_storage/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ use crate::compression_utils::{
decompress,
decompress_from_reader,
serialize_and_compress,
IsCompressed,
};
use crate::db::serialization::{StorageSerde, StorageSerdeError};
use crate::header::StarknetVersion;
Expand All @@ -127,9 +128,8 @@ use crate::state::data::IndexedDeprecatedContractClass;
use crate::version::Version;
use crate::{MarkerKind, OffsetKind};

const COMPRESSION_THRESHOLD: usize = 384;
const COMPRESSED: u8 = 1;
const UNCOMPRESSED: u8 = 0;
// The threshold for compressing transactions.
const COMPRESSION_THRESHOLD_BYTES: usize = 384;

auto_storage_serde! {
pub struct AccountDeploymentData(pub Vec<StarkFelt>);
Expand Down Expand Up @@ -262,6 +262,10 @@ auto_storage_serde! {
V1(InvokeTransactionV1) = 1,
V3(InvokeTransactionV3) = 2,
}
pub enum IsCompressed {
No = 0,
Yes = 1,
}
pub struct L1ToL2Payload(pub Vec<StarkFelt>);
pub struct L2ToL1Payload(pub Vec<StarkFelt>);
enum MarkerKind {
Expand Down Expand Up @@ -944,7 +948,8 @@ impl StorageSerde for ThinStateDiff {
#[cfg(test)]
create_storage_serde_test!(ThinStateDiff);

macro_rules! auto_storage_serde_compressed {
// The following structs are conditionally compressed based on their serialized size.
macro_rules! auto_storage_serde_conditionally_compressed {
() => {};
($(pub)? struct $name:ident { $(pub $field:ident : $ty:ty ,)* } $($rest:tt)*) => {
impl StorageSerde for $name {
Expand All @@ -953,24 +958,22 @@ macro_rules! auto_storage_serde_compressed {
$(
self.$field.serialize_into(&mut to_compress)?;
)*
if to_compress.len() > COMPRESSION_THRESHOLD {
res.write_all(&[COMPRESSED])?;
if to_compress.len() > COMPRESSION_THRESHOLD_BYTES {
IsCompressed::Yes.serialize_into(res)?;
let compressed = compress(to_compress.as_slice())?;
compressed.serialize_into(res)?;
} else {
res.write_all(&[UNCOMPRESSED])?;
IsCompressed::No.serialize_into(res)?;
to_compress.serialize_into(res)?;
}
Ok(())
}
fn deserialize_from(bytes: &mut impl std::io::Read) -> Option<Self> {
let mut compressed = [0u8; 1];
bytes.read_exact(&mut compressed).ok()?;
let is_compressed = IsCompressed::deserialize_from(bytes)?;
let maybe_compressed_data = Vec::<u8>::deserialize_from(bytes)?;
let data = match compressed[0] {
UNCOMPRESSED => maybe_compressed_data,
COMPRESSED => decompress(maybe_compressed_data.as_slice()).ok()?,
_ => return None,
let data = match is_compressed {
IsCompressed::No => maybe_compressed_data,
IsCompressed::Yes => decompress(maybe_compressed_data.as_slice()).ok()?,
};
let data = &mut data.as_slice();
Some(Self {
Expand All @@ -982,11 +985,12 @@ macro_rules! auto_storage_serde_compressed {
}
#[cfg(test)]
create_storage_serde_test!($name);
auto_storage_serde_compressed!($($rest)*);
auto_storage_serde_conditionally_compressed!($($rest)*);
};
}

auto_storage_serde_compressed! {
// The following transactions have variable length Calldata and are conditionally compressed.
auto_storage_serde_conditionally_compressed! {
pub struct DeployAccountTransactionV1 {
pub max_fee: Fee,
pub signature: TransactionSignature,
Expand Down
5 changes: 5 additions & 0 deletions crates/papyrus_storage/src/test_instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::body::events::{
ThinTransactionOutput,
};
use crate::body::TransactionIndex;
use crate::compression_utils::IsCompressed;
use crate::header::StarknetVersion;
use crate::mmap_file::LocationInFile;
use crate::state::data::IndexedDeprecatedContractClass;
Expand All @@ -30,6 +31,10 @@ auto_impl_get_test_instance! {
pub block_number: BlockNumber,
pub location_in_file: LocationInFile,
}
pub enum IsCompressed {
No = 0,
Yes = 1,
}
enum MarkerKind {
Header = 0,
Body = 1,
Expand Down

0 comments on commit 568887a

Please sign in to comment.