Skip to content

Commit 2af282f

Browse files
committed
feature(client-lib,client-cli): add arg --allow-missing in cardano-db verify command
1 parent 3e27367 commit 2af282f

File tree

5 files changed

+85
-15
lines changed

5 files changed

+85
-15
lines changed

mithril-client-cli/src/commands/cardano_db/download/v2.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use mithril_client::{
1515
use crate::{
1616
CommandContext,
1717
commands::{
18-
cardano_db::{download::DB_DIRECTORY_NAME, shared_steps},
18+
cardano_db::{
19+
download::DB_DIRECTORY_NAME,
20+
shared_steps::{self, ComputeCardanoDatabaseMessageOptions},
21+
},
1922
client_builder,
2023
},
2124
utils::{
@@ -139,13 +142,17 @@ impl PreparedCardanoDbV2Download {
139142
)
140143
})?;
141144

145+
let options = ComputeCardanoDatabaseMessageOptions {
146+
db_dir: restoration_options.db_dir.clone(),
147+
immutable_file_range: restoration_options.immutable_file_range,
148+
allow_missing: false,
149+
};
142150
let message = shared_steps::compute_cardano_db_snapshot_message(
143151
5,
144152
&progress_printer,
145153
&certificate,
146154
&cardano_db_message,
147-
&restoration_options.immutable_file_range,
148-
&restoration_options.db_dir,
155+
&options,
149156
&verified_digests,
150157
)
151158
.await?;

mithril-client-cli/src/commands/cardano_db/shared_steps.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{Context, anyhow};
22
use chrono::Utc;
33
use slog::{Logger, debug, warn};
4-
use std::path::Path;
4+
use std::path::{Path, PathBuf};
55

66
use mithril_client::{
77
CardanoDatabaseSnapshot, Client, MessageBuilder, MithrilCertificate, MithrilResult,
@@ -11,6 +11,12 @@ use mithril_client::{
1111

1212
use crate::utils::{CardanoDbUtils, ProgressPrinter};
1313

14+
pub struct ComputeCardanoDatabaseMessageOptions {
15+
pub db_dir: PathBuf,
16+
pub immutable_file_range: ImmutableFileRange,
17+
pub allow_missing: bool,
18+
}
19+
1420
pub async fn fetch_certificate_and_verifying_chain(
1521
step_number: u16,
1622
progress_printer: &ProgressPrinter,
@@ -69,8 +75,7 @@ pub async fn compute_cardano_db_snapshot_message(
6975
progress_printer: &ProgressPrinter,
7076
certificate: &MithrilCertificate,
7177
cardano_database_snapshot: &CardanoDatabaseSnapshot,
72-
immutable_file_range: &ImmutableFileRange,
73-
database_dir: &Path,
78+
options: &ComputeCardanoDatabaseMessageOptions,
7479
verified_digest: &VerifiedDigests,
7580
) -> MithrilResult<ProtocolMessage> {
7681
progress_printer.report_step(step_number, "Computing the cardano db snapshot message")?;
@@ -79,8 +84,9 @@ pub async fn compute_cardano_db_snapshot_message(
7984
MessageBuilder::new().compute_cardano_database_message(
8085
certificate,
8186
cardano_database_snapshot,
82-
immutable_file_range,
83-
database_dir,
87+
&options.immutable_file_range,
88+
options.allow_missing,
89+
&options.db_dir,
8490
verified_digest,
8591
),
8692
)

mithril-client-cli/src/commands/cardano_db/verify.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use mithril_client::{
1515
use crate::{
1616
CommandContext,
1717
commands::{
18-
cardano_db::{CardanoDbCommandsBackend, shared_steps},
18+
cardano_db::{
19+
CardanoDbCommandsBackend,
20+
shared_steps::{self, ComputeCardanoDatabaseMessageOptions},
21+
},
1922
client_builder,
2023
},
2124
configuration::{ConfigError, ConfigSource},
@@ -53,6 +56,10 @@ pub struct CardanoDbVerifyCommand {
5356
/// If not set, the verify will continue until the last certified immutable file.
5457
#[clap(long)]
5558
end: Option<ImmutableFileNumber>,
59+
60+
/// If set, the verification will not fail if some immutable files are missing.
61+
#[clap(long)]
62+
allow_missing: bool,
5663
}
5764

5865
impl CardanoDbVerifyCommand {
@@ -134,13 +141,17 @@ impl CardanoDbVerifyCommand {
134141
)
135142
.await?;
136143

144+
let options = ComputeCardanoDatabaseMessageOptions {
145+
db_dir: db_dir.to_path_buf(),
146+
immutable_file_range,
147+
allow_missing: self.allow_missing,
148+
};
137149
let message = shared_steps::compute_cardano_db_snapshot_message(
138150
3,
139151
&progress_printer,
140152
&certificate,
141153
&cardano_db_message,
142-
&immutable_file_range,
143-
db_dir,
154+
&options,
144155
&verified_digests,
145156
)
146157
.await;

mithril-client/src/message.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,19 @@ impl MessageBuilder {
230230
certificate: &CertificateMessage,
231231
cardano_database_snapshot: &CardanoDatabaseSnapshotMessage,
232232
immutable_file_range: &ImmutableFileRange,
233+
allow_missing: bool,
233234
database_dir: &Path,
234235
verified_digests: &VerifiedDigests,
235236
) -> Result<ProtocolMessage, ComputeCardanoDatabaseMessageError> {
236237
let network = certificate.metadata.network.clone();
237238
let immutable_file_number_range = immutable_file_range
238239
.to_range_inclusive(cardano_database_snapshot.beacon.immutable_file_number)
239240
.map_err(ComputeCardanoDatabaseMessageError::ImmutableFilesRange)?;
240-
let missing_immutable_files =
241-
Self::list_missing_immutable_files(database_dir, &immutable_file_number_range);
241+
let missing_immutable_files = if allow_missing {
242+
vec![]
243+
} else {
244+
Self::list_missing_immutable_files(database_dir, &immutable_file_number_range)
245+
};
242246
let immutable_digester = CardanoImmutableDigester::new(network, None, self.logger.clone());
243247
let computed_digest_entries = immutable_digester
244248
.compute_digests_for_range(database_dir, &immutable_file_number_range)
@@ -533,6 +537,7 @@ mod tests {
533537
&certificate,
534538
&CardanoDatabaseSnapshotMessage::dummy(),
535539
&immutable_file_range_to_prove,
540+
false,
536541
&database_dir,
537542
&verified_digests,
538543
)
@@ -543,15 +548,16 @@ mod tests {
543548
}
544549

545550
#[tokio::test]
546-
async fn compute_cardano_database_message_should_fail_if_immutable_is_missing() {
551+
async fn compute_cardano_database_message_should_fail_if_immutable_is_missing_and_allow_missing_not_set()
552+
{
547553
let beacon = CardanoDbBeacon {
548554
epoch: Epoch(123),
549555
immutable_file_number: 10,
550556
};
551557
let immutable_file_range = 1..=15;
552558
let immutable_file_range_to_prove = ImmutableFileRange::Range(2, 4);
553559
let (database_dir, certificate, verified_digests) = prepare_db_and_verified_digests(
554-
"compute_cardano_database_message_should_fail_if_immutable_is_missing",
560+
"compute_cardano_database_message_should_fail_if_immutable_is_missing_and_allow_missing_not_set",
555561
&beacon,
556562
&immutable_file_range,
557563
)
@@ -560,11 +566,13 @@ mod tests {
560566
let files_to_remove = vec!["00003.chunk", "00004.primary"];
561567
remove_immutable_files(&database_dir, &files_to_remove);
562568

569+
let allow_missing = false;
563570
let error = MessageBuilder::new()
564571
.compute_cardano_database_message(
565572
&certificate,
566573
&CardanoDatabaseSnapshotMessage::dummy(),
567574
&immutable_file_range_to_prove,
575+
allow_missing,
568576
&database_dir,
569577
&verified_digests,
570578
)
@@ -588,6 +596,41 @@ mod tests {
588596
);
589597
}
590598

599+
#[tokio::test]
600+
async fn compute_cardano_database_message_should_success_if_immutable_is_missing_and_allow_missing_is_set()
601+
{
602+
let beacon = CardanoDbBeacon {
603+
epoch: Epoch(123),
604+
immutable_file_number: 10,
605+
};
606+
let immutable_file_range = 1..=15;
607+
let immutable_file_range_to_prove = ImmutableFileRange::Range(2, 4);
608+
let (database_dir, certificate, verified_digests) = prepare_db_and_verified_digests(
609+
"compute_cardano_database_message_should_success_if_immutable_is_missing_and_allow_missing_is_set",
610+
&beacon,
611+
&immutable_file_range,
612+
)
613+
.await;
614+
615+
let files_to_remove = vec!["00003.chunk", "00004.primary"];
616+
remove_immutable_files(&database_dir, &files_to_remove);
617+
618+
let allow_missing = true;
619+
MessageBuilder::new()
620+
.compute_cardano_database_message(
621+
&certificate,
622+
&CardanoDatabaseSnapshotMessage::dummy(),
623+
&immutable_file_range_to_prove,
624+
allow_missing,
625+
&database_dir,
626+
&verified_digests,
627+
)
628+
.await
629+
.expect(
630+
"compute_cardano_database_message should succeed if a immutable is missing but 'allow_missing' is set",
631+
);
632+
}
633+
591634
#[tokio::test]
592635
async fn compute_cardano_database_message_should_fail_if_immutable_is_tampered() {
593636
let beacon = CardanoDbBeacon {
@@ -614,6 +657,7 @@ mod tests {
614657
&certificate,
615658
&CardanoDatabaseSnapshotMessage::dummy(),
616659
&immutable_file_range_to_prove,
660+
false,
617661
&database_dir,
618662
&verified_digests,
619663
)
@@ -665,6 +709,7 @@ mod tests {
665709
&certificate,
666710
&CardanoDatabaseSnapshotMessage::dummy(),
667711
&immutable_file_range_to_prove,
712+
false,
668713
&database_dir,
669714
&verified_digests,
670715
)

mithril-client/tests/cardano_db_snapshot_list_get_download_verify.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ async fn cardano_db_snapshot_list_get_download_verify() {
144144
&certificate,
145145
&cardano_db_snapshot,
146146
&immutable_file_range,
147+
false,
147148
&unpacked_dir,
148149
&verified_digests,
149150
)

0 commit comments

Comments
 (0)