Skip to content

Commit

Permalink
Make client append a empty 'clean' file after download succeed
Browse files Browse the repository at this point in the history
  • Loading branch information
Alenar committed Aug 3, 2023
1 parent 94dfd96 commit ed64fea
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions mithril-client/src/services/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
fmt::Write,
fs::File,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
Expand Down Expand Up @@ -209,14 +210,14 @@ impl SnapshotService for MithrilClientSnapshotService {

let unpack_dir = download_dir.join("db");
let progress_bar = MultiProgress::with_draw_target(progress_target);
progress_bar.println("1/7 - Checking local disk info…")?;
progress_bar.println("1/8 - Checking local disk info…")?;
let unpacker = SnapshotUnpacker;

if let Err(e) = unpacker.check_prerequisites(&unpack_dir, snapshot_entity.artifact.size) {
self.check_disk_space_error(e)?;
}

progress_bar.println("2/7 - Fetching the certificate's information…")?;
progress_bar.println("2/8 - Fetching the certificate's information…")?;
let certificate = self
.certificate_client
.get(&snapshot_entity.certificate_id)
Expand All @@ -227,11 +228,11 @@ impl SnapshotService for MithrilClientSnapshotService {
)
})?;

progress_bar.println("3/7 - Verifying the certificate chain…")?;
progress_bar.println("3/8 - Verifying the certificate chain…")?;
let verifier = self.verify_certificate_chain(genesis_verification_key, &certificate);
self.wait_spinner(&progress_bar, verifier).await?;

progress_bar.println("4/7 - Downloading the snapshot…")?;
progress_bar.println("4/8 - Downloading the snapshot…")?;
let pb = progress_bar.add(ProgressBar::new(snapshot_entity.artifact.size));
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.unwrap()
Expand All @@ -248,11 +249,11 @@ impl SnapshotService for MithrilClientSnapshotService {
)
})?;

progress_bar.println("5/7 - Unpacking the snapshot…")?;
progress_bar.println("5/8 - Unpacking the snapshot…")?;
let unpacker = unpacker.unpack_snapshot(&snapshot_path, &unpack_dir);
self.wait_spinner(&progress_bar, unpacker).await?;

progress_bar.println("6/7 - Computing the snapshot digest…")?;
progress_bar.println("6/8 - Computing the snapshot digest…")?;
let unpacked_snapshot_digest = self
.immutable_digester
.compute_digest(&unpack_dir, &certificate.beacon)
Expand All @@ -264,7 +265,7 @@ impl SnapshotService for MithrilClientSnapshotService {
)
})?;

progress_bar.println("7/7 - Verifying the snapshot signature…")?;
progress_bar.println("7/8 - Verifying the snapshot signature…")?;
let expected_message = {
let mut protocol_message = certificate.protocol_message.clone();
protocol_message.set_message_part(
Expand All @@ -288,6 +289,15 @@ impl SnapshotService for MithrilClientSnapshotService {
.into());
}

progress_bar.println("8/8 - Append 'clean' file to speedup node bootstrap…")?;
if let Err(error) = File::create(unpack_dir.join("clean")) {
warn!(
"Error while creating of a 'clean' file in the unpacked directory to speed\
up cardano node bootstrap: {error}.";
"unpacked_directory" => &unpack_dir.display()
);
};

Ok(unpack_dir)
}
}
Expand Down Expand Up @@ -556,6 +566,51 @@ mod tests {
assert_eq!(Some(OsStr::new("db")), filepath.file_name());
}

#[tokio::test]
async fn test_download_snapshot_ok_add_clean_file_allowing_node_bootstrap_speedup() {
let test_path = std::env::temp_dir()
.join("test_download_snapshot_ok_add_clean_file_allowing_node_bootstrap_speedup");
let _ = std::fs::remove_dir_all(&test_path);

let (http_client, certificate_verifier, digester) =
get_mocks_for_snapshot_service_configured_to_make_download_succeed();

let mut builder = get_dep_builder(Arc::new(http_client));
builder.certificate_verifier = Some(Arc::new(certificate_verifier));
builder.immutable_digester = Some(Arc::new(digester));
let snapshot_service = builder.get_snapshot_service().await.unwrap();

let snapshot = FromSnapshotMessageAdapter::adapt(get_snapshot_message());
build_dummy_snapshot(
"digest-10.tar.gz",
"1234567890".repeat(124).as_str(),
&test_path,
);

let (_, verifier) = setup_genesis();
let genesis_verification_key = verifier.to_verification_key();

let filepath = snapshot_service
.download(
&snapshot,
&test_path,
&key_encode_hex(genesis_verification_key).unwrap(),
ProgressDrawTarget::hidden(),
)
.await
.expect("Snapshot download should succeed.");

let clean_file = filepath.join("clean");

assert!(
clean_file.is_file(),
"'clean' file should exist and be a file"
);

let clean_file_metadata = clean_file.metadata().unwrap();
assert_eq!(clean_file_metadata.len(), 0, "'clean' file should be empty")
}

#[tokio::test]
async fn test_download_snapshot_invalid_digest() {
let test_path = std::env::temp_dir().join("test_download_snapshot_invalid_digest");
Expand Down

0 comments on commit ed64fea

Please sign in to comment.