11pub mod block;
2+ pub mod checkpoint;
23pub mod config;
34pub mod state;
45pub mod vote;
@@ -75,37 +76,37 @@ pub fn process_block(pre_state: &LeanState, block: &Block) -> anyhow::Result<Lea
7576 // Ignore votes whose source is not already justified,
7677 // or whose target is not in the history, or whose target is not a
7778 // valid justifiable slot
78- if !state. justified_slots [ vote. source_slot as usize ]
79- || vote. source != state. historical_block_hashes [ vote. source_slot as usize ]
80- || vote. target != state. historical_block_hashes [ vote. target_slot as usize ]
81- || vote. target_slot <= vote. source_slot
82- || !is_justifiable_slot ( & state. latest_finalized_slot , & vote. target_slot )
79+ if !state. justified_slots [ vote. source . slot as usize ]
80+ || vote. source . root != state. historical_block_hashes [ vote. source . slot as usize ]
81+ || vote. target . root != state. historical_block_hashes [ vote. target . slot as usize ]
82+ || vote. target . slot <= vote. source . slot
83+ || !is_justifiable_slot ( & state. latest_finalized . slot , & vote. target . slot )
8384 {
8485 continue ;
8586 }
8687
8788 // Track attempts to justify new hashes
88- state. initialize_justifications_for_root ( & vote. target ) ?;
89- state. set_justification ( & vote. target , & vote. validator_id , true ) ?;
89+ state. initialize_justifications_for_root ( & vote. target . root ) ?;
90+ state. set_justification ( & vote. target . root , & vote. validator_id , true ) ?;
9091
91- let count = state. count_justifications ( & vote. target ) ?;
92+ let count = state. count_justifications ( & vote. target . root ) ?;
9293
9394 // If 2/3 voted for the same new valid hash to justify
9495 if count == ( 2 * state. config . num_validators ) / 3 {
95- state. latest_justified_hash = vote. target ;
96- state. latest_justified_slot = vote. target_slot ;
97- state. justified_slots [ vote. target_slot as usize ] = true ;
96+ state. latest_justified . root = vote. target . root ;
97+ state. latest_justified . slot = vote. target . slot ;
98+ state. justified_slots [ vote. target . slot as usize ] = true ;
9899
99- state. remove_justifications ( & vote. target ) ?;
100+ state. remove_justifications ( & vote. target . root ) ?;
100101
101102 // Finalization: if the target is the next valid justifiable
102103 // hash after the source
103- let is_target_next_valid_justifiable_slot = !( ( vote. source_slot + 1 ) ..vote. target_slot )
104- . any ( |slot| is_justifiable_slot ( & state. latest_finalized_slot , & slot) ) ;
104+ let is_target_next_valid_justifiable_slot = !( ( vote. source . slot + 1 ) ..vote. target . slot )
105+ . any ( |slot| is_justifiable_slot ( & state. latest_finalized . slot , & slot) ) ;
105106
106107 if is_target_next_valid_justifiable_slot {
107- state. latest_finalized_hash = vote. source ;
108- state. latest_finalized_slot = vote. source_slot ;
108+ state. latest_finalized . root = vote. source . root ;
109+ state. latest_finalized . slot = vote. source . slot ;
109110 }
110111 }
111112 }
@@ -117,8 +118,8 @@ pub fn process_block(pre_state: &LeanState, block: &Block) -> anyhow::Result<Lea
117118pub fn get_latest_justified_hash ( post_states : & HashMap < B256 , LeanState > ) -> Option < B256 > {
118119 post_states
119120 . values ( )
120- . max_by_key ( |state| state. latest_justified_slot )
121- . map ( |state| state. latest_justified_hash )
121+ . max_by_key ( |state| state. latest_justified . slot )
122+ . map ( |state| state. latest_justified . root )
122123}
123124
124125/// Use LMD GHOST to get the head, given a particular root (usually the
@@ -159,8 +160,8 @@ pub fn get_fork_choice_head(
159160 let mut vote_weights = HashMap :: < B256 , u64 > :: new ( ) ;
160161
161162 for vote in latest_votes. values ( ) {
162- if blocks. contains_key ( & vote. head ) {
163- let mut block_hash = vote. head ;
163+ if blocks. contains_key ( & vote. head . root ) {
164+ let mut block_hash = vote. head . root ;
164165 while {
165166 let current_block = blocks
166167 . get ( & block_hash)
@@ -195,21 +196,16 @@ pub fn get_fork_choice_head(
195196 // choose the child with the most latest votes, tiebreaking by slot then hash
196197 let mut current_root = root;
197198
198- loop {
199- match children_map. get ( & current_root) {
200- None => {
201- break Ok ( current_root) ;
202- }
203- Some ( children) => {
204- current_root = * children
205- . iter ( )
206- . max_by_key ( |child_hash| {
207- let vote_weight = vote_weights. get ( * child_hash) . unwrap_or ( & 0 ) ;
208- let slot = blocks. get ( * child_hash) . map ( |block| block. slot ) . unwrap_or ( 0 ) ;
209- ( * vote_weight, slot, * ( * child_hash) )
210- } )
211- . ok_or_else ( || anyhow ! ( "No children found for current root: {current_root}" ) ) ?;
212- }
213- }
199+ while let Some ( children) = children_map. get ( & current_root) {
200+ current_root = * children
201+ . iter ( )
202+ . max_by_key ( |child_hash| {
203+ let vote_weight = vote_weights. get ( * child_hash) . unwrap_or ( & 0 ) ;
204+ let slot = blocks. get ( * child_hash) . map ( |block| block. slot ) . unwrap_or ( 0 ) ;
205+ ( * vote_weight, slot, * ( * child_hash) )
206+ } )
207+ . ok_or_else ( || anyhow ! ( "No children found for current root: {current_root}" ) ) ?;
214208 }
209+
210+ Ok ( current_root)
215211}
0 commit comments