diff --git a/eth2/fork_choice/src/bitwise_lmd_ghost.rs b/eth2/fork_choice/src/bitwise_lmd_ghost.rs index e057fa1ee..013c901b9 100644 --- a/eth2/fork_choice/src/bitwise_lmd_ghost.rs +++ b/eth2/fork_choice/src/bitwise_lmd_ghost.rs @@ -19,7 +19,7 @@ use types::{ //TODO: Pruning - Children //TODO: Handle Syncing -/// The optimised LMD-GHOST fork choice rule. +/// The optimised bitwise LMD-GHOST fork choice rule. /// NOTE: This uses u32 to represent difference between block heights. Thus this is only /// applicable for block height differences in the range of a u32. /// This can potentially be parallelized in some parts. @@ -30,6 +30,10 @@ fn log2_int(x: u32) -> u32 { if x == 0 { return 0; } + assert!( + x <= std::f32::MAX as u32, + "Height too large for fast log in bitwise fork choice" + ); log2_raw(x as f32) as u32 } @@ -37,7 +41,7 @@ fn power_of_2_below(x: u32) -> u32 { 2u32.pow(log2_int(x)) } -/// Stores the necessary data structures to run the optimised lmd ghost algorithm. +/// Stores the necessary data structures to run the optimised bitwise lmd ghost algorithm. pub struct BitwiseLMDGhost { /// A cache of known ancestors at given heights for a specific block. //TODO: Consider FnvHashMap diff --git a/eth2/types/src/slot_epoch_macros.rs b/eth2/types/src/slot_epoch_macros.rs index 54a8f2ce9..b0550f2f8 100644 --- a/eth2/types/src/slot_epoch_macros.rs +++ b/eth2/types/src/slot_epoch_macros.rs @@ -25,12 +25,14 @@ macro_rules! impl_into_u32 { ($main: ident) => { impl Into for $main { fn into(self) -> u32 { + assert!(self.0 < u64::from(std::u32::MAX), "Lossy conversion to u32"); self.0 as u32 } } impl $main { pub fn as_u32(&self) -> u32 { + assert!(self.0 < u64::from(std::u32::MAX), "Lossy conversion to u32"); self.0 as u32 } }