Skip to content

Commit fda10d2

Browse files
committed
WIP: TODO do we want to display 10 elements of each missing and tampered lists ?
1 parent f443e6d commit fda10d2

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

mithril-client/src/message.rs

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,59 @@ use crate::{
3737
common::{ProtocolMessage, ProtocolMessagePartKey},
3838
};
3939

40+
/// Type containing the lists of immutable files that are missing or tampered.
4041
#[derive(Debug, PartialEq)]
4142
pub struct ImmutableFilesLists {
42-
pub dir_path: PathBuf,
43+
/// The immutables files directory.
44+
pub immutables_dir: PathBuf,
45+
/// List of missing immutable files.
4346
pub missing: Vec<ImmutableFileName>,
47+
/// List of tampered immutable files.
4448
pub tampered: Vec<ImmutableFileName>,
4549
}
4650

51+
/// Compute Cardano database message related errors.
4752
#[derive(Error, Debug)]
4853
pub enum ComputeCardanoDatabaseMessageError {
54+
/// Error related to the verification of immutable files.
4955
ImmutableFilesVerification(ImmutableFilesLists),
5056

57+
/// Error related to the immutable files digests computation.
5158
ImmutableFilesDigester(#[from] ImmutableDigesterError),
5259

60+
/// Error related to the Merkle proof verification.
5361
MerkleProofVerification(#[source] MithrilError),
5462

63+
/// Error related to the immutable files range.
5564
ImmutableFilesRange(#[source] MithrilError),
5665
}
5766

5867
impl fmt::Display for ComputeCardanoDatabaseMessageError {
5968
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6069
match self {
61-
ComputeCardanoDatabaseMessageError::ImmutableFilesVerification(files) => {
62-
write!(f, "immutable files: {:?}", files)
70+
ComputeCardanoDatabaseMessageError::ImmutableFilesVerification(lists) => {
71+
let missing_number = lists.missing.len();
72+
let missing_files_subset = lists
73+
.missing
74+
.iter()
75+
.take(10)
76+
.map(|file| lists.immutables_dir.join(file).to_string_lossy().to_string())
77+
.collect::<Vec<_>>()
78+
.join("\n");
79+
80+
write!(
81+
f,
82+
"Number of missing immutable files: {missing_number:?}\n{missing_files_subset}"
83+
)
6384
}
6485
ComputeCardanoDatabaseMessageError::ImmutableFilesDigester(e) => {
65-
write!(f, "Immutable files digester error: {}", e)
86+
write!(f, "Immutable files digester error: {e}")
6687
}
6788
ComputeCardanoDatabaseMessageError::MerkleProofVerification(e) => {
68-
write!(f, "Merkle proof verification error: {}", e)
89+
write!(f, "Merkle proof verification error: {e}")
6990
}
7091
ComputeCardanoDatabaseMessageError::ImmutableFilesRange(e) => {
71-
write!(f, "Immutable files range error: {}", e)
92+
write!(f, "Immutable files range error: {e}")
7293
}
7394
}
7495
}
@@ -233,7 +254,7 @@ impl MessageBuilder {
233254

234255
Err(
235256
ComputeCardanoDatabaseMessageError::ImmutableFilesVerification(ImmutableFilesLists {
236-
dir_path: Self::immutable_dir(database_dir),
257+
immutables_dir: Self::immutable_dir(database_dir),
237258
missing: missing_immutable_files,
238259
tampered: tampered_files,
239260
}),
@@ -548,7 +569,7 @@ mod tests {
548569
assert_eq!(
549570
error_lists,
550571
ImmutableFilesLists {
551-
dir_path: MessageBuilder::immutable_dir(&database_dir),
572+
immutables_dir: MessageBuilder::immutable_dir(&database_dir),
552573
missing: to_vec_immutable_file_name(&files_to_remove),
553574
tampered: vec![],
554575
}
@@ -594,7 +615,7 @@ mod tests {
594615
assert_eq!(
595616
error_lists,
596617
ImmutableFilesLists {
597-
dir_path: MessageBuilder::immutable_dir(&database_dir),
618+
immutables_dir: MessageBuilder::immutable_dir(&database_dir),
598619
missing: vec![],
599620
tampered: to_vec_immutable_file_name(&files_to_tamper),
600621
}
@@ -643,12 +664,51 @@ mod tests {
643664
assert_eq!(
644665
error_lists,
645666
ImmutableFilesLists {
646-
dir_path: MessageBuilder::immutable_dir(&database_dir),
667+
immutables_dir: MessageBuilder::immutable_dir(&database_dir),
647668
missing: to_vec_immutable_file_name(&files_to_remove),
648669
tampered: to_vec_immutable_file_name(&files_to_tamper),
649670
}
650671
)
651672
}
652673
}
674+
675+
mod compute_cardano_database_message_error {
676+
use super::*;
677+
678+
//TODO do we really want to display 10 elements of each missing and tampered list? or just the number of elements?
679+
#[test]
680+
fn display_immutable_files_verification_should_limit_lists_to_10_elements() {
681+
let missing_file_number = 15;
682+
let missing: Vec<ImmutableFileName> = (1..=missing_file_number)
683+
.map(|i| ImmutableFileName::from(format!("{i:05}.chunk")))
684+
.collect();
685+
let expected_displayed_missing =
686+
missing.iter().take(10).cloned().collect::<Vec<_>>();
687+
let immutables_dir = PathBuf::from("/path/to/immutables");
688+
let lists = ImmutableFilesLists {
689+
immutables_dir: immutables_dir.clone(),
690+
missing,
691+
tampered: vec![],
692+
};
693+
694+
let error = ComputeCardanoDatabaseMessageError::ImmutableFilesVerification(lists);
695+
let display = format!("{error}");
696+
assert!(
697+
display.contains(&format!(
698+
"Number of missing immutable files: {missing_file_number}"
699+
)),
700+
"{display}"
701+
);
702+
for file_name in expected_displayed_missing.iter() {
703+
let file_path = immutables_dir.join(file_name.as_str());
704+
assert!(
705+
display.contains(&file_path.display().to_string()),
706+
"Expected '{display}' to contain : '{}'",
707+
file_path.display()
708+
);
709+
}
710+
assert!(!display.contains("00011.chunk"), "{display}");
711+
}
712+
}
653713
}
654714
}

0 commit comments

Comments
 (0)