22
33use std:: sync:: Arc ;
44
5+ use alloy_json_rpc:: ErrorPayload ;
56use alloy_primitives:: FixedBytes ;
6- use alloy_rpc_types_engine:: { ForkchoiceUpdated , PayloadId , PayloadStatus , PayloadStatusEnum } ;
7+ use alloy_rpc_types_engine:: {
8+ ForkchoiceUpdated , INVALID_FORK_CHOICE_STATE_ERROR , PayloadId , PayloadStatus , PayloadStatusEnum ,
9+ } ;
710use base_consensus_genesis:: RollupConfig ;
811use rstest:: rstest;
912use thiserror:: Error ;
1013use tokio:: sync:: mpsc;
1114
1215use crate :: {
1316 BuildTask , BuildTaskError , EngineBuildError , EngineClient , EngineForkchoiceVersion ,
14- EngineState , EngineTaskExt ,
17+ EngineState , EngineTaskError , EngineTaskErrorSeverity , EngineTaskExt ,
1518 test_utils:: {
1619 MockEngineClientBuilder , TestAttributesBuilder , TestEngineStateBuilder , test_block_info,
1720 test_engine_client_builder,
@@ -54,6 +57,8 @@ enum TestErr {
5457 EngineSyncing ,
5558 #[ error( "FinalizedAheadOfUnsafe." ) ]
5659 FinalizedAheadOfUnsafe ,
60+ #[ error( "ForkchoiceStateInvalid." ) ]
61+ ForkchoiceStateInvalid ,
5762 #[ error( "InvalidPayload." ) ]
5863 InvalidPayload ,
5964 #[ error( "MissingPayloadId." ) ]
@@ -77,6 +82,7 @@ async fn wrapped_execute<EngineClient_: EngineClient>(
7782 }
7883 EngineBuildError :: EngineSyncing => Err ( TestErr :: EngineSyncing ) ,
7984 EngineBuildError :: FinalizedAheadOfUnsafe ( _, _) => Err ( TestErr :: FinalizedAheadOfUnsafe ) ,
85+ EngineBuildError :: ForkchoiceStateInvalid => Err ( TestErr :: ForkchoiceStateInvalid ) ,
8086 EngineBuildError :: InvalidPayload ( _) => Err ( TestErr :: InvalidPayload ) ,
8187 EngineBuildError :: MissingPayloadId => Err ( TestErr :: MissingPayloadId ) ,
8288 EngineBuildError :: UnexpectedPayloadStatus ( _) => Err ( TestErr :: Unexpected ) ,
@@ -167,3 +173,70 @@ async fn test_execute_variants(
167173 }
168174 }
169175}
176+
177+ fn configure_fcu_error (
178+ b : MockEngineClientBuilder ,
179+ fcu_version : EngineForkchoiceVersion ,
180+ error : ErrorPayload ,
181+ cfg : & mut RollupConfig ,
182+ attributes_timestamp : u64 ,
183+ ) -> MockEngineClientBuilder {
184+ match fcu_version {
185+ EngineForkchoiceVersion :: V2 => {
186+ cfg. hardforks . ecotone_time = Some ( attributes_timestamp + 1 ) ;
187+ b. with_fork_choice_updated_v2_error ( error)
188+ }
189+ EngineForkchoiceVersion :: V3 => {
190+ cfg. hardforks . ecotone_time = Some ( attributes_timestamp) ;
191+ b. with_fork_choice_updated_v3_error ( error)
192+ }
193+ }
194+ }
195+
196+ #[ rstest]
197+ #[ tokio:: test]
198+ async fn test_invalid_forkchoice_state_triggers_reset (
199+ #[ values( EngineForkchoiceVersion :: V2 , EngineForkchoiceVersion :: V3 ) ]
200+ fcu_version : EngineForkchoiceVersion ,
201+ ) {
202+ let parent_block = test_block_info ( 0 ) ;
203+ let unsafe_block = test_block_info ( 1 ) ;
204+ let attributes_timestamp = unsafe_block. block_info . timestamp ;
205+
206+ let mut cfg = RollupConfig :: default ( ) ;
207+
208+ let error = ErrorPayload {
209+ code : INVALID_FORK_CHOICE_STATE_ERROR as i64 ,
210+ message : "Invalid fork choice state" . into ( ) ,
211+ data : None ,
212+ } ;
213+
214+ let engine_client = configure_fcu_error (
215+ test_engine_client_builder ( ) ,
216+ fcu_version,
217+ error,
218+ & mut cfg,
219+ attributes_timestamp,
220+ )
221+ . build ( ) ;
222+
223+ let attributes = TestAttributesBuilder :: new ( )
224+ . with_parent ( parent_block)
225+ . with_timestamp ( attributes_timestamp)
226+ . build ( ) ;
227+
228+ let task = BuildTask :: new ( Arc :: new ( engine_client) , Arc :: new ( cfg) , attributes, None ) ;
229+
230+ let mut state = TestEngineStateBuilder :: new ( )
231+ . with_unsafe_head ( unsafe_block)
232+ . with_safe_head ( parent_block)
233+ . with_finalized_head ( parent_block)
234+ . build ( ) ;
235+
236+ let result = wrapped_execute ( & task, & mut state) . await ;
237+
238+ assert_eq ! ( result, Err ( TestErr :: ForkchoiceStateInvalid ) ) ;
239+
240+ let err = BuildTaskError :: EngineBuildError ( EngineBuildError :: ForkchoiceStateInvalid ) ;
241+ assert_eq ! ( err. severity( ) , EngineTaskErrorSeverity :: Reset ) ;
242+ }
0 commit comments