Skip to content

Commit

Permalink
chore: add codec and serde features to prune types (#14327)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Feb 8, 2025
1 parent d84eab1 commit d56985a
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 51 deletions.
2 changes: 1 addition & 1 deletion crates/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/exex/exex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ serde = [
"rand/serde",
"secp256k1/serde",
"reth-primitives-traits/serde",
"reth-prune-types/serde",
]
25 changes: 20 additions & 5 deletions crates/prune/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
]
8 changes: 4 additions & 4 deletions crates/prune/types/src/checkpoint.rs
Original file line number Diff line number Diff line change
@@ -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<BlockNumber>,
Expand Down
4 changes: 2 additions & 2 deletions crates/prune/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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};

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<Address, PruneMode>);

impl ReceiptsLogPruneConfig {
Expand Down
10 changes: 5 additions & 5 deletions crates/prune/types/src/mode.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
21 changes: 4 additions & 17 deletions crates/prune/types/src/segment.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
45 changes: 30 additions & 15 deletions crates/prune/types/src/target.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<PruneMode>,
/// 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<PruneMode>,
/// 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::<MINIMUM_PRUNING_DISTANCE, _>"
#[cfg_attr(
any(test, feature = "serde"),
serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::<MINIMUM_PRUNING_DISTANCE, _>"
)
)]
pub receipts: Option<PruneMode>,
/// Account History pruning configuration.
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::<MINIMUM_PRUNING_DISTANCE, _>"
#[cfg_attr(
any(test, feature = "serde"),
serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::<MINIMUM_PRUNING_DISTANCE, _>"
)
)]
pub account_history: Option<PruneMode>,
/// Storage History pruning configuration.
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::<MINIMUM_PRUNING_DISTANCE, _>"
#[cfg_attr(
any(test, feature = "serde"),
serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::<MINIMUM_PRUNING_DISTANCE, _>"
)
)]
pub storage_history: Option<PruneMode>,
/// Receipts pruning configuration by retaining only those receipts that contain logs emitted
Expand Down Expand Up @@ -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<Option<PruneMode>, D::Error> {
use serde::Deserialize;
let prune_mode = Option::<PruneMode>::deserialize(deserializer)?;

match prune_mode {
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/db-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion crates/storage/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/storage/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d56985a

Please sign in to comment.