Skip to content

Commit 39094bb

Browse files
committed
apollo_batcher: add revert task
1 parent 7125d3b commit 39094bb

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

crates/apollo_batcher/src/commitment_manager/commitment_manager_impl.rs

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
use std::sync::Arc;
44

55
use apollo_batcher_config::config::{BatcherConfig, CommitmentManagerConfig};
6-
use apollo_committer_types::committer_types::{CommitBlockRequest, CommitBlockResponse};
6+
use apollo_committer_types::committer_types::{
7+
CommitBlockRequest,
8+
CommitBlockResponse,
9+
RevertBlockRequest,
10+
};
711
use apollo_committer_types::communication::{CommitterRequest, SharedCommitterClient};
812
use starknet_api::block::BlockNumber;
913
use starknet_api::block_hash::block_hash_calculator::{
@@ -73,10 +77,7 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
7377
}
7478

7579
/// Adds a commitment task to the state committer. If the task height does not match the
76-
/// task offset, an error is returned. If the tasks channel is full, the behavior depends on
77-
/// the config: if `wait_for_tasks_channel` is true, it will wait until there is space in the
78-
/// channel; otherwise, it will panic. Any other error when sending the task will also cause a
79-
/// panic.
80+
/// task offset, an error is returned.
8081
pub(crate) async fn add_commitment_task(
8182
&mut self,
8283
height: BlockNumber,
@@ -96,35 +97,43 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
9697
state_diff,
9798
state_diff_commitment,
9899
}));
99-
let error_message = format!(
100-
"Failed to send commitment task to state committer. Block: {height}, state diff \
101-
commitment: {state_diff_commitment:?}",
102-
);
100+
let task_details =
101+
format!("Commit block {height}, state diff commitment: {state_diff_commitment:?}",);
102+
self.add_task(commitment_task_input, &task_details).await?;
103+
self.successfully_added_commitment_task(height, state_diff_commitment);
104+
Ok(())
105+
}
106+
107+
/// If the tasks channel is full, the behavior depends on the config: if
108+
/// `wait_for_tasks_channel` is true, it will wait until there is space in the channel;
109+
/// otherwise, it will panic. Any other error when sending the task will also cause a panic.
110+
async fn add_task(
111+
&self,
112+
task_input: CommitterTaskInput,
113+
task_details: &str,
114+
) -> CommitmentManagerResult<()> {
115+
let error_message = format!("Failed to send task to state committer: {task_details}.",);
103116

104117
if self.config.wait_for_tasks_channel {
105-
info!(
106-
"Waiting to send commitment task for block {height} and state diff \
107-
{state_diff_commitment:?} to state committer."
108-
);
109-
match self.tasks_sender.send(commitment_task_input).await {
110-
Ok(_) => self.successfully_added_commitment_task(height, state_diff_commitment),
111-
Err(err) => panic!("{error_message}. error: {err}"),
112-
}
118+
info!("Waiting to send task for {task_details} to state committer.");
119+
self.tasks_sender
120+
.send(task_input)
121+
.await
122+
.unwrap_or_else(|err| panic!("{error_message}. error: {err}"));
113123
} else {
114-
match self.tasks_sender.try_send(commitment_task_input) {
115-
Ok(_) => self.successfully_added_commitment_task(height, state_diff_commitment),
124+
match self.tasks_sender.try_send(task_input) {
125+
Ok(_) => (),
116126
Err(TrySendError::Full(_)) => {
117127
let channel_size = self.tasks_sender.max_capacity();
118128
panic!(
119-
"Failed to send commitment task to state committer because the channel is \
120-
full. Block: {height}, state diff commitment: {state_diff_commitment:?}, \
121-
channel size: {channel_size}. Consider increasing the channel size or \
122-
enabling waiting in the config.",
129+
"{error_message}. The channel is full. channel size: {channel_size}. \
130+
Consider increasing the channel size or enabling waiting in the config.",
123131
);
124132
}
125133
Err(err) => panic!("{error_message}. error: {err}"),
126134
}
127135
}
136+
Ok(())
128137
}
129138

130139
/// Fetches all ready commitment results from the state committer. Panics if any task is a
@@ -169,13 +178,12 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
169178
&mut self,
170179
height: BlockNumber,
171180
state_diff_commitment: Option<StateDiffCommitment>,
172-
) -> CommitmentManagerResult<()> {
181+
) {
173182
info!(
174183
"Sent commitment task for block {height} and state diff {state_diff_commitment:?} to \
175184
state committer."
176185
);
177186
self.increase_commitment_task_offset();
178-
Ok(())
179187
}
180188

181189
/// Initializes the CommitmentManager. This includes starting the state committer task.
@@ -257,8 +265,29 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
257265

258266
// Associated functions.
259267

260-
pub(crate) async fn revert_block(height: BlockNumber, reversed_state_diff: ThinStateDiff) {
261-
unimplemented!()
268+
pub(crate) async fn add_revert_task(
269+
&mut self,
270+
height: BlockNumber,
271+
reversed_state_diff: ThinStateDiff,
272+
) -> CommitmentManagerResult<()> {
273+
let expected_height =
274+
self.commitment_task_offset.prev().expect("Can't revert before the genesis block.");
275+
if height != expected_height {
276+
return Err(CommitmentManagerError::WrongTaskHeight {
277+
expected: expected_height,
278+
actual: height,
279+
state_diff_commitment: None,
280+
});
281+
}
282+
let revert_task_input =
283+
CommitterTaskInput(CommitterRequest::RevertBlock(RevertBlockRequest {
284+
height,
285+
reversed_state_diff,
286+
}));
287+
let task_details = format!("Revert block {height}",);
288+
self.add_task(revert_task_input, &task_details).await?;
289+
info!("Sent revert task for block {height}.");
290+
Ok(())
262291
}
263292

264293
/// Returns the final commitment output for a given commitment task output.

crates/apollo_batcher/src/commitment_manager/commitment_manager_test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ async fn test_add_commitment_task(mut mock_dependencies: MockDependencies) {
211211

212212
#[rstest]
213213
#[tokio::test]
214-
#[should_panic(expected = "Failed to send commitment task to state committer because the channel \
215-
is full. Block: 4")]
214+
#[should_panic(expected = "The channel is full. channel size: 1.")]
216215
async fn test_add_commitment_task_full(mut mock_dependencies: MockDependencies) {
217216
add_initial_heights(&mut mock_dependencies);
218217
let state_diff = test_state_diff();

0 commit comments

Comments
 (0)