From 03a5a892d02a10501bf6384b24369e99bdcf9520 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 13 Feb 2019 10:34:56 +1100 Subject: [PATCH] Ensure latest attestations are considered only. --- eth2/fork_choice/src/optimised_lmd_ghost.rs | 31 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/eth2/fork_choice/src/optimised_lmd_ghost.rs b/eth2/fork_choice/src/optimised_lmd_ghost.rs index 9d6c03673..8c53c305a 100644 --- a/eth2/fork_choice/src/optimised_lmd_ghost.rs +++ b/eth2/fork_choice/src/optimised_lmd_ghost.rs @@ -234,9 +234,34 @@ impl ForkChoice for OptimisedLMDGhost { validator_index: u64, target_block_root: &Hash256, ) -> Result<(), ForkChoiceError> { - // simply add the attestation to the latest_attestation_target - self.latest_attestation_targets - .insert(validator_index, target_block_root.clone()); + // simply add the attestation to the latest_attestation_target if the block_height is + // larger + let attestation_target = self + .latest_attestation_targets + .entry(validator_index) + .or_insert_with(|| *target_block_root); + // if we already have a value + if attestation_target != target_block_root { + // get the height of the target block + let block_height = self + .block_store + .get_reader(&target_block_root)? + .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*target_block_root))? + .slot() + - GENESIS_SLOT; + + // get the height of the past target block + let past_block_height = self + .block_store + .get_reader(&attestation_target)? + .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*attestation_target))? + .slot() + - GENESIS_SLOT; + // update the attestation only if the new target is higher + if past_block_height < block_height { + *attestation_target = *target_block_root; + } + } Ok(()) }