| 141 | type Error = SynchronizeTaskError; |
| 142 | |
| 143 | async fn execute(&self, state: &mut EngineState) -> Result<Self::Output, SynchronizeTaskError> { |
| 144 | // Apply the sync state update to the engine state. |
| 145 | let new_sync_state = state.sync_state.updated(self.state_update); |
| 146 | |
| 147 | // Check if a forkchoice update is not needed, return early. |
| 148 | // A forkchoice update is not needed if... |
| 149 | // 1. The engine state is not default (initial forkchoice state has been emitted), and |
| 150 | // 2. The new sync state is the same as the current sync state (no changes to the sync |
| 151 | // state). |
| 152 | // |
| 153 | // NOTE: |
| 154 | // We shouldn't retry the synchronize task there. Since the `sync_state` is only updated |
| 155 | // inside the `SynchronizeTask` (except inside the ConsolidateTask, when the block is not |
| 156 | // the last in the batch) - the engine will get stuck retrying the `SynchronizeTask` |
| 157 | if !self.force |
| 158 | && state.sync_state != Default::default() |
| 159 | && state.sync_state == new_sync_state |
| 160 | { |
| 161 | debug!(target: "engine", ?new_sync_state, "No forkchoice update needed"); |
| 162 | return Ok(()); |
| 163 | } |
| 164 | |
| 165 | // Check if the head is behind the finalized head. |
| 166 | if new_sync_state.unsafe_head().block_info.number |
| 167 | < new_sync_state.finalized_head().block_info.number |
| 168 | { |
| 169 | return Err(SynchronizeTaskError::FinalizedAheadOfUnsafe( |
| 170 | new_sync_state.unsafe_head().block_info.number, |
| 171 | new_sync_state.finalized_head().block_info.number, |
| 172 | )); |
| 173 | } |
| 174 | |
| 175 | let fcu_time_start = Instant::now(); |
| 176 | |
| 177 | // Send the forkchoice update through the input. |
| 178 | let forkchoice = new_sync_state.create_forkchoice_state(); |
| 179 | |
| 180 | // Handle the forkchoice update result. |
| 181 | // NOTE: it doesn't matter which version we use here, because we're not sending any |
| 182 | // payload attributes. The forkchoice updated call is version agnostic if no payload |
| 183 | // attributes are provided. |
| 184 | let response = self.client.fork_choice_updated_v3(forkchoice, None).await; |
| 185 | |
| 186 | let valid_response = response.map_err(|e| { |
| 187 | // Fatal forkchoice update error. |
| 188 | let error = e |
| 189 | .as_error_resp() |
| 190 | .and_then(|e| { |
| 191 | (e.code == INVALID_FORK_CHOICE_STATE_ERROR as i64) |
| 192 | .then_some(SynchronizeTaskError::InvalidForkchoiceState) |
| 193 | }) |
| 194 | .unwrap_or_else(|| SynchronizeTaskError::ForkchoiceUpdateFailed(e)); |
| 195 | |
| 196 | debug!(target: "engine", error = ?error, "Unexpected forkchoice update error"); |
| 197 | |
| 198 | error |
| 199 | })?; |
| 200 | |