Skip to content

Commit c975df9

Browse files
committed
apollo_batcher: add revert task
1 parent c717bf8 commit c975df9

File tree

3 files changed

+57
-32
lines changed

3 files changed

+57
-32
lines changed

crates/apollo_batcher/src/commitment_manager/commitment_manager_impl.rs

Lines changed: 52 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,37 @@ 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+
self.add_task(commitment_task_input).await?;
100+
self.successfully_added_commitment_task(height, state_diff_commitment);
101+
Ok(())
102+
}
103+
104+
/// If the tasks channel is full, the behavior depends on the config: if
105+
/// `wait_for_tasks_channel` is true, it will wait until there is space in the channel;
106+
/// otherwise, it will panic. Any other error when sending the task will also cause a panic.
107+
async fn add_task(&self, task_input: CommitterTaskInput) -> CommitmentManagerResult<()> {
108+
let error_message = format!("Failed to send task to state committer: {task_input}.",);
102109

103110
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-
}
111+
info!("Waiting to send task for {task_input} to state committer.");
112+
self.tasks_sender
113+
.send(task_input)
114+
.await
115+
.unwrap_or_else(|err| panic!("{error_message}. error: {err}"));
112116
} else {
113-
match self.tasks_sender.try_send(commitment_task_input) {
114-
Ok(_) => self.successfully_added_commitment_task(height, state_diff_commitment),
117+
match self.tasks_sender.try_send(task_input) {
118+
Ok(_) => (),
115119
Err(TrySendError::Full(_)) => {
116120
let channel_size = self.tasks_sender.max_capacity();
117121
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.",
122+
"{error_message}. The channel is full. channel size: {channel_size}. \
123+
Consider increasing the channel size or enabling waiting in the config.",
122124
);
123125
}
124126
Err(err) => panic!("{error_message}. error: {err}"),
125127
}
126128
}
129+
Ok(())
127130
}
128131

129132
/// Fetches all ready commitment results from the state committer. Panics if any task is a
@@ -168,13 +171,12 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
168171
&mut self,
169172
height: BlockNumber,
170173
state_diff_commitment: Option<StateDiffCommitment>,
171-
) -> CommitmentManagerResult<()> {
174+
) {
172175
info!(
173176
"Sent commitment task for block {height} and state diff {state_diff_commitment:?} to \
174177
state committer."
175178
);
176179
self.increase_commitment_task_offset();
177-
Ok(())
178180
}
179181

180182
/// Initializes the CommitmentManager. This includes starting the state committer task.
@@ -203,6 +205,11 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
203205
self.commitment_task_offset.next().expect("Block number overflowed.");
204206
}
205207

208+
pub(crate) fn decrease_commitment_task_offset(&mut self) {
209+
self.commitment_task_offset =
210+
self.commitment_task_offset.prev().expect("Can't revert before the genesis block.");
211+
}
212+
206213
async fn read_commitment_input_and_add_task<R: BatcherStorageReader>(
207214
&mut self,
208215
height: BlockNumber,
@@ -256,8 +263,25 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
256263

257264
// Associated functions.
258265

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

263287
/// 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)