@@ -145,9 +145,10 @@ impl BlockProducerEnabled {
145145 if let Some ( won_slot) = state. current . won_slot ( ) {
146146 if let Some ( chain) = best_chain. last ( ) . map ( |best_tip| {
147147 if best_tip. global_slot ( ) == won_slot. global_slot ( ) {
148- // We are producing block which replaces current best tip
149- // instead of extending it.
150- best_chain[ ..( best_chain. len ( ) - 1 ) ] . to_vec ( )
148+ best_chain
149+ . get ( ..best_chain. len ( ) . saturating_sub ( 1 ) )
150+ . unwrap_or ( & [ ] )
151+ . to_vec ( )
151152 } else {
152153 best_chain. to_vec ( )
153154 }
@@ -430,14 +431,24 @@ impl BlockProducerEnabled {
430431 epoch_length : v2:: UnsignedExtendedUInt32StableV1 ( 1 . into ( ) ) ,
431432 } ;
432433 let epoch_count = v2:: UnsignedExtendedUInt32StableV1 (
433- ( pred_consensus_state. epoch_count . as_u32 ( ) + 1 ) . into ( ) ,
434+ ( pred_consensus_state
435+ . epoch_count
436+ . as_u32 ( )
437+ . checked_add ( 1 )
438+ . expect ( "overflow" ) )
439+ . into ( ) ,
434440 ) ;
435441 ( staking_data, next_data, epoch_count)
436442 } else {
437443 assert_eq ! ( pred_epoch, next_epoch) ;
438444 let mut next_data = pred_consensus_state. next_epoch_data . clone ( ) ;
439445 next_data. epoch_length = v2:: UnsignedExtendedUInt32StableV1 (
440- ( next_data. epoch_length . as_u32 ( ) + 1 ) . into ( ) ,
446+ ( next_data
447+ . epoch_length
448+ . as_u32 ( )
449+ . checked_add ( 1 )
450+ . expect ( "overflow" ) )
451+ . into ( ) ,
441452 ) ;
442453 (
443454 pred_consensus_state. staking_epoch_data . clone ( ) ,
@@ -475,7 +486,8 @@ impl BlockProducerEnabled {
475486
476487 let is_same_global_sub_window = pred_global_sub_window == next_global_sub_window;
477488 let are_windows_overlapping = pred_global_sub_window
478- + constraint_constants ( ) . sub_windows_per_window as u32
489+ . checked_add ( constraint_constants ( ) . sub_windows_per_window as u32 )
490+ . expect ( "overflow" )
479491 >= next_global_sub_window;
480492
481493 let current_sub_window_densities = pred_sub_window_densities
@@ -525,7 +537,7 @@ impl BlockProducerEnabled {
525537 0
526538 } ;
527539 if incr_window {
528- density + 1
540+ density. saturating_add ( 1 )
529541 } else {
530542 density
531543 }
@@ -545,7 +557,9 @@ impl BlockProducerEnabled {
545557 & global_slot_since_genesis,
546558 ) ;
547559 let consensus_state = v2:: ConsensusProofOfStakeDataConsensusStateValueStableV2 {
548- blockchain_length : v2:: UnsignedExtendedUInt32StableV1 ( ( pred_block. height ( ) + 1 ) . into ( ) ) ,
560+ blockchain_length : v2:: UnsignedExtendedUInt32StableV1 (
561+ ( pred_block. height ( ) . checked_add ( 1 ) . expect ( "overflow" ) ) . into ( ) ,
562+ ) ,
549563 epoch_count,
550564 min_window_density,
551565 sub_window_densities,
@@ -592,7 +606,11 @@ impl BlockProducerEnabled {
592606 0 => ( pred_block. hash ( ) . clone ( ) , List :: new ( ) ) ,
593607 chain_proof_len => {
594608 // TODO(binier): test
595- let mut iter = chain. iter ( ) . rev ( ) . take ( chain_proof_len + 1 ) . rev ( ) ;
609+ let mut iter = chain
610+ . iter ( )
611+ . rev ( )
612+ . take ( chain_proof_len. saturating_add ( 1 ) )
613+ . rev ( ) ;
596614 if let Some ( first_block) = iter. next ( ) {
597615 let first_hash = first_block. hash ( ) . clone ( ) ;
598616 let body_hashes = iter
0 commit comments