-
Notifications
You must be signed in to change notification settings - Fork 131
fix(l2): save last committed batch as current checkpoint #5152
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -180,7 +180,7 @@ impl L1Committer { | |||||
| Self::get_checkpoint_from_path( | ||||||
| genesis.clone(), | ||||||
| blockchain.options.clone(), | ||||||
| &checkpoints_dir.join(format!("checkpoint_batch_{}", last_committed_batch)), | ||||||
| &checkpoints_dir.join(batch_checkpoint_name(last_committed_batch)), | ||||||
| &rollup_store, | ||||||
| ) | ||||||
| .await?; | ||||||
|
|
@@ -261,114 +261,14 @@ impl L1Committer { | |||||
| let batch = match self.rollup_store.get_batch(batch_to_commit).await? { | ||||||
| Some(batch) => batch, | ||||||
| None => { | ||||||
| let last_committed_blocks = self | ||||||
| .rollup_store | ||||||
| .get_block_numbers_by_batch(last_committed_batch_number) | ||||||
| .await? | ||||||
| .ok_or( | ||||||
| CommitterError::RetrievalError(format!("Failed to get batch with batch number {last_committed_batch_number}. Batch is missing when it should be present. This is a bug")) | ||||||
| )?; | ||||||
| let last_block = last_committed_blocks | ||||||
| .last() | ||||||
| .ok_or( | ||||||
| CommitterError::RetrievalError(format!("Last committed batch ({last_committed_batch_number}) doesn't have any blocks. This is probably a bug.")) | ||||||
| )?; | ||||||
| let first_block_to_commit = last_block + 1; | ||||||
|
|
||||||
| // We need to guarantee that the checkpoint path is new | ||||||
| // to avoid causing a lock error under rocksdb feature. | ||||||
| let rand_suffix: u32 = rand::thread_rng().r#gen(); | ||||||
| let one_time_checkpoint_path = self | ||||||
| .checkpoints_dir | ||||||
| .join(format!("temp_checkpoint_{batch_to_commit}_{rand_suffix}")); | ||||||
|
|
||||||
| // For re-execution we need to use a checkpoint to the previous state | ||||||
| // (i.e. checkpoint of the state to the latest block from the previous | ||||||
| // batch, or the state of the genesis if this is the first batch). | ||||||
| // We already have this initial checkpoint as part of the L1Committer | ||||||
| // struct, but we need to create a one-time copy of it because | ||||||
| // we still need to use the current checkpoint store later for witness | ||||||
| // generation. | ||||||
|
|
||||||
| let (one_time_checkpoint_store, one_time_checkpoint_blockchain) = self | ||||||
| .create_checkpoint( | ||||||
| &self.current_checkpoint_store, | ||||||
| &one_time_checkpoint_path, | ||||||
| &self.rollup_store, | ||||||
| ) | ||||||
| .await?; | ||||||
|
|
||||||
| // Try to prepare batch | ||||||
| let result = self | ||||||
| .prepare_batch_from_block( | ||||||
| *last_block, | ||||||
| batch_to_commit, | ||||||
| one_time_checkpoint_store, | ||||||
| one_time_checkpoint_blockchain, | ||||||
| ) | ||||||
| .await; | ||||||
|
|
||||||
| if one_time_checkpoint_path.exists() { | ||||||
| let _ = remove_dir_all(&one_time_checkpoint_path).inspect_err(|e| { | ||||||
| error!( | ||||||
| "Failed to remove one-time checkpoint directory at path {one_time_checkpoint_path:?}. Should be removed manually. Error: {}", e.to_string() | ||||||
| ) | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| let ( | ||||||
| blobs_bundle, | ||||||
| new_state_root, | ||||||
| message_hashes, | ||||||
| privileged_transactions_hash, | ||||||
| last_block_of_batch, | ||||||
| ) = result?; | ||||||
|
|
||||||
| if *last_block == last_block_of_batch { | ||||||
| debug!("No new blocks to commit, skipping"); | ||||||
| let Some(batch) = self.produce_batch(batch_to_commit).await? else { | ||||||
| // The batch is empty (there's no new blocks from last batch) | ||||||
| return Ok(()); | ||||||
| } | ||||||
|
|
||||||
| let batch = Batch { | ||||||
| number: batch_to_commit, | ||||||
| first_block: first_block_to_commit, | ||||||
| last_block: last_block_of_batch, | ||||||
| state_root: new_state_root, | ||||||
| privileged_transactions_hash, | ||||||
| message_hashes, | ||||||
| blobs_bundle, | ||||||
| commit_tx: None, | ||||||
| verify_tx: None, | ||||||
| }; | ||||||
|
|
||||||
| self.rollup_store.seal_batch(batch.clone()).await?; | ||||||
|
|
||||||
| debug!( | ||||||
| first_block = batch.first_block, | ||||||
| last_block = batch.last_block, | ||||||
| "Batch {} stored in database", | ||||||
| batch.number | ||||||
| ); | ||||||
|
|
||||||
| batch | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| info!( | ||||||
| first_block = batch.first_block, | ||||||
| last_block = batch.last_block, | ||||||
| "Generating and storing witness for batch {}", | ||||||
| batch.number, | ||||||
| ); | ||||||
|
|
||||||
| self.generate_and_store_batch_prover_input(&batch).await?; | ||||||
|
|
||||||
| // We need to update the current checkpoint after generating the witness | ||||||
| // with it, and before sending the commitment. | ||||||
| // The actual checkpoint store directory is not pruned until the batch | ||||||
| // it served in is verified on L1. | ||||||
| self.update_current_checkpoint(&batch).await?; | ||||||
|
|
||||||
| info!( | ||||||
| first_block = batch.first_block, | ||||||
| last_block = batch.last_block, | ||||||
|
|
@@ -409,12 +309,116 @@ impl L1Committer { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| async fn produce_batch(&mut self, batch_number: u64) -> Result<Option<Batch>, CommitterError> { | ||||||
| let last_committed_blocks = self | ||||||
| .rollup_store | ||||||
| .get_block_numbers_by_batch(batch_number-1) | ||||||
| .await? | ||||||
| .ok_or( | ||||||
| CommitterError::RetrievalError(format!("Failed to get batch with batch number {}. Batch is missing when it should be present. This is a bug", batch_number)) | ||||||
|
||||||
| CommitterError::RetrievalError(format!("Failed to get batch with batch number {}. Batch is missing when it should be present. This is a bug", batch_number)) | |
| CommitterError::RetrievalError(format!("Failed to get blocks for batch number {}. Block numbers are missing when they should be present. This is a bug", batch_number)) |
MegaRedHand marked this conversation as resolved.
Show resolved
Hide resolved
MegaRedHand marked this conversation as resolved.
Show resolved
Hide resolved
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around the subtraction operator. Should be
batch_number - 1for consistency with Rust formatting conventions.