Skip to content

Commit 29216a9

Browse files
committed
apollo_batcher: add revert task
1 parent d6f4045 commit 29216a9

File tree

3 files changed

+58
-32
lines changed

3 files changed

+58
-32
lines changed

crates/apollo_batcher/src/commitment_manager/commitment_manager_impl.rs

Lines changed: 53 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::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,
@@ -95,35 +96,43 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
9596
state_diff,
9697
state_diff_commitment,
9798
});
98-
let error_message = format!(
99-
"Failed to send commitment task to state committer. Block: {height}, state diff \
100-
commitment: {state_diff_commitment:?}",
101-
);
99+
let task_details =
100+
format!("Commit block {height}, state diff commitment: {state_diff_commitment:?}",);
101+
self.add_task(commitment_task_input, &task_details).await?;
102+
self.successfully_added_commitment_task(height, state_diff_commitment);
103+
Ok(())
104+
}
105+
106+
/// If the tasks channel is full, the behavior depends on the config: if
107+
/// `wait_for_tasks_channel` is true, it will wait until there is space in the channel;
108+
/// otherwise, it will panic. Any other error when sending the task will also cause a panic.
109+
async fn add_task(
110+
&self,
111+
task_input: CommitterTaskInput,
112+
task_details: &str,
113+
) -> CommitmentManagerResult<()> {
114+
let error_message = format!("Failed to send task to state committer: {task_details}.",);
102115

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

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

180188
/// Initializes the CommitmentManager. This includes starting the state committer task.
@@ -256,8 +264,25 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
256264

257265
// Associated functions.
258266

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

263288
/// 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
@@ -167,7 +167,7 @@ async fn test_add_commitment_task(mut mock_dependencies: MockDependencies) {
167167
.await;
168168
assert_matches!(
169169
result,
170-
Err(CommitmentManagerError::WrongTaskHeight { expected, actual, .. })
170+
Err(CommitmentManagerError::WrongCommitmentTaskHeight { expected, actual, .. })
171171
if expected == INITIAL_HEIGHT && actual == incorrect_height
172172
);
173173

@@ -212,8 +212,7 @@ async fn test_add_commitment_task(mut mock_dependencies: MockDependencies) {
212212

213213
#[rstest]
214214
#[tokio::test]
215-
#[should_panic(expected = "Failed to send commitment task to state committer because the channel \
216-
is full. Block: 4")]
215+
#[should_panic(expected = "The channel is full. channel size: 1.")]
217216
async fn test_add_commitment_task_full(mut mock_dependencies: MockDependencies) {
218217
add_initial_heights(&mut mock_dependencies);
219218
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)