-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apollo_reverts): add revert blocks functionality
- Loading branch information
1 parent
383ab09
commit 99547a1
Showing
5 changed files
with
101 additions
and
0 deletions.
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 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 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 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "apollo_reverts" | ||
version.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
futures.workspace = true | ||
papyrus_storage.workspace = true | ||
starknet_api.workspace = true | ||
tracing.workspace = true |
This file contains 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::future::Future; | ||
|
||
use futures::future::pending; | ||
use futures::never::Never; | ||
use papyrus_storage::base_layer::BaseLayerStorageWriter; | ||
use papyrus_storage::body::BodyStorageWriter; | ||
use papyrus_storage::class_manager::ClassManagerStorageWriter; | ||
use papyrus_storage::header::HeaderStorageWriter; | ||
use papyrus_storage::state::StateStorageWriter; | ||
use papyrus_storage::StorageWriter; | ||
use starknet_api::block::BlockNumber; | ||
use tracing::info; | ||
|
||
pub async fn revert_blocks_and_eternal_pending<Fut>( | ||
mut current_block_number: BlockNumber, | ||
revert_up_to_and_including: BlockNumber, | ||
mut revert_block_fn: impl FnMut(BlockNumber) -> Fut, | ||
component_name: &str, | ||
) -> Never | ||
where | ||
Fut: Future<Output = ()>, | ||
{ | ||
if current_block_number <= revert_up_to_and_including { | ||
panic!( | ||
"{component_name} current block {current_block_number} is not larger than the target \ | ||
block {revert_up_to_and_including}. No reverts are needed." | ||
); | ||
} | ||
|
||
info!( | ||
"Reverting {component_name} from block {current_block_number} to block \ | ||
{revert_up_to_and_including}" | ||
); | ||
|
||
while current_block_number > revert_up_to_and_including { | ||
current_block_number = current_block_number.prev().expect( | ||
"A block number that's greater than another block number should return Some on prev", | ||
); | ||
info!("Reverting {component_name} block number {current_block_number}."); | ||
revert_block_fn(current_block_number).await; | ||
} | ||
|
||
info!( | ||
"Done reverting {component_name} up to block {revert_up_to_and_including} including. \ | ||
Starting eternal pending." | ||
); | ||
pending().await | ||
} | ||
|
||
pub fn revert_block( | ||
storage_writer: &mut StorageWriter, | ||
current_block_number: BlockNumber, | ||
) -> impl Future<Output = ()> { | ||
storage_writer | ||
.begin_rw_txn() | ||
.unwrap() | ||
.revert_header(current_block_number) | ||
.unwrap() | ||
.0 | ||
.revert_body(current_block_number) | ||
.unwrap() | ||
.0 | ||
.revert_state_diff(current_block_number) | ||
.unwrap() | ||
.0 | ||
.try_revert_class_manager_marker(current_block_number) | ||
.unwrap() | ||
.try_revert_base_layer_marker(current_block_number) | ||
.unwrap() | ||
.commit() | ||
.unwrap(); | ||
async {} | ||
} |