33use std:: sync:: Arc ;
44
55use 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+ } ;
711use apollo_committer_types:: communication:: SharedCommitterClient ;
812use starknet_api:: block:: BlockNumber ;
913use 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.
0 commit comments