Skip to content

Commit 6b12b71

Browse files
committed
WIP: adapt call of compute_cardano_database_message to handle the new error
1 parent fda10d2 commit 6b12b71

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl PreparedCardanoDbV1Download {
185185
db_dir: &Path,
186186
) -> MithrilResult<ProtocolMessage> {
187187
progress_printer.report_step(step_number, "Computing the cardano db message")?;
188-
let message = CardanoDbUtils::wait_spinner(
188+
let message = CardanoDbUtils::wait_spinner_v1(
189189
progress_printer,
190190
MessageBuilder::new().compute_snapshot_message(certificate, db_dir),
191191
)

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use std::{
44
sync::Arc,
55
};
66

7-
use anyhow::Context;
7+
use anyhow::{Context, anyhow};
88
use chrono::Utc;
99
use clap::Parser;
10-
use mithril_client::MithrilResult;
10+
use mithril_client::{ComputeCardanoDatabaseMessageError, MithrilResult};
1111

1212
use crate::{
1313
CommandContext,
@@ -122,26 +122,38 @@ impl CardanoDbVerifyCommand {
122122
db_dir,
123123
&verified_digests,
124124
)
125-
.await?;
126-
127-
shared_steps::verify_message_matches_certificate(
128-
&context.logger().clone(),
129-
4,
130-
&progress_printer,
131-
&certificate,
132-
&message,
133-
&cardano_db_message,
134-
db_dir,
135-
)
136-
.await?;
137-
138-
Self::log_verified_information(
139-
db_dir,
140-
&cardano_db_message.hash,
141-
context.is_json_output_enabled(),
142-
)?;
143-
144-
Ok(())
125+
.await;
126+
127+
//TODO due to wait spinner, we need to cast error to access the inner error's lists, could it be improved to avoid downcasting?
128+
match message {
129+
Err(e) => match e.downcast_ref::<ComputeCardanoDatabaseMessageError>() {
130+
Some(ComputeCardanoDatabaseMessageError::ImmutableFilesVerification(lists)) => {
131+
// let missing_files = lists.missing;
132+
Ok(())
133+
}
134+
_ => Err(anyhow!(e)),
135+
},
136+
Ok(message) => {
137+
shared_steps::verify_message_matches_certificate(
138+
&context.logger().clone(),
139+
4,
140+
&progress_printer,
141+
&certificate,
142+
&message,
143+
&cardano_db_message,
144+
db_dir,
145+
)
146+
.await?;
147+
148+
Self::log_verified_information(
149+
db_dir,
150+
&cardano_db_message.hash,
151+
context.is_json_output_enabled(),
152+
)?;
153+
154+
Ok(())
155+
}
156+
}
145157
}
146158

147159
fn log_verified_information(

mithril-client-cli/src/utils/cardano_db.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use indicatif::{MultiProgress, ProgressBar};
44
use std::time::Duration;
55

66
use super::CardanoDbDownloadCheckerError;
7-
use mithril_client::{MithrilError, MithrilResult};
7+
use mithril_client::{ComputeCardanoDatabaseMessageError, MithrilError, MithrilResult};
88

99
/// Utility functions for to the CardanoDb commands
1010
pub struct CardanoDbUtils;
@@ -22,7 +22,7 @@ impl CardanoDbUtils {
2222
}
2323

2424
/// Display a spinner while waiting for the result of a future
25-
pub async fn wait_spinner<T>(
25+
pub async fn wait_spinner_v1<T>(
2626
progress_bar: &MultiProgress,
2727
future: impl Future<Output = MithrilResult<T>>,
2828
) -> MithrilResult<T> {
@@ -40,6 +40,28 @@ impl CardanoDbUtils {
4040
}
4141
}
4242

43+
/// Display a spinner while waiting for the result of a future
44+
pub async fn wait_spinner<T>(
45+
progress_bar: &MultiProgress,
46+
future: impl Future<Output = Result<T, ComputeCardanoDatabaseMessageError>>,
47+
) -> MithrilResult<T> {
48+
let pb = progress_bar.add(ProgressBar::new_spinner());
49+
let spinner = async move {
50+
loop {
51+
pb.tick();
52+
tokio::time::sleep(Duration::from_millis(50)).await;
53+
}
54+
};
55+
56+
tokio::select! {
57+
_ = spinner => Err(anyhow!("timeout")),
58+
res = future => {match res {
59+
Ok(value) => Ok(value),
60+
Err(e) => Err(anyhow!(e)),
61+
}},
62+
}
63+
}
64+
4365
pub fn format_bytes_to_gigabytes(bytes: u64) -> String {
4466
let size_in_giga = bytes as f64 / (1024.0 * 1024.0 * 1024.0);
4567

0 commit comments

Comments
 (0)