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

WIP feat(codec): add compact #11956

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
34 changes: 32 additions & 2 deletions crates/storage/codecs/derive/src/compact/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,48 @@

use super::*;
use convert_case::{Case, Casing};
use syn::{Attribute, Lit, parse_quote};

/// Returns the path of the reth_codecs crate
pub fn codecs_path(attrs: &[Attribute]) -> syn::Path {
// TODO: support reth_codecs(crate = "") attribute so we can set a custom path, if this is unset use `reth_codecs`
for attr in attrs {
if attr.path().is_ident("compact") {
let mut crate_path = None;
if let Err(e) = attr.parse_nested_meta(|meta| {
if meta.path.is_ident("crate") {
let lit: Lit = meta.value()?.parse()?;
if let Lit::Str(lit_str) = lit {
crate_path = Some(lit_str.value());
} else {
return Err(syn::Error::new_spanned(lit, "Expected a string literal for crate path"));
}

}
Ok(())
}) {
panic!("Failed to parse `compact` attribute: {}", e);
}

}
}

parse_quote!(reth_codecs)
}

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

let to_compact = generate_to_compact(fields, ident, is_zstd);
let from_compact = generate_from_compact(fields, ident, is_zstd);
let reth_codecs = codecs_path(attrs);

let snake_case_ident = ident.to_string().to_case(Case::Snake);

Expand All @@ -28,11 +58,11 @@ pub fn generate_from_to(

let impl_compact = if has_lifetime {
quote! {
impl<#lifetime> Compact for #ident<#lifetime>
impl<#lifetime> #reth_codecs::Compact for #ident<#lifetime>
}
} else {
quote! {
impl Compact for #ident
impl #reth_codecs::Compact for #ident
}
};

Expand Down
8 changes: 4 additions & 4 deletions crates/storage/codecs/derive/src/compact/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ pub enum FieldTypes {
pub fn derive(input: TokenStream, is_zstd: bool) -> TokenStream {
let mut output = quote! {};

let DeriveInput { ident, data, generics, .. } = parse_macro_input!(input);
let DeriveInput { ident, data, generics, attrs, .. } = parse_macro_input!(input);

let has_lifetime = has_lifetime(&generics);

let fields = get_fields(&data);
output.extend(generate_flag_struct(&ident, has_lifetime, &fields, is_zstd));
output.extend(generate_from_to(&ident, has_lifetime, &fields, is_zstd));
output.extend(generate_from_to(&ident, has_lifetime, &fields, is_zstd, &attrs));
output.into()
}

Expand Down Expand Up @@ -233,10 +233,10 @@ mod tests {

// Generate code that will impl the `Compact` trait.
let mut output = quote! {};
let DeriveInput { ident, data, .. } = parse2(f_struct).unwrap();
let DeriveInput { ident, data, attrs, .. } = parse2(f_struct).unwrap();
let fields = get_fields(&data);
output.extend(generate_flag_struct(&ident, false, &fields, false));
output.extend(generate_from_to(&ident, false, &fields, false));
output.extend(generate_from_to(&ident, false, &fields, false, &attrs));

// Expected output in a TokenStream format. Commas matter!
let should_output = quote! {
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/codecs/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod compact;
/// own encoding and do not rely on the bitflag struct.
/// - `Bytes` fields and any types containing a `Bytes` field should be placed last to ensure
/// efficient decoding.
#[proc_macro_derive(Compact, attributes(maybe_zero))]
#[proc_macro_derive(Compact, attributes(maybe_zero, compact))]
pub fn derive(input: TokenStream) -> TokenStream {
let is_zstd = false;
compact::derive(input, is_zstd)
Expand Down
1 change: 1 addition & 0 deletions crates/storage/codecs/src/alloy/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use alloy_primitives::{Bytes, ChainId, TxKind, U256};

/// Legacy transaction.
#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
#[compact(crate = "reth_codecs")]
#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))]
#[cfg_attr(test, crate::add_arbitrary_tests(compact))]
pub(crate) struct TxLegacy {
Expand Down
Loading