Skip to content

Commit

Permalink
Implements snarkos clean
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Nov 29, 2021
1 parent cf560f3 commit 52d5a7c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 39 deletions.
54 changes: 48 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ path = "./ledger"
version = "2.0.0"

[dependencies.aleo-std]
version = "0.1"
#version = "0.1"
path = "../aleo-std"

[dependencies.anyhow]
version = "1"
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ USAGE:
snarkos [FLAGS] [OPTIONS] [SUBCOMMAND]
FLAGS:
--dev If the flag is set, the node will start in development mode
--display If the flag is set, the node will render a read-only display
-h, --help Prints help information
--norpc If the flag is set, the node will not initialize the RPC server
Expand All @@ -136,14 +137,15 @@ FLAGS:
OPTIONS:
--connect <connect> Specify the IP address and port of a peer to connect to
--miner <miner> Specify this as a mining node, with the given miner address
-n, --network <network> Specify the network of this node [default: 2]
--network <network> Specify the network of this node [default: 2]
--node <node> Specify the IP address and port for the node server [default: 0.0.0.0:4132]
--rpc <rpc> Specify the IP address and port for the RPC server [default: 0.0.0.0:3032]
--password <rpc-password> Specify the password for the RPC server [default: pass]
--username <rpc-username> Specify the username for the RPC server [default: root]
--verbosity <verbosity> Specify the verbosity of the node [options: 0, 1, 2, 3] [default: 2]
SUBCOMMANDS:
clean Removes the ledger files from storage
experimental Experimental features
help Prints this message or the help of the given subcommand(s)
update Updates snarkOS to the latest version
Expand All @@ -153,12 +155,12 @@ SUBCOMMANDS:

In one terminal, start the first node by running:
```
cargo run --release -- --node 0.0.0.0:4135 --rpc 0.0.0.0:3035 --miner aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah
cargo run --release -- --dev --node 0.0.0.0:4135 --rpc 0.0.0.0:3035 --miner aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah
```

After the first node starts, in a second terminal, run:
```
cargo run --release
cargo run --release --dev
```

We welcome all contributions to snarkOS. Please refer to the [license](#7-license) for the terms of contributions.
Expand Down
42 changes: 13 additions & 29 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub struct Node {
/// Specify the verbosity of the node [options: 0, 1, 2, 3]
#[structopt(default_value = "2", long = "verbosity")]
pub verbosity: u8,
/// If the flag is set, the node will start in development mode.
#[structopt(long)]
pub dev: bool,
/// If the flag is set, the node will render a read-only display.
#[structopt(long)]
pub display: bool,
Expand Down Expand Up @@ -98,19 +101,14 @@ impl Node {
}

/// Returns the storage path of the node.
pub(crate) fn storage_path(&self, _local_ip: SocketAddr) -> String {
pub(crate) fn storage_path(&self, _local_ip: SocketAddr) -> PathBuf {
cfg_if::cfg_if! {
if #[cfg(feature = "test")] {
// Tests may use any available ports, and removes the storage artifacts afterwards,
// so that there is no need to adhere to a specific number assignment logic.
format!("/tmp/snarkos-test-ledger-{}", _local_ip.port())
PathBuf::from(format!("/tmp/snarkos-test-ledger-{}", _local_ip.port()))
} else {
// TODO (howardwu): Remove this check after introducing proper configurations.
assert!(
self.node.port() >= 4130,
"Until configuration files are established, the node port must be at least 4130 or greater"
);
format!(".ledger-{}", (self.node.port() - 4130))
aleo_std::aleo_ledger_dir(self.network, self.dev)
}
}
}
Expand Down Expand Up @@ -189,7 +187,7 @@ pub fn initialize_logger(verbosity: u8, log_sender: Option<mpsc::Sender<Vec<u8>>

#[derive(StructOpt, Debug)]
pub enum Command {
#[structopt(name = "clean", about = "Removes the ledger from storage")]
#[structopt(name = "clean", about = "Removes the ledger files from storage")]
Clean(Clean),
#[structopt(name = "update", about = "Updates snarkOS to the latest version")]
Update(Update),
Expand Down Expand Up @@ -257,31 +255,18 @@ impl Clean {

/// Removes the specified ledger from storage.
fn remove_ledger(network: u16, is_dev: bool) -> Result<String> {
// Retrieve the starting directory.
let mut path = match is_dev {
// In development mode, the ledger is stored in the snarkOS root directory.
true => PathBuf::from(env!("CARGO_MANIFEST_DIR")),
// In production mode, the ledger is stored in the `~/.aleo/` directory.
false => aleo_std::aleo_dir()
};

// Construct the path to the ledger in storage.
match is_dev {
// In development mode, the ledger files are stored in a hidden folder.
true => path.push(format!(".ledger-{}", network)),
// In production mode, the ledger files are stored in a visible folder.
false => {
path.push("storage");
path.push(format!("ledger-{}", network));
}
};

let path = aleo_std::aleo_ledger_dir(network, is_dev);
// Check if the path to the ledger exists in storage.
if path.exists() {
// Remove the ledger files from storage.
match std::fs::remove_dir_all(&path) {
Ok(_) => Ok(format!("Successfully removed the ledger files from storage. ({})", path.display())),
Err(error) => Err(anyhow!("Failed to remove the ledger files from storage. ({})\n{}", path.display(), error))
Err(error) => Err(anyhow!(
"Failed to remove the ledger files from storage. ({})\n{}",
path.display(),
error
)),
}
} else {
Ok(format!("No ledger files were found in storage. ({})", path.display()))
Expand All @@ -294,7 +279,6 @@ pub struct Update {
/// Lists all available versions of snarkOS
#[structopt(short = "l", long)]
list: bool,

/// Suppress outputs to terminal
#[structopt(short = "q", long)]
quiet: bool,
Expand Down

0 comments on commit 52d5a7c

Please sign in to comment.