1- // Copyright 2018-2019 Kodebox, Inc.
1+ // Copyright 2018-2020 Kodebox, Inc.
22// This file is part of CodeChain.
33//
44// This program is free software: you can redistribute it and/or modify
@@ -33,7 +33,8 @@ use kvdb::DBTransaction;
3333use parking_lot:: { Mutex , MutexGuard } ;
3434use rlp:: Encodable ;
3535use std:: borrow:: Borrow ;
36- use std:: collections:: { HashMap , HashSet } ;
36+ use std:: collections:: HashSet ;
37+ use std:: iter:: FromIterator ;
3738use std:: sync:: Arc ;
3839
3940pub struct Importer {
@@ -134,40 +135,22 @@ impl Importer {
134135 if !is_empty {
135136 ctrace ! ( CLIENT , "Call new_blocks even though block verification queue is not empty" ) ;
136137 }
137- let ( enacted, retracted ) = self . calculate_enacted_retracted ( & import_results) ;
138- self . miner . chain_new_blocks ( client, & imported_blocks, & invalid_blocks, & enacted, & retracted ) ;
139- client. new_blocks ( & imported_blocks, & invalid_blocks, & enacted, & retracted , & [ ] ) ;
138+ let enacted = self . extract_enacted ( import_results) ;
139+ self . miner . chain_new_blocks ( client, & imported_blocks, & invalid_blocks, & enacted) ;
140+ client. new_blocks ( & imported_blocks, & invalid_blocks, & enacted, & [ ] ) ;
140141 }
141142 }
142143
143144 client. db ( ) . flush ( ) . expect ( "DB flush failed." ) ;
144145 imported
145146 }
146147
147- pub fn calculate_enacted_retracted ( & self , import_results : & [ ImportRoute ] ) -> ( Vec < BlockHash > , Vec < BlockHash > ) {
148- fn map_to_vec ( map : Vec < ( BlockHash , bool ) > ) -> Vec < BlockHash > {
149- map. into_iter ( ) . map ( |( k, _v) | k) . collect ( )
150- }
151-
152- // In ImportRoute we get all the blocks that have been enacted and retracted by single insert.
153- // Because we are doing multiple inserts some of the blocks that were enacted in import `k`
154- // could be retracted in import `k+1`. This is why to understand if after all inserts
155- // the block is enacted or retracted we iterate over all routes and at the end final state
156- // will be in the hashmap
157- let map = import_results. iter ( ) . fold ( HashMap :: new ( ) , |mut map, route| {
158- for hash in & route. enacted {
159- map. insert ( * hash, true ) ;
160- }
161- for hash in & route. retracted {
162- map. insert ( * hash, false ) ;
163- }
164- map
148+ pub fn extract_enacted ( & self , import_results : Vec < ImportRoute > ) -> Vec < BlockHash > {
149+ let set = import_results. into_iter ( ) . fold ( HashSet :: new ( ) , |mut set, route| {
150+ set. extend ( route. enacted ) ;
151+ set
165152 } ) ;
166-
167- // Split to enacted retracted (using hashmap value)
168- let ( enacted, retracted) = map. into_iter ( ) . partition ( |& ( _k, v) | v) ;
169- // And convert tuples to keys
170- ( map_to_vec ( enacted) , map_to_vec ( retracted) )
153+ Vec :: from_iter ( set)
171154 }
172155
173156 // NOTE: the header of the block passed here is not necessarily sealed, as
@@ -336,7 +319,7 @@ impl Importer {
336319 }
337320
338321 self . header_queue . mark_as_bad ( & bad. drain ( ) . collect :: < Vec < _ > > ( ) ) ;
339- let ( enacted, retracted ) = self . calculate_enacted_retracted ( & routes) ;
322+ let enacted = self . extract_enacted ( routes) ;
340323
341324 let new_best_proposal_header_hash = client. block_chain ( ) . best_proposal_header ( ) . hash ( ) ;
342325 let best_proposal_header_changed = if prev_best_proposal_header_hash != new_best_proposal_header_hash {
@@ -349,7 +332,6 @@ impl Importer {
349332 & imported,
350333 & bad. iter ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) ,
351334 & enacted,
352- & retracted,
353335 & [ ] ,
354336 best_proposal_header_changed,
355337 ) ;
@@ -370,7 +352,7 @@ impl Importer {
370352 client. db ( ) . write_buffered ( batch) ;
371353 chain. commit ( ) ;
372354 }
373- client. new_headers ( & [ hash] , & [ ] , & [ ] , & [ ] , & [ ] , None ) ;
355+ client. new_headers ( & [ hash] , & [ ] , & [ ] , & [ ] , None ) ;
374356
375357 client. db ( ) . flush ( ) . expect ( "DB flush failed." ) ;
376358 }
@@ -388,8 +370,8 @@ impl Importer {
388370 client. db ( ) . write_buffered ( batch) ;
389371 chain. commit ( ) ;
390372 }
391- self . miner . chain_new_blocks ( client, & [ hash] , & [ ] , & [ ] , & [ ] ) ;
392- client. new_blocks ( & [ hash] , & [ ] , & [ ] , & [ ] , & [ ] ) ;
373+ self . miner . chain_new_blocks ( client, & [ hash] , & [ ] , & [ ] ) ;
374+ client. new_blocks ( & [ hash] , & [ ] , & [ ] , & [ ] ) ;
393375
394376 client. db ( ) . flush ( ) . expect ( "DB flush failed." ) ;
395377 }
0 commit comments