Skip to content

Commit 3c9fe60

Browse files
committed
Make client append a empty 'clean' file after download succeed
1 parent 7b65665 commit 3c9fe60

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

mithril-client/src/services/snapshot.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
22
fmt::Write,
3+
fs::File,
34
path::{Path, PathBuf},
45
sync::Arc,
56
time::Duration,
@@ -252,6 +253,14 @@ impl SnapshotService for MithrilClientSnapshotService {
252253
let unpacker = unpacker.unpack_snapshot(&snapshot_path, &db_dir);
253254
self.wait_spinner(&progress_bar, unpacker).await?;
254255

256+
// Append 'clean' file to speedup node bootstrap
257+
if let Err(error) = File::create(db_dir.join("clean")) {
258+
warn!(
259+
"Could not create clean shutdown marker file in directory {}: {error}",
260+
db_dir.display()
261+
);
262+
};
263+
255264
progress_bar.println("6/7 - Computing the snapshot digest…")?;
256265
let unpacked_snapshot_digest = self
257266
.immutable_digester
@@ -551,6 +560,51 @@ mod tests {
551560
assert_eq!(Some(OsStr::new("db")), filepath.file_name());
552561
}
553562

563+
#[tokio::test]
564+
async fn test_download_snapshot_ok_add_clean_file_allowing_node_bootstrap_speedup() {
565+
let test_path = std::env::temp_dir()
566+
.join("test_download_snapshot_ok_add_clean_file_allowing_node_bootstrap_speedup");
567+
let _ = std::fs::remove_dir_all(&test_path);
568+
569+
let (http_client, certificate_verifier, digester) =
570+
get_mocks_for_snapshot_service_configured_to_make_download_succeed();
571+
572+
let mut builder = get_dep_builder(Arc::new(http_client));
573+
builder.certificate_verifier = Some(Arc::new(certificate_verifier));
574+
builder.immutable_digester = Some(Arc::new(digester));
575+
let snapshot_service = builder.get_snapshot_service().await.unwrap();
576+
577+
let snapshot = FromSnapshotMessageAdapter::adapt(get_snapshot_message());
578+
build_dummy_snapshot(
579+
"digest-10.tar.gz",
580+
"1234567890".repeat(124).as_str(),
581+
&test_path,
582+
);
583+
584+
let (_, verifier) = setup_genesis();
585+
let genesis_verification_key = verifier.to_verification_key();
586+
587+
let filepath = snapshot_service
588+
.download(
589+
&snapshot,
590+
&test_path,
591+
&key_encode_hex(genesis_verification_key).unwrap(),
592+
ProgressDrawTarget::hidden(),
593+
)
594+
.await
595+
.expect("Snapshot download should succeed.");
596+
597+
let clean_file = filepath.join("clean");
598+
599+
assert!(
600+
clean_file.is_file(),
601+
"'clean' file should exist and be a file"
602+
);
603+
604+
let clean_file_metadata = clean_file.metadata().unwrap();
605+
assert_eq!(clean_file_metadata.len(), 0, "'clean' file should be empty")
606+
}
607+
554608
#[tokio::test]
555609
async fn test_download_snapshot_invalid_digest() {
556610
let test_path = std::env::temp_dir().join("test_download_snapshot_invalid_digest");

0 commit comments

Comments
 (0)