From 3aa30b230794a2a02721abfce63ac5fc69579273 Mon Sep 17 00:00:00 2001 From: Patrick Beza Date: Thu, 8 Aug 2024 13:28:25 +0200 Subject: [PATCH] Code review fix: idempotent SQL queries --- core/lib/dal/src/tee_proof_generation_dal.rs | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/core/lib/dal/src/tee_proof_generation_dal.rs b/core/lib/dal/src/tee_proof_generation_dal.rs index fdaaf976b2f0..2bd73323eb10 100644 --- a/core/lib/dal/src/tee_proof_generation_dal.rs +++ b/core/lib/dal/src/tee_proof_generation_dal.rs @@ -133,6 +133,7 @@ impl TeeProofGenerationDal<'_, '_> { tee_proof_generation_details (l1_batch_number, tee_type, status, created_at, updated_at) VALUES ($1, $2, 'ready_to_be_proven', NOW(), NOW()) + ON CONFLICT (l1_batch_number, tee_type) DO NOTHING "#, batch_number, tee_type.to_string(), @@ -140,19 +141,11 @@ impl TeeProofGenerationDal<'_, '_> { let instrumentation = Instrumented::new("insert_tee_proof_generation_job") .with_arg("l1_batch_number", &batch_number) .with_arg("tee_type", &tee_type); - let result = instrumentation + instrumentation .clone() .with(query) .execute(self.storage) .await?; - if result.rows_affected() == 0 { - let err = instrumentation.constraint_error(anyhow::anyhow!( - "Unable to insert TEE proof for batch number {}, TEE type {}", - batch_number, - tee_type - )); - return Err(err); - } Ok(()) } @@ -172,18 +165,11 @@ impl TeeProofGenerationDal<'_, '_> { let instrumentation = Instrumented::new("save_attestation") .with_arg("pubkey", &pubkey) .with_arg("attestation", &attestation); - let result = instrumentation + instrumentation .clone() .with(query) .execute(self.storage) .await?; - if result.rows_affected() == 0 { - let err = instrumentation.constraint_error(anyhow::anyhow!( - "Unable to insert TEE attestation: pubkey {:?} already has an attestation assigned", - pubkey - )); - return Err(err); - } Ok(()) }