Skip to content

Commit dc2227a

Browse files
authored
Minor changes to justification code (#2367)
1 parent 338a2d1 commit dc2227a

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

consensus/state_processing/src/per_epoch_processing/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub enum EpochProcessingError {
2121
SszTypesError(ssz_types::Error),
2222
ArithError(safe_arith::ArithError),
2323
InconsistentStateFork(InconsistentFork),
24+
InvalidJustificationBit(ssz_types::Error),
2425
}
2526

2627
impl From<InclusionError> for EpochProcessingError {

consensus/state_processing/src/per_epoch_processing/weigh_justification_and_finalization.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::per_epoch_processing::Error;
22
use safe_arith::SafeArith;
3+
use std::ops::Range;
34
use types::{BeaconState, Checkpoint, EthSpec};
45

56
/// Update the justified and finalized checkpoints for matching target attestations.
@@ -37,29 +38,31 @@ pub fn weigh_justification_and_finalization<T: EthSpec>(
3738
}
3839

3940
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+
};
4049

4150
// 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
4452
{
4553
*state.finalized_checkpoint_mut() = old_previous_justified_checkpoint;
4654
}
4755
// 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
5057
{
5158
*state.finalized_checkpoint_mut() = old_previous_justified_checkpoint;
5259
}
5360
// 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 {
5762
*state.finalized_checkpoint_mut() = old_current_justified_checkpoint;
5863
}
5964
// 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 {
6366
*state.finalized_checkpoint_mut() = old_current_justified_checkpoint;
6467
}
6568

0 commit comments

Comments
 (0)