Skip to content

Commit

Permalink
Unify criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezzfelipe committed Oct 9, 2024
1 parent a0045fe commit 60c1036
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
11 changes: 4 additions & 7 deletions src/bin/dolos/bootstrap/mithril.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
35 changes: 15 additions & 20 deletions src/bin/dolos/bootstrap/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -11,34 +11,29 @@ pub struct Args {
skip_if_not_empty: bool,
}

fn open_empty_wal(config: &crate::Config, args: &Args) -> miette::Result<Option<WalStore>> {
fn open_empty_wal(config: &crate::Config) -> miette::Result<WalStore> {
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(())
}
13 changes: 7 additions & 6 deletions src/bin/dolos/bootstrap/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions src/bin/dolos/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<wal::redb::WalStore, Error> {
let root = &config.storage.path;

Expand Down

0 comments on commit 60c1036

Please sign in to comment.