@@ -78,21 +78,22 @@ pub async fn find_block_to_request(
7878
7979 // If the consensus threshold is not met with the transition from the start block to the ideal block,
8080 // the first block to match the threshold will be used as the block to request.
81+ let start_block_validators = client. fetch_validators ( start_block + 1 ) . await ?;
82+ let start_validator_set = Set :: new ( start_block_validators, None ) ;
83+
8184 let mut curr_end_block = ideal_block_to_request;
8285 loop {
8386 if curr_end_block - start_block == 1 {
8487 return Ok ( Some ( curr_end_block) ) ;
8588 }
86- let start_block_validators = client. fetch_validators ( start_block) . await ?;
87- let start_validator_set = Set :: new ( start_block_validators, None ) ;
8889 let target_block_validators = client. fetch_validators ( curr_end_block) . await ?;
8990 let target_validator_set = Set :: new ( target_block_validators, None ) ;
9091 let target_block_commit = client. fetch_commit ( curr_end_block) . await ?;
9192
9293 if is_valid_skip (
93- start_validator_set,
94- target_validator_set,
95- target_block_commit. result . signed_header . commit ,
94+ & start_validator_set,
95+ & target_validator_set,
96+ & target_block_commit. result . signed_header . commit ,
9697 ) {
9798 return Ok ( Some ( curr_end_block) ) ;
9899 }
@@ -281,38 +282,39 @@ async fn fetch_light_block(
281282
282283/// Determines if a valid skip is possible between start_block and target_block.
283284pub fn is_valid_skip (
284- start_validator_set : TendermintValidatorSet ,
285- target_validator_set : TendermintValidatorSet ,
286- target_block_commit : Commit ,
285+ start_validator_set : & TendermintValidatorSet ,
286+ target_validator_set : & TendermintValidatorSet ,
287+ target_block_commit : & Commit ,
287288) -> bool {
288289 let threshold = 2_f64 / 3_f64 ;
290+
289291 let mut shared_voting_power = 0_u64 ;
290- let target_block_total_voting_power = target_validator_set. total_voting_power ( ) . value ( ) ;
291- let start_block_validators = start_validator_set. validators ( ) ;
292- let mut start_block_idx = 0 ;
293- let start_block_num_validators = start_block_validators. len ( ) ;
294-
295- // Exit if the threshold is met.
296- while ( target_block_total_voting_power as f64 ) * threshold > shared_voting_power as f64
297- && start_block_idx < start_block_num_validators
298- {
299- if let Some ( target_block_validator) =
300- target_validator_set. validator ( start_block_validators[ start_block_idx] . address )
301- {
302- // Confirm that the validator has signed on target_block.
303- for sig in target_block_commit. signatures . iter ( ) {
304- if let Some ( validator_address) = sig. validator_address ( ) {
305- if validator_address == target_block_validator. address {
306- // Add the shared voting power to the validator
307- shared_voting_power += target_block_validator. power . value ( ) ;
308- }
309- }
292+ let mut target_shared_voting_power = 0_u64 ;
293+
294+ for sig in target_block_commit. signatures . iter ( ) {
295+ if !sig. is_commit ( ) {
296+ continue ;
297+ }
298+
299+ if let Some ( validator_address) = sig. validator_address ( ) {
300+ if let Some ( trusted_validator) = start_validator_set. validator ( validator_address) {
301+ shared_voting_power += trusted_validator. power ( ) ;
302+ }
303+
304+ if let Some ( target_validator) = target_validator_set. validator ( validator_address) {
305+ target_shared_voting_power += target_validator. power ( ) ;
310306 }
311307 }
312- start_block_idx += 1 ;
313308 }
314309
315- ( target_block_total_voting_power as f64 ) * threshold <= shared_voting_power as f64
310+ let start_meets_threshold = ( start_validator_set. total_voting_power ( ) . value ( ) as f64 )
311+ * threshold
312+ <= shared_voting_power as f64 ;
313+ let target_meets_threshold = ( target_validator_set. total_voting_power ( ) . value ( ) as f64 )
314+ * threshold
315+ <= target_shared_voting_power as f64 ;
316+
317+ start_meets_threshold && target_meets_threshold
316318}
317319
318320/// Fetches a header hash for a specific block height.
0 commit comments