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

feat(config, primitives): validate Receipts prune part #3587

Merged
merged 3 commits into from
Jul 10, 2023
Merged
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
7 changes: 5 additions & 2 deletions crates/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use reth_downloaders::{
headers::reverse_headers::ReverseHeadersDownloaderBuilder,
};
use reth_network::{NetworkConfigBuilder, PeersConfig, SessionsConfig};
use reth_primitives::PruneMode;
use reth_primitives::{serde_helper::deserialize_opt_prune_mode_with_min_distance, PruneMode};
use secp256k1::SecretKey;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
Expand Down Expand Up @@ -305,7 +305,10 @@ pub struct PruneParts {
#[serde(skip_serializing_if = "Option::is_none")]
pub transaction_lookup: Option<PruneMode>,
/// Receipts pruning configuration.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_opt_prune_mode_with_min_distance::<64, _>"
)]
pub receipts: Option<PruneMode>,
/// Account History pruning configuration.
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/src/serde_helper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::H256;
pub use jsonu256::*;

pub mod num;
mod prune;
pub use prune::deserialize_opt_prune_mode_with_min_distance;

/// serde functions for handling primitive `u64` as [U64](crate::U64)
pub mod u64_hex {
Expand Down
49 changes: 49 additions & 0 deletions crates/primitives/src/serde_helper/prune.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::PruneMode;
use serde::{Deserialize, Deserializer};

/// Deserializes [`Option<PruneMode>`] and validates that the value contained in
/// [PruneMode::Distance] (if any) is not less than the const generic parameter `MIN_DISTANCE`.
pub fn deserialize_opt_prune_mode_with_min_distance<
'de,
const MIN_DISTANCE: u64,
D: Deserializer<'de>,
>(
deserializer: D,
) -> Result<Option<PruneMode>, D::Error> {
let prune_mode = Option::<PruneMode>::deserialize(deserializer)?;

match prune_mode {
Some(PruneMode::Distance(distance)) if distance < MIN_DISTANCE => {
Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Unsigned(distance),
// This message should have "expected" wording, so we say "not less than"
&format!("prune mode distance not less than {MIN_DISTANCE} blocks").as_str(),
))
}
_ => Ok(prune_mode),
}
}

#[cfg(test)]
mod test {
use crate::PruneMode;
use assert_matches::assert_matches;
use serde::Deserialize;

#[test]
fn deserialize_opt_prune_mode_with_min_distance() {
#[derive(Debug, Deserialize, PartialEq, Eq)]
struct V(
#[serde(
deserialize_with = "super::deserialize_opt_prune_mode_with_min_distance::<10, _>"
)]
Option<PruneMode>,
);

assert!(serde_json::from_str::<V>(r#"{"distance": 10}"#).is_ok());
assert_matches!(
serde_json::from_str::<V>(r#"{"distance": 9}"#),
Err(err) if err.to_string() == "invalid value: integer `9`, expected prune mode distance not less than 10 blocks"
);
}
}