snarkos_cli/commands/
clean.rs

1// Copyright (c) 2019-2025 Provable Inc.
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;
17use snarkvm::console::network::{CanaryV0, MainnetV0, Network};
18
19use aleo_std::StorageMode;
20use anyhow::{Result, bail};
21use clap::Parser;
22use colored::Colorize;
23use std::path::PathBuf;
24
25/// Cleans the snarkOS node storage.
26#[derive(Debug, Parser)]
27pub struct Clean {
28    /// Specify the network to remove from storage (0 = mainnet, 1 = testnet, 2 = canary)
29    #[clap(default_value_t=MainnetV0::ID, long = "network", value_parser = clap::value_parser!(u16).range((MainnetV0::ID as i64)..=(CanaryV0::ID as i64)))]
30    pub network: u16,
31    /// Enables development mode, specify the unique ID of the local node to clean.
32    #[clap(long)]
33    pub dev: Option<u16>,
34    /// Specify the path to a directory containing the ledger. Overrides the default path (also for
35    /// dev).
36    #[clap(long = "path")]
37    pub path: Option<PathBuf>,
38}
39
40impl Clean {
41    /// Cleans the snarkOS node storage.
42    pub fn parse(self) -> Result<String> {
43        // Initialize the storage mode.
44        let storage_mode = match self.path {
45            Some(path) => StorageMode::Custom(path),
46            None => match self.dev {
47                Some(id) => StorageMode::Development(id),
48                None => StorageMode::Production,
49            },
50        };
51
52        // Remove the current proposal cache file, if it exists.
53        let proposal_cache_path = proposal_cache_path(self.network, &storage_mode);
54        if proposal_cache_path.exists() {
55            if let Err(err) = std::fs::remove_file(&proposal_cache_path) {
56                bail!("Failed to remove the current proposal cache file at {}: {err}", proposal_cache_path.display());
57            }
58        }
59        // Remove the specified ledger from storage.
60        Self::remove_ledger(self.network, &storage_mode)
61    }
62
63    /// Removes the specified ledger from storage.
64    pub(crate) fn remove_ledger(network: u16, mode: &StorageMode) -> Result<String> {
65        // Construct the path to the ledger in storage.
66        let path = aleo_std::aleo_ledger_dir(network, mode);
67
68        // Prepare the path string.
69        let path_string = format!("(in \"{}\")", path.display()).dimmed();
70
71        // Check if the path to the ledger exists in storage.
72        if path.exists() {
73            // Remove the ledger files from storage.
74            match std::fs::remove_dir_all(&path) {
75                Ok(_) => Ok(format!("✅ Cleaned the snarkOS node storage {path_string}")),
76                Err(error) => {
77                    bail!("Failed to remove the snarkOS node storage {path_string}\n{}", error.to_string().dimmed())
78                }
79            }
80        } else {
81            Ok(format!("✅ No snarkOS node storage was found {path_string}"))
82        }
83    }
84}