forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprune.rs
More file actions
121 lines (107 loc) · 4.27 KB
/
prune.rs
File metadata and controls
121 lines (107 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! Command that prunes the OP proofs storage.
use clap::Parser;
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_commands::common::{AccessRights, CliNodeTypes, Environment, EnvironmentArgs};
use reth_node_core::version::version_metadata;
use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_primitives::OpPrimitives;
use reth_optimism_node::args::ProofsStorageVersion;
use reth_optimism_trie::{
OpProofStoragePruner, OpProofsProviderRO, OpProofsStore,
db::{MdbxProofsStorage, MdbxProofsStorageV2},
};
use std::{path::PathBuf, sync::Arc};
use tracing::info;
/// Prunes the proofs storage by removing old proof history and state updates.
#[derive(Debug, Parser)]
pub struct PruneCommand<C: ChainSpecParser> {
#[command(flatten)]
env: EnvironmentArgs<C>,
/// The path to the storage DB for proofs history.
#[arg(
long = "proofs-history.storage-path",
value_name = "PROOFS_HISTORY_STORAGE_PATH",
required = true
)]
pub storage_path: PathBuf,
/// The window to span blocks for proofs history. Value is the number of blocks.
/// Default is 1 month of blocks based on 2 seconds block time.
/// 30 * 24 * 60 * 60 / 2 = `1_296_000`
#[arg(
long = "proofs-history.window",
default_value_t = 1_296_000,
value_name = "PROOFS_HISTORY_WINDOW"
)]
pub proofs_history_window: u64,
/// The batch size for pruning operations.
#[arg(
long = "proofs-history.prune-batch-size",
default_value_t = 1000,
value_name = "PROOFS_HISTORY_PRUNE_BATCH_SIZE"
)]
pub proofs_history_prune_batch_size: u64,
/// Storage schema version. Must match the version used when starting the node.
#[arg(
long = "proofs-history.storage-version",
value_name = "PROOFS_HISTORY_STORAGE_VERSION",
default_value = "v1"
)]
pub storage_version: ProofsStorageVersion,
}
impl<C: ChainSpecParser<ChainSpec = OpChainSpec>> PruneCommand<C> {
/// Execute [`PruneCommand`].
pub async fn execute<N: CliNodeTypes<ChainSpec = C::ChainSpec, Primitives = OpPrimitives>>(
self,
runtime: reth_tasks::Runtime,
) -> eyre::Result<()> {
info!(target: "reth::cli", "reth {} starting", version_metadata().short_version);
info!(target: "reth::cli", "Pruning OP proofs storage at: {:?}", self.storage_path);
// Initialize the environment with read-only access
let Environment { provider_factory, .. } = self.env.init::<N>(AccessRights::RO, runtime)?;
macro_rules! run_prune {
($storage:expr) => {{
let storage = $storage;
let provider_ro = storage.provider_ro()?;
let earliest_block = provider_ro.get_earliest_block_number()?;
let latest_block = provider_ro.get_latest_block_number()?;
info!(
target: "reth::cli",
?earliest_block,
?latest_block,
"Current proofs storage block range"
);
drop(provider_ro);
let pruner = OpProofStoragePruner::new(
storage,
provider_factory,
self.proofs_history_window,
self.proofs_history_prune_batch_size,
);
pruner.run();
}};
}
match self.storage_version {
ProofsStorageVersion::V1 => {
let storage: Arc<MdbxProofsStorage> = Arc::new(
MdbxProofsStorage::new(&self.storage_path)
.map_err(|e| eyre::eyre!("Failed to create MdbxProofsStorage: {e}"))?,
);
run_prune!(storage);
}
ProofsStorageVersion::V2 => {
let storage: Arc<MdbxProofsStorageV2> = Arc::new(
MdbxProofsStorageV2::new(&self.storage_path)
.map_err(|e| eyre::eyre!("Failed to create MdbxProofsStorageV2: {e}"))?,
);
run_prune!(storage);
}
}
Ok(())
}
}
impl<C: ChainSpecParser> PruneCommand<C> {
/// Returns the underlying chain being used to run this command
pub const fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
Some(&self.env.chain)
}
}