@@ -26,16 +26,15 @@ use ckey::{public_to_address, Address, Password, PlatformAddress, Public};
26
26
use cstate:: { FindActionHandler , TopLevelState } ;
27
27
use ctypes:: errors:: { HistoryError , RuntimeError } ;
28
28
use ctypes:: transaction:: { Action , IncompleteTransaction , Timelock } ;
29
- use ctypes:: { BlockHash , BlockNumber , TxHash } ;
29
+ use ctypes:: { BlockHash , TxHash } ;
30
30
use cvm:: ChainTimeInfo ;
31
31
use kvdb:: KeyValueDB ;
32
32
use parking_lot:: { Mutex , RwLock } ;
33
- use primitives:: { Bytes , H256 } ;
33
+ use primitives:: Bytes ;
34
34
35
35
use super :: mem_pool:: { Error as MemPoolError , MemPool } ;
36
36
pub use super :: mem_pool_types:: MemPoolFees ;
37
37
use super :: mem_pool_types:: { AccountDetails , MemPoolInput , TxOrigin , TxTimelock } ;
38
- use super :: sealing_queue:: SealingQueue ;
39
38
use super :: { MinerService , MinerStatus , TransactionImportResult } ;
40
39
use crate :: account_provider:: { AccountProvider , Error as AccountProviderError } ;
41
40
use crate :: block:: { ClosedBlock , IsBlock } ;
@@ -103,20 +102,13 @@ pub struct AuthoringParams {
103
102
pub extra_data : Bytes ,
104
103
}
105
104
106
- struct SealingWork {
107
- queue : SealingQueue ,
108
- enabled : bool ,
109
- }
110
-
111
105
type TransactionListener = Box < dyn Fn ( & [ TxHash ] ) + Send + Sync > ;
112
106
113
107
pub struct Miner {
114
108
mem_pool : Arc < RwLock < MemPool > > ,
115
109
transaction_listener : RwLock < Vec < TransactionListener > > ,
116
110
next_allowed_reseal : Mutex < Instant > ,
117
111
next_mandatory_reseal : RwLock < Instant > ,
118
- sealing_block_last_request : Mutex < u64 > ,
119
- sealing_work : Mutex < SealingWork > ,
120
112
params : RwLock < AuthoringParams > ,
121
113
engine : Arc < dyn CodeChainEngine > ,
122
114
options : MinerOptions ,
@@ -163,11 +155,6 @@ impl Miner {
163
155
next_allowed_reseal : Mutex :: new ( Instant :: now ( ) ) ,
164
156
next_mandatory_reseal : RwLock :: new ( Instant :: now ( ) + options. reseal_max_period ) ,
165
157
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
- } ) ,
171
158
engine : scheme. engine . clone ( ) ,
172
159
options,
173
160
sealing_enabled : AtomicBool :: new ( true ) ,
@@ -190,22 +177,6 @@ impl Miner {
190
177
& self . options
191
178
}
192
179
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
-
209
180
fn add_transactions_to_pool < C : AccountData + BlockChainTrait + EngineInfo > (
210
181
& self ,
211
182
client : & C ,
@@ -375,45 +346,15 @@ impl Miner {
375
346
} )
376
347
}
377
348
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
-
405
349
/// Prepares new block for sealing including top transactions from queue.
406
350
fn prepare_block <
407
351
C : AccountData + BlockChainTrait + BlockProducer + ChainTimeInfo + EngineInfo + FindActionHandler + TermInfo ,
408
352
> (
409
353
& self ,
410
354
parent_block_id : BlockId ,
411
355
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) = {
417
358
ctrace ! ( MINER , "prepare_block: No existing work - making new block" ) ;
418
359
let params = self . params . read ( ) . clone ( ) ;
419
360
let open_block = chain. prepare_open_block ( parent_block_id, params. author , params. extra_data ) ;
@@ -432,7 +373,7 @@ impl Miner {
432
373
. top_transactions ( max_body_size, Some ( open_block. header ( ) . timestamp ( ) ) , DEFAULT_RANGE )
433
374
. transactions ;
434
375
435
- ( transactions, open_block, last_work_hash , block_number)
376
+ ( transactions, open_block, block_number)
436
377
} ;
437
378
438
379
let parent_header = {
@@ -542,7 +483,7 @@ impl Miner {
542
483
chain. chain_info ( ) . best_block_timestamp ,
543
484
) ;
544
485
}
545
- Ok ( Some ( ( block, original_work_hash ) ) )
486
+ Ok ( Some ( block) )
546
487
}
547
488
548
489
/// 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 {
616
557
617
558
fn status ( & self ) -> MinerStatus {
618
559
let status = self . mem_pool . read ( ) . status ( ) ;
619
- let sealing_work = self . sealing_work . lock ( ) ;
620
560
MinerStatus {
621
561
transactions_in_pending_queue : status. pending ,
622
562
transactions_in_future_queue : status. future ,
623
- tranasction_in_pending_block : sealing_work. queue . peek_last_ref ( ) . map_or ( 0 , |b| b. transactions ( ) . len ( ) ) ,
624
563
}
625
564
}
626
565
@@ -636,11 +575,6 @@ impl MinerService for Miner {
636
575
ctrace ! ( MINER , "Set author to {:?}" , address) ;
637
576
// Sign test message
638
577
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
- }
644
578
self . engine . set_signer ( ap. clone ( ) , address) ;
645
579
Ok ( ( ) )
646
580
} else {
@@ -713,57 +647,6 @@ impl MinerService for Miner {
713
647
self . engine . engine_type ( )
714
648
}
715
649
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
-
767
650
fn update_sealing < C > ( & self , chain : & C , parent_block : BlockId , allow_empty_block : bool )
768
651
where
769
652
C : AccountData
@@ -776,41 +659,38 @@ impl MinerService for Miner {
776
659
+ TermInfo , {
777
660
ctrace ! ( MINER , "update_sealing: preparing a block" ) ;
778
661
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" ) ;
791
666
return
792
667
}
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) ;
806
676
return
807
677
}
678
+ } ;
808
679
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" ) ;
813
684
}
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 ( ) ;
814
694
}
815
695
}
816
696
@@ -874,7 +754,7 @@ impl MinerService for Miner {
874
754
if imported. is_ok ( ) && self . options . reseal_on_own_transaction && self . transaction_reseal_allowed ( ) && !self . engine_type ( ) . ignore_reseal_on_transaction ( )
875
755
// Make sure to do it after transaction is imported and lock is dropped.
876
756
// We need to create pending block and enable sealing.
877
- && ( self . engine . seals_internally ( ) || ! self . prepare_work_sealing ( chain ) )
757
+ && self . engine . seals_internally ( )
878
758
{
879
759
// If new block has not been prepared (means we already had one)
880
760
// or Engine might be able to seal internally,
@@ -1010,7 +890,7 @@ pub mod test {
1010
890
use ckey:: { Private , Signature } ;
1011
891
use ctimer:: TimerLoop ;
1012
892
use ctypes:: transaction:: Transaction ;
1013
- use primitives:: H512 ;
893
+ use primitives:: { H256 , H512 } ;
1014
894
1015
895
use super :: super :: super :: client:: ClientConfig ;
1016
896
use super :: super :: super :: service:: ClientIoMessage ;
0 commit comments