Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
fix: BWIP race condition (matter-labs#2405)
Browse files Browse the repository at this point in the history
## What ❔

Separately insert proof_generation_details and gen data blob URLs.

## Why ❔

Sometimes BWIP generates data before insert_proof_generation_details is
called, which results in errors.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
Artemka374 authored and irnb committed Jul 12, 2024
1 parent b2f05e4 commit b43fc0c
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 24 deletions.

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

0 comments on commit b43fc0c

Please sign in to comment.