11use std:: { sync:: Arc , time:: Duration } ;
22
3+ use alloy:: providers:: mock;
34use cb_common:: {
45 pbs:: { BuilderApiVersion , GetPayloadInfo , SubmitBlindedBlockResponse } ,
56 signer:: random_secret,
67 types:: Chain ,
78} ;
89use cb_pbs:: { DefaultBuilderApi , PbsService , PbsState } ;
910use cb_tests:: {
10- mock_relay:: { MockRelayState , start_mock_relay_service} ,
11+ mock_relay:: { self , MockRelayState , start_mock_relay_service} ,
1112 mock_validator:: { MockValidator , load_test_signed_blinded_block} ,
1213 utils:: { generate_mock_relay, get_pbs_static_config, setup_test_env, to_pbs_config} ,
1314} ;
@@ -17,7 +18,7 @@ use tracing::info;
1718
1819#[ tokio:: test]
1920async fn test_submit_block_v1 ( ) -> Result < ( ) > {
20- let res = submit_block_impl ( 3800 , & BuilderApiVersion :: V1 ) . await ?;
21+ let res = submit_block_impl ( 3800 , & BuilderApiVersion :: V1 , false , false ) . await ?;
2122 assert_eq ! ( res. status( ) , StatusCode :: OK ) ;
2223
2324 let signed_blinded_block = load_test_signed_blinded_block ( ) ;
@@ -32,12 +33,31 @@ async fn test_submit_block_v1() -> Result<()> {
3233
3334#[ tokio:: test]
3435async fn test_submit_block_v2 ( ) -> Result < ( ) > {
35- let res = submit_block_impl ( 3850 , & BuilderApiVersion :: V2 ) . await ?;
36+ let res = submit_block_impl ( 3802 , & BuilderApiVersion :: V2 , false , false ) . await ?;
3637 assert_eq ! ( res. status( ) , StatusCode :: ACCEPTED ) ;
3738 assert_eq ! ( res. bytes( ) . await ?. len( ) , 0 ) ;
3839 Ok ( ( ) )
3940}
4041
42+ // Test that when submitting a block using v2 to a relay that does not support
43+ // v2, PBS falls back to v1 and successfully submits the block.
44+ #[ tokio:: test]
45+ async fn test_submit_block_v2_without_relay_support ( ) -> Result < ( ) > {
46+ let res = submit_block_impl ( 3804 , & BuilderApiVersion :: V2 , true , false ) . await ?;
47+ assert_eq ! ( res. status( ) , StatusCode :: ACCEPTED ) ;
48+ assert_eq ! ( res. bytes( ) . await ?. len( ) , 0 ) ;
49+ Ok ( ( ) )
50+ }
51+
52+ // Test that when submitting a block using v2 to a relay that returns 404s
53+ // for both v1 and v2, PBS doesn't loop forever.
54+ #[ tokio:: test]
55+ async fn test_submit_block_on_broken_relay ( ) -> Result < ( ) > {
56+ let res = submit_block_impl ( 3806 , & BuilderApiVersion :: V2 , true , true ) . await ?;
57+ assert_eq ! ( res. status( ) , StatusCode :: BAD_GATEWAY ) ;
58+ Ok ( ( ) )
59+ }
60+
4161#[ tokio:: test]
4262async fn test_submit_block_too_large ( ) -> Result < ( ) > {
4363 setup_test_env ( ) ;
@@ -68,7 +88,12 @@ async fn test_submit_block_too_large() -> Result<()> {
6888 Ok ( ( ) )
6989}
7090
71- async fn submit_block_impl ( pbs_port : u16 , api_version : & BuilderApiVersion ) -> Result < Response > {
91+ async fn submit_block_impl (
92+ pbs_port : u16 ,
93+ api_version : & BuilderApiVersion ,
94+ remove_v2_support : bool ,
95+ force_404s : bool ,
96+ ) -> Result < Response > {
7297 setup_test_env ( ) ;
7398 let signer = random_secret ( ) ;
7499 let pubkey = signer. public_key ( ) ;
@@ -77,7 +102,14 @@ async fn submit_block_impl(pbs_port: u16, api_version: &BuilderApiVersion) -> Re
77102
78103 // Run a mock relay
79104 let relays = vec ! [ generate_mock_relay( pbs_port + 1 , pubkey) ?] ;
80- let mock_state = Arc :: new ( MockRelayState :: new ( chain, signer) ) ;
105+ let mut mock_relay_state = MockRelayState :: new ( chain, signer) ;
106+ if remove_v2_support {
107+ mock_relay_state = mock_relay_state. with_no_submit_block_v2 ( ) ;
108+ }
109+ if force_404s {
110+ mock_relay_state = mock_relay_state. with_not_found_for_submit_block ( ) ;
111+ }
112+ let mock_state = Arc :: new ( mock_relay_state) ;
81113 tokio:: spawn ( start_mock_relay_service ( mock_state. clone ( ) , pbs_port + 1 ) ) ;
82114
83115 // Run the PBS service
@@ -99,6 +131,7 @@ async fn submit_block_impl(pbs_port: u16, api_version: &BuilderApiVersion) -> Re
99131 mock_validator. do_submit_block_v2 ( Some ( signed_blinded_block) ) . await ?
100132 }
101133 } ;
102- assert_eq ! ( mock_state. received_submit_block( ) , 1 ) ;
134+ let expected_count = if force_404s { 0 } else { 1 } ;
135+ assert_eq ! ( mock_state. received_submit_block( ) , expected_count) ;
103136 Ok ( res)
104137}
0 commit comments