Skip to content

Commit daa1620

Browse files
author
Clément Turmel
committed
refactor(client-lib): rename ComputeCardanoDatabaseMessageError to CardanoDatabaseVerificationError
1 parent f3faf78 commit daa1620

File tree

3 files changed

+220
-13
lines changed

3 files changed

+220
-13
lines changed

mithril-client/src/cardano_database_client/api.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ use mithril_cardano_node_internal_database::entities::ImmutableFile;
1616

1717
use crate::aggregator_client::AggregatorClient;
1818
#[cfg(feature = "fs")]
19-
use crate::cardano_database_client::{
20-
VerifiedDigests, proving::ComputeCardanoDatabaseMessageError,
21-
};
19+
use crate::cardano_database_client::{VerifiedDigests, proving::CardanoDatabaseVerificationError};
2220
#[cfg(feature = "fs")]
2321
use crate::feedback::FeedbackSender;
2422
#[cfg(feature = "fs")]
@@ -126,7 +124,7 @@ impl CardanoDatabaseClient {
126124
allow_missing: bool,
127125
database_dir: &Path,
128126
verified_digests: &VerifiedDigests,
129-
) -> Result<MKProof, ComputeCardanoDatabaseMessageError> {
127+
) -> Result<MKProof, CardanoDatabaseVerificationError> {
130128
self.artifact_prover
131129
.verify_cardano_database(
132130
certificate,

mithril-client/src/cardano_database_client/proving.rs

Lines changed: 217 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ cfg_fs! {
6363

6464
/// Compute Cardano database message related errors.
6565
#[derive(Error, Debug)]
66-
pub enum ComputeCardanoDatabaseMessageError {
66+
pub enum CardanoDatabaseVerificationError {
6767
/// Error related to the verification of immutable files.
6868
ImmutableFilesVerification(ImmutableVerificationResult),
6969

@@ -78,9 +78,62 @@ cfg_fs! {
7878
}
7979

8080
//TODO to retrieve from message.rs
81-
impl fmt::Display for ComputeCardanoDatabaseMessageError {
82-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83-
Ok(())
81+
impl fmt::Display for CardanoDatabaseVerificationError {
82+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83+
match self {
84+
CardanoDatabaseVerificationError::ImmutableFilesVerification(lists) => {
85+
fn get_first_10_files_path(
86+
files: &[ImmutableFileName],
87+
immutables_dir: &Path,
88+
) -> String {
89+
files
90+
.iter()
91+
.take(10)
92+
.map(|file| immutables_dir.join(file).to_string_lossy().to_string())
93+
.collect::<Vec<_>>()
94+
.join("\n")
95+
}
96+
97+
if !lists.missing.is_empty() {
98+
let missing_files_subset = get_first_10_files_path(&lists.missing, &lists.immutables_dir);
99+
writeln!(
100+
f,
101+
"Number of missing immutable files: {}",
102+
lists.missing.len()
103+
)?;
104+
writeln!(f, "First 10 missing immutable files paths:")?;
105+
writeln!(f, "{missing_files_subset}")?;
106+
}
107+
if !lists.missing.is_empty() && !lists.tampered.is_empty() {
108+
writeln!(f)?;
109+
}
110+
if !lists.tampered.is_empty() {
111+
let tampered_files_subset = get_first_10_files_path(&lists.tampered, &lists.immutables_dir);
112+
writeln!(f,"Number of tampered immutable files: {}",lists.tampered.len())?;
113+
writeln!(f, "First 10 tampered immutable files paths:")?;
114+
writeln!(f, "{tampered_files_subset}")?;
115+
}
116+
if (!lists.missing.is_empty() || !lists.tampered.is_empty()) && !lists.non_verifiable.is_empty() {
117+
writeln!(f)?;
118+
}
119+
if !lists.non_verifiable.is_empty() {
120+
let non_verifiable_files_subset = get_first_10_files_path(&lists.non_verifiable, &lists.immutables_dir);
121+
writeln!(f, "Number of non verifiable immutable files: {}", lists.non_verifiable.len())?;
122+
writeln!(f, "First 10 non verifiable immutable files paths:")?;
123+
writeln!(f, "{non_verifiable_files_subset}")?;
124+
}
125+
Ok(())
126+
}
127+
CardanoDatabaseVerificationError::DigestsComputation(e) => {
128+
write!(f, "Immutable files digester error: {e:?}")
129+
}
130+
CardanoDatabaseVerificationError::MerkleProofVerification(e) => {
131+
write!(f, "Merkle proof verification error: {e:?}")
132+
}
133+
CardanoDatabaseVerificationError::ImmutableFilesRangeCreation(e) => {
134+
write!(f, "Immutable files range error: {e:?}")
135+
}
136+
}
84137
}
85138
}
86139
}
@@ -227,11 +280,11 @@ impl InternalArtifactProver {
227280
allow_missing: bool,
228281
database_dir: &Path,
229282
verified_digests: &VerifiedDigests,
230-
) -> Result<MKProof, ComputeCardanoDatabaseMessageError> {
283+
) -> Result<MKProof, CardanoDatabaseVerificationError> {
231284
let network = certificate.metadata.network.clone();
232285
let immutable_file_number_range = immutable_file_range
233286
.to_range_inclusive(cardano_database_snapshot.beacon.immutable_file_number)
234-
.map_err(ComputeCardanoDatabaseMessageError::ImmutableFilesRangeCreation)?;
287+
.map_err(CardanoDatabaseVerificationError::ImmutableFilesRangeCreation)?;
235288
let missing_immutable_files = if allow_missing {
236289
vec![]
237290
} else {
@@ -253,7 +306,7 @@ impl InternalArtifactProver {
253306
{
254307
merkle_proof
255308
.verify()
256-
.map_err(ComputeCardanoDatabaseMessageError::MerkleProofVerification)?;
309+
.map_err(CardanoDatabaseVerificationError::MerkleProofVerification)?;
257310

258311
//TODO: we are not creating a message here anymore, we just wantreturning the merkle proof
259312
// let mut message = certificate.protocol_message.clone();
@@ -280,7 +333,7 @@ impl InternalArtifactProver {
280333
Ok(_) => (vec![], vec![]),
281334
};
282335
Err(
283-
ComputeCardanoDatabaseMessageError::ImmutableFilesVerification(
336+
CardanoDatabaseVerificationError::ImmutableFilesVerification(
284337
ImmutableVerificationResult {
285338
immutables_dir: Self::immutable_dir(database_dir),
286339
missing: missing_immutable_files,
@@ -1134,7 +1187,7 @@ mod tests {
11341187
.expect_err("verify_cardano_database should fail if a immutable is missing");
11351188

11361189
let error_lists = match error {
1137-
ComputeCardanoDatabaseMessageError::ImmutableFilesVerification(lists) => lists,
1190+
CardanoDatabaseVerificationError::ImmutableFilesVerification(lists) => lists,
11381191
_ => panic!("Expected ImmutableFilesVerification error, got: {error}"),
11391192
};
11401193

@@ -1149,4 +1202,159 @@ mod tests {
11491202
);
11501203
}
11511204
}
1205+
1206+
mod compute_cardano_database_message_error {
1207+
use super::*;
1208+
1209+
fn generate_immutable_files_verification_error(
1210+
missing_range: Option<RangeInclusive<usize>>,
1211+
tampered_range: Option<RangeInclusive<usize>>,
1212+
non_verifiable_range: Option<RangeInclusive<usize>>,
1213+
immutable_path: &str,
1214+
) -> CardanoDatabaseVerificationError {
1215+
let missing: Vec<ImmutableFileName> = match missing_range {
1216+
Some(range) => range
1217+
.map(|i| ImmutableFileName::from(format!("{i:05}.chunk")))
1218+
.collect(),
1219+
None => vec![],
1220+
};
1221+
let tampered: Vec<ImmutableFileName> = match tampered_range {
1222+
Some(range) => range
1223+
.map(|i| ImmutableFileName::from(format!("{i:05}.chunk")))
1224+
.collect(),
1225+
None => vec![],
1226+
};
1227+
1228+
let non_verifiable: Vec<ImmutableFileName> = match non_verifiable_range {
1229+
Some(range) => range
1230+
.map(|i| ImmutableFileName::from(format!("{i:05}.chunk")))
1231+
.collect(),
1232+
None => vec![],
1233+
};
1234+
1235+
CardanoDatabaseVerificationError::ImmutableFilesVerification(
1236+
ImmutableVerificationResult {
1237+
immutables_dir: PathBuf::from(immutable_path),
1238+
missing,
1239+
tampered,
1240+
non_verifiable,
1241+
},
1242+
)
1243+
}
1244+
1245+
fn normalize_path_separators(s: &str) -> String {
1246+
s.replace('\\', "/")
1247+
}
1248+
1249+
#[test]
1250+
fn display_immutable_files_verification_error_should_displayed_lists_with_10_elements() {
1251+
let error = generate_immutable_files_verification_error(
1252+
Some(1..=15),
1253+
Some(20..=31),
1254+
Some(40..=41),
1255+
"/path/to/immutables",
1256+
);
1257+
1258+
let display = normalize_path_separators(&format!("{error}"));
1259+
1260+
assert_eq!(
1261+
display,
1262+
r###"Number of missing immutable files: 15
1263+
First 10 missing immutable files paths:
1264+
/path/to/immutables/00001.chunk
1265+
/path/to/immutables/00002.chunk
1266+
/path/to/immutables/00003.chunk
1267+
/path/to/immutables/00004.chunk
1268+
/path/to/immutables/00005.chunk
1269+
/path/to/immutables/00006.chunk
1270+
/path/to/immutables/00007.chunk
1271+
/path/to/immutables/00008.chunk
1272+
/path/to/immutables/00009.chunk
1273+
/path/to/immutables/00010.chunk
1274+
1275+
Number of tampered immutable files: 12
1276+
First 10 tampered immutable files paths:
1277+
/path/to/immutables/00020.chunk
1278+
/path/to/immutables/00021.chunk
1279+
/path/to/immutables/00022.chunk
1280+
/path/to/immutables/00023.chunk
1281+
/path/to/immutables/00024.chunk
1282+
/path/to/immutables/00025.chunk
1283+
/path/to/immutables/00026.chunk
1284+
/path/to/immutables/00027.chunk
1285+
/path/to/immutables/00028.chunk
1286+
/path/to/immutables/00029.chunk
1287+
1288+
Number of non verifiable immutable files: 2
1289+
First 10 non verifiable immutable files paths:
1290+
/path/to/immutables/00040.chunk
1291+
/path/to/immutables/00041.chunk
1292+
"###
1293+
);
1294+
}
1295+
1296+
#[test]
1297+
fn display_immutable_files_should_display_tampered_files_only() {
1298+
let error = generate_immutable_files_verification_error(
1299+
None,
1300+
Some(1..=1),
1301+
None,
1302+
"/path/to/immutables",
1303+
);
1304+
1305+
let display = normalize_path_separators(&format!("{error}"));
1306+
1307+
assert_eq!(
1308+
display,
1309+
r###"Number of tampered immutable files: 1
1310+
First 10 tampered immutable files paths:
1311+
/path/to/immutables/00001.chunk
1312+
"###
1313+
);
1314+
}
1315+
1316+
#[test]
1317+
fn display_immutable_files_should_display_missing_files_only() {
1318+
let error = generate_immutable_files_verification_error(
1319+
Some(1..=1),
1320+
None,
1321+
None,
1322+
"/path/to/immutables",
1323+
);
1324+
1325+
let display = normalize_path_separators(&format!("{error}"));
1326+
1327+
assert_eq!(
1328+
display,
1329+
r###"Number of missing immutable files: 1
1330+
First 10 missing immutable files paths:
1331+
/path/to/immutables/00001.chunk
1332+
"###
1333+
);
1334+
}
1335+
1336+
#[test]
1337+
fn display_immutable_files_should_display_non_verifiable_files_only() {
1338+
let error = generate_immutable_files_verification_error(
1339+
None,
1340+
None,
1341+
Some(1..=5),
1342+
"/path/to/immutables",
1343+
);
1344+
1345+
let display = normalize_path_separators(&format!("{error}"));
1346+
1347+
assert_eq!(
1348+
display,
1349+
r###"Number of non verifiable immutable files: 5
1350+
First 10 non verifiable immutable files paths:
1351+
/path/to/immutables/00001.chunk
1352+
/path/to/immutables/00002.chunk
1353+
/path/to/immutables/00003.chunk
1354+
/path/to/immutables/00004.chunk
1355+
/path/to/immutables/00005.chunk
1356+
"###
1357+
);
1358+
}
1359+
}
11521360
}

mithril-client/src/message.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ mod tests {
829829
}
830830
}
831831

832+
//TODO: TO REMOVE (Moved to proving.rs)
832833
mod compute_cardano_database_message_error {
833834
use super::*;
834835

0 commit comments

Comments
 (0)