Skip to content

Commit 4a1bee6

Browse files
committed
Make certificate hashes re-computation tolerate unchanged hashes
1 parent d8d97cc commit 4a1bee6

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

mithril-aggregator/src/tools/certificates_hash_migrator.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl CertificatesHashMigrator {
4848
// arbitrary high value to get all existing certificates
4949
.get_latest_certificates(usize::MAX)
5050
.await?;
51+
let mut certificates_to_remove = vec![];
5152

5253
let mut migrated_certificates = vec![];
5354
let mut old_and_new_hashes: HashMap<String, String> = HashMap::new();
@@ -57,7 +58,7 @@ impl CertificatesHashMigrator {
5758
// in order to have a strong guarantee that when inserting a certificate in the db its
5859
// previous_hash exist we have to work in the reverse order.
5960
debug!("🔧 Certificate Hash Migrator: computing new hash for all certificates");
60-
for mut certificate in old_certificates.clone().into_iter().rev() {
61+
for mut certificate in old_certificates.into_iter().rev() {
6162
let old_previous_hash = if certificate.is_genesis() {
6263
certificate.previous_hash.clone()
6364
} else {
@@ -72,40 +73,37 @@ impl CertificatesHashMigrator {
7273
old_previous_hash
7374
};
7475

75-
let new_hash = {
76+
if let Some(new_hash) = {
7677
let computed_hash = certificate.compute_hash();
77-
// return none if the hash did not change to trigger an error
78+
// return none if the hash did not change
7879
(computed_hash != certificate.hash).then_some(computed_hash)
79-
}
80-
.ok_or(anyhow!(
81-
"Hash did not change for certificate {:?}, hash: {}",
82-
certificate.beacon,
83-
certificate.hash
84-
))?
85-
.to_owned();
86-
87-
old_and_new_hashes.insert(certificate.hash.clone(), new_hash.clone());
80+
} {
81+
old_and_new_hashes.insert(certificate.hash.clone(), new_hash.clone());
82+
83+
if certificate.is_genesis() {
84+
trace!(
85+
"🔧 Certificate Hash Migrator: new hash computed for genesis certificate {:?}",
86+
certificate.beacon;
87+
"old_hash" => &certificate.hash,
88+
"new_hash" => &new_hash,
89+
);
90+
} else {
91+
trace!(
92+
"🔧 Certificate Hash Migrator: new hash computed for certificate {:?}",
93+
certificate.beacon;
94+
"old_hash" => &certificate.hash,
95+
"new_hash" => &new_hash,
96+
"old_previous_hash" => &old_previous_hash,
97+
"new_previous_hash" => &certificate.previous_hash
98+
);
99+
}
88100

89-
if certificate.is_genesis() {
90-
trace!(
91-
"🔧 Certificate Hash Migrator: new hash computed for genesis certificate {:?}",
92-
certificate.beacon;
93-
"old_hash" => &certificate.hash,
94-
"new_hash" => &new_hash,
95-
);
101+
certificates_to_remove.push(certificate.clone());
102+
certificate.hash = new_hash;
103+
migrated_certificates.push(certificate);
96104
} else {
97-
trace!(
98-
"🔧 Certificate Hash Migrator: new hash computed for certificate {:?}",
99-
certificate.beacon;
100-
"old_hash" => &certificate.hash,
101-
"new_hash" => &new_hash,
102-
"old_previous_hash" => &old_previous_hash,
103-
"new_previous_hash" => &certificate.previous_hash
104-
);
105+
old_and_new_hashes.insert(certificate.hash.clone(), certificate.hash);
105106
}
106-
107-
certificate.hash = new_hash;
108-
migrated_certificates.push(certificate);
109107
}
110108

111109
// 2 - Certificates migrated, we can insert them in the db
@@ -123,7 +121,7 @@ impl CertificatesHashMigrator {
123121
})?;
124122
}
125123

126-
Ok((old_certificates, old_and_new_hashes))
124+
Ok((certificates_to_remove, old_and_new_hashes))
127125
}
128126

129127
async fn update_signed_entities_certificate_hash(
@@ -590,7 +588,7 @@ mod test {
590588
}
591589

592590
#[tokio::test]
593-
async fn should_fail_if_any_hash_doesnt_change() {
591+
async fn should_not_fail_if_some_hash_dont_change() {
594592
let connection = Arc::new(connection_without_foreign_key_support());
595593
let certificate = {
596594
let mut cert = dummy_genesis("whatever", 1, 2);
@@ -608,6 +606,6 @@ mod test {
608606
migrator
609607
.migrate()
610608
.await
611-
.expect_err("Migration should fail if an hash doesnt change");
609+
.expect("Migration should not fail if a hash doesn't change");
612610
}
613611
}

0 commit comments

Comments
 (0)