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,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.
0 commit comments