Skip to content

Commit 7bf23e0

Browse files
committed
apollo_batcher: add revert task
1 parent 151df5f commit 7bf23e0

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;
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::{
@@ -95,10 +99,7 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
9599
}
96100

97101
/// Adds a commitment task to the state committer. If the task height does not match the
98-
/// task offset, an error is returned. If the tasks channel is full, the behavior depends on
99-
/// the config: if `wait_for_tasks_channel` is true, it will wait until there is space in the
100-
/// channel; otherwise, it will panic. Any other error when sending the task will also cause a
101-
/// panic.
102+
/// task offset, an error is returned.
102103
pub(crate) async fn add_commitment_task(
103104
&mut self,
104105
height: BlockNumber,
@@ -118,35 +119,43 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
118119
state_diff,
119120
state_diff_commitment,
120121
}));
121-
let error_message = format!(
122-
"Failed to send commitment task to state committer. Block: {height}, state diff \
123-
commitment: {state_diff_commitment:?}",
124-
);
122+
let task_details =
123+
format!("Commit block {height}, state diff commitment: {state_diff_commitment:?}",);
124+
self.add_task(commitment_task_input, &task_details).await?;
125+
self.successfully_added_commitment_task(height, state_diff_commitment);
126+
Ok(())
127+
}
128+
129+
/// If the tasks channel is full, the behavior depends on the config: if
130+
/// `wait_for_tasks_channel` is true, it will wait until there is space in the channel;
131+
/// otherwise, it will panic. Any other error when sending the task will also cause a panic.
132+
async fn add_task(
133+
&self,
134+
task_input: CommitterTaskInput,
135+
task_details: &str,
136+
) -> CommitmentManagerResult<()> {
137+
let error_message = format!("Failed to send task to state committer: {task_details}.",);
125138

126139
if self.config.wait_for_tasks_channel {
127-
info!(
128-
"Waiting to send commitment task for block {height} and state diff \
129-
{state_diff_commitment:?} to state committer."
130-
);
131-
match self.tasks_sender.send(commitment_task_input).await {
132-
Ok(_) => self.successfully_added_commitment_task(height, state_diff_commitment),
133-
Err(err) => panic!("{error_message}. error: {err}"),
134-
}
140+
info!("Waiting to send task for {task_details} to state committer.");
141+
self.tasks_sender
142+
.send(task_input)
143+
.await
144+
.unwrap_or_else(|err| panic!("{error_message}. error: {err}"));
135145
} else {
136-
match self.tasks_sender.try_send(commitment_task_input) {
137-
Ok(_) => self.successfully_added_commitment_task(height, state_diff_commitment),
146+
match self.tasks_sender.try_send(task_input) {
147+
Ok(_) => (),
138148
Err(TrySendError::Full(_)) => {
139149
let channel_size = self.tasks_sender.max_capacity();
140150
panic!(
141-
"Failed to send commitment task to state committer because the channel is \
142-
full. Block: {height}, state diff commitment: {state_diff_commitment:?}, \
143-
channel size: {channel_size}. Consider increasing the channel size or \
144-
enabling waiting in the config.",
151+
"{error_message}. The channel is full. channel size: {channel_size}. \
152+
Consider increasing the channel size or enabling waiting in the config.",
145153
);
146154
}
147155
Err(err) => panic!("{error_message}. error: {err}"),
148156
}
149157
}
158+
Ok(())
150159
}
151160

152161
/// Fetches all ready commitment results from the state committer. Panics if any task is a
@@ -191,13 +200,12 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
191200
&mut self,
192201
height: BlockNumber,
193202
state_diff_commitment: Option<StateDiffCommitment>,
194-
) -> CommitmentManagerResult<()> {
203+
) {
195204
info!(
196205
"Sent commitment task for block {height} and state diff {state_diff_commitment:?} to \
197206
state committer."
198207
);
199208
self.increase_commitment_task_offset();
200-
Ok(())
201209
}
202210

203211
/// Initializes the CommitmentManager. This includes starting the state committer task.
@@ -279,8 +287,29 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
279287

280288
// Associated functions.
281289

282-
pub(crate) async fn revert_block(height: BlockNumber, reversed_state_diff: ThinStateDiff) {
283-
unimplemented!()
290+
pub(crate) async fn add_revert_task(
291+
&mut self,
292+
height: BlockNumber,
293+
reversed_state_diff: ThinStateDiff,
294+
) -> CommitmentManagerResult<()> {
295+
let expected_height =
296+
self.commitment_task_offset.prev().expect("Can't revert before the genesis block.");
297+
if height != expected_height {
298+
return Err(CommitmentManagerError::WrongTaskHeight {
299+
expected: expected_height,
300+
actual: height,
301+
state_diff_commitment: None,
302+
});
303+
}
304+
let revert_task_input =
305+
CommitterTaskInput(CommitterRequest::RevertBlock(RevertBlockRequest {
306+
height,
307+
reversed_state_diff,
308+
}));
309+
let task_details = format!("Revert block {height}",);
310+
self.add_task(revert_task_input, &task_details).await?;
311+
info!("Sent revert task for block {height}.");
312+
Ok(())
284313
}
285314

286315
/// 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
@@ -214,8 +214,7 @@ async fn test_add_commitment_task(mut mock_dependencies: MockDependencies) {
214214

215215
#[rstest]
216216
#[tokio::test]
217-
#[should_panic(expected = "Failed to send commitment task to state committer because the channel \
218-
is full. Block: 4")]
217+
#[should_panic(expected = "The channel is full. channel size: 1.")]
219218
async fn test_add_commitment_task_full(mut mock_dependencies: MockDependencies) {
220219
add_initial_heights(&mut mock_dependencies);
221220
let state_diff = test_state_diff();

0 commit comments

Comments
 (0)