Skip to content

Commit

Permalink
Refactor: use fully-qualified paths in Compact derives(Deon Branch) (#…
Browse files Browse the repository at this point in the history
…12279)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
DanielEmmanuel1 and mattsse authored Nov 3, 2024
1 parent 4e3b32c commit 61f19ab
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 34 deletions.
5 changes: 2 additions & 3 deletions crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use alloy_rlp::{length_of_length, Decodable, Encodable, RlpDecodable, RlpEncodab
use bytes::{Buf, BufMut};
use core::{cmp::Ordering, ops::Deref};
use derive_more::{DerefMut, From, IntoIterator};
#[cfg(feature = "reth-codec")]
use reth_codecs::Compact;
use serde::{Deserialize, Serialize};

/// Receipt containing result of transaction execution.
Expand Down Expand Up @@ -91,7 +89,7 @@ impl Receipts {
self.receipt_vec.len()
}

/// Returns `true` if the `Receipts` vector is empty.
/// Returns true if the `Receipts` vector is empty.
pub fn is_empty(&self) -> bool {
self.receipt_vec.is_empty()
}
Expand Down Expand Up @@ -518,6 +516,7 @@ mod tests {
use super::*;
use crate::revm_primitives::Bytes;
use alloy_primitives::{address, b256, bytes, hex_literal::hex};
use reth_codecs::Compact;

#[test]
fn test_decode_receipt() {
Expand Down
3 changes: 0 additions & 3 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ use tx_type::{
COMPACT_IDENTIFIER_LEGACY,
};

#[cfg(test)]
use reth_codecs::Compact;

use alloc::vec::Vec;

/// Either a transaction hash or number.
Expand Down
3 changes: 0 additions & 3 deletions crates/primitives/src/transaction/tx_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use alloy_primitives::{U64, U8};
use alloy_rlp::{Decodable, Encodable};
use serde::{Deserialize, Serialize};

#[cfg(test)]
use reth_codecs::Compact;

/// Identifier parameter for legacy transaction
#[cfg(any(test, feature = "reth-codec"))]
pub(crate) const COMPACT_IDENTIFIER_LEGACY: usize = 0;
Expand Down
20 changes: 18 additions & 2 deletions crates/storage/codecs/derive/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@ pub fn maybe_generate_tests(
let mut traits = vec![];
let mut roundtrips = vec![];
let mut additional_tests = vec![];
let mut is_crate = false;

for arg in args {
let mut iter = args.into_iter().peekable();

// we check if there's a crate argument which is used from inside the codecs crate directly
if let Some(arg) = iter.peek() {
if arg.to_string() == "crate" {
is_crate = true;
iter.next();
}
}

for arg in iter {
if arg.to_string() == "compact" {
traits.push(quote! { use super::Compact; });
let path = if is_crate {
quote! { use crate::Compact; }
} else {
quote! { use reth_codecs::Compact; }
};
traits.push(path);
roundtrips.push(quote! {
{
let mut buf = vec![];
Expand Down
33 changes: 30 additions & 3 deletions crates/storage/codecs/derive/src/compact/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

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

/// Generates code to implement the `Compact` trait for a data type.
pub fn generate_from_to(
ident: &Ident,
attrs: &[Attribute],
has_lifetime: bool,
fields: &FieldList,
is_zstd: bool,
Expand All @@ -20,6 +22,8 @@ pub fn generate_from_to(
let fuzz = format_ident!("fuzz_test_{snake_case_ident}");
let test = format_ident!("fuzz_{snake_case_ident}");

let reth_codecs = parse_reth_codecs_path(attrs).unwrap();

let lifetime = if has_lifetime {
quote! { 'a }
} else {
Expand All @@ -28,11 +32,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 All @@ -53,6 +57,7 @@ pub fn generate_from_to(
#[allow(dead_code)]
#[test_fuzz::test_fuzz]
fn #fuzz(obj: #ident) {
use #reth_codecs::Compact;
let mut buf = vec![];
let len = obj.clone().to_compact(&mut buf);
let (same_obj, buf) = #ident::from_compact(buf.as_ref(), len);
Expand Down Expand Up @@ -191,7 +196,7 @@ fn generate_to_compact(fields: &FieldList, ident: &Ident, is_zstd: bool) -> Vec<
}

// Just because a type supports compression, doesn't mean all its values are to be compressed.
// We skip the smaller ones, and thus require a flag `__zstd` to specify if this value is
// We skip the smaller ones, and thus require a flag` __zstd` to specify if this value is
// compressed or not.
if is_zstd {
lines.push(quote! {
Expand Down Expand Up @@ -232,3 +237,25 @@ fn generate_to_compact(fields: &FieldList, ident: &Ident, is_zstd: bool) -> Vec<

lines
}

/// Function to extract the crate path from `reth_codecs(crate = "...")` attribute.
fn parse_reth_codecs_path(attrs: &[Attribute]) -> syn::Result<syn::Path> {
// let default_crate_path: syn::Path = syn::parse_str("reth-codecs").unwrap();
let mut reth_codecs_path: syn::Path = syn::parse_quote!(reth_codecs);
for attr in attrs {
if attr.path().is_ident("reth_codecs") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("crate") {
let value = meta.value()?;
let lit: LitStr = value.parse()?;
reth_codecs_path = syn::parse_str(&lit.value())?;
Ok(())
} else {
Err(meta.error("unsupported attribute"))
}
})?;
}
}

Ok(reth_codecs_path)
}
11 changes: 6 additions & 5 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, &attrs, has_lifetime, &fields, is_zstd));
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, &attrs, false, &fields, false));

// Expected output in a TokenStream format. Commas matter!
let should_output = quote! {
Expand Down Expand Up @@ -285,6 +285,7 @@ mod tests {
#[allow(dead_code)]
#[test_fuzz::test_fuzz]
fn fuzz_test_test_struct(obj: TestStruct) {
use reth_codecs::Compact;
let mut buf = vec![];
let len = obj.clone().to_compact(&mut buf);
let (same_obj, buf) = TestStruct::from_compact(buf.as_ref(), len);
Expand All @@ -295,7 +296,7 @@ mod tests {
pub fn fuzz_test_struct() {
fuzz_test_test_struct(TestStruct::default())
}
impl Compact for TestStruct {
impl reth_codecs::Compact for TestStruct {
fn to_compact<B>(&self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]> {
let mut flags = TestStructFlags::default();
let mut total_length = 0;
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/codecs/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ 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, reth_codecs))]
pub fn derive(input: TokenStream) -> TokenStream {
let is_zstd = false;
compact::derive(input, is_zstd)
}

/// Adds `zstd` compression to derived [`Compact`].
#[proc_macro_derive(CompactZstd, attributes(maybe_zero))]
#[proc_macro_derive(CompactZstd, attributes(maybe_zero, reth_codecs))]
pub fn derive_zstd(input: TokenStream) -> TokenStream {
let is_zstd = true;
compact::derive(input, is_zstd)
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/codecs/src/alloy/authorization_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use reth_codecs_derive::add_arbitrary_tests;
///
/// Notice: Make sure this struct is 1:1 with `alloy_eips::eip7702::Authorization`
#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[add_arbitrary_tests(compact)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct Authorization {
chain_id: u64,
address: Address,
Expand Down
10 changes: 7 additions & 3 deletions crates/storage/codecs/src/alloy/genesis_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use reth_codecs_derive::add_arbitrary_tests;
///
/// Notice: Make sure this struct is 1:1 with `alloy_genesis::GenesisAccount`
#[derive(Debug, Clone, PartialEq, Eq, Compact)]
#[reth_codecs(crate = "crate")]
pub(crate) struct GenesisAccountRef<'a> {
/// The nonce of the account at genesis.
nonce: Option<u64>,
Expand All @@ -27,12 +28,13 @@ pub(crate) struct GenesisAccountRef<'a> {
/// Acts as bridge which simplifies Compact implementation for
/// `AlloyGenesisAccount`.
#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[add_arbitrary_tests(compact)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct GenesisAccount {
/// The nonce of the account at genesis.
nonce: Option<u64>,
Expand All @@ -47,21 +49,23 @@ pub(crate) struct GenesisAccount {
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[add_arbitrary_tests(compact)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct StorageEntries {
entries: Vec<StorageEntry>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[add_arbitrary_tests(compact)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct StorageEntry {
key: B256,
value: B256,
Expand Down
2 changes: 2 additions & 0 deletions crates/storage/codecs/src/alloy/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use alloy_primitives::{Address, BlockNumber, Bloom, Bytes, B256, U256};
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)]
#[reth_codecs(crate = "crate")]
pub(crate) struct Header {
parent_hash: B256,
ommers_hash: B256,
Expand Down Expand Up @@ -54,6 +55,7 @@ pub(crate) struct Header {
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)]
#[reth_codecs(crate = "crate")]
pub(crate) struct HeaderExt {
requests_hash: Option<B256>,
}
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/codecs/src/alloy/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use alloy_primitives::{Bytes, ChainId, TxKind, U256};
///
/// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip1559`]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Compact, Default)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(any(test, feature = "test-utils"), crate::add_arbitrary_tests(compact))]
#[cfg_attr(any(test, feature = "test-utils"), crate::add_arbitrary_tests(crate, compact))]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
pub(crate) struct TxEip1559 {
chain_id: ChainId,
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/codecs/src/alloy/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ use reth_codecs_derive::add_arbitrary_tests;
///
/// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip2930`]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[add_arbitrary_tests(compact)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct TxEip2930 {
chain_id: ChainId,
nonce: u64,
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/codecs/src/alloy/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ use reth_codecs_derive::add_arbitrary_tests;
///
/// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip4844`]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(any(test, feature = "test-utils"), derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[add_arbitrary_tests(compact)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct TxEip4844 {
chain_id: ChainId,
nonce: u64,
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/codecs/src/alloy/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ use reth_codecs_derive::add_arbitrary_tests;
///
/// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip7702`]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[add_arbitrary_tests(compact)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct TxEip7702 {
chain_id: ChainId,
nonce: u64,
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/codecs/src/alloy/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use alloy_primitives::{Bytes, ChainId, TxKind, U256};

/// Legacy transaction.
#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize),
crate::add_arbitrary_tests(compact)
crate::add_arbitrary_tests(crate, compact)
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
pub(crate) struct TxLegacy {
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/codecs/src/alloy/transaction/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use reth_codecs_derive::add_arbitrary_tests;
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[add_arbitrary_tests(compact)]
#[reth_codecs(crate = "crate")]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct TxDeposit {
source_hash: B256,
from: Address,
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/codecs/src/alloy/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use reth_codecs_derive::add_arbitrary_tests;
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[add_arbitrary_tests(compact)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct Withdrawal {
/// Monotonically increasing identifier issued by consensus layer.
index: u64,
Expand Down
Loading

0 comments on commit 61f19ab

Please sign in to comment.