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,40 @@ 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 (
108+ & self ,
109+ task_input : CommitterTaskInput ,
110+ ) -> CommitmentManagerResult < ( ) > {
111+ let error_message = format ! ( "Failed to send task to state committer: {task_input}." , ) ;
102112
103113 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- }
114+ info ! ( "Waiting to send task for {task_input} to state committer." ) ;
115+ self . tasks_sender
116+ . send ( task_input)
117+ . await
118+ . unwrap_or_else ( |err| panic ! ( "{error_message}. error: {err}" ) ) ;
112119 } else {
113- match self . tasks_sender . try_send ( commitment_task_input ) {
114- Ok ( _) => self . successfully_added_commitment_task ( height , state_diff_commitment ) ,
120+ match self . tasks_sender . try_send ( task_input ) {
121+ Ok ( _) => ( ) ,
115122 Err ( TrySendError :: Full ( _) ) => {
116123 let channel_size = self . tasks_sender . max_capacity ( ) ;
117124 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.",
125+ "{error_message}. The channel is full. channel size: {channel_size}. \
126+ Consider increasing the channel size or enabling waiting in the config.",
122127 ) ;
123128 }
124129 Err ( err) => panic ! ( "{error_message}. error: {err}" ) ,
125130 }
126131 }
132+ Ok ( ( ) )
127133 }
128134
129135 /// Fetches all ready commitment results from the state committer. Panics if any task is a
@@ -168,13 +174,12 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
168174 & mut self ,
169175 height : BlockNumber ,
170176 state_diff_commitment : Option < StateDiffCommitment > ,
171- ) -> CommitmentManagerResult < ( ) > {
177+ ) {
172178 info ! (
173179 "Sent commitment task for block {height} and state diff {state_diff_commitment:?} to \
174180 state committer."
175181 ) ;
176182 self . increase_commitment_task_offset ( ) ;
177- Ok ( ( ) )
178183 }
179184
180185 /// Initializes the CommitmentManager. This includes starting the state committer task.
@@ -203,6 +208,11 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
203208 self . commitment_task_offset . next ( ) . expect ( "Block number overflowed." ) ;
204209 }
205210
211+ pub ( crate ) fn decrease_commitment_task_offset ( & mut self ) {
212+ self . commitment_task_offset =
213+ self . commitment_task_offset . prev ( ) . expect ( "Can't revert before the genesis block." ) ;
214+ }
215+
206216 async fn read_commitment_input_and_add_task < R : BatcherStorageReader > (
207217 & mut self ,
208218 height : BlockNumber ,
@@ -256,8 +266,25 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
256266
257267 // Associated functions.
258268
259- pub ( crate ) async fn revert_block ( height : BlockNumber , reversed_state_diff : ThinStateDiff ) {
260- unimplemented ! ( )
269+ pub ( crate ) async fn add_revert_task (
270+ & mut self ,
271+ height : BlockNumber ,
272+ reversed_state_diff : ThinStateDiff ,
273+ ) -> CommitmentManagerResult < ( ) > {
274+ let expected_height =
275+ self . commitment_task_offset . prev ( ) . expect ( "Can't revert before the genesis block." ) ;
276+ if height != expected_height {
277+ return Err ( CommitmentManagerError :: WrongRevertTaskHeight {
278+ expected : expected_height,
279+ actual : height,
280+ } ) ;
281+ }
282+ let revert_task_input =
283+ CommitterTaskInput :: Revert ( RevertBlockRequest { height, reversed_state_diff } ) ;
284+ self . add_task ( revert_task_input) . await ?;
285+ info ! ( "Sent revert task for block {height}." ) ;
286+ self . decrease_commitment_task_offset ( ) ;
287+ Ok ( ( ) )
261288 }
262289
263290 /// Returns the final commitment output for a given commitment task output.
0 commit comments