Skip to content

Commit

Permalink
feat(primitives): increase minimum pruning distance (#4750)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Sep 28, 2023
1 parent 6d7dacf commit 0adc856
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions bin/reth/src/args/pruning_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::sync::Arc;
#[derive(Debug, Args, PartialEq, Default)]
#[command(next_help_heading = "Pruning")]
pub struct PruningArgs {
/// Run full node. Only the most recent 128 block states are stored. This flag takes
/// priority over pruning configuration in reth.toml.
/// Run full node. Only the most recent [`MINIMUM_PRUNING_DISTANCE`] block states are stored.
/// This flag takes priority over pruning configuration in reth.toml.
#[arg(long, default_value_t = false)]
pub full: bool,
}
Expand Down
5 changes: 2 additions & 3 deletions crates/primitives/src/prune/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mod tests {

#[test]
fn test_prune_target_block() {
let tip = 1000;
let tip = 20000;
let min_blocks = MINIMUM_PRUNING_DISTANCE;
let prune_part = PrunePart::Receipts;

Expand All @@ -91,7 +91,6 @@ mod tests {
PruneMode::Before(tip - MINIMUM_PRUNING_DISTANCE - 1),
Ok(Some(tip - MINIMUM_PRUNING_DISTANCE - 2)),
),
// MINIMUM_PRUNING_DISTANCE is 128
(PruneMode::Before(tip - 1), Err(PrunePartError::Configuration(prune_part))),
];

Expand All @@ -113,7 +112,7 @@ mod tests {

#[test]
fn test_should_prune() {
let tip = 1000;
let tip = 20000;
let should_prune = true;

let tests = vec![
Expand Down
20 changes: 12 additions & 8 deletions crates/primitives/src/prune/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ use crate::{
use paste::paste;
use serde::{Deserialize, Serialize};

/// Minimum distance necessary from the tip so blockchain tree can work correctly.
pub const MINIMUM_PRUNING_DISTANCE: u64 = 128;
/// 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
/// consensus protocol.
/// 2. Another 10k blocks to have a room for maneuver in case when things go wrong and a manual
/// unwind is required.
pub const MINIMUM_PRUNING_DISTANCE: u64 = 32 * 2 + 10_000;

/// Pruning configuration for every part of the data that can be pruned.
#[derive(Debug, Clone, Default, Deserialize, Eq, PartialEq, Serialize)]
Expand All @@ -22,19 +26,19 @@ pub struct PruneModes {
/// and offers improved performance.
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_opt_prune_mode_with_min_blocks::<64, _>"
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::<64, _>"
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::<64, _>"
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 @@ -101,8 +105,8 @@ impl PruneModes {
impl_prune_parts!(
(sender_recovery, SenderRecovery, None),
(transaction_lookup, TransactionLookup, None),
(receipts, Receipts, Some(64)),
(account_history, AccountHistory, Some(64)),
(storage_history, StorageHistory, Some(64))
(receipts, Receipts, Some(MINIMUM_PRUNING_DISTANCE)),
(account_history, AccountHistory, Some(MINIMUM_PRUNING_DISTANCE)),
(storage_history, StorageHistory, Some(MINIMUM_PRUNING_DISTANCE))
);
}
15 changes: 10 additions & 5 deletions crates/prune/src/pruner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ impl<DB: Database> Pruner<DB> {
tip_block_number: BlockNumber,
) -> PrunerResult {
// Contract log filtering removes every receipt possible except the ones in the list. So,
// for the other receipts it's as if they had a `PruneMode::Distance()` of 128.
// for the other receipts it's as if they had a `PruneMode::Distance()` of
// `MINIMUM_PRUNING_DISTANCE`.
let to_block = PruneMode::Distance(MINIMUM_PRUNING_DISTANCE)
.prune_target_block(
tip_block_number,
Expand Down Expand Up @@ -1523,15 +1524,19 @@ mod tests {
let tx = TestTransaction::default();
let mut rng = generators::rng();

let tip = 300;
let blocks = random_block_range(&mut rng, 0..=tip, B256::ZERO, 1..5);
let tip = 20000;
let blocks = [
random_block_range(&mut rng, 0..=100, B256::ZERO, 1..5),
random_block_range(&mut rng, (100 + 1)..=(tip - 100), B256::ZERO, 0..1),
random_block_range(&mut rng, (tip - 100 + 1)..=tip, B256::ZERO, 1..5),
]
.concat();
tx.insert_blocks(blocks.iter(), None).expect("insert blocks");

let mut receipts = Vec::new();

let (deposit_contract_addr, _) = random_eoa_account(&mut rng);
for block in &blocks {
assert!(!block.body.is_empty());
for (txi, transaction) in block.body.iter().enumerate() {
let mut receipt = random_receipt(&mut rng, transaction, Some(1));
receipt.logs.push(random_log(
Expand Down Expand Up @@ -1569,7 +1574,7 @@ mod tests {
..Default::default()
},
// Less than total amount of blocks to prune to test the batching logic
PruneBatchSizes::default().with_storage_history(10),
PruneBatchSizes::default().with_receipts(10),
);

let result = pruner.prune_receipts_by_logs(&provider, tip);
Expand Down
6 changes: 3 additions & 3 deletions crates/stages/src/stages/index_account_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ mod tests {
.unwrap();

// run
let input = ExecInput { target: Some(100), ..Default::default() };
let input = ExecInput { target: Some(20000), ..Default::default() };
let mut stage = IndexAccountHistoryStage {
prune_modes: PruneModes {
account_history: Some(PruneMode::Before(36)),
Expand All @@ -435,15 +435,15 @@ mod tests {
let factory = ProviderFactory::new(tx.tx.as_ref(), MAINNET.clone());
let provider = factory.provider_rw().unwrap();
let out = stage.execute(&provider, input).await.unwrap();
assert_eq!(out, ExecOutput { checkpoint: StageCheckpoint::new(100), done: true });
assert_eq!(out, ExecOutput { checkpoint: StageCheckpoint::new(20000), done: true });
provider.commit().unwrap();

// verify
let table = cast(tx.table::<tables::AccountHistory>().unwrap());
assert_eq!(table, BTreeMap::from([(shard(u64::MAX), vec![36, 100])]));

// unwind
unwind(&tx, 100, 0).await;
unwind(&tx, 20000, 0).await;

// verify initial state
let table = tx.table::<tables::AccountHistory>().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions crates/stages/src/stages/index_storage_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ mod tests {
.unwrap();

// run
let input = ExecInput { target: Some(100), ..Default::default() };
let input = ExecInput { target: Some(20000), ..Default::default() };
let mut stage = IndexStorageHistoryStage {
prune_modes: PruneModes {
storage_history: Some(PruneMode::Before(36)),
Expand All @@ -448,15 +448,15 @@ mod tests {
let factory = ProviderFactory::new(tx.tx.as_ref(), MAINNET.clone());
let provider = factory.provider_rw().unwrap();
let out = stage.execute(&provider, input).await.unwrap();
assert_eq!(out, ExecOutput { checkpoint: StageCheckpoint::new(100), done: true });
assert_eq!(out, ExecOutput { checkpoint: StageCheckpoint::new(20000), done: true });
provider.commit().unwrap();

// verify
let table = cast(tx.table::<tables::StorageHistory>().unwrap());
assert_eq!(table, BTreeMap::from([(shard(u64::MAX), vec![36, 100]),]));

// unwind
unwind(&tx, 100, 0).await;
unwind(&tx, 20000, 0).await;

// verify initial state
let table = tx.table::<tables::StorageHistory>().unwrap();
Expand Down

0 comments on commit 0adc856

Please sign in to comment.