From d56985a29802dc1fac24e08f43227f5efb74a7da Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 8 Feb 2025 16:52:38 +0100 Subject: [PATCH] chore: add codec and serde features to prune types (#14327) --- crates/config/Cargo.toml | 2 +- crates/exex/exex/Cargo.toml | 1 + crates/prune/types/Cargo.toml | 25 ++++++++++++---- crates/prune/types/src/checkpoint.rs | 8 ++--- crates/prune/types/src/lib.rs | 4 +-- crates/prune/types/src/mode.rs | 10 +++---- crates/prune/types/src/segment.rs | 21 +++---------- crates/prune/types/src/target.rs | 45 ++++++++++++++++++---------- crates/storage/db-api/Cargo.toml | 2 +- crates/storage/db/Cargo.toml | 2 +- crates/storage/provider/Cargo.toml | 1 + 11 files changed, 70 insertions(+), 51 deletions(-) diff --git a/crates/config/Cargo.toml b/crates/config/Cargo.toml index 0232aecc6..d7e7f3c4e 100644 --- a/crates/config/Cargo.toml +++ b/crates/config/Cargo.toml @@ -13,7 +13,7 @@ workspace = true [dependencies] # reth reth-network-types = { workspace = true, features = ["serde"] } -reth-prune-types.workspace = true +reth-prune-types = { workspace = true, features = ["serde"] } reth-stages-types = { workspace = true, features = ["serde"] } # serde diff --git a/crates/exex/exex/Cargo.toml b/crates/exex/exex/Cargo.toml index ec40b1c1a..81992af8d 100644 --- a/crates/exex/exex/Cargo.toml +++ b/crates/exex/exex/Cargo.toml @@ -78,4 +78,5 @@ serde = [ "rand/serde", "secp256k1/serde", "reth-primitives-traits/serde", + "reth-prune-types/serde", ] diff --git a/crates/prune/types/Cargo.toml b/crates/prune/types/Cargo.toml index 7764fd837..c6e645158 100644 --- a/crates/prune/types/Cargo.toml +++ b/crates/prune/types/Cargo.toml @@ -12,16 +12,22 @@ description = "Commonly used types for prune usage in reth." workspace = true [dependencies] -reth-codecs.workspace = true +reth-codecs = { workspace = true, optional = true } alloy-primitives.workspace = true derive_more.workspace = true -modular-bitfield.workspace = true -serde.workspace = true thiserror.workspace = true + +modular-bitfield = { workspace = true, optional = true } +serde = { workspace = true, features = ["derive"], optional = true } arbitrary = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] +reth-codecs.workspace = true + +alloy-primitives = { workspace = true, features = ["serde"] } +serde.workspace = true +modular-bitfield.workspace = true arbitrary = { workspace = true, features = ["derive"] } assert_matches.workspace = true proptest.workspace = true @@ -33,9 +39,18 @@ toml.workspace = true [features] test-utils = [ "dep:arbitrary", - "reth-codecs/test-utils", + "reth-codecs?/test-utils", ] arbitrary = [ "alloy-primitives/arbitrary", - "reth-codecs/arbitrary", + "reth-codecs?/arbitrary", +] +reth-codec = [ + "dep:reth-codecs", + "dep:modular-bitfield", +] +serde = [ + "dep:serde", + "alloy-primitives/serde", + "reth-codecs?/serde", ] diff --git a/crates/prune/types/src/checkpoint.rs b/crates/prune/types/src/checkpoint.rs index e0397c5af..7b6102806 100644 --- a/crates/prune/types/src/checkpoint.rs +++ b/crates/prune/types/src/checkpoint.rs @@ -1,12 +1,12 @@ use crate::PruneMode; use alloy_primitives::{BlockNumber, TxNumber}; -use reth_codecs::{add_arbitrary_tests, Compact}; -use serde::{Deserialize, Serialize}; /// Saves the pruning progress of a stage. -#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Compact)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[cfg_attr(any(test, feature = "reth-codec"), derive(reth_codecs::Compact))] +#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))] #[cfg_attr(any(test, feature = "test-utils"), derive(Default, arbitrary::Arbitrary))] -#[add_arbitrary_tests(compact)] +#[cfg_attr(any(test, feature = "serde"), derive(serde::Serialize, serde::Deserialize))] pub struct PruneCheckpoint { /// Highest pruned block number. If it's [None], the pruning for block `0` is not finished yet. pub block_number: Option, diff --git a/crates/prune/types/src/lib.rs b/crates/prune/types/src/lib.rs index bbf2cfe5f..54c62b42d 100644 --- a/crates/prune/types/src/lib.rs +++ b/crates/prune/types/src/lib.rs @@ -23,7 +23,6 @@ pub use pruner::{ SegmentOutputCheckpoint, }; pub use segment::{PrunePurpose, PruneSegment, PruneSegmentError}; -use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; pub use target::{PruneModes, MINIMUM_PRUNING_DISTANCE}; @@ -31,7 +30,8 @@ use alloy_primitives::{Address, BlockNumber}; use std::ops::Deref; /// Configuration for pruning receipts not associated with logs emitted by the specified contracts. -#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Default)] +#[cfg_attr(any(test, feature = "serde"), derive(serde::Serialize, serde::Deserialize))] pub struct ReceiptsLogPruneConfig(pub BTreeMap); impl ReceiptsLogPruneConfig { diff --git a/crates/prune/types/src/mode.rs b/crates/prune/types/src/mode.rs index 75194e01b..42d34b30c 100644 --- a/crates/prune/types/src/mode.rs +++ b/crates/prune/types/src/mode.rs @@ -1,13 +1,13 @@ use crate::{segment::PrunePurpose, PruneSegment, PruneSegmentError}; use alloy_primitives::BlockNumber; -use reth_codecs::{add_arbitrary_tests, Compact}; -use serde::{Deserialize, Serialize}; /// Prune mode. -#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Compact)] -#[serde(rename_all = "lowercase")] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[cfg_attr(any(test, feature = "test-utils"), derive(arbitrary::Arbitrary))] -#[add_arbitrary_tests(compact)] +#[cfg_attr(any(test, feature = "reth-codec"), derive(reth_codecs::Compact))] +#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))] +#[cfg_attr(any(test, feature = "serde"), derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(any(test, feature = "serde"), serde(rename_all = "lowercase"))] pub enum PruneMode { /// Prune all blocks. Full, diff --git a/crates/prune/types/src/segment.rs b/crates/prune/types/src/segment.rs index e0b73aab7..443acf1ed 100644 --- a/crates/prune/types/src/segment.rs +++ b/crates/prune/types/src/segment.rs @@ -1,26 +1,13 @@ use crate::MINIMUM_PRUNING_DISTANCE; use derive_more::Display; -use reth_codecs::{add_arbitrary_tests, Compact}; -use serde::{Deserialize, Serialize}; use thiserror::Error; /// Segment of the data that can be pruned. -#[derive( - Debug, - Display, - Clone, - Copy, - Eq, - PartialEq, - Ord, - PartialOrd, - Hash, - Serialize, - Deserialize, - Compact, -)] +#[derive(Debug, Display, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] #[cfg_attr(test, derive(arbitrary::Arbitrary))] -#[add_arbitrary_tests(compact)] +#[cfg_attr(any(test, feature = "reth-codec"), derive(reth_codecs::Compact))] +#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))] +#[cfg_attr(any(test, feature = "serde"), derive(serde::Serialize, serde::Deserialize))] pub enum PruneSegment { /// Prune segment responsible for the `TransactionSenders` table. SenderRecovery, diff --git a/crates/prune/types/src/target.rs b/crates/prune/types/src/target.rs index ca355b394..59ebd803b 100644 --- a/crates/prune/types/src/target.rs +++ b/crates/prune/types/src/target.rs @@ -1,5 +1,4 @@ use crate::{PruneMode, ReceiptsLogPruneConfig}; -use serde::{Deserialize, Deserializer, Serialize}; /// Minimum distance from the tip necessary for the node to work correctly: /// 1. Minimum 2 epochs (32 blocks per epoch) required to handle any reorg according to the @@ -9,32 +8,42 @@ use serde::{Deserialize, Deserializer, Serialize}; pub const MINIMUM_PRUNING_DISTANCE: u64 = 32 * 2 + 10_000; /// Pruning configuration for every segment of the data that can be pruned. -#[derive(Debug, Clone, Default, Deserialize, Eq, PartialEq, Serialize)] -#[serde(default)] +#[derive(Debug, Clone, Default, Eq, PartialEq)] +#[cfg_attr(any(test, feature = "serde"), derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(any(test, feature = "serde"), serde(default))] pub struct PruneModes { /// Sender Recovery pruning configuration. - #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(any(test, feature = "serde"), serde(skip_serializing_if = "Option::is_none"))] pub sender_recovery: Option, /// Transaction Lookup pruning configuration. - #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(any(test, feature = "serde"), serde(skip_serializing_if = "Option::is_none"))] pub transaction_lookup: Option, /// Receipts pruning configuration. This setting overrides `receipts_log_filter` /// and offers improved performance. - #[serde( - skip_serializing_if = "Option::is_none", - deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::" + #[cfg_attr( + any(test, feature = "serde"), + serde( + skip_serializing_if = "Option::is_none", + deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::" + ) )] pub receipts: Option, /// Account History pruning configuration. - #[serde( - skip_serializing_if = "Option::is_none", - deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::" + #[cfg_attr( + any(test, feature = "serde"), + serde( + skip_serializing_if = "Option::is_none", + deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::" + ) )] pub account_history: Option, /// Storage History pruning configuration. - #[serde( - skip_serializing_if = "Option::is_none", - deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::" + #[cfg_attr( + any(test, feature = "serde"), + serde( + skip_serializing_if = "Option::is_none", + deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::" + ) )] pub storage_history: Option, /// Receipts pruning configuration by retaining only those receipts that contain logs emitted @@ -82,9 +91,15 @@ impl PruneModes { /// 2. For [`PruneMode::Distance(distance`)], it fails if `distance < MIN_BLOCKS + 1`. `+ 1` is /// needed because `PruneMode::Distance(0)` means that we leave zero blocks from the latest, /// meaning we have one block in the database. -fn deserialize_opt_prune_mode_with_min_blocks<'de, const MIN_BLOCKS: u64, D: Deserializer<'de>>( +#[cfg(any(test, feature = "serde"))] +fn deserialize_opt_prune_mode_with_min_blocks< + 'de, + const MIN_BLOCKS: u64, + D: serde::Deserializer<'de>, +>( deserializer: D, ) -> Result, D::Error> { + use serde::Deserialize; let prune_mode = Option::::deserialize(deserializer)?; match prune_mode { diff --git a/crates/storage/db-api/Cargo.toml b/crates/storage/db-api/Cargo.toml index 94248c838..3a55380cb 100644 --- a/crates/storage/db-api/Cargo.toml +++ b/crates/storage/db-api/Cargo.toml @@ -17,8 +17,8 @@ reth-codecs.workspace = true reth-db-models.workspace = true reth-primitives = { workspace = true, features = ["reth-codec"] } reth-primitives-traits = { workspace = true, features = ["serde", "reth-codec"] } -reth-prune-types.workspace = true reth-stages-types = { workspace = true, features = ["serde", "reth-codec"] } +reth-prune-types = { workspace = true, features = ["reth-codec"] } reth-storage-errors.workspace = true reth-trie-common.workspace = true diff --git a/crates/storage/db/Cargo.toml b/crates/storage/db/Cargo.toml index 97c386c31..aed1c1235 100644 --- a/crates/storage/db/Cargo.toml +++ b/crates/storage/db/Cargo.toml @@ -19,7 +19,7 @@ reth-primitives-traits = { workspace = true, features = ["reth-codec"] } reth-fs-util.workspace = true reth-storage-errors.workspace = true reth-nippy-jar.workspace = true -reth-prune-types.workspace = true +reth-prune-types = { workspace = true, features = ["reth-codec", "serde"] } reth-stages-types.workspace = true reth-trie-common = { workspace = true, features = ["serde"] } reth-tracing.workspace = true diff --git a/crates/storage/provider/Cargo.toml b/crates/storage/provider/Cargo.toml index 2e2113671..cb9c6b976 100644 --- a/crates/storage/provider/Cargo.toml +++ b/crates/storage/provider/Cargo.toml @@ -100,6 +100,7 @@ serde = [ "reth-trie-db/serde", "reth-trie/serde", "reth-stages-types/serde", + "reth-prune-types/serde", ] test-utils = [ "reth-db/test-utils",