Skip to content

Commit 21d837e

Browse files
committed
fix(aggregator): include the two latest ledger state snapshots in the ancillary archive
1 parent 4d382b9 commit 21d837e

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

mithril-aggregator/src/services/snapshotter/compressed_archive_snapshotter.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,19 @@ impl CompressedArchiveSnapshotter {
259259

260260
let db_ledger_dir = self.db_directory.join(LEDGER_DIR);
261261
let ledger_files = LedgerFile::list_all_in_dir(&db_ledger_dir)?;
262-
let last_ledger = ledger_files.last().ok_or(anyhow!(
263-
"No ledger file found in directory: `{}`",
264-
db_ledger_dir.display()
265-
))?;
266-
files_to_snapshot.push(PathBuf::from(LEDGER_DIR).join(&last_ledger.filename));
262+
let latest_ledger_files: Vec<PathBuf> = ledger_files
263+
.iter()
264+
.rev()
265+
.take(2)
266+
.map(|ledger_file| PathBuf::from(LEDGER_DIR).join(&ledger_file.filename))
267+
.collect();
268+
if latest_ledger_files.is_empty() {
269+
return Err(anyhow!(
270+
"No ledger state snapshot found in the ledger directory: '{}'",
271+
db_ledger_dir.display()
272+
));
273+
}
274+
files_to_snapshot.extend(latest_ledger_files);
267275

268276
fs::create_dir(target_folder.join(IMMUTABLE_DIR))
269277
.with_context(|| format!("Can not create folder: `{}`", target_folder.display()))?;
@@ -548,6 +556,23 @@ mod tests {
548556
);
549557
}
550558

559+
#[tokio::test]
560+
async fn getting_files_to_include_fails_when_no_ledger_file_found() {
561+
let test_dir = temp_dir_create!();
562+
let cardano_db = DummyCardanoDbBuilder::new(current_function!())
563+
.with_immutables(&[1, 2])
564+
.build();
565+
let snapshotter =
566+
snapshotter_for_test(&test_dir, cardano_db.get_dir(), CompressionAlgorithm::Gzip);
567+
let ancillary_snapshot_dir = test_dir.join("ancillary_snapshot");
568+
fs::create_dir(&ancillary_snapshot_dir).unwrap();
569+
570+
snapshotter
571+
.get_files_and_directories_for_ancillary_snapshot(1, &ancillary_snapshot_dir)
572+
.await
573+
.expect_err("Should fail if no ledger file found");
574+
}
575+
551576
#[tokio::test]
552577
async fn delete_temporary_working_directory_after_snapshot_is_created() {
553578
let test_dir = temp_dir_create!();
@@ -603,7 +628,7 @@ mod tests {
603628
}
604629

605630
#[tokio::test]
606-
async fn create_archive_should_embed_only_last_ledger_and_last_immutables() {
631+
async fn create_archive_should_embed_only_two_last_ledgers_and_last_immutables() {
607632
let test_dir = temp_dir_create!();
608633
let cardano_db = DummyCardanoDbBuilder::new(current_function!())
609634
.with_immutables(&[1, 2, 3])
@@ -627,13 +652,14 @@ mod tests {
627652
let unpack_dir = snapshot.unpack_gzip(&test_dir);
628653
assert_dir_eq!(
629654
&unpack_dir,
630-
// Only the last ledger file should be included
655+
// Only the two last ledger files should be included
631656
format!(
632657
"* {IMMUTABLE_DIR}/
633658
** 00003.chunk
634659
** 00003.primary
635660
** 00003.secondary
636661
* {LEDGER_DIR}/
662+
** 637
637663
** 737
638664
* {}",
639665
AncillaryFilesManifest::ANCILLARY_MANIFEST_FILE_NAME
@@ -702,6 +728,7 @@ mod tests {
702728
&PathBuf::from(IMMUTABLE_DIR).join("00003.chunk"),
703729
&PathBuf::from(IMMUTABLE_DIR).join("00003.primary"),
704730
&PathBuf::from(IMMUTABLE_DIR).join("00003.secondary"),
731+
&PathBuf::from(LEDGER_DIR).join("321"),
705732
&PathBuf::from(LEDGER_DIR).join("737"),
706733
],
707734
manifest.data.keys().collect::<Vec<_>>()

mithril-aggregator/src/services/snapshotter/interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub trait Snapshotter: Sync + Send {
1717

1818
/// Create a new snapshot of ancillary files.
1919
///
20-
/// Ancillary files include the last, uncompleted, immutable trio and the last ledger file.
20+
/// Ancillary files include the last, uncompleted, immutable trio and the two last ledger files.
2121
async fn snapshot_ancillary(
2222
&self,
2323
immutable_file_number: ImmutableFileNumber,

0 commit comments

Comments
 (0)