Skip to content

Commit 2d834f1

Browse files
committed
apollo_batcher: add revert task
1 parent 106d5cd commit 2d834f1

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

crates/apollo_batcher/src/commitment_manager/commitment_manager_impl.rs

Lines changed: 56 additions & 28 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,18 +77,15 @@ 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,
8384
state_diff: ThinStateDiff,
8485
state_diff_commitment: Option<StateDiffCommitment>,
8586
) -> CommitmentManagerResult<()> {
8687
if height != self.commitment_task_offset {
87-
return Err(CommitmentManagerError::WrongTaskHeight {
88+
return Err(CommitmentManagerError::WrongCommitmentTaskHeight {
8889
expected: self.commitment_task_offset,
8990
actual: height,
9091
state_diff_commitment,
@@ -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,28 @@ 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::WrongRevertTaskHeight {
277+
expected: expected_height,
278+
actual: height,
279+
});
280+
}
281+
let revert_task_input =
282+
CommitterTaskInput(CommitterRequest::RevertBlock(RevertBlockRequest {
283+
height,
284+
reversed_state_diff,
285+
}));
286+
let task_details = format!("Revert block {height}",);
287+
self.add_task(revert_task_input, &task_details).await?;
288+
info!("Sent revert task for block {height}.");
289+
Ok(())
262290
}
263291

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

crates/apollo_batcher/src/commitment_manager/commitment_manager_test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ async fn test_add_commitment_task(mut mock_dependencies: MockDependencies) {
166166
.await;
167167
assert_matches!(
168168
result,
169-
Err(CommitmentManagerError::WrongTaskHeight { expected, actual, .. })
169+
Err(CommitmentManagerError::WrongCommitmentTaskHeight { expected, actual, .. })
170170
if expected == INITIAL_HEIGHT && actual == incorrect_height
171171
);
172172

@@ -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();

crates/apollo_batcher/src/commitment_manager/errors.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ pub enum CommitmentManagerError {
1717
"Wrong commitment task height. Expected: {expected}, Actual: {actual}. State diff \
1818
commitment: {state_diff_commitment:?}"
1919
)]
20-
WrongTaskHeight {
20+
WrongCommitmentTaskHeight {
2121
expected: BlockNumber,
2222
actual: BlockNumber,
2323
state_diff_commitment: Option<StateDiffCommitment>,
2424
},
25+
#[error("Wrong revert task height. Expected: {expected}, Actual: {actual}.")]
26+
WrongRevertTaskHeight { expected: BlockNumber, actual: BlockNumber },
2527
}

0 commit comments

Comments
 (0)