snarkos_cli/commands/
clean.rs

1// Copyright 2024 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        // Remove the current proposal cache file, if it exists.
42        let proposal_cache_path = proposal_cache_path(self.network, self.dev);
43        if proposal_cache_path.exists() {
44            if let Err(err) = std::fs::remove_file(&proposal_cache_path) {
45                bail!("Failed to remove the current proposal cache file at {}: {err}", proposal_cache_path.display());
46            }
47        }
48        // Remove the specified ledger from storage.
49        Self::remove_ledger(self.network, match self.path {
50            Some(path) => StorageMode::Custom(path),
51            None => StorageMode::from(self.dev),
52        })
53    }
54
55    /// Removes the specified ledger from storage.
56    pub(crate) fn remove_ledger(network: u16, mode: StorageMode) -> Result<String> {
57        // Construct the path to the ledger in storage.
58        let path = aleo_std::aleo_ledger_dir(network, mode);
59
60        // Prepare the path string.
61        let path_string = format!("(in \"{}\")", path.display()).dimmed();
62
63        // Check if the path to the ledger exists in storage.
64        if path.exists() {
65            // Remove the ledger files from storage.
66            match std::fs::remove_dir_all(&path) {
67                Ok(_) => Ok(format!("✅ Cleaned the snarkOS node storage {path_string}")),
68                Err(error) => {
69                    bail!("Failed to remove the snarkOS node storage {path_string}\n{}", error.to_string().dimmed())
70                }
71            }
72        } else {
73            Ok(format!("✅ No snarkOS node storage was found {path_string}"))
74        }
75    }
76}