Skip to content

Commit 84a469c

Browse files
committed
Make Cert hash migrator fail if an hash did not change
1 parent 6e1a4b4 commit 84a469c

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

mithril-aggregator/src/tools/certificates_hash_migrator.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,16 @@ impl CertificatesHashMigrator {
7171
old_previous_hash
7272
};
7373

74-
let new_hash = certificate.compute_hash();
74+
let new_hash = {
75+
let computed_hash = certificate.compute_hash();
76+
// return none if the hash did not change to trigger an error
77+
(computed_hash != certificate.hash).then_some(computed_hash)
78+
}
79+
.ok_or(format!(
80+
"Hash did not change for certificate {:?}, hash: {}",
81+
certificate.beacon, certificate.hash
82+
))?;
83+
7584
old_and_new_hashes.insert(certificate.hash.clone(), new_hash.clone());
7685

7786
if certificate.is_genesis() {
@@ -555,4 +564,26 @@ mod test {
555564
)
556565
.await;
557566
}
567+
568+
#[tokio::test]
569+
async fn should_fail_if_any_hash_doesnt_change() {
570+
let connection = Arc::new(Mutex::new(connection_without_foreign_key_support()));
571+
let certificate = {
572+
let mut cert = dummy_genesis("whatever", 1, 2);
573+
cert.hash = cert.compute_hash();
574+
cert
575+
};
576+
fill_certificates_and_signed_entities_in_db(connection.clone(), &[(certificate, None)])
577+
.await
578+
.unwrap();
579+
580+
let migrator = CertificatesHashMigrator::new(
581+
CertificateRepository::new(connection.clone()),
582+
Arc::new(SignedEntityStoreAdapter::new(connection.clone())),
583+
);
584+
migrator
585+
.migrate()
586+
.await
587+
.expect_err("Migration should fail if an hash doesnt change");
588+
}
558589
}

0 commit comments

Comments
 (0)