Skip to content

Commit

Permalink
feat: make to_compact borrow (#9488)
Browse files Browse the repository at this point in the history
Co-authored-by: joshieDo <[email protected]>
  • Loading branch information
mattsse and joshieDo authored Jul 17, 2024
1 parent 5cd22b5 commit c3347f3
Show file tree
Hide file tree
Showing 32 changed files with 336 additions and 152 deletions.
4 changes: 2 additions & 2 deletions crates/primitives-traits/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Bytecode {
}

impl Compact for Bytecode {
fn to_compact<B>(self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
Expand Down Expand Up @@ -209,7 +209,7 @@ mod tests {
2,
JumpTable::from_slice(&[0]),
)));
let len = bytecode.clone().to_compact(&mut buf);
let len = bytecode.to_compact(&mut buf);
assert_eq!(len, 16);

let (decoded, remainder) = Bytecode::from_compact(&buf, len);
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives-traits/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod tests {
fn test_roundtrip_conversion_between_log_and_alloy_log(log in arb::<Log>()) {
// Convert log to buffer and then create alloy_log from buffer and compare
let mut compacted_log = Vec::<u8>::new();
let len = log.clone().to_compact(&mut compacted_log);
let len = log.to_compact(&mut compacted_log);

let alloy_log = AlloyLog::from_compact(&compacted_log, len).0;
assert_eq!(log, alloy_log.into());
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives-traits/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl From<(B256, U256)> for StorageEntry {
// and compress second part of the value. If we have compression
// over whole value (Even SubKey) that would mess up fetching of values with seek_by_key_subkey
impl Compact for StorageEntry {
fn to_compact<B>(self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives-traits/src/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod tests {
// Convert to buffer and then create alloy_access_list from buffer and
// compare
let mut compacted_reth_withdrawal = Vec::<u8>::new();
let len = withdrawal.clone().to_compact(&mut compacted_reth_withdrawal);
let len = withdrawal.to_compact(&mut compacted_reth_withdrawal);

// decode the compacted buffer to AccessList
let alloy_withdrawal = Withdrawal::from_compact(&compacted_reth_withdrawal, len).0;
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ mod tests {
};

let mut data = vec![];
receipt.clone().to_compact(&mut data);
receipt.to_compact(&mut data);
let (decoded, _) = Receipt::from_compact(&data[..], data.len());
assert_eq!(decoded, receipt);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/transaction/access_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod tests {
// Convert access_list to buffer and then create alloy_access_list from buffer and
// compare
let mut compacted_reth_access_list = Vec::<u8>::new();
let len = access_list.clone().to_compact(&mut compacted_reth_access_list);
let len = access_list.to_compact(&mut compacted_reth_access_list);

// decode the compacted buffer to AccessList
let alloy_access_list = AccessList::from_compact(&compacted_reth_access_list, len).0;
Expand Down
6 changes: 3 additions & 3 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ impl From<TxEip7702> for Transaction {
impl reth_codecs::Compact for Transaction {
// Serializes the TxType to the buffer if necessary, returning 2 bits of the type as an
// identifier instead of the length.
fn to_compact<B>(self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
Expand Down Expand Up @@ -910,7 +910,7 @@ impl TransactionSignedNoHash {

#[cfg(any(test, feature = "reth-codec"))]
impl reth_codecs::Compact for TransactionSignedNoHash {
fn to_compact<B>(self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
Expand Down Expand Up @@ -2083,7 +2083,7 @@ mod tests {
fn test_transaction_signed_to_from_compact(tx_signed_no_hash: TransactionSignedNoHash) {
// zstd aware `to_compact`
let mut buff: Vec<u8> = Vec::new();
let written_bytes = tx_signed_no_hash.clone().to_compact(&mut buff);
let written_bytes = tx_signed_no_hash.to_compact(&mut buff);
let (decoded, _) = TransactionSignedNoHash::from_compact(&buff, written_bytes);
assert_eq!(tx_signed_no_hash, decoded);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/transaction/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Signature {

#[cfg(any(test, feature = "reth-codec"))]
impl reth_codecs::Compact for Signature {
fn to_compact<B>(self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
Expand Down
8 changes: 4 additions & 4 deletions crates/primitives/src/transaction/tx_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl TryFrom<U64> for TxType {

#[cfg(any(test, feature = "reth-codec"))]
impl reth_codecs::Compact for TxType {
fn to_compact<B>(self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
Expand All @@ -147,16 +147,16 @@ impl reth_codecs::Compact for TxType {
Self::Eip2930 => 1,
Self::Eip1559 => 2,
Self::Eip4844 => {
buf.put_u8(self as u8);
buf.put_u8(*self as u8);
COMPACT_EXTENDED_IDENTIFIER_FLAG
}
Self::Eip7702 => {
buf.put_u8(self as u8);
buf.put_u8(*self as u8);
COMPACT_EXTENDED_IDENTIFIER_FLAG
}
#[cfg(feature = "optimism")]
Self::Deposit => {
buf.put_u8(self as u8);
buf.put_u8(*self as u8);
COMPACT_EXTENDED_IDENTIFIER_FLAG
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/stages/types/src/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl MerkleCheckpoint {
}

impl Compact for MerkleCheckpoint {
fn to_compact<B>(self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
Expand All @@ -47,7 +47,7 @@ impl Compact for MerkleCheckpoint {

buf.put_u16(self.walker_stack.len() as u16);
len += 2;
for item in self.walker_stack {
for item in &self.walker_stack {
len += item.to_compact(buf);
}

Expand Down Expand Up @@ -393,7 +393,7 @@ mod tests {
};

let mut buf = Vec::new();
let encoded = checkpoint.clone().to_compact(&mut buf);
let encoded = checkpoint.to_compact(&mut buf);
let (decoded, _) = MerkleCheckpoint::from_compact(&buf, encoded);
assert_eq!(decoded, checkpoint);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/codecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ alloy-eips = { workspace = true, default-features = false, features = [
"arbitrary",
"serde",
] }
alloy-primitives = { workspace = true, features = ["arbitrary", "serde"] }
alloy-primitives = { workspace = true, features = ["arbitrary", "serde", "rand"] }
alloy-consensus = { workspace = true, features = ["arbitrary"] }
test-fuzz.workspace = true
serde_json.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/codecs/derive/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn maybe_generate_tests(args: TokenStream, ast: &DeriveInput) -> TokenStream
let mut buf = vec![];
let len = field.clone().to_compact(&mut buf);
let (decoded, _) = super::#type_ident::from_compact(&buf, len);
assert!(field == decoded);
assert!(field == decoded, "maybe_generate_tests::compact");
}
});
} else if arg.to_string() == "rlp" {
Expand All @@ -37,7 +37,7 @@ pub fn maybe_generate_tests(args: TokenStream, ast: &DeriveInput) -> TokenStream
let len = field.encode(&mut buf);
let mut b = &mut buf.as_slice();
let decoded = super::#type_ident::decode(b).unwrap();
assert_eq!(field, decoded);
assert_eq!(field, decoded, "maybe_generate_tests::rlp");
// ensure buffer is fully consumed by decode
assert!(b.is_empty(), "buffer was not consumed entirely");

Expand Down
27 changes: 21 additions & 6 deletions crates/storage/codecs/derive/src/compact/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::*;
/// their potential presence.
pub(crate) fn generate_flag_struct(
ident: &Ident,
has_lifetime: bool,
fields: &FieldList,
is_zstd: bool,
) -> TokenStream2 {
Expand Down Expand Up @@ -52,15 +53,29 @@ pub(crate) fn generate_flag_struct(
let docs =
format!("Fieldset that facilitates compacting the parent type. Used bytes: {total_bytes} | Unused bits: {unused_bits}");
let bitflag_encoded_bytes = format!("Used bytes by [`{flags_ident}`]");
let impl_bitflag_encoded_bytes = if has_lifetime {
quote! {
impl<'a> #ident<'a> {
#[doc = #bitflag_encoded_bytes]
pub const fn bitflag_encoded_bytes() -> usize {
#total_bytes as usize
}
}
}
} else {
quote! {
impl #ident {
#[doc = #bitflag_encoded_bytes]
pub const fn bitflag_encoded_bytes() -> usize {
#total_bytes as usize
}
}
}
};

// Generate the flag struct.
quote! {
impl #ident {
#[doc = #bitflag_encoded_bytes]
pub const fn bitflag_encoded_bytes() -> usize {
#total_bytes as usize
}
}
#impl_bitflag_encoded_bytes
pub use #mod_flags_ident::#flags_ident;
#[allow(non_snake_case)]
mod #mod_flags_ident {
Expand Down
72 changes: 54 additions & 18 deletions crates/storage/codecs/derive/src/compact/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use super::*;
use convert_case::{Case, Casing};

/// Generates code to implement the `Compact` trait for a data type.
pub fn generate_from_to(ident: &Ident, fields: &FieldList, is_zstd: bool) -> TokenStream2 {
pub fn generate_from_to(
ident: &Ident,
has_lifetime: bool,
fields: &FieldList,
is_zstd: bool,
) -> TokenStream2 {
let flags = format_ident!("{ident}Flags");

let to_compact = generate_to_compact(fields, ident, is_zstd);
Expand All @@ -15,35 +20,66 @@ pub fn generate_from_to(ident: &Ident, fields: &FieldList, is_zstd: bool) -> Tok
let fuzz = format_ident!("fuzz_test_{snake_case_ident}");
let test = format_ident!("fuzz_{snake_case_ident}");

// Build function
quote! {
let lifetime = if has_lifetime {
quote! { 'a }
} else {
quote! {}
};

#[cfg(test)]
#[allow(dead_code)]
#[test_fuzz::test_fuzz]
fn #fuzz(obj: #ident) {
let mut buf = vec![];
let len = obj.clone().to_compact(&mut buf);
let (same_obj, buf) = #ident::from_compact(buf.as_ref(), len);
assert_eq!(obj, same_obj);
let impl_compact = if has_lifetime {
quote! {
impl<#lifetime> Compact for #ident<#lifetime>
}
} else {
quote! {
impl Compact for #ident
}
};

#[test]
pub fn #test() {
#fuzz(#ident::default())
let fn_from_compact = if has_lifetime {
quote! { unimplemented!("from_compact not supported with ref structs") }
} else {
quote! {
let (flags, mut buf) = #flags::from(buf);
#from_compact
}
};

let fuzz_tests = if has_lifetime {
quote! {}
} else {
quote! {
#[cfg(test)]
#[allow(dead_code)]
#[test_fuzz::test_fuzz]
fn #fuzz(obj: #ident) {
let mut buf = vec![];
let len = obj.clone().to_compact(&mut buf);
let (same_obj, buf) = #ident::from_compact(buf.as_ref(), len);
assert_eq!(obj, same_obj);
}

#[test]
pub fn #test() {
#fuzz(#ident::default())
}
}
};

// Build function
quote! {
#fuzz_tests

impl Compact for #ident {
fn to_compact<B>(self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]> {
#impl_compact {
fn to_compact<B>(&self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]> {
let mut flags = #flags::default();
let mut total_length = 0;
#(#to_compact)*
total_length
}

fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) {
let (flags, mut buf) = #flags::from(buf);
#from_compact
#fn_from_compact
}
}
}
Expand Down
Loading

0 comments on commit c3347f3

Please sign in to comment.