@@ -54,19 +54,18 @@ impl LocalStorageCertificateVerifierCache {
5454 previous_certificate_hash : & PreviousCertificateHash ,
5555 expire_at : DateTime < Utc > ,
5656 ) -> MithrilResult < ( ) > {
57- if let Some ( storage) = open_local_storage ( ) ? {
58- let key = self . cache_key ( certificate_hash) ;
59- storage
60- . set_item (
61- & key,
62- & serde_json:: to_string ( & CachedCertificate :: new (
63- previous_certificate_hash,
64- expire_at,
65- ) )
66- . map_err ( |err| anyhow ! ( "Error serializing cache: {err:?}" ) ) ?,
67- )
68- . map_err ( |err| anyhow ! ( "Error storing key `{key}` in local storage: {err:?}" ) ) ?;
69- }
57+ let key = self . cache_key ( certificate_hash) ;
58+ open_local_storage ( ) ?
59+ . set_item (
60+ & key,
61+ & serde_json:: to_string ( & CachedCertificate :: new (
62+ previous_certificate_hash,
63+ expire_at,
64+ ) )
65+ . map_err ( |err| anyhow ! ( "Error serializing cache: {err:?}" ) ) ?,
66+ )
67+ . map_err ( |err| anyhow ! ( "Error storing key `{key}` in local storage: {err:?}" ) ) ?;
68+
7069 Ok ( ( ) )
7170 }
7271
@@ -80,11 +79,12 @@ impl LocalStorageCertificateVerifierCache {
8079 }
8180}
8281
83- fn open_local_storage ( ) -> MithrilResult < Option < Storage > > {
82+ fn open_local_storage ( ) -> MithrilResult < Storage > {
8483 let window = window ( )
8584 . with_context ( || "No window object" ) ?
8685 . local_storage ( )
87- . map_err ( |err| anyhow ! ( "Error accessing local storage: {err:?}" ) ) ?;
86+ . map_err ( |err| anyhow ! ( "Error accessing local storage: {err:?}" ) ) ?
87+ . with_context ( || "No local storage object" ) ?;
8888 Ok ( window)
8989}
9090
@@ -108,50 +108,45 @@ impl CertificateVerifierCache for LocalStorageCertificateVerifierCache {
108108 & self ,
109109 certificate_hash : & CertificateHash ,
110110 ) -> MithrilResult < Option < String > > {
111- match open_local_storage ( ) ? {
112- None => return Ok ( None ) ,
113- Some ( storage) => {
114- let key = self . cache_key ( certificate_hash) ;
115- match storage. get_item ( & key) . map_err ( |err| {
116- anyhow ! ( "Error accessing key `{key}` from local storage: {err:?}" )
117- } ) ? {
118- Some ( value) => {
119- let cached = Self :: parse_cached_certificate ( value) ?;
120- if Utc :: now ( ) >= cached. expire_at {
121- Ok ( None )
122- } else {
123- Ok ( Some ( cached. previous_hash ) )
124- }
125- }
126- None => Ok ( None ) ,
111+ let key = self . cache_key ( certificate_hash) ;
112+ match open_local_storage ( ) ?
113+ . get_item ( & key)
114+ . map_err ( |err| anyhow ! ( "Error accessing key `{key}` from local storage: {err:?}" ) ) ?
115+ {
116+ Some ( value) => {
117+ let cached = Self :: parse_cached_certificate ( value) ?;
118+ if Utc :: now ( ) >= cached. expire_at {
119+ Ok ( None )
120+ } else {
121+ Ok ( Some ( cached. previous_hash ) )
127122 }
128123 }
124+ None => Ok ( None ) ,
129125 }
130126 }
131127
132128 async fn reset ( & self ) -> MithrilResult < ( ) > {
133- if let Some ( storage) = open_local_storage ( ) ? {
134- let len = storage
135- . length ( )
136- . map_err ( |err| anyhow ! ( "Error accessing local storage length: {err:?}" ) ) ?;
137- let mut key_to_remove = vec ! [ ] ;
138-
139- for i in 0 ..len {
140- match storage. key ( i) . map_err ( |err| {
141- anyhow ! ( "Error accessing key index `{i}` from local storage: {err:?}" )
142- } ) ? {
143- Some ( key) if key. starts_with ( & self . cache_key_prefix ) => key_to_remove. push ( key) ,
144- _ => continue ,
145- }
146- }
129+ let storage = open_local_storage ( ) ?;
130+ let len = storage
131+ . length ( )
132+ . map_err ( |err| anyhow ! ( "Error accessing local storage length: {err:?}" ) ) ?;
133+ let mut key_to_remove = vec ! [ ] ;
147134
148- for key in key_to_remove {
149- storage. remove_item ( & key) . map_err ( |err| {
150- anyhow ! ( "Error removing key `{key}` from local storage: {err:?}" )
151- } ) ?;
135+ for i in 0 ..len {
136+ match storage. key ( i) . map_err ( |err| {
137+ anyhow ! ( "Error accessing key index `{i}` from local storage: {err:?}" )
138+ } ) ? {
139+ Some ( key) if key. starts_with ( & self . cache_key_prefix ) => key_to_remove. push ( key) ,
140+ _ => continue ,
152141 }
153142 }
154143
144+ for key in key_to_remove {
145+ storage
146+ . remove_item ( & key)
147+ . map_err ( |err| anyhow ! ( "Error removing key `{key}` from local storage: {err:?}" ) ) ?;
148+ }
149+
155150 Ok ( ( ) )
156151 }
157152}
@@ -164,7 +159,7 @@ pub(crate) mod test_tools {
164159
165160 /// `Test only` Return the raw content of the local storage
166161 pub ( crate ) fn local_storage_content ( ) -> HashMap < String , String > {
167- let storage = open_local_storage ( ) . unwrap ( ) . unwrap ( ) ;
162+ let storage = open_local_storage ( ) . unwrap ( ) ;
168163 let len = storage. length ( ) . unwrap ( ) ;
169164 let mut content = HashMap :: new ( ) ;
170165
@@ -220,7 +215,7 @@ pub(crate) mod test_tools {
220215 certificate_hash : & CertificateHash ,
221216 expire_at : DateTime < Utc > ,
222217 ) {
223- let storage = open_local_storage ( ) . unwrap ( ) . unwrap ( ) ;
218+ let storage = open_local_storage ( ) . unwrap ( ) ;
224219 let key = self . cache_key ( certificate_hash) ;
225220 let existing_value = Self :: parse_cached_certificate (
226221 storage. get_item ( & key) . unwrap ( ) . expect ( "Key not found" ) ,
@@ -243,7 +238,7 @@ pub(crate) mod test_tools {
243238 & self ,
244239 certificate_hash : & CertificateHash ,
245240 ) -> Option < CachedCertificate > {
246- let storage = open_local_storage ( ) . unwrap ( ) . unwrap ( ) ;
241+ let storage = open_local_storage ( ) . unwrap ( ) ;
247242 storage
248243 . get_item ( & self . cache_key ( certificate_hash) )
249244 . unwrap ( )
@@ -286,7 +281,7 @@ mod tests {
286281 #[ wasm_bindgen_test]
287282 async fn store_in_empty_cache_add_new_item_that_expire_after_parametrized_delay ( ) {
288283 let expiration_delay = TimeDelta :: hours ( 1 ) ;
289- let now = Utc :: now ( ) ;
284+ let start_time = Utc :: now ( ) ;
290285 let cache = LocalStorageCertificateVerifierCache :: new (
291286 "store_in_empty_cache_add_new_item_that_expire_after_parametrized_delay" ,
292287 expiration_delay,
@@ -302,7 +297,7 @@ mod tests {
302297
303298 assert_eq ! ( 1 , cache. len( ) ) ;
304299 assert_eq ! ( "parent" , cached. previous_hash) ;
305- assert ! ( cached. expire_at - now >= expiration_delay) ;
300+ assert ! ( cached. expire_at - start_time >= expiration_delay) ;
306301 }
307302
308303 #[ wasm_bindgen_test]
@@ -333,31 +328,32 @@ mod tests {
333328 #[ wasm_bindgen_test]
334329 async fn storing_same_hash_update_parent_hash_and_expiration_time ( ) {
335330 let expiration_delay = TimeDelta :: days ( 2 ) ;
336- let now = Utc :: now ( ) ;
331+ let start_time = Utc :: now ( ) ;
337332 let cache = LocalStorageCertificateVerifierCache :: new (
338333 "storing_same_hash_update_parent_hash_and_expiration_time" ,
339334 expiration_delay,
340335 )
341336 . with_items ( [ ( "hash" , "first_parent" ) , ( "another_hash" , "another_parent" ) ] ) ;
342337
338+ let initial_value = cache. get_cached_value ( "hash" ) . unwrap ( ) ;
339+
343340 cache
344341 . store_validated_certificate ( "hash" , "updated_parent" )
345342 . await
346343 . unwrap ( ) ;
347344
348- let updated_value = cache
349- . get_cached_value ( "hash" )
350- . expect ( "Cache should have been populated" ) ;
345+ let updated_value = cache. get_cached_value ( "hash" ) . unwrap ( ) ;
351346
352347 assert_eq ! ( 2 , cache. len( ) ) ;
353348 assert_eq ! (
354349 Some ( "another_parent" . to_string( ) ) ,
355350 cache. get_previous_hash( "another_hash" ) . await . unwrap( ) ,
356- "Existing but not updated value should not have been altered, content: {:#?}, now : {:?}" ,
357- local_storage_content( ) , now ,
351+ "Existing but not updated value should not have been altered, content: {:#?}, start_time : {:?}" ,
352+ local_storage_content( ) , start_time ,
358353 ) ;
359354 assert_eq ! ( "updated_parent" , updated_value. previous_hash) ;
360- assert ! ( updated_value. expire_at - now >= expiration_delay) ;
355+ assert_ne ! ( initial_value, updated_value) ;
356+ assert ! ( updated_value. expire_at - start_time >= expiration_delay) ;
361357 }
362358 }
363359
@@ -424,7 +420,7 @@ mod tests {
424420 TimeDelta :: hours ( 1 ) ,
425421 )
426422 . with_items ( [ ( "hash" , "parent" ) , ( "another_hash" , "another_parent" ) ] ) ;
427- let storage = open_local_storage ( ) . unwrap ( ) . unwrap ( ) ;
423+ let storage = open_local_storage ( ) . unwrap ( ) ;
428424 storage
429425 . set_item ( "key_from_another_component" , "another_value" )
430426 . unwrap ( ) ;
0 commit comments