@@ -48,6 +48,7 @@ impl CertificatesHashMigrator {
48
48
// arbitrary high value to get all existing certificates
49
49
. get_latest_certificates ( usize:: MAX )
50
50
. await ?;
51
+ let mut certificates_to_remove = vec ! [ ] ;
51
52
52
53
let mut migrated_certificates = vec ! [ ] ;
53
54
let mut old_and_new_hashes: HashMap < String , String > = HashMap :: new ( ) ;
@@ -57,7 +58,7 @@ impl CertificatesHashMigrator {
57
58
// in order to have a strong guarantee that when inserting a certificate in the db its
58
59
// previous_hash exist we have to work in the reverse order.
59
60
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 ( ) {
61
62
let old_previous_hash = if certificate. is_genesis ( ) {
62
63
certificate. previous_hash . clone ( )
63
64
} else {
@@ -72,40 +73,37 @@ impl CertificatesHashMigrator {
72
73
old_previous_hash
73
74
} ;
74
75
75
- let new_hash = {
76
+ if let Some ( new_hash) = {
76
77
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
78
79
( 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
+ }
88
100
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) ;
96
104
} 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 ) ;
105
106
}
106
-
107
- certificate. hash = new_hash;
108
- migrated_certificates. push ( certificate) ;
109
107
}
110
108
111
109
// 2 - Certificates migrated, we can insert them in the db
@@ -123,7 +121,7 @@ impl CertificatesHashMigrator {
123
121
} ) ?;
124
122
}
125
123
126
- Ok ( ( old_certificates , old_and_new_hashes) )
124
+ Ok ( ( certificates_to_remove , old_and_new_hashes) )
127
125
}
128
126
129
127
async fn update_signed_entities_certificate_hash (
@@ -590,7 +588,7 @@ mod test {
590
588
}
591
589
592
590
#[ tokio:: test]
593
- async fn should_fail_if_any_hash_doesnt_change ( ) {
591
+ async fn should_not_fail_if_some_hash_dont_change ( ) {
594
592
let connection = Arc :: new ( connection_without_foreign_key_support ( ) ) ;
595
593
let certificate = {
596
594
let mut cert = dummy_genesis ( "whatever" , 1 , 2 ) ;
@@ -608,6 +606,6 @@ mod test {
608
606
migrator
609
607
. migrate ( )
610
608
. await
611
- . expect_err ( "Migration should fail if an hash doesnt change" ) ;
609
+ . expect ( "Migration should not fail if a hash doesn't change" ) ;
612
610
}
613
611
}
0 commit comments