@@ -21,6 +21,7 @@ pub struct Import {
2121 shared : Shared ,
2222 chain : ChainController ,
2323 switch : Switch ,
24+ num_threads : usize ,
2425}
2526
2627impl Import {
@@ -30,12 +31,14 @@ impl Import {
3031 shared : Shared ,
3132 source : ImportSource ,
3233 switch : Switch ,
34+ num_threads : usize ,
3335 ) -> Self {
3436 Import {
3537 chain,
3638 shared,
3739 source,
3840 switch,
41+ num_threads,
3942 }
4043 }
4144
@@ -78,7 +81,7 @@ impl Import {
7881 }
7982
8083 let f: Box < dyn Read + Send > = match & self . source {
81- ImportSource :: Path ( source) => Box :: new ( fs:: File :: open ( & source) ?) ,
84+ ImportSource :: Path ( source) => Box :: new ( fs:: File :: open ( source) ?) ,
8285 ImportSource :: Stdin => {
8386 // read from stdin
8487 Box :: new ( std:: io:: stdin ( ) )
@@ -131,10 +134,12 @@ impl Import {
131134 }
132135 }
133136
134- #[ cfg( feature = "progress_bar" ) ]
135137 let progress_bar = {
136138 let bar = match & self . source {
137- ImportSource :: Path ( source) => ProgressBar :: new ( fs:: metadata ( & source) ?. len ( ) ) ,
139+ ImportSource :: Path ( source) => {
140+ let file_size = fs:: metadata ( source) ?. len ( ) ;
141+ ProgressBar :: new ( file_size)
142+ }
138143 ImportSource :: Stdin => ProgressBar :: new_spinner ( ) ,
139144 } ;
140145 let style = ProgressStyle :: default_bar ( )
@@ -146,12 +151,13 @@ impl Import {
146151
147152 let mut largest_block_number = 0 ;
148153 const BLOCKS_COUNT_PER_CHUNK : usize = 1024 * 6 ;
149- let ( blocks_tx, blocks_rx) = ckb_channel:: bounded :: < Arc < BlockView > > ( BLOCKS_COUNT_PER_CHUNK ) ;
154+ let ( blocks_tx, blocks_rx) =
155+ ckb_channel:: bounded :: < ( Arc < BlockView > , usize ) > ( BLOCKS_COUNT_PER_CHUNK ) ;
150156 std:: thread:: spawn ( {
151- #[ cfg( feature = "progress_bar" ) ]
152- let progress_bar = progress_bar. clone ( ) ;
157+ let num_threads = self . num_threads ;
153158 move || {
154159 let pool = rayon:: ThreadPoolBuilder :: new ( )
160+ . num_threads ( num_threads)
155161 . build ( )
156162 . expect ( "rayon thread pool must build" ) ;
157163 pool. install ( || {
@@ -168,33 +174,35 @@ impl Import {
168174 let block: JsonBlock =
169175 serde_json:: from_str ( line) . expect ( "parse block from json" ) ;
170176 let block: Arc < core:: BlockView > = Arc :: new ( block. into ( ) ) ;
171- blocks_tx. send ( block) . expect ( "send block to channel" ) ;
172-
173- #[ cfg( feature = "progress_bar" ) ]
174- progress_bar. inc ( line. len ( ) as u64 ) ;
177+ blocks_tx
178+ . send ( ( block, line. len ( ) ) )
179+ . expect ( "send block to channel" ) ;
175180 } ) ;
176181 }
177182 drop ( blocks_tx) ;
178183 } ) ;
179184 }
180185 } ) ;
181186
182- let callback = |verify_result : VerifyResult | {
183- if let Err ( err) = verify_result {
184- eprintln ! ( "Error verifying block: {:?}" , err) ;
185- }
186- } ;
187-
188- for block in blocks_rx {
187+ for ( block, block_size) in blocks_rx {
189188 if !block. is_genesis ( ) {
190189 use ckb_chain:: LonelyBlock ;
191190
192191 largest_block_number = largest_block_number. max ( block. number ( ) ) ;
193192
193+ let progress_bar = progress_bar. clone ( ) ;
194+ let callback = Box :: new ( move |verify_result : VerifyResult | {
195+ if let Err ( err) = verify_result {
196+ eprintln ! ( "Error verifying block: {:?}" , err) ;
197+ } else {
198+ progress_bar. inc ( block_size as u64 ) ;
199+ }
200+ } ) ;
201+
194202 let lonely_block = LonelyBlock {
195203 block,
196204 switch : Some ( self . switch ) ,
197- verify_callback : Some ( Box :: new ( callback) ) ,
205+ verify_callback : Some ( callback) ,
198206 } ;
199207 self . chain . asynchronous_process_lonely_block ( lonely_block) ;
200208 }
@@ -209,7 +217,6 @@ impl Import {
209217 std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) ;
210218 }
211219
212- #[ cfg( feature = "progress_bar" ) ]
213220 progress_bar. finish_with_message ( "done!" ) ;
214221 Ok ( ( ) )
215222 }
0 commit comments