@@ -64,7 +64,7 @@ use std::collections::VecDeque;
64
64
use std:: sync:: { Arc , Weak } ;
65
65
use std:: thread;
66
66
use std:: time;
67
- use std:: time:: { Duration , SystemTime , UNIX_EPOCH } ;
67
+ use std:: time:: { Instant , Duration , SystemTime , UNIX_EPOCH } ;
68
68
69
69
use block:: ExecutedBlock ;
70
70
use client:: { BlockId , EngineClient } ;
@@ -89,11 +89,9 @@ use time_utils::CheckedSystemTime;
89
89
90
90
use self :: block_state:: CliqueBlockState ;
91
91
use self :: params:: CliqueParams ;
92
- use self :: step_service:: StepService ;
93
92
94
93
mod params;
95
94
mod block_state;
96
- mod step_service;
97
95
mod util;
98
96
99
97
// TODO(niklasad1): extract tester types into a separate mod to be shared in the code base
@@ -168,7 +166,6 @@ pub struct Clique {
168
166
block_state_by_hash : RwLock < LruCache < H256 , CliqueBlockState > > ,
169
167
proposals : RwLock < HashMap < Address , VoteType > > ,
170
168
signer : RwLock < Option < Box < EngineSigner > > > ,
171
- step_service : Option < StepService > ,
172
169
}
173
170
174
171
#[ cfg( test) ]
@@ -181,33 +178,45 @@ pub struct Clique {
181
178
pub block_state_by_hash : RwLock < LruCache < H256 , CliqueBlockState > > ,
182
179
pub proposals : RwLock < HashMap < Address , VoteType > > ,
183
180
pub signer : RwLock < Option < Box < EngineSigner > > > ,
184
- pub step_service : Option < StepService > ,
185
181
}
186
182
187
183
impl Clique {
188
184
/// Initialize Clique engine from empty state.
189
185
pub fn new ( params : CliqueParams , machine : EthereumMachine ) -> Result < Arc < Self > , Error > {
190
- let mut engine = Clique {
186
+ /// Step Clique at most every 2 seconds
187
+ const SEALING_FREQ : Duration = Duration :: from_secs ( 2 ) ;
188
+
189
+ let engine = Clique {
191
190
epoch_length : params. epoch ,
192
191
period : params. period ,
193
192
client : Default :: default ( ) ,
194
193
block_state_by_hash : RwLock :: new ( LruCache :: new ( STATE_CACHE_NUM ) ) ,
195
194
proposals : Default :: default ( ) ,
196
195
signer : Default :: default ( ) ,
197
196
machine,
198
- step_service : None ,
199
197
} ;
200
- if params. period > 0 {
201
- engine. step_service = Some ( StepService :: new ( ) ) ;
202
- let engine = Arc :: new ( engine) ;
203
- let weak_eng = Arc :: downgrade ( & engine) ;
204
- if let Some ( step_service) = & engine. step_service {
205
- step_service. start ( weak_eng) ;
206
- }
207
- Ok ( engine)
208
- } else {
209
- Ok ( Arc :: new ( engine) )
210
- }
198
+ let engine = Arc :: new ( engine) ;
199
+ let weak_eng = Arc :: downgrade ( & engine) ;
200
+
201
+ thread:: Builder :: new ( ) . name ( "StepService" . into ( ) )
202
+ . spawn ( move || {
203
+ loop {
204
+ let next_step_at = Instant :: now ( ) + SEALING_FREQ ;
205
+ trace ! ( target: "miner" , "StepService: triggering sealing" ) ;
206
+ if let Some ( eng) = weak_eng. upgrade ( ) {
207
+ eng. step ( )
208
+ } else {
209
+ warn ! ( target: "shutdown" , "StepService: engine is dropped; exiting." ) ;
210
+ break ;
211
+ }
212
+
213
+ let now = Instant :: now ( ) ;
214
+ if now < next_step_at {
215
+ thread:: sleep ( next_step_at - now) ;
216
+ }
217
+ }
218
+ } ) ?;
219
+ Ok ( engine)
211
220
}
212
221
213
222
#[ cfg( test) ]
@@ -225,7 +234,6 @@ impl Clique {
225
234
proposals : Default :: default ( ) ,
226
235
signer : Default :: default ( ) ,
227
236
machine : Spec :: new_test_machine ( ) ,
228
- step_service : None ,
229
237
}
230
238
}
231
239
@@ -348,15 +356,6 @@ impl Clique {
348
356
}
349
357
}
350
358
351
- impl Drop for Clique {
352
- fn drop ( & mut self ) {
353
- if let Some ( step_service) = & self . step_service {
354
- trace ! ( target: "shutdown" , "Clique; stopping step service" ) ;
355
- step_service. stop ( ) ;
356
- }
357
- }
358
- }
359
-
360
359
impl Engine < EthereumMachine > for Clique {
361
360
fn name ( & self ) -> & str { "Clique" }
362
361
0 commit comments