Skip to content

Commit 72935e1

Browse files
committed
Falls back to the non-optimized unrealized FFG processing method if progressive_balances_cache isn't initialized.
1 parent 964f0bd commit 72935e1

File tree

1 file changed

+79
-72
lines changed

1 file changed

+79
-72
lines changed

consensus/fork_choice/src/fork_choice.rs

Lines changed: 79 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub enum Error<T> {
7979
UnrealizedVoteProcessing(state_processing::EpochProcessingError),
8080
ParticipationCacheBuild(BeaconStateError),
8181
ParticipationCacheError(ParticipationCacheError),
82+
ParticipationCacheMissing,
8283
ValidatorStatuses(BeaconStateError),
8384
ProgressiveBalancesCacheCheckFailed(String),
8485
}
@@ -759,54 +760,80 @@ where
759760
parent_justified.epoch == block_epoch && parent_finalized.epoch + 1 >= block_epoch
760761
});
761762

762-
let (unrealized_justified_checkpoint, unrealized_finalized_checkpoint) =
763-
if let Some((parent_justified, parent_finalized)) = parent_checkpoints {
764-
(parent_justified, parent_finalized)
765-
} else {
766-
let justification_and_finalization_state = match block {
767-
BeaconBlockRef::Capella(_)
768-
| BeaconBlockRef::Merge(_)
769-
| BeaconBlockRef::Altair(_) => match progressive_balances_mode {
770-
ProgressiveBalancesMode::Disabled => {
771-
let participation_cache = ParticipationCache::new(state, spec)
772-
.map_err(Error::ParticipationCacheBuild)?;
773-
per_epoch_processing::altair::process_justification_and_finalization(
774-
state,
775-
&participation_cache,
776-
)?
777-
}
778-
ProgressiveBalancesMode::Fast
779-
| ProgressiveBalancesMode::Checked
780-
| ProgressiveBalancesMode::Strict => {
781-
process_justification_and_finalization_from_progressive_cache::<E, T>(
782-
state,
783-
spec,
784-
progressive_balances_mode,
785-
log,
786-
)?
787-
}
788-
},
789-
BeaconBlockRef::Base(_) => {
790-
let mut validator_statuses =
791-
per_epoch_processing::base::ValidatorStatuses::new(state, spec)
792-
.map_err(Error::ValidatorStatuses)?;
793-
validator_statuses
794-
.process_attestations(state)
795-
.map_err(Error::ValidatorStatuses)?;
796-
per_epoch_processing::base::process_justification_and_finalization(
763+
let (unrealized_justified_checkpoint, unrealized_finalized_checkpoint) = if let Some((
764+
parent_justified,
765+
parent_finalized,
766+
)) =
767+
parent_checkpoints
768+
{
769+
(parent_justified, parent_finalized)
770+
} else {
771+
let justification_and_finalization_state = match block {
772+
BeaconBlockRef::Capella(_)
773+
| BeaconBlockRef::Merge(_)
774+
| BeaconBlockRef::Altair(_) => match progressive_balances_mode {
775+
ProgressiveBalancesMode::Disabled => {
776+
let participation_cache = ParticipationCache::new(state, spec)
777+
.map_err(Error::ParticipationCacheBuild)?;
778+
per_epoch_processing::altair::process_justification_and_finalization(
797779
state,
798-
&validator_statuses.total_balances,
799-
spec,
780+
&participation_cache,
800781
)?
801782
}
802-
};
803-
804-
(
805-
justification_and_finalization_state.current_justified_checkpoint(),
806-
justification_and_finalization_state.finalized_checkpoint(),
807-
)
783+
ProgressiveBalancesMode::Fast
784+
| ProgressiveBalancesMode::Checked
785+
| ProgressiveBalancesMode::Strict => {
786+
let maybe_participation_cache = progressive_balances_mode
787+
.perform_comparative_checks()
788+
.then(|| {
789+
ParticipationCache::new(state, spec)
790+
.map_err(Error::ParticipationCacheBuild)
791+
})
792+
.transpose()?;
793+
794+
process_justification_and_finalization_from_progressive_cache::<E, T>(
795+
state,
796+
maybe_participation_cache.as_ref(),
797+
)
798+
.or_else(|e| {
799+
if progressive_balances_mode == ProgressiveBalancesMode::Checked {
800+
error!(
801+
log,
802+
"Processing with progressive balances cache failed in checked mode";
803+
"info" => "falling back to the non-optimized processing method",
804+
"error" => ?e,
805+
);
806+
per_epoch_processing::altair::process_justification_and_finalization(
807+
state,
808+
maybe_participation_cache.as_ref().ok_or(Error::ParticipationCacheMissing)?,
809+
).map_err(Error::from)
810+
} else {
811+
Err(e)
812+
}
813+
})?
814+
}
815+
},
816+
BeaconBlockRef::Base(_) => {
817+
let mut validator_statuses =
818+
per_epoch_processing::base::ValidatorStatuses::new(state, spec)
819+
.map_err(Error::ValidatorStatuses)?;
820+
validator_statuses
821+
.process_attestations(state)
822+
.map_err(Error::ValidatorStatuses)?;
823+
per_epoch_processing::base::process_justification_and_finalization(
824+
state,
825+
&validator_statuses.total_balances,
826+
spec,
827+
)?
828+
}
808829
};
809830

831+
(
832+
justification_and_finalization_state.current_justified_checkpoint(),
833+
justification_and_finalization_state.finalized_checkpoint(),
834+
)
835+
};
836+
810837
// Update best known unrealized justified & finalized checkpoints
811838
if unrealized_justified_checkpoint.epoch
812839
> self.fc_store.unrealized_justified_checkpoint().epoch
@@ -1532,11 +1559,13 @@ where
15321559
}
15331560
}
15341561

1562+
/// Process justification and finalization using progressive cache. Also performs a comparative
1563+
/// check against the `ParticipationCache` if it is supplied.
1564+
///
1565+
/// Returns an error if the cache is not initialized or if there is a mismatch on the comparative check.
15351566
fn process_justification_and_finalization_from_progressive_cache<E, T>(
15361567
state: &BeaconState<E>,
1537-
spec: &ChainSpec,
1538-
progressive_balances_mode: ProgressiveBalancesMode,
1539-
log: &Logger,
1568+
maybe_participation_cache: Option<&ParticipationCache>,
15401569
) -> Result<JustificationAndFinalizationState<E>, Error<T::Error>>
15411570
where
15421571
E: EthSpec,
@@ -1555,36 +1584,14 @@ where
15551584
progressive_balances_cache.current_epoch_target_attesting_balance()?;
15561585
let total_active_balance = state.get_total_active_balance()?;
15571586

1558-
if progressive_balances_mode.perform_comparative_checks() {
1559-
let participation_cache =
1560-
ParticipationCache::new(state, spec).map_err(Error::ParticipationCacheBuild)?;
1561-
1562-
if let Err(e) = check_progressive_balances::<E, T>(
1587+
if let Some(participation_cache) = maybe_participation_cache {
1588+
check_progressive_balances::<E, T>(
15631589
state,
1564-
&participation_cache,
1590+
participation_cache,
15651591
previous_target_balance,
15661592
current_target_balance,
15671593
total_active_balance,
1568-
) {
1569-
return if progressive_balances_mode == ProgressiveBalancesMode::Strict {
1570-
// if comparative check fails in `Strict` mode, return error
1571-
Err(e)
1572-
} else {
1573-
error!(
1574-
log,
1575-
"Progressive balances mismatch";
1576-
"info" => "falling back to epoch processing calculation",
1577-
"error" => ?e,
1578-
);
1579-
// if comparative check fails in `Checked` mode, fall back to the epoch processing
1580-
// method.
1581-
per_epoch_processing::altair::process_justification_and_finalization(
1582-
state,
1583-
&participation_cache,
1584-
)
1585-
.map_err(Error::from)
1586-
};
1587-
}
1594+
)?;
15881595
}
15891596

15901597
weigh_justification_and_finalization(

0 commit comments

Comments
 (0)