Add asserts to ensure block heights are not too large.

This commit is contained in:
Age Manning 2019-02-20 12:36:54 +11:00
parent d8584cbed2
commit 2fbdc53147
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
2 changed files with 8 additions and 2 deletions

View File

@ -19,7 +19,7 @@ use types::{
//TODO: Pruning - Children //TODO: Pruning - Children
//TODO: Handle Syncing //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 /// 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. /// applicable for block height differences in the range of a u32.
/// This can potentially be parallelized in some parts. /// This can potentially be parallelized in some parts.
@ -30,6 +30,10 @@ fn log2_int(x: u32) -> u32 {
if x == 0 { if x == 0 {
return 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 log2_raw(x as f32) as u32
} }
@ -37,7 +41,7 @@ fn power_of_2_below(x: u32) -> u32 {
2u32.pow(log2_int(x)) 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<T: ClientDB + Sized> { pub struct BitwiseLMDGhost<T: ClientDB + Sized> {
/// A cache of known ancestors at given heights for a specific block. /// A cache of known ancestors at given heights for a specific block.
//TODO: Consider FnvHashMap //TODO: Consider FnvHashMap

View File

@ -25,12 +25,14 @@ macro_rules! impl_into_u32 {
($main: ident) => { ($main: ident) => {
impl Into<u32> for $main { impl Into<u32> for $main {
fn into(self) -> u32 { fn into(self) -> u32 {
assert!(self.0 < u64::from(std::u32::MAX), "Lossy conversion to u32");
self.0 as u32 self.0 as u32
} }
} }
impl $main { impl $main {
pub fn as_u32(&self) -> u32 { pub fn as_u32(&self) -> u32 {
assert!(self.0 < u64::from(std::u32::MAX), "Lossy conversion to u32");
self.0 as u32 self.0 as u32
} }
} }