1616
1717use std:: sync:: atomic:: { AtomicUsize , Ordering as AtomicOrdering } ;
1818use std:: sync:: { Arc , Weak } ;
19- use std:: time:: Instant ;
19+ use std:: time:: { Duration , Instant } ;
2020
2121use cio:: IoChannel ;
2222use ckey:: { Address , PlatformAddress , Public } ;
@@ -25,6 +25,7 @@ use cnetwork::NodeId;
2525use cstate:: {
2626 ActionHandler , AssetScheme , AssetSchemeAddress , OwnedAsset , OwnedAssetAddress , StateDB , TopLevelState , TopStateView ,
2727} ;
28+ use ctimer:: { TimeoutHandler , TimerApi , TimerToken } ;
2829use ctypes:: invoice:: Invoice ;
2930use ctypes:: transaction:: Transaction ;
3031use ctypes:: { BlockNumber , ShardId } ;
@@ -41,7 +42,8 @@ use super::{
4142 AccountData , AssetClient , Balance , BlockChain as BlockChainTrait , BlockChainClient , BlockChainInfo , BlockInfo ,
4243 BlockProducer , ChainInfo , ChainNotify , ClientConfig , DatabaseClient , EngineClient , EngineInfo ,
4344 Error as ClientError , ExecuteClient , ImportBlock , ImportResult , ImportSealedBlock , MiningBlockChainClient ,
44- ParcelInfo , PrepareOpenBlock , RegularKey , RegularKeyOwner , ReopenBlock , Seq , Shard , StateOrBlock , TransactionInfo ,
45+ ParcelInfo , PrepareOpenBlock , RegularKey , RegularKeyOwner , ReopenBlock , ResealTimer , Seq , Shard , StateOrBlock ,
46+ TransactionInfo ,
4547} ;
4648use crate :: block:: { ClosedBlock , IsBlock , OpenBlock , SealedBlock } ;
4749use crate :: blockchain:: {
@@ -79,6 +81,8 @@ pub struct Client {
7981 genesis_accounts : Vec < Address > ,
8082
8183 importer : Importer ,
84+
85+ reseal_timer : RwLock < Option < TimerApi > > ,
8286}
8387
8488impl Client {
@@ -121,13 +125,18 @@ impl Client {
121125 queue_parcels : AtomicUsize :: new ( 0 ) ,
122126 genesis_accounts,
123127 importer,
128+ reseal_timer : RwLock :: new ( None ) ,
124129 } ) ;
125130
126131 // ensure buffered changes are flushed.
127132 client. db . read ( ) . flush ( ) . map_err ( ClientError :: Database ) ?;
128133 Ok ( client)
129134 }
130135
136+ pub fn register_reseal_timer ( & self , timer : TimerApi ) {
137+ self . register_timer ( timer) ;
138+ }
139+
131140 /// Returns engine reference.
132141 pub fn engine ( & self ) -> & CodeChainEngine {
133142 & * self . engine
@@ -260,6 +269,34 @@ impl Client {
260269 }
261270}
262271
272+ const RESEAL_TIMER_TOKEN : TimerToken = 0 ;
273+
274+ impl TimeoutHandler for Client {
275+ fn on_timeout ( & self , token : TimerToken ) {
276+ match token {
277+ RESEAL_TIMER_TOKEN => {
278+ if !self . ready_parcels ( ) . is_empty ( ) {
279+ self . update_sealing ( ) ;
280+ }
281+ }
282+ _ => unreachable ! ( ) ,
283+ }
284+ }
285+ }
286+
287+ impl ResealTimer for Client {
288+ fn register_timer ( & self , timer : TimerApi ) {
289+ * self . reseal_timer . write ( ) = Some ( timer) ;
290+ }
291+
292+ fn set_timer ( & self , duration : Duration ) {
293+ if let Some ( reseal_timer) = self . reseal_timer . read ( ) . as_ref ( ) {
294+ reseal_timer. cancel ( RESEAL_TIMER_TOKEN ) . expect ( "Timer clear succeeds" ) ;
295+ reseal_timer. schedule_once ( duration, RESEAL_TIMER_TOKEN ) . expect ( "Timer set succeeds" ) ;
296+ } ;
297+ }
298+ }
299+
263300impl DatabaseClient for Client {
264301 fn database ( & self ) -> Arc < KeyValueDB > {
265302 Arc :: clone ( & self . db ( ) )
0 commit comments