Skip to content

Commit 199c04b

Browse files
author
Clément Turmel
committed
refactor(client-cli): realign client CLI download and verify command with new API call
1 parent 2f17f70 commit 199c04b

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl PreparedCardanoDbV2Download {
6565
} else {
6666
ProgressOutputType::Tty
6767
};
68-
let progress_printer = ProgressPrinter::new(progress_output_type, 6);
68+
let progress_printer = ProgressPrinter::new(progress_output_type, 7);
6969
let client = client_builder(context.config_parameters())?
7070
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
7171
progress_output_type,
@@ -147,19 +147,30 @@ impl PreparedCardanoDbV2Download {
147147
immutable_file_range: restoration_options.immutable_file_range,
148148
allow_missing: false,
149149
};
150-
let message = shared_steps::compute_cardano_db_snapshot_message(
150+
151+
let merkle_proof = shared_steps::verify_cardano_database(
151152
5,
152153
&progress_printer,
154+
&client,
153155
&certificate,
154156
&cardano_db_message,
155157
&options,
156158
&verified_digests,
157159
)
160+
.await
161+
.with_context(|| format!("Can not verify cardano database for hash: '{}'", self.hash))?;
162+
163+
let message = shared_steps::compute_cardano_db_snapshot_message(
164+
6,
165+
&progress_printer,
166+
&certificate,
167+
&merkle_proof,
168+
)
158169
.await?;
159170

160171
shared_steps::verify_message_matches_certificate(
161172
context.logger(),
162-
6,
173+
7,
163174
&progress_printer,
164175
&certificate,
165176
&message,

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
66
use mithril_client::{
77
CardanoDatabaseSnapshot, Client, MessageBuilder, MithrilCertificate, MithrilResult,
88
cardano_database_client::{ImmutableFileRange, VerifiedDigests},
9-
common::{ImmutableFileNumber, ProtocolMessage},
9+
common::{ImmutableFileNumber, MKProof, ProtocolMessage},
1010
};
1111

1212
use crate::utils::{CardanoDbUtils, ProgressPrinter};
@@ -69,19 +69,19 @@ pub async fn download_and_verify_digests(
6969
Ok(verified_digests)
7070
}
7171

72-
/// Computes the Cardano database snapshot message using the provided certificate and Merkle proof.
73-
pub async fn compute_cardano_db_snapshot_message(
72+
pub async fn verify_cardano_database(
7473
step_number: u16,
7574
progress_printer: &ProgressPrinter,
75+
client: &Client,
7676
certificate: &MithrilCertificate,
7777
cardano_database_snapshot: &CardanoDatabaseSnapshot,
7878
options: &ComputeCardanoDatabaseMessageOptions,
7979
verified_digest: &VerifiedDigests,
80-
) -> MithrilResult<ProtocolMessage> {
81-
progress_printer.report_step(step_number, "Computing the cardano db snapshot message")?;
82-
let message = CardanoDbUtils::wait_spinner(
80+
) -> MithrilResult<MKProof> {
81+
progress_printer.report_step(step_number, "Verifying the cardano database")?;
82+
let merkle_proof = CardanoDbUtils::wait_spinner(
8383
progress_printer,
84-
MessageBuilder::new().compute_cardano_database_message(
84+
client.cardano_database_v2().verify_cardano_database(
8585
certificate,
8686
cardano_database_snapshot,
8787
&options.immutable_file_range,
@@ -91,6 +91,24 @@ pub async fn compute_cardano_db_snapshot_message(
9191
),
9292
)
9393
.await
94+
.with_context(|| "Can not verify the cardano database")?;
95+
96+
Ok(merkle_proof)
97+
}
98+
99+
/// Computes the Cardano database snapshot message using the provided certificate and Merkle proof.
100+
pub async fn compute_cardano_db_snapshot_message(
101+
step_number: u16,
102+
progress_printer: &ProgressPrinter,
103+
certificate: &MithrilCertificate,
104+
merkle_proof: &MKProof,
105+
) -> MithrilResult<ProtocolMessage> {
106+
progress_printer.report_step(step_number, "Computing the cardano db snapshot message")?;
107+
let message = CardanoDbUtils::wait_spinner(
108+
progress_printer,
109+
MessageBuilder::new().compute_cardano_database_message(certificate, merkle_proof),
110+
)
111+
.await
94112
.with_context(|| "Can not compute the cardano db snapshot message")?;
95113

96114
Ok(message)

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use anyhow::Context;
88
use chrono::{DateTime, Utc};
99
use clap::Parser;
1010
use mithril_client::{
11-
CardanoDatabaseSnapshot, ComputeCardanoDatabaseMessageError, ImmutableVerificationResult,
12-
MithrilResult, cardano_database_client::ImmutableFileRange, common::ImmutableFileNumber,
11+
CardanoDatabaseSnapshot, MithrilResult,
12+
cardano_database_client::ImmutableFileRange,
13+
cardano_database_client::{CardanoDatabaseVerificationError, ImmutableVerificationResult},
14+
common::ImmutableFileNumber,
1315
};
1416

1517
use crate::{
@@ -85,7 +87,7 @@ impl CardanoDbVerifyCommand {
8587
} else {
8688
ProgressOutputType::Tty
8789
};
88-
let progress_printer = ProgressPrinter::new(progress_output_type, 4);
90+
let progress_printer = ProgressPrinter::new(progress_output_type, 5);
8991
let client = client_builder(context.config_parameters())?
9092
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
9193
progress_output_type,
@@ -146,19 +148,21 @@ impl CardanoDbVerifyCommand {
146148
immutable_file_range,
147149
allow_missing: self.allow_missing,
148150
};
149-
let message = shared_steps::compute_cardano_db_snapshot_message(
151+
152+
let merkle_proof = shared_steps::verify_cardano_database(
150153
3,
151154
&progress_printer,
155+
&client,
152156
&certificate,
153157
&cardano_db_message,
154158
&options,
155159
&verified_digests,
156160
)
157161
.await;
158162

159-
match message {
160-
Err(e) => match e.downcast_ref::<ComputeCardanoDatabaseMessageError>() {
161-
Some(ComputeCardanoDatabaseMessageError::ImmutableFilesVerification(lists)) => {
163+
match merkle_proof {
164+
Err(e) => match e.downcast_ref::<CardanoDatabaseVerificationError>() {
165+
Some(CardanoDatabaseVerificationError::ImmutableFilesVerification(lists)) => {
162166
Self::print_immutables_verification_error(
163167
lists,
164168
context.is_json_output_enabled(),
@@ -167,10 +171,18 @@ impl CardanoDbVerifyCommand {
167171
}
168172
_ => Err(e),
169173
},
170-
Ok(message) => {
174+
Ok(merkle_proof) => {
175+
let message = shared_steps::compute_cardano_db_snapshot_message(
176+
4,
177+
&progress_printer,
178+
&certificate,
179+
&merkle_proof,
180+
)
181+
.await?;
182+
171183
shared_steps::verify_message_matches_certificate(
172184
&context.logger().clone(),
173-
4,
185+
5,
174186
&progress_printer,
175187
&certificate,
176188
&message,

0 commit comments

Comments
 (0)