snarkos_cli/commands/clean.rs
1// Copyright 2024-2025 Aleo Network Foundation
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use snarkos_node::bft::helpers::proposal_cache_path;
17
18use aleo_std::StorageMode;
19use anyhow::{Result, bail};
20use clap::Parser;
21use colored::Colorize;
22use std::path::PathBuf;
23
24/// Cleans the snarkOS node storage.
25#[derive(Debug, Parser)]
26pub struct Clean {
27 /// Specify the network to remove from storage.
28 #[clap(default_value = "0", long = "network")]
29 pub network: u16,
30 /// Enables development mode, specify the unique ID of the local node to clean.
31 #[clap(long)]
32 pub dev: Option<u16>,
33 /// Specify the path to a directory containing the ledger
34 #[clap(long = "path")]
35 pub path: Option<PathBuf>,
36}
37
38impl Clean {
39 /// Cleans the snarkOS node storage.
40 pub fn parse(self) -> Result<String> {
41 // Initialize the storage mode.
42 let storage_mode = match self.path {
43 Some(path) => StorageMode::Custom(path),
44 None => match self.dev {
45 Some(id) => StorageMode::Development(id),
46 None => StorageMode::Production,
47 },
48 };
49
50 // Remove the current proposal cache file, if it exists.
51 let proposal_cache_path = proposal_cache_path(self.network, &storage_mode);
52 if proposal_cache_path.exists() {
53 if let Err(err) = std::fs::remove_file(&proposal_cache_path) {
54 bail!("Failed to remove the current proposal cache file at {}: {err}", proposal_cache_path.display());
55 }
56 }
57 // Remove the specified ledger from storage.
58 Self::remove_ledger(self.network, &storage_mode)
59 }
60
61 /// Removes the specified ledger from storage.
62 pub(crate) fn remove_ledger(network: u16, mode: &StorageMode) -> Result<String> {
63 // Construct the path to the ledger in storage.
64 let path = aleo_std::aleo_ledger_dir(network, mode);
65
66 // Prepare the path string.
67 let path_string = format!("(in \"{}\")", path.display()).dimmed();
68
69 // Check if the path to the ledger exists in storage.
70 if path.exists() {
71 // Remove the ledger files from storage.
72 match std::fs::remove_dir_all(&path) {
73 Ok(_) => Ok(format!("✅ Cleaned the snarkOS node storage {path_string}")),
74 Err(error) => {
75 bail!("Failed to remove the snarkOS node storage {path_string}\n{}", error.to_string().dimmed())
76 }
77 }
78 } else {
79 Ok(format!("✅ No snarkOS node storage was found {path_string}"))
80 }
81 }
82}