Skip to content

Commit

Permalink
feat: add --consume-snapshot to forest daemon (#3439)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 authored and elmattic committed Sep 1, 2023
1 parent 01ccab0 commit 84e5aae
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
- [#3322](https://github.com/ChainSafe/forest/issues/3322): Added prompt to
`forest-cli archive export` to overwrite file if the file specified with
`--output-path` already exists and a `--force` flag to suppress the prompt.
- [#3439](https://github.com/ChainSafe/forest/pull/3439): Add
`--consume-snapshot` option to `forest` command.

### Changed

Expand Down
1 change: 1 addition & 0 deletions documentation/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use of the following flags:
| --kademlia | Boolean | Determines whether Kademilia is allowed |
| --mdns | Boolean | Determines whether MDNS is allowed |
| --import-snapshot | OS File Path | Path to snapshot CAR file |
| --consume-snapshot | OS File Path | Path to snapshot CAR file (delete after importing) |
| --import-chain | OS File Path | Path to chain CAR file |
| --skip-load | Boolean | Skips loading CAR File and uses header to index chain |
| --req-window | Integer | Sets the number of tipsets requested over chain exchange |
Expand Down
3 changes: 3 additions & 0 deletions src/cli_shared/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct Client {
/// If this is true, then we do not validate the imported snapshot.
/// Otherwise, we validate and compute the states.
pub snapshot: bool,
/// If this is true, delete the snapshot at `snapshot_path` if it's a local file.
pub consume_snapshot: bool,
pub snapshot_height: Option<i64>,
pub snapshot_head: Option<i64>,
pub snapshot_path: Option<PathBuf>,
Expand Down Expand Up @@ -83,6 +85,7 @@ impl Default for Client {
rpc_token: None,
snapshot_path: None,
snapshot: false,
consume_snapshot: false,
snapshot_height: None,
snapshot_head: None,
skip_load: false,
Expand Down
12 changes: 12 additions & 0 deletions src/cli_shared/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ pub struct CliOpts {
/// Import a snapshot from a local CAR file or URL
#[arg(long)]
pub import_snapshot: Option<String>,
/// Import a snapshot from a local CAR file and delete it, or from a URL
#[arg(long)]
pub consume_snapshot: Option<String>,
/// Halt with exit code 0 after successfully importing a snapshot
#[arg(long)]
pub halt_after_import: bool,
Expand Down Expand Up @@ -188,12 +191,21 @@ impl CliOpts {
}
if self.import_snapshot.is_some() && self.import_chain.is_some() {
anyhow::bail!("Can't set import_snapshot and import_chain at the same time!")
} else if self.import_snapshot.is_some() && self.consume_snapshot.is_some() {
anyhow::bail!("Can't set import_snapshot and consume_snapshot at the same time!")
} else if self.consume_snapshot.is_some() && self.import_chain.is_some() {
anyhow::bail!("Can't set consume_snapshot and import_chain at the same time!")
}

if let Some(snapshot_path) = &self.import_snapshot {
cfg.client.snapshot_path = Some(snapshot_path.into());
cfg.client.snapshot = true;
}
if let Some(snapshot_path) = &self.consume_snapshot {
cfg.client.snapshot_path = Some(snapshot_path.into());
cfg.client.snapshot = true;
cfg.client.consume_snapshot = true;
}
if let Some(snapshot_path) = &self.import_chain {
cfg.client.snapshot_path = Some(snapshot_path.into());
cfg.client.snapshot = false;
Expand Down
10 changes: 6 additions & 4 deletions src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,12 @@ pub(super) async fn start(
// Import chain if needed
if !opts.skip_load.unwrap_or_default() {
if let Some(path) = &config.client.snapshot_path {
// TODO: respect `--consume-snapshot` CLI option once it's implemented.
// See <https://github.com/ChainSafe/forest/issues/3334>.
let (car_db_path, ts) =
import_chain_as_forest_car(path, &forest_car_db_dir, false).await?;
let (car_db_path, ts) = import_chain_as_forest_car(
path,
&forest_car_db_dir,
config.client.consume_snapshot,
)
.await?;
db.read_only_files(std::iter::once(car_db_path.clone()))?;
debug!("Loaded car DB at {}", car_db_path.display());
state_manager
Expand Down

0 comments on commit 84e5aae

Please sign in to comment.