mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
Refactor: use fully-qualified paths in Compact derives(Deon Branch) (#12279)
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
@ -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.
|
||||
@ -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()
|
||||
}
|
||||
@ -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() {
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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![];
|
||||
|
||||
@ -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,
|
||||
@ -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 {
|
||||
@ -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
|
||||
}
|
||||
};
|
||||
|
||||
@ -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);
|
||||
@ -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! {
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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()
|
||||
}
|
||||
|
||||
@ -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! {
|
||||
@ -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);
|
||||
@ -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;
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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>,
|
||||
@ -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>,
|
||||
@ -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,
|
||||
|
||||
@ -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,
|
||||
@ -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>,
|
||||
}
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -662,7 +662,8 @@ mod tests {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Compact, arbitrary::Arbitrary)]
|
||||
#[add_arbitrary_tests(compact)]
|
||||
#[add_arbitrary_tests(crate, compact)]
|
||||
#[reth_codecs(crate = "crate")]
|
||||
struct TestStruct {
|
||||
f_u64: u64,
|
||||
f_u256: U256,
|
||||
@ -714,7 +715,8 @@ mod tests {
|
||||
#[derive(
|
||||
Debug, PartialEq, Clone, Default, Serialize, Deserialize, Compact, arbitrary::Arbitrary,
|
||||
)]
|
||||
#[add_arbitrary_tests(compact)]
|
||||
#[add_arbitrary_tests(crate, compact)]
|
||||
#[reth_codecs(crate = "crate")]
|
||||
enum TestEnum {
|
||||
#[default]
|
||||
Var0,
|
||||
|
||||
Reference in New Issue
Block a user