Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: BWIP race condition #2405

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE proof_generation_details ALTER COLUMN proof_gen_data_blob_url SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE proof_generation_details ALTER COLUMN proof_gen_data_blob_url DROP NOT NULL;
54 changes: 46 additions & 8 deletions core/lib/dal/src/proof_generation_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,60 @@ impl ProofGenerationDal<'_, '_> {
Ok(())
}

pub async fn save_merkle_paths_artifacts_metadata(
&mut self,
batch_number: L1BatchNumber,
proof_gen_data_blob_url: &str,
) -> DalResult<()> {
let batch_number = i64::from(batch_number.0);
let query = sqlx::query!(
r#"
UPDATE proof_generation_details
SET
proof_gen_data_blob_url = $1,
updated_at = NOW()
WHERE
l1_batch_number = $2
"#,
proof_gen_data_blob_url,
batch_number
);
let instrumentation = Instrumented::new("save_proof_artifacts_metadata")
.with_arg("proof_gen_data_blob_url", &proof_gen_data_blob_url)
.with_arg("l1_batch_number", &batch_number);
let result = instrumentation
.clone()
.with(query)
.execute(self.storage)
.await?;
if result.rows_affected() == 0 {
let err = instrumentation.constraint_error(anyhow::anyhow!(
"Cannot save proof_gen_data_blob_url for a batch number {} that does not exist",
batch_number
));
return Err(err);
}

Ok(())
}

/// The caller should ensure that `l1_batch_number` exists in the database.
pub async fn insert_proof_generation_details(
&mut self,
l1_batch_number: L1BatchNumber,
proof_gen_data_blob_url: &str,
) -> DalResult<()> {
let result = sqlx::query!(
r#"
INSERT INTO
proof_generation_details (l1_batch_number, status, proof_gen_data_blob_url, created_at, updated_at)
proof_generation_details (l1_batch_number, status, created_at, updated_at)
VALUES
($1, 'unpicked', $2, NOW(), NOW())
($1, 'unpicked', NOW(), NOW())
ON CONFLICT (l1_batch_number) DO NOTHING
"#,
i64::from(l1_batch_number.0),
proof_gen_data_blob_url,
i64::from(l1_batch_number.0),
)
.instrument("insert_proof_generation_details")
.with_arg("l1_batch_number", &l1_batch_number)
.with_arg("proof_gen_data_blob_url", &proof_gen_data_blob_url)
.report_latency()
.execute(self.storage)
.await?;
Expand Down Expand Up @@ -303,7 +337,7 @@ mod tests {
assert_eq!(unpicked_l1_batch, None);

conn.proof_generation_dal()
.insert_proof_generation_details(L1BatchNumber(1), "generation_data")
.insert_proof_generation_details(L1BatchNumber(1))
.await
.unwrap();

Expand All @@ -316,13 +350,17 @@ mod tests {

// Calling the method multiple times should work fine.
conn.proof_generation_dal()
.insert_proof_generation_details(L1BatchNumber(1), "generation_data")
.insert_proof_generation_details(L1BatchNumber(1))
.await
.unwrap();
conn.proof_generation_dal()
.save_vm_runner_artifacts_metadata(L1BatchNumber(1), "vm_run")
.await
.unwrap();
conn.proof_generation_dal()
.save_merkle_paths_artifacts_metadata(L1BatchNumber(1), "data")
.await
.unwrap();
conn.blocks_dal()
.save_l1_batch_tree_data(
L1BatchNumber(1),
Expand Down
6 changes: 5 additions & 1 deletion core/node/metadata_calculator/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ impl TreeUpdater {
// Save the proof generation details to Postgres
storage
.proof_generation_dal()
.insert_proof_generation_details(l1_batch_number, object_key)
.insert_proof_generation_details(l1_batch_number)
.await?;
storage
.proof_generation_dal()
.save_merkle_paths_artifacts_metadata(l1_batch_number, object_key)
.await?;
}
drop(storage);
Expand Down
5 changes: 5 additions & 0 deletions core/node/vm_runner/src/impls/bwip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ impl StateKeeperOutputHandler for BasicWitnessInputProducerOutputHandler {

tracing::info!(%l1_batch_number, "Saved VM run data");

connection
.proof_generation_dal()
.insert_proof_generation_details(l1_batch_number)
.await?;

connection
.proof_generation_dal()
.save_vm_runner_artifacts_metadata(l1_batch_number, &blob_url)
Expand Down
Loading