Skip to content

Commit

Permalink
feat(apollo_reverts): add revert blocks functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
noamsp-starkware committed Feb 5, 2025
1 parent d1dcbe1 commit 02ce315
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
resolver = "2"

members = [
"crates/apollo_reverts",
"crates/blockifier",
"crates/blockifier_reexecution",
"crates/mempool_test_utils",
Expand Down Expand Up @@ -83,6 +84,7 @@ alloy-sol-types = "0.8.3"
alloy-transport = "0.3.5"
alloy-transport-http = "0.3.5"
anyhow = "1.0.44"
apollo_reverts = { path = "crates/apollo_reverts", version = "0.0.0" }
ark-ec = "0.4.2"
ark-ff = "0.4.0-alpha.7"
ark-secp256k1 = "0.4.0"
Expand Down
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Configuration = {
rules: {
'scope-empty': [2, 'never'],
'scope-enum': [2, 'always', [
"apollo_reverts",
'blockifier',
'blockifier_reexecution',
'cairo_native',
Expand Down
15 changes: 15 additions & 0 deletions crates/apollo_reverts/Cargo.toml
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
73 changes: 73 additions & 0 deletions crates/apollo_reverts/src/lib.rs
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 {}
}

0 comments on commit 02ce315

Please sign in to comment.