@@ -26,16 +26,15 @@ use ckey::{public_to_address, Address, Password, PlatformAddress, Public};
2626use cstate:: { FindActionHandler , TopLevelState } ;
2727use ctypes:: errors:: { HistoryError , RuntimeError } ;
2828use ctypes:: transaction:: { Action , IncompleteTransaction , Timelock } ;
29- use ctypes:: { BlockHash , BlockNumber , TxHash } ;
29+ use ctypes:: { BlockHash , TxHash } ;
3030use cvm:: ChainTimeInfo ;
3131use kvdb:: KeyValueDB ;
3232use parking_lot:: { Mutex , RwLock } ;
33- use primitives:: { Bytes , H256 } ;
33+ use primitives:: Bytes ;
3434
3535use super :: mem_pool:: { Error as MemPoolError , MemPool } ;
3636pub use super :: mem_pool_types:: MemPoolFees ;
3737use super :: mem_pool_types:: { AccountDetails , MemPoolInput , TxOrigin , TxTimelock } ;
38- use super :: sealing_queue:: SealingQueue ;
3938use super :: { MinerService , MinerStatus , TransactionImportResult } ;
4039use crate :: account_provider:: { AccountProvider , Error as AccountProviderError } ;
4140use crate :: block:: { ClosedBlock , IsBlock } ;
@@ -103,20 +102,13 @@ pub struct AuthoringParams {
103102 pub extra_data : Bytes ,
104103}
105104
106- struct SealingWork {
107- queue : SealingQueue ,
108- enabled : bool ,
109- }
110-
111105type TransactionListener = Box < dyn Fn ( & [ TxHash ] ) + Send + Sync > ;
112106
113107pub struct Miner {
114108 mem_pool : Arc < RwLock < MemPool > > ,
115109 transaction_listener : RwLock < Vec < TransactionListener > > ,
116110 next_allowed_reseal : Mutex < Instant > ,
117111 next_mandatory_reseal : RwLock < Instant > ,
118- sealing_block_last_request : Mutex < u64 > ,
119- sealing_work : Mutex < SealingWork > ,
120112 params : RwLock < AuthoringParams > ,
121113 engine : Arc < dyn CodeChainEngine > ,
122114 options : MinerOptions ,
@@ -163,11 +155,6 @@ impl Miner {
163155 next_allowed_reseal : Mutex :: new ( Instant :: now ( ) ) ,
164156 next_mandatory_reseal : RwLock :: new ( Instant :: now ( ) + options. reseal_max_period ) ,
165157 params : RwLock :: new ( AuthoringParams :: default ( ) ) ,
166- sealing_block_last_request : Mutex :: new ( 0 ) ,
167- sealing_work : Mutex :: new ( SealingWork {
168- queue : SealingQueue :: new ( ) ,
169- enabled : options. force_sealing ,
170- } ) ,
171158 engine : scheme. engine . clone ( ) ,
172159 options,
173160 sealing_enabled : AtomicBool :: new ( true ) ,
@@ -190,22 +177,6 @@ impl Miner {
190177 & self . options
191178 }
192179
193- /// Check is reseal is allowed and necessary.
194- fn requires_reseal ( & self , best_block : BlockNumber ) -> bool {
195- let sealing_work = self . sealing_work . lock ( ) ;
196- if sealing_work. enabled {
197- ctrace ! ( MINER , "requires_reseal: sealing enabled" ) ;
198- let last_request = * self . sealing_block_last_request . lock ( ) ;
199-
200- ctrace ! ( MINER , "requires_reseal: best_block={}, last_request={}" , best_block, last_request) ;
201-
202- true
203- } else {
204- cdebug ! ( MINER , "requires_reseal: sealing is disabled" ) ;
205- false
206- }
207- }
208-
209180 fn add_transactions_to_pool < C : AccountData + BlockChainTrait + EngineInfo > (
210181 & self ,
211182 client : & C ,
@@ -375,45 +346,15 @@ impl Miner {
375346 } )
376347 }
377348
378- /// Prepares work which has to be done to seal.
379- fn prepare_work ( & self , block : ClosedBlock , original_work_hash : Option < H256 > ) {
380- let mut sealing_work = self . sealing_work . lock ( ) ;
381- let last_work_hash = sealing_work. queue . peek_last_ref ( ) . map ( |pb| pb. block ( ) . header ( ) . hash ( ) ) ;
382- ctrace ! (
383- MINER ,
384- "prepare_work: Checking whether we need to reseal: orig={:?} last={:?}, this={:?}" ,
385- original_work_hash,
386- last_work_hash,
387- block. block( ) . header( ) . hash( )
388- ) ;
389- if last_work_hash. map_or ( true , |h| h != block. block ( ) . header ( ) . hash ( ) ) {
390- ctrace ! (
391- MINER ,
392- "prepare_work: Pushing a new, refreshed or borrowed pending {}..." ,
393- block. block( ) . header( ) . hash( )
394- ) ;
395- sealing_work. queue . push ( block) ;
396- // If push notifications are enabled we assume all work items are used.
397- } ;
398- ctrace ! (
399- MINER ,
400- "prepare_work: leaving (last={:?})" ,
401- sealing_work. queue. peek_last_ref( ) . map( |b| b. block( ) . header( ) . hash( ) )
402- ) ;
403- }
404-
405349 /// Prepares new block for sealing including top transactions from queue.
406350 fn prepare_block <
407351 C : AccountData + BlockChainTrait + BlockProducer + ChainTimeInfo + EngineInfo + FindActionHandler + TermInfo ,
408352 > (
409353 & self ,
410354 parent_block_id : BlockId ,
411355 chain : & C ,
412- ) -> Result < Option < ( ClosedBlock , Option < H256 > ) > , Error > {
413- let ( transactions, mut open_block, original_work_hash, block_number) = {
414- let sealing_work = self . sealing_work . lock ( ) ;
415-
416- let last_work_hash = sealing_work. queue . peek_last_ref ( ) . map ( |pb| * pb. block ( ) . header ( ) . hash ( ) ) ;
356+ ) -> Result < Option < ClosedBlock > , Error > {
357+ let ( transactions, mut open_block, block_number) = {
417358 ctrace ! ( MINER , "prepare_block: No existing work - making new block" ) ;
418359 let params = self . params . read ( ) . clone ( ) ;
419360 let open_block = chain. prepare_open_block ( parent_block_id, params. author , params. extra_data ) ;
@@ -432,7 +373,7 @@ impl Miner {
432373 . top_transactions ( max_body_size, Some ( open_block. header ( ) . timestamp ( ) ) , DEFAULT_RANGE )
433374 . transactions ;
434375
435- ( transactions, open_block, last_work_hash , block_number)
376+ ( transactions, open_block, block_number)
436377 } ;
437378
438379 let parent_header = {
@@ -542,7 +483,7 @@ impl Miner {
542483 chain. chain_info ( ) . best_block_timestamp ,
543484 ) ;
544485 }
545- Ok ( Some ( ( block, original_work_hash ) ) )
486+ Ok ( Some ( block) )
546487 }
547488
548489 /// Attempts to perform internal sealing (one that does not require work) and handles the result depending on the type of Seal.
@@ -616,11 +557,9 @@ impl MinerService for Miner {
616557
617558 fn status ( & self ) -> MinerStatus {
618559 let status = self . mem_pool . read ( ) . status ( ) ;
619- let sealing_work = self . sealing_work . lock ( ) ;
620560 MinerStatus {
621561 transactions_in_pending_queue : status. pending ,
622562 transactions_in_future_queue : status. future ,
623- tranasction_in_pending_block : sealing_work. queue . peek_last_ref ( ) . map_or ( 0 , |b| b. transactions ( ) . len ( ) ) ,
624563 }
625564 }
626565
@@ -636,11 +575,6 @@ impl MinerService for Miner {
636575 ctrace ! ( MINER , "Set author to {:?}" , address) ;
637576 // Sign test message
638577 ap. get_unlocked_account ( & address) ?. sign ( & Default :: default ( ) ) ?;
639- // Limit the scope of the locks.
640- {
641- let mut sealing_work = self . sealing_work . lock ( ) ;
642- sealing_work. enabled = true ;
643- }
644578 self . engine . set_signer ( ap. clone ( ) , address) ;
645579 Ok ( ( ) )
646580 } else {
@@ -713,57 +647,6 @@ impl MinerService for Miner {
713647 self . engine . engine_type ( )
714648 }
715649
716- fn prepare_work_sealing <
717- C : AccountData + BlockChainTrait + BlockProducer + ChainTimeInfo + EngineInfo + FindActionHandler + TermInfo ,
718- > (
719- & self ,
720- client : & C ,
721- ) -> bool {
722- ctrace ! ( MINER , "prepare_work_sealing: entering" ) ;
723- let prepare_new = {
724- let mut sealing_work = self . sealing_work . lock ( ) ;
725- let have_work = sealing_work. queue . peek_last_ref ( ) . is_some ( ) ;
726- ctrace ! ( MINER , "prepare_work_sealing: have_work={}" , have_work) ;
727- if !have_work {
728- sealing_work. enabled = true ;
729- true
730- } else {
731- false
732- }
733- } ;
734- if prepare_new {
735- // --------------------------------------------------------------------------
736- // | NOTE Code below requires transaction_queue and sealing_work locks. |
737- // | Make sure to release the locks before calling that method. |
738- // --------------------------------------------------------------------------
739- match self . prepare_block ( BlockId :: Latest , client) {
740- Ok ( Some ( ( block, original_work_hash) ) ) => {
741- self . prepare_work ( block, original_work_hash) ;
742- }
743- Ok ( None ) => {
744- ctrace ! ( MINER , "prepare_work_sealing: cannot prepare block" ) ;
745- }
746- Err ( err) => {
747- ctrace ! ( MINER , "prepare_work_sealing: cannot prepare block: {:?}" , err) ;
748- }
749- }
750- }
751- let mut sealing_block_last_request = self . sealing_block_last_request . lock ( ) ;
752- let best_number = client. chain_info ( ) . best_block_number ;
753- if * sealing_block_last_request != best_number {
754- ctrace ! (
755- MINER ,
756- "prepare_work_sealing: Miner received request (was {}, now {}) - waking up." ,
757- * sealing_block_last_request,
758- best_number
759- ) ;
760- * sealing_block_last_request = best_number;
761- }
762-
763- // Return if we restarted
764- prepare_new
765- }
766-
767650 fn update_sealing < C > ( & self , chain : & C , parent_block : BlockId , allow_empty_block : bool )
768651 where
769652 C : AccountData
@@ -776,41 +659,38 @@ impl MinerService for Miner {
776659 + TermInfo , {
777660 ctrace ! ( MINER , "update_sealing: preparing a block" ) ;
778661
779- let parent_block_number = chain. block_header ( & parent_block) . expect ( "Parent is always exist" ) . number ( ) ;
780- if self . requires_reseal ( parent_block_number) {
781- let block = match self . prepare_block ( parent_block, chain) {
782- Ok ( Some ( ( block, _) ) ) => {
783- if !allow_empty_block && block. block ( ) . transactions ( ) . is_empty ( ) {
784- ctrace ! ( MINER , "update_sealing: block is empty, and allow_empty_block is false" ) ;
785- return
786- }
787- block
788- }
789- Ok ( None ) => {
790- ctrace ! ( MINER , "update_sealing: cannot prepare block" ) ;
662+ let block = match self . prepare_block ( parent_block, chain) {
663+ Ok ( Some ( block) ) => {
664+ if !allow_empty_block && block. block ( ) . transactions ( ) . is_empty ( ) {
665+ ctrace ! ( MINER , "update_sealing: block is empty, and allow_empty_block is false" ) ;
791666 return
792667 }
793- Err ( err) => {
794- ctrace ! ( MINER , "update_sealing: cannot prepare block: {:?}" , err) ;
795- return
796- }
797- } ;
798-
799- if self . engine . seals_internally ( ) {
800- ctrace ! ( MINER , "update_sealing: engine indicates internal sealing" ) ;
801- if self . seal_and_import_block_internally ( chain, block) {
802- ctrace ! ( MINER , "update_sealing: imported internally sealed block" ) ;
803- }
804- } else {
805- ctrace ! ( MINER , "update_sealing: engine is not keen to seal internally right now" ) ;
668+ block
669+ }
670+ Ok ( None ) => {
671+ ctrace ! ( MINER , "update_sealing: cannot prepare block" ) ;
672+ return
673+ }
674+ Err ( err) => {
675+ ctrace ! ( MINER , "update_sealing: cannot prepare block: {:?}" , err) ;
806676 return
807677 }
678+ } ;
808679
809- // Sealing successful
810- * self . next_allowed_reseal . lock ( ) = Instant :: now ( ) + self . options . reseal_min_period ;
811- if ! self . options . no_reseal_timer {
812- chain . set_min_timer ( ) ;
680+ if self . engine . seals_internally ( ) {
681+ ctrace ! ( MINER , "update_sealing: engine indicates internal sealing" ) ;
682+ if self . seal_and_import_block_internally ( chain , block ) {
683+ ctrace ! ( MINER , "update_sealing: imported internally sealed block" ) ;
813684 }
685+ } else {
686+ ctrace ! ( MINER , "update_sealing: engine is not keen to seal internally right now" ) ;
687+ return
688+ }
689+
690+ // Sealing successful
691+ * self . next_allowed_reseal . lock ( ) = Instant :: now ( ) + self . options . reseal_min_period ;
692+ if !self . options . no_reseal_timer {
693+ chain. set_min_timer ( ) ;
814694 }
815695 }
816696
@@ -874,7 +754,7 @@ impl MinerService for Miner {
874754 if imported. is_ok ( ) && self . options . reseal_on_own_transaction && self . transaction_reseal_allowed ( ) && !self . engine_type ( ) . ignore_reseal_on_transaction ( )
875755 // Make sure to do it after transaction is imported and lock is dropped.
876756 // We need to create pending block and enable sealing.
877- && ( self . engine . seals_internally ( ) || ! self . prepare_work_sealing ( chain ) )
757+ && self . engine . seals_internally ( )
878758 {
879759 // If new block has not been prepared (means we already had one)
880760 // or Engine might be able to seal internally,
@@ -1010,7 +890,7 @@ pub mod test {
1010890 use ckey:: { Private , Signature } ;
1011891 use ctimer:: TimerLoop ;
1012892 use ctypes:: transaction:: Transaction ;
1013- use primitives:: H512 ;
893+ use primitives:: { H256 , H512 } ;
1014894
1015895 use super :: super :: super :: client:: ClientConfig ;
1016896 use super :: super :: super :: service:: ClientIoMessage ;
0 commit comments