diff --git a/core/lib/dal/.sqlx/query-d8bc4af72e3d94df53967c83d577a1e1abf3d268b16498cc65758af66781cbb6.json b/core/lib/dal/.sqlx/query-2169cc7dfb074566ceb0bd5754d6e7a755c4bc6c25c7e6caff5fd6142813d349.json similarity index 54% rename from core/lib/dal/.sqlx/query-d8bc4af72e3d94df53967c83d577a1e1abf3d268b16498cc65758af66781cbb6.json rename to core/lib/dal/.sqlx/query-2169cc7dfb074566ceb0bd5754d6e7a755c4bc6c25c7e6caff5fd6142813d349.json index 0ed8005289f7..e48fddcf6175 100644 --- a/core/lib/dal/.sqlx/query-d8bc4af72e3d94df53967c83d577a1e1abf3d268b16498cc65758af66781cbb6.json +++ b/core/lib/dal/.sqlx/query-2169cc7dfb074566ceb0bd5754d6e7a755c4bc6c25c7e6caff5fd6142813d349.json @@ -1,15 +1,16 @@ { "db_name": "PostgreSQL", - "query": "\n INSERT INTO\n tee_proof_generation_details (l1_batch_number, tee_type, status, created_at, updated_at)\n VALUES\n ($1, $2, 'ready_to_be_proven', NOW(), NOW())\n ON CONFLICT (l1_batch_number, tee_type) DO NOTHING\n ", + "query": "\n INSERT INTO\n tee_proof_generation_details (l1_batch_number, tee_type, status, created_at, updated_at)\n VALUES\n ($1, $2, $3, NOW(), NOW())\n ON CONFLICT (l1_batch_number, tee_type) DO NOTHING\n ", "describe": { "columns": [], "parameters": { "Left": [ "Int8", + "Text", "Text" ] }, "nullable": [] }, - "hash": "d8bc4af72e3d94df53967c83d577a1e1abf3d268b16498cc65758af66781cbb6" + "hash": "2169cc7dfb074566ceb0bd5754d6e7a755c4bc6c25c7e6caff5fd6142813d349" } diff --git a/core/lib/dal/.sqlx/query-414749a3d8d1ac4f2c66b386df47d113257cca7a7fc6c8036b61cc0e005099a8.json b/core/lib/dal/.sqlx/query-414749a3d8d1ac4f2c66b386df47d113257cca7a7fc6c8036b61cc0e005099a8.json new file mode 100644 index 000000000000..e0c5103fac90 --- /dev/null +++ b/core/lib/dal/.sqlx/query-414749a3d8d1ac4f2c66b386df47d113257cca7a7fc6c8036b61cc0e005099a8.json @@ -0,0 +1,16 @@ +{ + "db_name": "PostgreSQL", + "query": "\n UPDATE tee_proof_generation_details\n SET\n status = $1,\n updated_at = NOW()\n WHERE\n l1_batch_number = $2\n AND tee_type = $3\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Text", + "Int8", + "Text" + ] + }, + "nullable": [] + }, + "hash": "414749a3d8d1ac4f2c66b386df47d113257cca7a7fc6c8036b61cc0e005099a8" +} diff --git a/core/lib/dal/.sqlx/query-47975cc0b5e4f3a6b5224cb452b8fee3209a950943dc2b4da82c324e1c09132f.json b/core/lib/dal/.sqlx/query-47975cc0b5e4f3a6b5224cb452b8fee3209a950943dc2b4da82c324e1c09132f.json deleted file mode 100644 index 7e5f9e1713c4..000000000000 --- a/core/lib/dal/.sqlx/query-47975cc0b5e4f3a6b5224cb452b8fee3209a950943dc2b4da82c324e1c09132f.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n UPDATE tee_proof_generation_details\n SET\n status = 'picked_by_prover',\n updated_at = NOW(),\n prover_taken_at = NOW()\n WHERE\n tee_type = $1\n AND l1_batch_number = (\n SELECT\n proofs.l1_batch_number\n FROM\n tee_proof_generation_details AS proofs\n JOIN tee_verifier_input_producer_jobs AS inputs ON proofs.l1_batch_number = inputs.l1_batch_number\n WHERE\n inputs.status = $2\n AND (\n proofs.status = 'ready_to_be_proven'\n OR (\n proofs.status = 'picked_by_prover'\n AND proofs.prover_taken_at < NOW() - $3::INTERVAL\n )\n )\n AND proofs.l1_batch_number >= $4\n ORDER BY\n l1_batch_number ASC\n LIMIT\n 1\n FOR UPDATE\n SKIP LOCKED\n )\n RETURNING\n tee_proof_generation_details.l1_batch_number\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "l1_batch_number", - "type_info": "Int8" - } - ], - "parameters": { - "Left": [ - "Text", - { - "Custom": { - "name": "tee_verifier_input_producer_job_status", - "kind": { - "Enum": [ - "Queued", - "ManuallySkipped", - "InProgress", - "Successful", - "Failed" - ] - } - } - }, - "Interval", - "Int8" - ] - }, - "nullable": [ - false - ] - }, - "hash": "47975cc0b5e4f3a6b5224cb452b8fee3209a950943dc2b4da82c324e1c09132f" -} diff --git a/core/lib/dal/.sqlx/query-5c5bdb0e419049f9fb4d8b3bbec468765628fd2c3b7c2a408d18b5aba0df9a30.json b/core/lib/dal/.sqlx/query-5c5bdb0e419049f9fb4d8b3bbec468765628fd2c3b7c2a408d18b5aba0df9a30.json deleted file mode 100644 index 2d9a24d6d79c..000000000000 --- a/core/lib/dal/.sqlx/query-5c5bdb0e419049f9fb4d8b3bbec468765628fd2c3b7c2a408d18b5aba0df9a30.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n UPDATE tee_proof_generation_details\n SET\n status = 'unpicked',\n updated_at = NOW()\n WHERE\n l1_batch_number = $1\n AND tee_type = $2\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8", - "Text" - ] - }, - "nullable": [] - }, - "hash": "5c5bdb0e419049f9fb4d8b3bbec468765628fd2c3b7c2a408d18b5aba0df9a30" -} diff --git a/core/lib/dal/.sqlx/query-a8fdcb5180fc5fd125a003e9675f213a0b02b3ff96398920bc0250397bb2a95f.json b/core/lib/dal/.sqlx/query-6292dc157e2b4c64c513b4b8f043ea4423fdaf5d03cb70e8c3a67b6d4a24d29e.json similarity index 53% rename from core/lib/dal/.sqlx/query-a8fdcb5180fc5fd125a003e9675f213a0b02b3ff96398920bc0250397bb2a95f.json rename to core/lib/dal/.sqlx/query-6292dc157e2b4c64c513b4b8f043ea4423fdaf5d03cb70e8c3a67b6d4a24d29e.json index 8b67041427d3..62b1be92c909 100644 --- a/core/lib/dal/.sqlx/query-a8fdcb5180fc5fd125a003e9675f213a0b02b3ff96398920bc0250397bb2a95f.json +++ b/core/lib/dal/.sqlx/query-6292dc157e2b4c64c513b4b8f043ea4423fdaf5d03cb70e8c3a67b6d4a24d29e.json @@ -1,10 +1,11 @@ { "db_name": "PostgreSQL", - "query": "\n UPDATE tee_proof_generation_details\n SET\n tee_type = $1,\n status = 'generated',\n pubkey = $2,\n signature = $3,\n proof = $4,\n updated_at = NOW()\n WHERE\n l1_batch_number = $5\n ", + "query": "\n UPDATE tee_proof_generation_details\n SET\n tee_type = $1,\n status = $2,\n pubkey = $3,\n signature = $4,\n proof = $5,\n updated_at = NOW()\n WHERE\n l1_batch_number = $6\n ", "describe": { "columns": [], "parameters": { "Left": [ + "Text", "Text", "Bytea", "Bytea", @@ -14,5 +15,5 @@ }, "nullable": [] }, - "hash": "a8fdcb5180fc5fd125a003e9675f213a0b02b3ff96398920bc0250397bb2a95f" + "hash": "6292dc157e2b4c64c513b4b8f043ea4423fdaf5d03cb70e8c3a67b6d4a24d29e" } diff --git a/core/lib/dal/.sqlx/query-e048951ded9e4a4a28238334bc4dc118360ab83bae3196ec941216901be629da.json b/core/lib/dal/.sqlx/query-86dbcf93abdd4206d2d62b140cf5dd11537925d02f5a7f2bae23c8dc48204e3f.json similarity index 77% rename from core/lib/dal/.sqlx/query-e048951ded9e4a4a28238334bc4dc118360ab83bae3196ec941216901be629da.json rename to core/lib/dal/.sqlx/query-86dbcf93abdd4206d2d62b140cf5dd11537925d02f5a7f2bae23c8dc48204e3f.json index 70f7f9d12fa4..42cf55bd939e 100644 --- a/core/lib/dal/.sqlx/query-e048951ded9e4a4a28238334bc4dc118360ab83bae3196ec941216901be629da.json +++ b/core/lib/dal/.sqlx/query-86dbcf93abdd4206d2d62b140cf5dd11537925d02f5a7f2bae23c8dc48204e3f.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n proofs.l1_batch_number\n FROM\n tee_proof_generation_details AS proofs\n JOIN tee_verifier_input_producer_jobs AS inputs ON proofs.l1_batch_number = inputs.l1_batch_number\n WHERE\n inputs.status = $1\n AND proofs.status = 'ready_to_be_proven'\n ORDER BY\n proofs.l1_batch_number ASC\n LIMIT\n 1\n ", + "query": "\n SELECT\n proofs.l1_batch_number\n FROM\n tee_proof_generation_details AS proofs\n JOIN tee_verifier_input_producer_jobs AS inputs ON proofs.l1_batch_number = inputs.l1_batch_number\n WHERE\n inputs.status = $1\n AND proofs.status = $2\n ORDER BY\n proofs.l1_batch_number ASC\n LIMIT\n 1\n ", "describe": { "columns": [ { @@ -24,12 +24,13 @@ ] } } - } + }, + "Text" ] }, "nullable": [ false ] }, - "hash": "e048951ded9e4a4a28238334bc4dc118360ab83bae3196ec941216901be629da" + "hash": "86dbcf93abdd4206d2d62b140cf5dd11537925d02f5a7f2bae23c8dc48204e3f" } diff --git a/core/lib/dal/.sqlx/query-e3f4af790fca9149f4edff070170294ec464d184ad732692faa61d2ef99c84e9.json b/core/lib/dal/.sqlx/query-e3f4af790fca9149f4edff070170294ec464d184ad732692faa61d2ef99c84e9.json new file mode 100644 index 000000000000..abe74036f4c6 --- /dev/null +++ b/core/lib/dal/.sqlx/query-e3f4af790fca9149f4edff070170294ec464d184ad732692faa61d2ef99c84e9.json @@ -0,0 +1,40 @@ +{ + "db_name": "PostgreSQL", + "query": "\n UPDATE tee_proof_generation_details\n SET\n status = $1,\n updated_at = NOW(),\n prover_taken_at = NOW()\n WHERE\n tee_type = $2\n AND l1_batch_number = (\n SELECT\n proofs.l1_batch_number\n FROM\n tee_proof_generation_details AS proofs\n JOIN tee_verifier_input_producer_jobs AS inputs ON proofs.l1_batch_number = inputs.l1_batch_number\n WHERE\n inputs.status = $3\n AND (\n proofs.status = $4\n OR (\n proofs.status = $1\n AND proofs.prover_taken_at < NOW() - $5::INTERVAL\n )\n )\n AND proofs.l1_batch_number >= $6\n ORDER BY\n l1_batch_number ASC\n LIMIT\n 1\n FOR UPDATE\n SKIP LOCKED\n )\n RETURNING\n tee_proof_generation_details.l1_batch_number\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "l1_batch_number", + "type_info": "Int8" + } + ], + "parameters": { + "Left": [ + "Text", + "Text", + { + "Custom": { + "name": "tee_verifier_input_producer_job_status", + "kind": { + "Enum": [ + "Queued", + "ManuallySkipped", + "InProgress", + "Successful", + "Failed" + ] + } + } + }, + "Text", + "Interval", + "Int8" + ] + }, + "nullable": [ + false + ] + }, + "hash": "e3f4af790fca9149f4edff070170294ec464d184ad732692faa61d2ef99c84e9" +} diff --git a/core/lib/dal/doc/TeeProofGenerationDal.md b/core/lib/dal/doc/TeeProofGenerationDal.md index 167e6b3c42ce..fcfa379816c7 100644 --- a/core/lib/dal/doc/TeeProofGenerationDal.md +++ b/core/lib/dal/doc/TeeProofGenerationDal.md @@ -11,11 +11,9 @@ title: Status Diagram --- stateDiagram-v2 -[*] --> ready_to_be_proven : insert_tee_proof_generation_job -ready_to_be_proven --> picked_by_prover : lock_batch_for_proving +[*] --> unpicked : insert_tee_proof_generation_job +unpicked --> picked_by_prover : lock_batch_for_proving picked_by_prover --> generated : save_proof_artifacts_metadata -generated --> [*] - picked_by_prover --> unpicked : unlock_batch -unpicked --> [*] +generated --> [*] ``` diff --git a/core/lib/dal/migrations/20240828130000_tee_unpicked_status.down.sql b/core/lib/dal/migrations/20240828130000_tee_unpicked_status.down.sql new file mode 100644 index 000000000000..84d806c91287 --- /dev/null +++ b/core/lib/dal/migrations/20240828130000_tee_unpicked_status.down.sql @@ -0,0 +1,3 @@ +UPDATE tee_proof_generation_details +SET status = 'ready_to_be_proven' +WHERE status = 'unpicked'; diff --git a/core/lib/dal/migrations/20240828130000_tee_unpicked_status.up.sql b/core/lib/dal/migrations/20240828130000_tee_unpicked_status.up.sql new file mode 100644 index 000000000000..46b34c8d1485 --- /dev/null +++ b/core/lib/dal/migrations/20240828130000_tee_unpicked_status.up.sql @@ -0,0 +1,3 @@ +UPDATE tee_proof_generation_details +SET status = 'unpicked' +WHERE status = 'ready_to_be_proven'; diff --git a/core/lib/dal/src/tee_proof_generation_dal.rs b/core/lib/dal/src/tee_proof_generation_dal.rs index 80e364273f69..cc6b87a07aca 100644 --- a/core/lib/dal/src/tee_proof_generation_dal.rs +++ b/core/lib/dal/src/tee_proof_generation_dal.rs @@ -1,6 +1,7 @@ #![doc = include_str!("../doc/TeeProofGenerationDal.md")] use std::time::Duration; +use strum::{Display, EnumString}; use zksync_db_connection::{ connection::Connection, error::DalResult, @@ -19,6 +20,16 @@ pub struct TeeProofGenerationDal<'a, 'c> { pub(crate) storage: &'a mut Connection<'c, Core>, } +#[derive(Debug, EnumString, Display)] +enum TeeProofGenerationJobStatus { + #[strum(serialize = "unpicked")] + Unpicked, + #[strum(serialize = "picked_by_prover")] + PickedByProver, + #[strum(serialize = "generated")] + Generated, +} + impl TeeProofGenerationDal<'_, '_> { pub async fn lock_batch_for_proving( &mut self, @@ -32,11 +43,11 @@ impl TeeProofGenerationDal<'_, '_> { r#" UPDATE tee_proof_generation_details SET - status = 'picked_by_prover', + status = $1, updated_at = NOW(), prover_taken_at = NOW() WHERE - tee_type = $1 + tee_type = $2 AND l1_batch_number = ( SELECT proofs.l1_batch_number @@ -44,15 +55,15 @@ impl TeeProofGenerationDal<'_, '_> { tee_proof_generation_details AS proofs JOIN tee_verifier_input_producer_jobs AS inputs ON proofs.l1_batch_number = inputs.l1_batch_number WHERE - inputs.status = $2 + inputs.status = $3 AND ( - proofs.status = 'ready_to_be_proven' + proofs.status = $4 OR ( - proofs.status = 'picked_by_prover' - AND proofs.prover_taken_at < NOW() - $3::INTERVAL + proofs.status = $1 + AND proofs.prover_taken_at < NOW() - $5::INTERVAL ) ) - AND proofs.l1_batch_number >= $4 + AND proofs.l1_batch_number >= $6 ORDER BY l1_batch_number ASC LIMIT @@ -63,8 +74,10 @@ impl TeeProofGenerationDal<'_, '_> { RETURNING tee_proof_generation_details.l1_batch_number "#, + TeeProofGenerationJobStatus::PickedByProver.to_string(), tee_type.to_string(), TeeVerifierInputProducerJobStatus::Successful as TeeVerifierInputProducerJobStatus, + TeeProofGenerationJobStatus::Unpicked.to_string(), processing_timeout, min_batch_number ); @@ -91,12 +104,13 @@ impl TeeProofGenerationDal<'_, '_> { r#" UPDATE tee_proof_generation_details SET - status = 'unpicked', + status = $1, updated_at = NOW() WHERE - l1_batch_number = $1 - AND tee_type = $2 + l1_batch_number = $2 + AND tee_type = $3 "#, + TeeProofGenerationJobStatus::Unpicked.to_string(), batch_number, tee_type.to_string() ) @@ -117,30 +131,33 @@ impl TeeProofGenerationDal<'_, '_> { signature: &[u8], proof: &[u8], ) -> DalResult<()> { + let batch_number = i64::from(batch_number.0); let query = sqlx::query!( r#" UPDATE tee_proof_generation_details SET tee_type = $1, - status = 'generated', - pubkey = $2, - signature = $3, - proof = $4, + status = $2, + pubkey = $3, + signature = $4, + proof = $5, updated_at = NOW() WHERE - l1_batch_number = $5 + l1_batch_number = $6 "#, tee_type.to_string(), + TeeProofGenerationJobStatus::Generated.to_string(), pubkey, signature, proof, - i64::from(batch_number.0) + batch_number ); let instrumentation = Instrumented::new("save_proof_artifacts_metadata") .with_arg("tee_type", &tee_type) .with_arg("pubkey", &pubkey) .with_arg("signature", &signature) - .with_arg("proof", &proof); + .with_arg("proof", &proof) + .with_arg("l1_batch_number", &batch_number); let result = instrumentation .clone() .with(query) @@ -168,11 +185,12 @@ impl TeeProofGenerationDal<'_, '_> { INSERT INTO tee_proof_generation_details (l1_batch_number, tee_type, status, created_at, updated_at) VALUES - ($1, $2, 'ready_to_be_proven', NOW(), NOW()) + ($1, $2, $3, NOW(), NOW()) ON CONFLICT (l1_batch_number, tee_type) DO NOTHING "#, batch_number, tee_type.to_string(), + TeeProofGenerationJobStatus::Unpicked.to_string(), ); let instrumentation = Instrumented::new("insert_tee_proof_generation_job") .with_arg("l1_batch_number", &batch_number) @@ -229,14 +247,16 @@ impl TeeProofGenerationDal<'_, '_> { tee_attestations ta ON tp.pubkey = ta.pubkey WHERE tp.l1_batch_number = $1 - AND tp.status = 'generated' + AND tp.status = $2 {} ORDER BY tp.l1_batch_number ASC, tp.tee_type ASC "#, - tee_type.map_or_else(String::new, |_| "AND tp.tee_type = $2".to_string()) + tee_type.map_or_else(String::new, |_| "AND tp.tee_type = $3".to_string()) ); - let mut query = sqlx::query_as(&query).bind(i64::from(batch_number.0)); + let mut query = sqlx::query_as(&query) + .bind(i64::from(batch_number.0)) + .bind(TeeProofGenerationJobStatus::Generated.to_string()); if let Some(tee_type) = tee_type { query = query.bind(tee_type.to_string()); @@ -257,13 +277,14 @@ impl TeeProofGenerationDal<'_, '_> { JOIN tee_verifier_input_producer_jobs AS inputs ON proofs.l1_batch_number = inputs.l1_batch_number WHERE inputs.status = $1 - AND proofs.status = 'ready_to_be_proven' + AND proofs.status = $2 ORDER BY proofs.l1_batch_number ASC LIMIT 1 "#, - TeeVerifierInputProducerJobStatus::Successful as TeeVerifierInputProducerJobStatus + TeeVerifierInputProducerJobStatus::Successful as TeeVerifierInputProducerJobStatus, + TeeProofGenerationJobStatus::Unpicked.to_string(), ); let batch_number = Instrumented::new("get_oldest_unpicked_batch") .with(query) diff --git a/core/node/proof_data_handler/src/tests.rs b/core/node/proof_data_handler/src/tests.rs index 88d4930e6920..5d7569d5720c 100644 --- a/core/node/proof_data_handler/src/tests.rs +++ b/core/node/proof_data_handler/src/tests.rs @@ -232,7 +232,7 @@ async fn mock_tee_batch_status( .await .expect("Failed to mark tee_verifier_input_producer_job job as successful"); - // mock SQL table with relevant information about the status of TEE proof generation ('ready_to_be_proven') + // mock SQL table with relevant information about the status of TEE proof generation proof_dal .insert_tee_proof_generation_job(batch_number, TeeType::Sgx)