@@ -28,6 +28,7 @@ use ethcore::client::{BlockStatus, BlockId, BlockImportError, BlockImportErrorKi
2828use ethcore:: error:: { ImportErrorKind , QueueErrorKind , BlockError } ;
2929use sync_io:: SyncIo ;
3030use blocks:: { BlockCollection , SyncBody , SyncHeader } ;
31+ use chain:: BlockSet ;
3132
3233const MAX_HEADERS_TO_REQUEST : usize = 128 ;
3334const MAX_BODIES_TO_REQUEST : usize = 32 ;
@@ -36,6 +37,17 @@ const SUBCHAIN_SIZE: u64 = 256;
3637const MAX_ROUND_PARENTS : usize = 16 ;
3738const MAX_PARALLEL_SUBCHAIN_DOWNLOAD : usize = 5 ;
3839
40+ macro_rules! trace_sync {
41+ ( $self: ident, target: $target: expr, $( $arg: tt) * ) => {
42+ trace!( target: $target, $( $arg) +, $self. block_set) ;
43+ }
44+ }
45+ macro_rules! debug_sync {
46+ ( $self: ident, target: $target: expr, $( $arg: tt) * ) => {
47+ debug!( target: $target, $( $arg) +, $self. block_set) ;
48+ }
49+ }
50+
3951#[ derive( Copy , Clone , Eq , PartialEq , Debug ) ]
4052/// Downloader state
4153pub enum State {
@@ -89,6 +101,8 @@ impl From<rlp::DecoderError> for BlockDownloaderImportError {
89101/// Block downloader strategy.
90102/// Manages state and block data for a block download process.
91103pub struct BlockDownloader {
104+ /// Which set of blocks to download
105+ block_set : BlockSet ,
92106 /// Downloader state
93107 state : State ,
94108 /// Highest block number seen
@@ -117,29 +131,15 @@ pub struct BlockDownloader {
117131}
118132
119133impl BlockDownloader {
120- /// Create a new instance of syncing strategy. This won't reorganize to before the
121- /// last kept state.
122- pub fn new ( sync_receipts : bool , start_hash : & H256 , start_number : BlockNumber ) -> Self {
123- BlockDownloader {
124- state : State :: Idle ,
125- highest_block : None ,
126- last_imported_block : start_number,
127- last_imported_hash : start_hash. clone ( ) ,
128- last_round_start : start_number,
129- last_round_start_hash : start_hash. clone ( ) ,
130- blocks : BlockCollection :: new ( sync_receipts) ,
131- imported_this_round : None ,
132- round_parents : VecDeque :: new ( ) ,
133- download_receipts : sync_receipts,
134- target_hash : None ,
135- retract_step : 1 ,
136- limit_reorg : true ,
137- }
138- }
139-
140- /// Create a new instance of sync with unlimited reorg allowed.
141- pub fn with_unlimited_reorg ( sync_receipts : bool , start_hash : & H256 , start_number : BlockNumber ) -> Self {
134+ /// Create a new instance of syncing strategy.
135+ /// For BlockSet::NewBlocks this won't reorganize to before the last kept state.
136+ pub fn new ( block_set : BlockSet , start_hash : & H256 , start_number : BlockNumber ) -> Self {
137+ let ( limit_reorg, sync_receipts) = match block_set {
138+ BlockSet :: NewBlocks => ( true , false ) ,
139+ BlockSet :: OldBlocks => ( false , true )
140+ } ;
142141 BlockDownloader {
142+ block_set : block_set,
143143 state : State :: Idle ,
144144 highest_block : None ,
145145 last_imported_block : start_number,
@@ -152,7 +152,7 @@ impl BlockDownloader {
152152 download_receipts : sync_receipts,
153153 target_hash : None ,
154154 retract_step : 1 ,
155- limit_reorg : false ,
155+ limit_reorg : limit_reorg ,
156156 }
157157 }
158158
0 commit comments