@@ -3,21 +3,21 @@ use alloy_eips::BlockNumberOrTag;
33use alloy_primitives:: B256 ;
44use alloy_rpc_types_engine:: { ExecutionPayloadV3 , ForkchoiceUpdated , PayloadStatus } ;
55use jsonrpsee:: {
6- core:: RpcResult ,
7- http_client:: { transport:: HttpBackend , HttpClient } ,
6+ core:: { client:: SubscriptionClientT , RpcResult } ,
87 proc_macros:: rpc,
98} ;
109use reth:: rpc:: { api:: EngineApiClient , types:: engine:: ForkchoiceState } ;
1110use reth_node_api:: { EngineTypes , PayloadTypes } ;
1211use reth_optimism_node:: OpEngineTypes ;
1312use reth_payload_builder:: PayloadId ;
14- use reth_rpc_layer:: { AuthClientLayer , AuthClientService , JwtSecret } ;
13+ use reth_rpc_layer:: { AuthClientLayer , JwtSecret } ;
1514use serde_json:: Value ;
1615use std:: str:: FromStr ;
1716
1817/// Helper for engine api operations
1918pub struct EngineApi {
20- pub engine_api_client : HttpClient < AuthClientService < HttpBackend > > ,
19+ url : url:: Url ,
20+ jwt_secret : JwtSecret ,
2121}
2222
2323/// Builder for EngineApi configuration
@@ -46,15 +46,9 @@ impl EngineApiBuilder {
4646 }
4747
4848 pub fn build ( self ) -> Result < EngineApi , Box < dyn std:: error:: Error > > {
49- let secret_layer = AuthClientLayer :: new ( JwtSecret :: from_str ( & self . jwt_secret ) ?) ;
50- let middleware = tower:: ServiceBuilder :: default ( ) . layer ( secret_layer) ;
51- let client = jsonrpsee:: http_client:: HttpClientBuilder :: default ( )
52- . set_http_middleware ( middleware)
53- . build ( & self . url )
54- . expect ( "Failed to create http client" ) ;
55-
5649 Ok ( EngineApi {
57- engine_api_client : client,
50+ url : self . url . parse ( ) ?,
51+ jwt_secret : JwtSecret :: from_str ( & self . jwt_secret ) ?,
5852 } )
5953 }
6054}
@@ -74,6 +68,16 @@ impl EngineApi {
7468 . build ( )
7569 }
7670
71+ pub fn http_client ( & self ) -> impl SubscriptionClientT + Clone + Send + Sync + Unpin + ' static {
72+ // Create a middleware that adds a new JWT token to every request.
73+ let secret_layer = AuthClientLayer :: new ( self . jwt_secret ) ;
74+ let middleware = tower:: ServiceBuilder :: default ( ) . layer ( secret_layer) ;
75+ jsonrpsee:: http_client:: HttpClientBuilder :: default ( )
76+ . set_http_middleware ( middleware)
77+ . build ( & self . url )
78+ . expect ( "Failed to create http client" )
79+ }
80+
7781 pub async fn get_payload_v3 (
7882 & self ,
7983 payload_id : PayloadId ,
@@ -85,7 +89,7 @@ impl EngineApi {
8589 ) ;
8690
8791 Ok (
88- EngineApiClient :: < OpEngineTypes > :: get_payload_v3 ( & self . engine_api_client , payload_id)
92+ EngineApiClient :: < OpEngineTypes > :: get_payload_v3 ( & self . http_client ( ) , payload_id)
8993 . await ?,
9094 )
9195 }
@@ -99,7 +103,7 @@ impl EngineApi {
99103 println ! ( "Submitting new payload at {}..." , chrono:: Utc :: now( ) ) ;
100104
101105 Ok ( EngineApiClient :: < OpEngineTypes > :: new_payload_v3 (
102- & self . engine_api_client ,
106+ & self . http_client ( ) ,
103107 payload,
104108 versioned_hashes,
105109 parent_beacon_block_root,
@@ -116,7 +120,7 @@ impl EngineApi {
116120 println ! ( "Updating forkchoice at {}..." , chrono:: Utc :: now( ) ) ;
117121
118122 Ok ( EngineApiClient :: < OpEngineTypes > :: fork_choice_updated_v3 (
119- & self . engine_api_client ,
123+ & self . http_client ( ) ,
120124 ForkchoiceState {
121125 head_block_hash : new_head,
122126 safe_block_hash : current_head,
@@ -137,10 +141,7 @@ impl EngineApi {
137141 number : BlockNumberOrTag ,
138142 include_txs : bool ,
139143 ) -> eyre:: Result < Option < alloy_rpc_types_eth:: Block > > {
140- Ok (
141- BlockApiClient :: get_block_by_number ( & self . engine_api_client , number, include_txs)
142- . await ?,
143- )
144+ Ok ( BlockApiClient :: get_block_by_number ( & self . http_client ( ) , number, include_txs) . await ?)
144145 }
145146}
146147
0 commit comments