Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Partial Check command #58

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
"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 @@
"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 @@
Ok(())
}

pub fn check_for_restore_points(

Check warning on line 278 in src/partial_quicksync.rs

View check run for this annotation

Codecov / codecov/patch

src/partial_quicksync.rs#L278

Added line #L278 was not covered by tests
base_url: &str,
target_db_path: &Path,
untrusted_layers: u32,
jump_back: usize,
) -> Result<()> {
let (start_points, _, _) =

Check warning on line 284 in src/partial_quicksync.rs

View check run for this annotation

Codecov / codecov/patch

src/partial_quicksync.rs#L284

Added line #L284 was not covered by tests
get_restore_points(base_url, target_db_path, untrusted_layers, jump_back)?;

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

Check warning on line 287 in src/partial_quicksync.rs

View check run for this annotation

Codecov / codecov/patch

src/partial_quicksync.rs#L287

Added line #L287 was not covered by tests

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(())

Check warning on line 293 in src/partial_quicksync.rs

View check run for this annotation

Codecov / codecov/patch

src/partial_quicksync.rs#L289-L293

Added lines #L289 - L293 were not covered by tests
}

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