Skip to content

Commit 95737b2

Browse files
hanabi1224elmattic
authored andcommitted
feat: forest-cli db clean (#2191)
1 parent 5880f7c commit 95737b2

File tree

10 files changed

+104
-22
lines changed

10 files changed

+104
-22
lines changed

.github/workflows/rust.yml

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ jobs:
128128
- name: import snapshot and run Forest
129129
run: |
130130
forest --chain calibnet --target-peer-count 50 --encrypt-keystore false --halt-after-import --import-snapshot /tmp/snapshots/*.car
131+
forest-cli --chain calibnet db stats
131132
forest --chain calibnet --target-peer-count 50 --encrypt-keystore false --detach
132133
- name: wait for sync and check health
133134
run: |

Cargo.lock

+15-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fil_actors_runtime = { package = "fil_actors_runtime_common", version = "=9.0
8383
fil_actors_runtime_v9 = { package = "fil_actors_runtime", version = "9.0.0", git = "https://github.com/filecoin-project/builtin-actors", branch = "release/v9" }
8484
filecoin-proofs-api = "12.0"
8585
flume = "0.10"
86+
fs_extra = "1.2"
8687
futures = "0.3"
8788
futures-util = "0.3"
8889
fvm = "2.1"
@@ -96,6 +97,7 @@ fvm_shared = "2.0"
9697
git-version = "0.3"
9798
hex = "0.4"
9899
http = "0.2.8"
100+
human-repr = "1.0"
99101
hyper = "0.14"
100102
hyper-rustls = "0.23"
101103
jsonrpc-v2 = { version = "0.11", default-features = false, features = ["easy-errors", "macros", "bytes-v05"] }

forest/cli/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ forest_rpc-api.workspace = true
3535
forest_rpc-client.workspace = true
3636
forest_state_manager.workspace = true
3737
forest_utils.workspace = true
38+
fs_extra.workspace = true
3839
futures.workspace = true
3940
fvm.workspace = true
4041
fvm_ipld_car.workspace = true
@@ -43,6 +44,7 @@ fvm_shared = { workspace = true, default-features = false
4344
git-version.workspace = true
4445
hex.workspace = true
4546
http.workspace = true
47+
human-repr.workspace = true
4648
jsonrpc-v2.workspace = true
4749
lazy_static.workspace = true
4850
libp2p = { workspace = true, default-features = false, features = ["identify"] }

forest/cli/src/cli/db_cmd.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2019-2022 ChainSafe Systems
2+
// SPDX-License-Identifier: Apache-2.0, MIT
3+
4+
use forest_cli_shared::cli::{db_path, Config};
5+
use log::error;
6+
use structopt::StructOpt;
7+
8+
use crate::cli::prompt_confirm;
9+
10+
#[derive(Debug, StructOpt)]
11+
pub enum DBCommands {
12+
/// Show DB stats
13+
Stats,
14+
/// DB Clean up
15+
Clean {
16+
/// Answer yes to all forest-cli yes/no questions without prompting
17+
#[structopt(long)]
18+
force: bool,
19+
},
20+
}
21+
22+
impl DBCommands {
23+
pub async fn run(&self, config: &Config) {
24+
match self {
25+
Self::Stats => {
26+
use human_repr::HumanCount;
27+
28+
let dir = db_path(config);
29+
println!("Database path: {}", dir.display());
30+
let size = fs_extra::dir::get_size(dir).unwrap_or_default();
31+
println!("Database size: {}", size.human_count_bytes());
32+
}
33+
Self::Clean { force } => {
34+
let dir = db_path(config);
35+
if !dir.is_dir() {
36+
println!(
37+
"Aborted. Database path {} is not a valid directory",
38+
dir.display()
39+
);
40+
return;
41+
}
42+
println!("Deleting {}", dir.display());
43+
if !force && !prompt_confirm() {
44+
println!("Aborted.");
45+
return;
46+
}
47+
match fs_extra::dir::remove(&dir) {
48+
Ok(_) => {
49+
println!("Deleted {}", dir.display());
50+
}
51+
Err(err) => {
52+
error!("{err}");
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}

forest/cli/src/cli/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
mod auth_cmd;
1010
mod chain_cmd;
1111
mod config_cmd;
12+
mod db_cmd;
1213
mod fetch_params_cmd;
1314
mod genesis_cmd;
1415
mod mpool_cmd;
@@ -21,6 +22,7 @@ mod wallet_cmd;
2122

2223
pub(super) use self::auth_cmd::AuthCommands;
2324
pub(super) use self::chain_cmd::ChainCommands;
25+
pub(super) use self::db_cmd::DBCommands;
2426
pub(super) use self::fetch_params_cmd::FetchCommands;
2527
pub(super) use self::genesis_cmd::GenesisCommands;
2628
pub(super) use self::mpool_cmd::MpoolCommands;
@@ -100,6 +102,9 @@ pub enum Subcommand {
100102

101103
/// Send funds between accounts
102104
Send(SendCommand),
105+
106+
/// Database management
107+
DB(DBCommands),
103108
}
104109

105110
/// Pretty-print a JSON-RPC error and exit
@@ -200,6 +205,14 @@ pub(super) fn print_stdout(out: String) {
200205
.unwrap();
201206
}
202207

208+
fn prompt_confirm() -> bool {
209+
println!("Do you want to continue? [y/n]");
210+
let mut line = String::new();
211+
std::io::stdin().read_line(&mut line).unwrap();
212+
let line = line.trim().to_lowercase();
213+
line == "y" || line == "yes"
214+
}
215+
203216
#[cfg(test)]
204217
mod test {
205218
use super::*;

forest/cli/src/cli/snapshot_cmd.rs

-8
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,6 @@ fn delete_snapshot(snapshot_path: &PathBuf) {
382382
}
383383
}
384384

385-
fn prompt_confirm() -> bool {
386-
println!("Do you want to continue? [y/n]");
387-
let mut line = String::new();
388-
std::io::stdin().read_line(&mut line).unwrap();
389-
let line = line.trim().to_lowercase();
390-
line == "y" || line == "yes"
391-
}
392-
393385
fn is_car_or_tmp(path: &Path) -> bool {
394386
let ext = path.extension().unwrap_or_default();
395387
ext == "car" || ext == "tmp" || ext == "aria2"

forest/cli/src/subcommand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ pub(super) async fn process(command: Subcommand, config: Config) {
4141
cmd.run(config).await.unwrap();
4242
}
4343
Subcommand::Snapshot(cmd) => cmd.run(config).await,
44+
Subcommand::DB(cmd) => cmd.run(&config).await,
4445
}
4546
}

forest/daemon/src/daemon.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use forest_chain::ChainStore;
99
use forest_chain_sync::consensus::SyncGossipSubmitter;
1010
use forest_chain_sync::ChainMuxer;
1111
use forest_cli_shared::cli::{
12-
cli_error_and_die, default_snapshot_dir, is_aria2_installed, snapshot_fetch, Client, Config,
13-
FOREST_VERSION_STRING,
12+
cli_error_and_die, db_path, default_snapshot_dir, is_aria2_installed, snapshot_fetch, Client,
13+
Config, FOREST_VERSION_STRING,
1414
};
1515
use forest_db::rocks::RocksDb;
1616
use forest_fil_types::verifier::FullVerifier;
@@ -480,14 +480,6 @@ async fn sync_from_snapshot(config: &Config, state_manager: &Arc<StateManager<Ro
480480
}
481481
}
482482

483-
fn db_path(config: &Config) -> PathBuf {
484-
chain_path(config).join("db")
485-
}
486-
487-
fn chain_path(config: &Config) -> PathBuf {
488-
PathBuf::from(&config.client.data_dir).join(&config.chain.name)
489-
}
490-
491483
fn get_actual_chain_name(internal_network_name: &str) -> &str {
492484
match internal_network_name {
493485
"testnetnet" => "mainnet",

forest/shared/src/cli/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,16 @@ pub fn default_snapshot_dir(config: &Config) -> PathBuf {
282282
.join(config.chain.name.clone())
283283
}
284284

285+
/// Gets database directory
286+
pub fn db_path(config: &Config) -> PathBuf {
287+
chain_path(config).join("db")
288+
}
289+
290+
/// Gets chain data directory
291+
pub fn chain_path(config: &Config) -> PathBuf {
292+
PathBuf::from(&config.client.data_dir).join(&config.chain.name)
293+
}
294+
285295
/// Print an error message and exit the program with an error code
286296
/// Used for handling high level errors such as invalid parameters
287297
pub fn cli_error_and_die(msg: impl AsRef<str>, code: i32) -> ! {

0 commit comments

Comments
 (0)