Skip to content

Commit

Permalink
Merge pull request #58 from spacemeshos/partial_check
Browse files Browse the repository at this point in the history
  • Loading branch information
pigmej authored Oct 15, 2024
2 parents 690d619 + e613483 commit ef05c1b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
33 changes: 32 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use checksum::*;
use download::download_with_retries;
use go_spacemesh::get_version;
use parsers::*;
use partial_quicksync::partial_restore;
use partial_quicksync::{check_for_restore_points, partial_restore};
use sql::get_last_layer_from_db;
use utils::*;

Expand Down Expand Up @@ -97,6 +97,22 @@ enum Commands {
#[clap(short = 'u', long, default_value = partial_quicksync::DEFAULT_BASE_URL)]
base_url: String,
},
/// Partial check availability
PartialCheck {
/// Path to the node state.sql
#[clap(short = 's', long)]
state_sql: PathBuf,
/// Number of layers present in the DB that are not trusted to be fully synced.
/// These layers will also be synced.
#[clap(long, default_value_t = 10)]
untrusted_layers: u32,
/// Jump-back to recover earlier than latest layer. It will jump back one row in recovery metadata
#[clap(short = 'j', long, default_value_t = 0)]
jump_back: usize,
/// URL to download parts from
#[clap(short = 'u', long, default_value = partial_quicksync::DEFAULT_BASE_URL)]
base_url: String,
},
}

fn go_spacemesh_default_path() -> &'static str {
Expand Down Expand Up @@ -352,5 +368,20 @@ fn main() -> anyhow::Result<()> {
jump_back,
)
}
Commands::PartialCheck {
state_sql,
base_url,
untrusted_layers,
jump_back,
} => {
let state_sql_path = resolve_path(&state_sql).context("resolving state.sql path")?;
if !state_sql_path
.try_exists()
.context("checking if state file exists")?
{
return Err(anyhow!("state file not found: {:?}", state_sql_path));
}
check_for_restore_points(&base_url, &state_sql_path, untrusted_layers, jump_back)
}
}
}
38 changes: 34 additions & 4 deletions src/partial_quicksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,12 @@ fn decompress_file(input_path: &Path, output_path: &Path) -> Result<()> {
Ok(())
}

pub fn partial_restore(
fn get_restore_points(
base_url: &str,
target_db_path: &Path,
download_path: &Path,
untrusted_layers: u32,
jump_back: usize,
) -> Result<()> {
) -> Result<(Vec<RestorePoint>, String, usize)> {
let client = Client::new();
let conn = Connection::open(target_db_path)?;
let user_version = get_user_version(&conn)?;
Expand Down Expand Up @@ -197,6 +196,20 @@ pub fn partial_restore(
"No suitable restore points found, seems that state.sql is too old"
);

Ok((start_points, remote_metadata, user_version))
}

pub fn partial_restore(
base_url: &str,
target_db_path: &Path,
download_path: &Path,
untrusted_layers: u32,
jump_back: usize,
) -> Result<()> {
let (start_points, _, user_version) =
get_restore_points(base_url, target_db_path, untrusted_layers, jump_back)?;
let client = Client::new();

let restore_string = client
.get(format!(
"{}/{}/restore.sql?version={}",
Expand All @@ -212,7 +225,6 @@ pub fn partial_restore(
"Looking for restore points with untrusted_layers={untrusted_layers}, jump_back={jump_back}"
);
println!("Found {total} potential restore points");
conn.close().expect("closing DB connection");

let source_db_path_zst = &download_path.join("backup_source.db.zst");
let source_db_path = &download_path.join("backup_source.db");
Expand Down Expand Up @@ -263,6 +275,24 @@ pub fn partial_restore(
Ok(())
}

pub fn check_for_restore_points(
base_url: &str,
target_db_path: &Path,
untrusted_layers: u32,
jump_back: usize,
) -> Result<()> {
let (start_points, _, _) =
get_restore_points(base_url, target_db_path, untrusted_layers, jump_back)?;

anyhow::ensure!(!start_points.is_empty(), "No restore points available.");

let first = start_points.first().unwrap();
let last = start_points.last().unwrap();
println!("Possible to restore from: {}", first.from);
println!("Possible to restore up to: {}", last.to);
Ok(())
}

#[cfg(test)]
impl RestorePoint {
fn new<H: Into<String>>(from: u32, to: u32, hash: H) -> Self {
Expand Down

0 comments on commit ef05c1b

Please sign in to comment.