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,7 @@ 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 ;
3737use std:: sync:: Arc ;
3838
3939pub struct Importer {
@@ -134,40 +134,24 @@ impl Importer {
134134 if !is_empty {
135135 ctrace ! ( CLIENT , "Call new_blocks even though block verification queue is not empty" ) ;
136136 }
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 , & [ ] ) ;
137+ let enacted = self . extract_enacted ( & import_results) ;
138+ self . miner . chain_new_blocks ( client, & imported_blocks, & invalid_blocks, & enacted) ;
139+ client. new_blocks ( & imported_blocks, & invalid_blocks, & enacted, & [ ] ) ;
140140 }
141141 }
142142
143143 client. db ( ) . flush ( ) . expect ( "DB flush failed." ) ;
144144 imported
145145 }
146146
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
147+ pub fn extract_enacted ( & self , import_results : & [ ImportRoute ] ) -> Vec < BlockHash > {
148+ let set = import_results. iter ( ) . fold ( HashSet :: new ( ) , |mut set, route| {
149+ route. enacted . iter ( ) . for_each ( |hash| {
150+ set. insert ( * hash) ;
151+ } ) ;
152+ set
165153 } ) ;
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) )
154+ set. into_iter ( ) . collect ( )
171155 }
172156
173157 // NOTE: the header of the block passed here is not necessarily sealed, as
@@ -336,7 +320,7 @@ impl Importer {
336320 }
337321
338322 self . header_queue . mark_as_bad ( & bad. drain ( ) . collect :: < Vec < _ > > ( ) ) ;
339- let ( enacted, retracted ) = self . calculate_enacted_retracted ( & routes) ;
323+ let enacted = self . extract_enacted ( & routes) ;
340324
341325 let new_best_proposal_header_hash = client. block_chain ( ) . best_proposal_header ( ) . hash ( ) ;
342326 let best_proposal_header_changed = if prev_best_proposal_header_hash != new_best_proposal_header_hash {
@@ -349,7 +333,6 @@ impl Importer {
349333 & imported,
350334 & bad. iter ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) ,
351335 & enacted,
352- & retracted,
353336 & [ ] ,
354337 best_proposal_header_changed,
355338 ) ;
@@ -370,7 +353,7 @@ impl Importer {
370353 client. db ( ) . write_buffered ( batch) ;
371354 chain. commit ( ) ;
372355 }
373- client. new_headers ( & [ hash] , & [ ] , & [ ] , & [ ] , & [ ] , None ) ;
356+ client. new_headers ( & [ hash] , & [ ] , & [ ] , & [ ] , None ) ;
374357
375358 client. db ( ) . flush ( ) . expect ( "DB flush failed." ) ;
376359 }
@@ -388,8 +371,8 @@ impl Importer {
388371 client. db ( ) . write_buffered ( batch) ;
389372 chain. commit ( ) ;
390373 }
391- self . miner . chain_new_blocks ( client, & [ hash] , & [ ] , & [ ] , & [ ] ) ;
392- client. new_blocks ( & [ hash] , & [ ] , & [ ] , & [ ] , & [ ] ) ;
374+ self . miner . chain_new_blocks ( client, & [ hash] , & [ ] , & [ ] ) ;
375+ client. new_blocks ( & [ hash] , & [ ] , & [ ] , & [ ] ) ;
393376
394377 client. db ( ) . flush ( ) . expect ( "DB flush failed." ) ;
395378 }
0 commit comments