-
Notifications
You must be signed in to change notification settings - Fork 1
feat: graceful shutdown #150
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
Open
frisitano
wants to merge
7
commits into
main
Choose a base branch
from
feat/graceful-shutdown-impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
958a891
feat: graceful shutdown
frisitano 575d675
introduce unwind for graceful shutdown
frisitano f6a59f1
refactor graceful shutdown
frisitano 643f995
implement graceful shutdown test case
frisitano f666553
clenaup and add test batch data
frisitano b8f8cca
Merge branch 'main' into feat/graceful-shutdown-impl
frisitano 163674f
cleanup
frisitano 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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 |
---|---|---|
|
@@ -20,8 +20,16 @@ pub trait DatabaseOperations: DatabaseConnectionProvider { | |
async fn insert_batch(&self, batch_commit: BatchCommitData) -> Result<(), DatabaseError> { | ||
tracing::trace!(target: "scroll::db", batch_hash = ?batch_commit.hash, batch_index = batch_commit.index, "Inserting batch input into database."); | ||
let batch_commit: models::batch_commit::ActiveModel = batch_commit.into(); | ||
batch_commit.insert(self.get_connection()).await?; | ||
Ok(()) | ||
match models::batch_commit::Entity::insert(batch_commit) | ||
.on_conflict( | ||
OnConflict::column(models::batch_commit::Column::Index).do_nothing().to_owned(), | ||
) | ||
.exec(self.get_connection()) | ||
.await | ||
{ | ||
Err(sea_orm::DbErr::RecordNotInserted) => Ok(()), | ||
x => Ok(x.map(|_| ())?), | ||
} | ||
} | ||
|
||
/// Finalize a [`BatchCommitData`] with the provided `batch_hash` in the database and set the | ||
|
@@ -114,7 +122,12 @@ pub trait DatabaseOperations: DatabaseConnectionProvider { | |
async fn insert_l1_message(&self, l1_message: L1MessageEnvelope) -> Result<(), DatabaseError> { | ||
tracing::trace!(target: "scroll::db", queue_index = l1_message.transaction.queue_index, "Inserting L1 message into database."); | ||
let l1_message: models::l1_message::ActiveModel = l1_message.into(); | ||
l1_message.insert(self.get_connection()).await?; | ||
models::l1_message::Entity::insert(l1_message) | ||
.on_conflict( | ||
OnConflict::column(models::l1_message::Column::QueueIndex).do_nothing().to_owned(), | ||
) | ||
.exec(self.get_connection()) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
|
@@ -217,14 +230,24 @@ pub trait DatabaseOperations: DatabaseConnectionProvider { | |
} | ||
|
||
/// Get the latest safe L2 [`BlockInfo`] from the database. | ||
async fn get_latest_safe_l2_block(&self) -> Result<Option<BlockInfo>, DatabaseError> { | ||
async fn get_latest_safe_l2_block( | ||
&self, | ||
) -> Result<Option<(BlockInfo, BatchInfo)>, DatabaseError> { | ||
tracing::trace!(target: "scroll::db", "Fetching latest safe L2 block from database."); | ||
Ok(models::l2_block::Entity::find() | ||
.filter(models::l2_block::Column::BatchIndex.is_not_null()) | ||
.order_by_desc(models::l2_block::Column::BlockNumber) | ||
.one(self.get_connection()) | ||
.await | ||
.map(|x| x.map(|x| x.block_info()))?) | ||
.map(|x| { | ||
x.map(|x| { | ||
( | ||
x.block_info(), | ||
x.batch_info() | ||
.expect("Batch info must be present due to database query arguments"), | ||
) | ||
}) | ||
})?) | ||
} | ||
|
||
/// Get the latest L2 [`BlockInfo`] from the database. | ||
|
@@ -237,6 +260,33 @@ pub trait DatabaseOperations: DatabaseConnectionProvider { | |
.map(|x| x.map(|x| x.block_info()))?) | ||
} | ||
|
||
/// Get the safe block for startup from the database. | ||
/// | ||
/// This method fetches the batch info for the latest safe L2 block, it then retrieves the | ||
/// latest block for the previous batch (i.e., the batch before the latest safe block) and | ||
/// returns the block info. | ||
async fn get_startup_safe_block(&self) -> Result<Option<BlockInfo>, DatabaseError> { | ||
tracing::trace!(target: "scroll::db", "Fetching startup safe block from database."); | ||
let safe = if let Some(batch_info) = self | ||
.get_latest_safe_l2_block() | ||
.await? | ||
.map(|(_, batch_info)| batch_info) | ||
.filter(|b| b.index > 1) | ||
{ | ||
let batch = self | ||
.get_batch_by_index(batch_info.index - 1) | ||
.await? | ||
.expect("Batch info must be present due to database query arguments"); | ||
let block = self.get_highest_block_for_batch(batch.hash).await?; | ||
tracing::info!(target:"test", "{:?}", block); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leftover from debug |
||
block | ||
} else { | ||
None | ||
}; | ||
|
||
Ok(safe) | ||
} | ||
|
||
/// Delete all L2 blocks with a block number greater than the provided block number. | ||
async fn delete_l2_blocks_gt(&self, block_number: u64) -> Result<u64, DatabaseError> { | ||
tracing::trace!(target: "scroll::db", block_number, "Deleting L2 blocks greater than provided block number."); | ||
|
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
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
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
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
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.
could we have an edge case where we index an L1 message, shutdown, the L1 reorgs, and we don't update it because of the
do_nothing
directive?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.
good catch! This is a possibility. Let me review and see what makes the most sense and I will add a proposal here which we can discuss before I implement.