Skip to content

Commit a962791

Browse files
authored
fix: fetch unvalidated time proofs for conclusion events (#723)
1 parent 19cd57b commit a962791

3 files changed

Lines changed: 48 additions & 13 deletions

File tree

event-svc/src/event/service.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

event-svc/src/event/validator/event.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ impl EventValidator {
275275
}
276276
}
277277
}
278+
279+
pub fn time_event_validator(&self) -> &TimeEventValidator {
280+
&self.time_event_validator
281+
}
278282
}
279283

280284
#[cfg(test)]

event-svc/src/store/sql/access/event.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,15 @@ impl EventAccess {
598598
}
599599

600600
/// Get chain inclusion proof for a transaction hash
601-
pub async fn get_chain_proof(&self, chain_id: &str, tx_hash: &str) -> Result<ChainProof> {
602-
let row: ChainProof = sqlx::query_as(ChainProofQuery::by_chain_id_and_tx_hash())
601+
pub async fn get_chain_proof(
602+
&self,
603+
chain_id: &str,
604+
tx_hash: &str,
605+
) -> Result<Option<ChainProof>> {
606+
let row: Option<ChainProof> = sqlx::query_as(ChainProofQuery::by_chain_id_and_tx_hash())
603607
.bind(chain_id)
604608
.bind(tx_hash)
605-
.fetch_one(self.pool.reader())
609+
.fetch_optional(self.pool.reader())
606610
.await?;
607611
Ok(row)
608612
}

0 commit comments

Comments
 (0)