diff --git a/src/bin/dolos/bootstrap/mithril.rs b/src/bin/dolos/bootstrap/mithril.rs index f91da43..60da7c9 100644 --- a/src/bin/dolos/bootstrap/mithril.rs +++ b/src/bin/dolos/bootstrap/mithril.rs @@ -6,7 +6,7 @@ use pallas::ledger::traverse::MultiEraBlock; use std::{path::Path, sync::Arc}; use tracing::{debug, info, warn}; -use crate::{feedback::Feedback, MithrilConfig}; +use crate::{common::storage_is_empty, feedback::Feedback, MithrilConfig}; #[derive(Debug, clap::Args, Default)] pub struct Args { @@ -210,12 +210,9 @@ fn import_hardano_into_wal( pub fn run(config: &crate::Config, args: &Args, feedback: &Feedback) -> miette::Result<()> { //crate::common::setup_tracing(&config.logging)?; - if args.skip_if_not_empty { - // Open WAL in closure to ensure it is dropped before continuing. - let should_skip = crate::common::open_wal(config).is_ok(); - if should_skip { - return Ok(()); - } + if args.skip_if_not_empty && !storage_is_empty(config) { + info!("Skipping bootstrap because storage is not empty."); + return Ok(()); } let mithril = config diff --git a/src/bin/dolos/bootstrap/relay.rs b/src/bin/dolos/bootstrap/relay.rs index 71abd9b..2fbaec5 100644 --- a/src/bin/dolos/bootstrap/relay.rs +++ b/src/bin/dolos/bootstrap/relay.rs @@ -2,7 +2,7 @@ use dolos::wal::redb::WalStore; use miette::{bail, Context, IntoDiagnostic}; use tracing::info; -use crate::feedback::Feedback; +use crate::{common::storage_is_empty, feedback::Feedback}; #[derive(Debug, clap::Args, Default)] pub struct Args { @@ -11,34 +11,29 @@ pub struct Args { skip_if_not_empty: bool, } -fn open_empty_wal(config: &crate::Config, args: &Args) -> miette::Result> { +fn open_empty_wal(config: &crate::Config) -> miette::Result { let wal = crate::common::open_wal(config)?; let is_empty = wal.is_empty().into_diagnostic()?; if !is_empty { - if args.skip_if_not_empty { - return Ok(None); - } else { - bail!("can't continue with data already available"); - } + bail!("can't continue with data already available"); } - Ok(Some(wal)) + Ok(wal) } pub fn run(config: &crate::Config, args: &Args, _feedback: &Feedback) -> miette::Result<()> { - match open_empty_wal(config, args).context("opening WAL")? { - Some(mut wal) => { - wal.initialize_from_origin() - .into_diagnostic() - .context("initializing WAL")?; - - Ok(()) - } - None => { - info!("Skipping bootstrap, data already present."); - Ok(()) - } + if args.skip_if_not_empty && !storage_is_empty(config) { + info!("Skipping bootstrap because storage is not empty."); + return Ok(()); } + + let mut wal = open_empty_wal(config).context("opening WAL")?; + + wal.initialize_from_origin() + .into_diagnostic() + .context("initializing WAL")?; + + Ok(()) } diff --git a/src/bin/dolos/bootstrap/snapshot.rs b/src/bin/dolos/bootstrap/snapshot.rs index dee537b..faf46ea 100644 --- a/src/bin/dolos/bootstrap/snapshot.rs +++ b/src/bin/dolos/bootstrap/snapshot.rs @@ -3,7 +3,10 @@ use miette::{Context, IntoDiagnostic}; use tar::Archive; use tracing::info; -use crate::feedback::{Feedback, ProgressReader}; +use crate::{ + common::storage_is_empty, + feedback::{Feedback, ProgressReader}, +}; #[derive(Debug, clap::Args, Default)] pub struct Args { @@ -20,11 +23,9 @@ fn fetch_snapshot(config: &crate::Config, feedback: &Feedback, args: &Args) -> m .ok_or_else(|| miette::miette!("Snapshot URL not specified in config"))?; // Check if exists and is not empty. - if let Ok(mut entries) = std::fs::read_dir(&config.storage.path) { - if entries.next().is_some() && args.skip_if_not_empty { - info!("Skipping bootstrap, data already present."); - return Ok(()); - } + if args.skip_if_not_empty && !storage_is_empty(config) { + info!("Skipping bootstrap because storage is not empty."); + return Ok(()); } std::fs::create_dir_all(&config.storage.path) diff --git a/src/bin/dolos/common.rs b/src/bin/dolos/common.rs index b3a8e7c..648ace8 100644 --- a/src/bin/dolos/common.rs +++ b/src/bin/dolos/common.rs @@ -15,6 +15,15 @@ use crate::{GenesisConfig, LoggingConfig}; pub type Stores = (wal::redb::WalStore, state::LedgerStore); +pub fn storage_is_empty(config: &crate::Config) -> bool { + // Check if exists and is not empty. + if let Ok(mut entries) = std::fs::read_dir(&config.storage.path) { + entries.next().is_none() + } else { + true + } +} + pub fn open_wal(config: &crate::Config) -> Result { let root = &config.storage.path;