Skip to content

Commit

Permalink
feat(apollo_reverts): add revert blocks functionality
Browse files Browse the repository at this point in the history
This commit introduces the create apollo revets, which unifies block
reverting functionality.

This commit refactors consensus manager and batcher to adhere to the
functionality introduced in apollo reverts.

Revert config is now a global sequencer config, should future crates
need revert functionality, they shall inherit the configurations via the
global sequencer config.
  • Loading branch information
noamsp-starkware committed Feb 9, 2025
1 parent 590f54c commit 5ae9de9
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 74 deletions.
16 changes: 16 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/blockifier_test_utils",
Expand Down Expand Up @@ -84,6 +85,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',
'blockifier_test_utils',
Expand Down
26 changes: 18 additions & 8 deletions config/sequencer/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -889,15 +889,15 @@
"privacy": "Public",
"value": "consensus_proposals"
},
"consensus_manager_config.revert_up_to_and_including": {
"description": "The batcher will revert blocks up to this block number (including). Use this configurations carefully to prevent significant revert operations and data loss.",
"privacy": "Private",
"value": 18446744073709551615
"consensus_manager_config.revert_config.revert_on": {
"description": "Whether the component should revert blocks.",
"pointer_target": "revert_config.revert_on",
"privacy": "Public"
},
"consensus_manager_config.revert_up_to_and_including.#is_none": {
"description": "Flag for an optional field.",
"privacy": "TemporaryValue",
"value": true
"consensus_manager_config.revert_config.revert_up_to_and_including": {
"description": "The component will revert blocks up to this block number (including).",
"pointer_target": "revert_config.revert_up_to_and_including",
"privacy": "Public"
},
"consensus_manager_config.votes_topic": {
"description": "The topic for consensus votes.",
Expand Down Expand Up @@ -1149,6 +1149,16 @@
"param_type": "String",
"privacy": "TemporaryValue"
},
"revert_config.revert_on": {
"description": "Whether the component should revert blocks.",
"privacy": "TemporaryValue",
"value": false
},
"revert_config.revert_up_to_and_including": {
"description": "The component will revert blocks up to this block number (including).",
"privacy": "TemporaryValue",
"value": 18446744073709551615
},
"state_sync_config.central_sync_client_config.#is_none": {
"description": "Flag for an optional field.",
"privacy": "TemporaryValue",
Expand Down
18 changes: 18 additions & 0 deletions crates/apollo_reverts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "apollo_reverts"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[lints]
workspace = true

[dependencies]
futures.workspace = true
papyrus_config.workspace = true
papyrus_storage.workspace = true
serde.workspace = true
starknet_api.workspace = true
tracing.workspace = true
validator.workspace = true
116 changes: 116 additions & 0 deletions crates/apollo_reverts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use std::collections::BTreeMap;
use std::future::Future;

use futures::future::pending;
use futures::never::Never;
use papyrus_config::dumping::{ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
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 serde::{Deserialize, Serialize};
use starknet_api::block::BlockNumber;
use tracing::info;
use validator::Validate;

#[derive(Clone, Debug, Serialize, Deserialize, Validate, PartialEq)]
pub struct RevertConfig {
pub revert_on: bool,
pub revert_up_to_and_including: BlockNumber,
}

impl Default for RevertConfig {
fn default() -> Self {
Self {
revert_on: false,
// Use u64::MAX as a placeholder to prevent setting this value to
// a low block number by mistake, which will cause significant revert operations.
revert_up_to_and_including: BlockNumber(u64::MAX),
}
}
}

impl SerializeConfig for RevertConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from_iter([
ser_param(
"revert_on",
&self.revert_on,
"Whether the component should revert blocks.",
ParamPrivacyInput::Public,
),
ser_param(
"revert_up_to_and_including",
&self.revert_up_to_and_including,
"The component will revert blocks up to this block number (including).",
// Use this configurations carefully to prevent significant revert operations and
// data loss
ParamPrivacyInput::Public,
),
])
}
}

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 {}
}
1 change: 1 addition & 0 deletions crates/starknet_batcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository.workspace = true
workspace = true

[dependencies]
apollo_reverts.workspace = true
async-trait.workspace = true
blockifier.workspace = true
chrono.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;

use async_trait::async_trait;
use blockifier::state::contract_class_manager::ContractClassManager;
use futures::executor::block_on;
#[cfg(test)]
use mockall::automock;
use papyrus_storage::state::{StateStorageReader, StateStorageWriter};
Expand Down Expand Up @@ -735,9 +736,8 @@ impl BatcherStorageWriterTrait for papyrus_storage::StorageWriter {
}

fn revert_block(&mut self, height: BlockNumber) -> papyrus_storage::StorageResult<()> {
let (txn, reverted_state_diff) = self.begin_rw_txn()?.revert_state_diff(height)?;
trace!("Reverted state diff: {:#?}", reverted_state_diff);
txn.commit()
block_on(apollo_reverts::revert_block(self, height));
Ok(())
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/starknet_consensus_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository.workspace = true
workspace = true

[dependencies]
apollo_reverts.workspace = true
async-trait.workspace = true
papyrus_config.workspace = true
papyrus_network.workspace = true
Expand Down
23 changes: 5 additions & 18 deletions crates/starknet_consensus_manager/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use std::collections::BTreeMap;

use papyrus_config::dumping::{
append_sub_config_name,
ser_optional_param,
ser_param,
SerializeConfig,
};
use apollo_reverts::RevertConfig;
use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use papyrus_network::NetworkConfig;
use serde::{Deserialize, Serialize};
Expand All @@ -24,7 +20,7 @@ pub struct ConsensusManagerConfig {
#[validate]
pub network_config: NetworkConfig,
pub cende_config: CendeConfig,
pub revert_up_to_and_including: Option<BlockNumber>,
pub revert_config: RevertConfig,
pub votes_topic: String,
pub proposals_topic: String,
pub broadcast_buffer_size: usize,
Expand Down Expand Up @@ -59,20 +55,11 @@ impl SerializeConfig for ConsensusManagerConfig {
ParamPrivacyInput::Public,
),
]);
config.extend(ser_optional_param(
&self.revert_up_to_and_including,
// Use u64::MAX as a placeholder to prevent setting this value to
// a low block number by mistake, which will cause significant revert operations.
BlockNumber(u64::MAX),
"revert_up_to_and_including",
"The batcher will revert blocks up to this block number (including). Use this \
configurations carefully to prevent significant revert operations and data loss.",
ParamPrivacyInput::Private,
));
config.extend(append_sub_config_name(self.consensus_config.dump(), "consensus_config"));
config.extend(append_sub_config_name(self.context_config.dump(), "context_config"));
config.extend(append_sub_config_name(self.cende_config.dump(), "cende_config"));
config.extend(append_sub_config_name(self.network_config.dump(), "network_config"));
config.extend(append_sub_config_name(self.revert_config.dump(), "revert_config"));
config
}
}
Expand All @@ -84,7 +71,7 @@ impl Default for ConsensusManagerConfig {
context_config: ContextConfig::default(),
cende_config: CendeConfig::default(),
network_config: NetworkConfig::default(),
revert_up_to_and_including: None,
revert_config: RevertConfig::default(),
votes_topic: "consensus_votes".to_string(),
proposals_topic: "consensus_proposals".to_string(),
broadcast_buffer_size: 10000,
Expand Down
Loading

0 comments on commit 5ae9de9

Please sign in to comment.