11//! Contains the implementation of the mining mode for the local engine.
22
3- use alloy_consensus:: BlockHeader ;
43use alloy_primitives:: { TxHash , B256 } ;
54use alloy_rpc_types_engine:: ForkchoiceState ;
65use eyre:: OptionExt ;
@@ -10,14 +9,15 @@ use reth_payload_builder::PayloadBuilderHandle;
109use reth_payload_primitives:: {
1110 BuiltPayload , EngineApiMessageVersion , PayloadAttributesBuilder , PayloadKind , PayloadTypes ,
1211} ;
12+ use reth_primitives_traits:: { HeaderTy , SealedHeaderFor } ;
1313use reth_storage_api:: BlockReader ;
1414use reth_transaction_pool:: TransactionPool ;
1515use std:: {
1616 collections:: VecDeque ,
1717 future:: Future ,
1818 pin:: Pin ,
1919 task:: { Context , Poll } ,
20- time:: { Duration , UNIX_EPOCH } ,
20+ time:: Duration ,
2121} ;
2222use tokio:: time:: Interval ;
2323use tokio_stream:: wrappers:: ReceiverStream ;
@@ -106,36 +106,39 @@ pub struct LocalMiner<T: PayloadTypes, B, Pool: TransactionPool + Unpin> {
106106 mode : MiningMode < Pool > ,
107107 /// The payload builder for the engine
108108 payload_builder : PayloadBuilderHandle < T > ,
109- /// Timestamp for the next block .
110- last_timestamp : u64 ,
109+ /// Latest block in the chain so far .
110+ last_header : SealedHeaderFor < < T :: BuiltPayload as BuiltPayload > :: Primitives > ,
111111 /// Stores latest mined blocks.
112112 last_block_hashes : VecDeque < B256 > ,
113113}
114114
115115impl < T , B , Pool > LocalMiner < T , B , Pool >
116116where
117117 T : PayloadTypes ,
118- B : PayloadAttributesBuilder < <T as PayloadTypes >:: PayloadAttributes > ,
118+ B : PayloadAttributesBuilder <
119+ T :: PayloadAttributes ,
120+ HeaderTy < <T :: BuiltPayload as BuiltPayload >:: Primitives > ,
121+ > ,
119122 Pool : TransactionPool + Unpin ,
120123{
121124 /// Spawns a new [`LocalMiner`] with the given parameters.
122125 pub fn new (
123- provider : impl BlockReader ,
126+ provider : impl BlockReader < Header = HeaderTy < < T :: BuiltPayload as BuiltPayload > :: Primitives > > ,
124127 payload_attributes_builder : B ,
125128 to_engine : ConsensusEngineHandle < T > ,
126129 mode : MiningMode < Pool > ,
127130 payload_builder : PayloadBuilderHandle < T > ,
128131 ) -> Self {
129- let latest_header =
132+ let last_header =
130133 provider. sealed_header ( provider. best_block_number ( ) . unwrap ( ) ) . unwrap ( ) . unwrap ( ) ;
131134
132135 Self {
133136 payload_attributes_builder,
134137 to_engine,
135138 mode,
136139 payload_builder,
137- last_timestamp : latest_header . timestamp ( ) ,
138- last_block_hashes : VecDeque :: from ( [ latest_header . hash ( ) ] ) ,
140+ last_block_hashes : VecDeque :: from ( [ last_header . hash ( ) ] ) ,
141+ last_header ,
139142 }
140143 }
141144
@@ -193,19 +196,11 @@ where
193196 /// Generates payload attributes for a new block, passes them to FCU and inserts built payload
194197 /// through newPayload.
195198 async fn advance ( & mut self ) -> eyre:: Result < ( ) > {
196- let timestamp = std:: cmp:: max (
197- self . last_timestamp . saturating_add ( 1 ) ,
198- std:: time:: SystemTime :: now ( )
199- . duration_since ( UNIX_EPOCH )
200- . expect ( "cannot be earlier than UNIX_EPOCH" )
201- . as_secs ( ) ,
202- ) ;
203-
204199 let res = self
205200 . to_engine
206201 . fork_choice_updated (
207202 self . forkchoice_state ( ) ,
208- Some ( self . payload_attributes_builder . build ( timestamp ) ) ,
203+ Some ( self . payload_attributes_builder . build ( & self . last_header ) ) ,
209204 EngineApiMessageVersion :: default ( ) ,
210205 )
211206 . await ?;
@@ -222,16 +217,16 @@ where
222217 eyre:: bail!( "No payload" )
223218 } ;
224219
225- let block = payload. block ( ) ;
220+ let header = payload. block ( ) . sealed_header ( ) . clone ( ) ;
226221 let payload = T :: block_to_payload ( payload. block ( ) . clone ( ) ) ;
227222 let res = self . to_engine . new_payload ( payload) . await ?;
228223
229224 if !res. is_valid ( ) {
230225 eyre:: bail!( "Invalid payload" )
231226 }
232227
233- self . last_timestamp = timestamp ;
234- self . last_block_hashes . push_back ( block . hash ( ) ) ;
228+ self . last_block_hashes . push_back ( header . hash ( ) ) ;
229+ self . last_header = header ;
235230 // ensure we keep at most 64 blocks
236231 if self . last_block_hashes . len ( ) > 64 {
237232 self . last_block_hashes . pop_front ( ) ;
0 commit comments