@@ -72,7 +72,7 @@ impl HeaderChain {
7272
7373 let details = BlockDetails {
7474 number : genesis. number ( ) ,
75- total_score : genesis. score ( ) ,
75+ view : genesis. view ( ) ,
7676 parent : genesis. parent_hash ( ) ,
7777 } ;
7878
@@ -134,7 +134,7 @@ impl HeaderChain {
134134 let mut new_details = HashMap :: new ( ) ;
135135 new_details. insert ( hash, BlockDetails {
136136 number : header. number ( ) ,
137- total_score : 0 . into ( ) ,
137+ view : header . view ( ) ,
138138 parent : header. parent_hash ( ) ,
139139 } ) ;
140140
@@ -252,12 +252,11 @@ impl HeaderChain {
252252 /// Uses the given parent details or attempts to load them from the database.
253253 fn new_detail_entries ( & self , header : & HeaderView < ' _ > ) -> HashMap < BlockHash , BlockDetails > {
254254 let parent_hash = header. parent_hash ( ) ;
255- let parent_details = self . block_details ( & parent_hash) . expect ( "Invalid parent hash" ) ;
256255
257256 // create current block details.
258257 let details = BlockDetails {
259258 number : header. number ( ) ,
260- total_score : parent_details . total_score + header. score ( ) ,
259+ view : header. view ( ) ,
261260 parent : parent_hash,
262261 } ;
263262
@@ -267,14 +266,22 @@ impl HeaderChain {
267266 block_details
268267 }
269268
269+ /// Compare the number and the view of current block with these of best block
270+ fn is_new_header_eligible_to_be_best ( & self , new_header : & HeaderView < ' _ > ) -> bool {
271+ let best_proposal_block_hash = self . best_header_hash ( ) ;
272+ let best_proposal_block_detail =
273+ self . block_details ( & best_proposal_block_hash) . expect ( "Best proposal block always exists" ) ;
274+
275+ ( new_header. number ( ) , best_proposal_block_detail. view ) > ( best_proposal_block_detail. number , new_header. view ( ) )
276+ }
277+
270278 /// Calculate how best block is changed
271279 fn best_header_changed ( & self , new_header : & HeaderView < ' _ > , engine : & dyn CodeChainEngine ) -> BestHeaderChanged {
272280 let parent_hash_of_new_header = new_header. parent_hash ( ) ;
273281 let parent_details_of_new_header = self . block_details ( & parent_hash_of_new_header) . expect ( "Invalid parent hash" ) ;
274282 let grandparent_hash_of_new_header = parent_details_of_new_header. parent ;
275283 let prev_best_hash = self . best_header_hash ( ) ;
276- let is_new_best = parent_details_of_new_header. total_score + new_header. score ( )
277- > self . best_proposal_header_detail ( ) . total_score
284+ let is_new_best = self . is_new_header_eligible_to_be_best ( new_header)
278285 && engine. can_change_canon_chain ( parent_hash_of_new_header, grandparent_hash_of_new_header, prev_best_hash) ;
279286
280287 if is_new_best {
@@ -364,10 +371,6 @@ impl HeaderChain {
364371 pub fn best_proposal_header ( & self ) -> encoded:: Header {
365372 self . block_header_data ( & self . best_proposal_header_hash ( ) ) . expect ( "Highest header always exists" )
366373 }
367-
368- pub fn best_proposal_header_detail ( & self ) -> BlockDetails {
369- self . block_details ( & self . best_proposal_header_hash ( ) ) . expect ( "Best Proposal header always exists" )
370- }
371374}
372375
373376/// Interface for querying blocks by hash and by number.
0 commit comments