@@ -404,7 +404,9 @@ impl EventService {
404404
405405 match event {
406406 ceramic_event:: unvalidated:: Event :: Time ( time_event) => {
407- let proof = self . get_chain_proof ( & time_event) . await ?;
407+ let proof = self . discover_chain_proof ( & time_event) . await . map_err ( |e| {
408+ Error :: new_app ( anyhow:: anyhow!( "Failed to discover chain proof: {:?}" , e) )
409+ } ) ?;
408410
409411 Ok ( ConclusionEvent :: Time ( ConclusionTime {
410412 event_cid,
@@ -541,22 +543,47 @@ impl EventService {
541543 }
542544 }
543545
544- /// This is a helper function to get the chain proof for a given event from the database, or throw an error if it
545- /// wasn't found.
546- async fn get_chain_proof (
546+ /// This is a helper function to get the chain proof for a given event from the database, or to validate and store
547+ /// it if it doesn't exist.
548+ /// TODO: Remove the code for fetching and storing the proof once existing users have migrated to the new version.
549+ /// v0.55.0 onwards, there should be no proofs that have not already been validated and stored by the time we reach
550+ /// this point.
551+ async fn discover_chain_proof (
547552 & self ,
548553 event : & ceramic_event:: unvalidated:: TimeEvent ,
549- ) -> Result < ChainProof > {
554+ ) -> std :: result :: Result < ChainProof , crate :: eth_rpc :: Error > {
550555 let tx_hash = event. proof ( ) . tx_hash ( ) ;
551556 let tx_hash = tx_hash_try_from_cid ( tx_hash) . unwrap ( ) . to_string ( ) ;
552- let chain_proof = self
557+ if let Some ( proof ) = self
553558 . event_access
554559 . get_chain_proof ( event. proof ( ) . chain_id ( ) , & tx_hash)
555560 . await
556- . map_err ( |e| {
557- Error :: new_app ( anyhow:: anyhow!( "Failed to discover chain proof: {:?}" , e) )
558- } ) ?;
559- Ok ( chain_proof)
561+ . map_err ( |e| crate :: eth_rpc:: Error :: Application ( e. into ( ) ) ) ?
562+ {
563+ return Ok ( proof) ;
564+ }
565+
566+ // TODO: The following code can be removed once all existing users have migrated to the new version. There
567+ // should be no proofs that have not already been validated and stored by the time we reach this point.
568+ warn ! (
569+ "Chain proof for tx {} not found in database, validating and storing it now." ,
570+ tx_hash
571+ ) ;
572+
573+ // Try using the RPC provider and store the proof
574+ let proof = self
575+ . event_validator
576+ . time_event_validator ( )
577+ . validate_chain_inclusion ( event)
578+ . await ?;
579+
580+ let proof = ChainProof :: from ( proof) ;
581+ self . event_access
582+ . persist_chain_inclusion_proofs ( & [ proof. clone ( ) ] )
583+ . await
584+ . map_err ( |e| crate :: eth_rpc:: Error :: Application ( e. into ( ) ) ) ?;
585+
586+ Ok ( proof)
560587 }
561588}
562589
0 commit comments