|
1 | 1 | use crate::per_epoch_processing::Error; |
2 | 2 | use safe_arith::SafeArith; |
| 3 | +use std::ops::Range; |
3 | 4 | use types::{BeaconState, Checkpoint, EthSpec}; |
4 | 5 |
|
5 | 6 | /// Update the justified and finalized checkpoints for matching target attestations. |
@@ -37,29 +38,31 @@ pub fn weigh_justification_and_finalization<T: EthSpec>( |
37 | 38 | } |
38 | 39 |
|
39 | 40 | let bits = state.justification_bits().clone(); |
| 41 | + let all_bits_set = |range: Range<usize>| -> Result<bool, Error> { |
| 42 | + for i in range { |
| 43 | + if !bits.get(i).map_err(Error::InvalidJustificationBit)? { |
| 44 | + return Ok(false); |
| 45 | + } |
| 46 | + } |
| 47 | + Ok(true) |
| 48 | + }; |
40 | 49 |
|
41 | 50 | // The 2nd/3rd/4th most recent epochs are all justified, the 2nd using the 4th as source. |
42 | | - if (1..4).all(|i| bits.get(i).unwrap_or(false)) |
43 | | - && old_previous_justified_checkpoint.epoch.safe_add(3)? == current_epoch |
| 51 | + if all_bits_set(1..4)? && old_previous_justified_checkpoint.epoch.safe_add(3)? == current_epoch |
44 | 52 | { |
45 | 53 | *state.finalized_checkpoint_mut() = old_previous_justified_checkpoint; |
46 | 54 | } |
47 | 55 | // The 2nd/3rd most recent epochs are both justified, the 2nd using the 3rd as source. |
48 | | - else if (1..3).all(|i| bits.get(i).unwrap_or(false)) |
49 | | - && old_previous_justified_checkpoint.epoch.safe_add(2)? == current_epoch |
| 56 | + if all_bits_set(1..3)? && old_previous_justified_checkpoint.epoch.safe_add(2)? == current_epoch |
50 | 57 | { |
51 | 58 | *state.finalized_checkpoint_mut() = old_previous_justified_checkpoint; |
52 | 59 | } |
53 | 60 | // The 1st/2nd/3rd most recent epochs are all justified, the 1st using the 3nd as source. |
54 | | - if (0..3).all(|i| bits.get(i).unwrap_or(false)) |
55 | | - && old_current_justified_checkpoint.epoch.safe_add(2)? == current_epoch |
56 | | - { |
| 61 | + if all_bits_set(0..3)? && old_current_justified_checkpoint.epoch.safe_add(2)? == current_epoch { |
57 | 62 | *state.finalized_checkpoint_mut() = old_current_justified_checkpoint; |
58 | 63 | } |
59 | 64 | // The 1st/2nd most recent epochs are both justified, the 1st using the 2nd as source. |
60 | | - else if (0..2).all(|i| bits.get(i).unwrap_or(false)) |
61 | | - && old_current_justified_checkpoint.epoch.safe_add(1)? == current_epoch |
62 | | - { |
| 65 | + if all_bits_set(0..2)? && old_current_justified_checkpoint.epoch.safe_add(1)? == current_epoch { |
63 | 66 | *state.finalized_checkpoint_mut() = old_current_justified_checkpoint; |
64 | 67 | } |
65 | 68 |
|
|
0 commit comments