From 8ba7601ab3d4cf480ac9535e1402feaaa65f5f4c Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 23 Aug 2018 18:32:18 +1000 Subject: [PATCH] Add get_signed_parent_hashes (untested) --- lighthouse/state/mod.rs | 1 + lighthouse/state/transition/helpers.rs | 68 ++++++++++++++++++++++++++ lighthouse/state/transition/mod.rs | 11 +++++ 3 files changed, 80 insertions(+) create mode 100644 lighthouse/state/transition/helpers.rs create mode 100644 lighthouse/state/transition/mod.rs diff --git a/lighthouse/state/mod.rs b/lighthouse/state/mod.rs index 5d5c2d7da..d10acf8fd 100644 --- a/lighthouse/state/mod.rs +++ b/lighthouse/state/mod.rs @@ -13,4 +13,5 @@ pub mod chain_config; pub mod block; pub mod crosslink_record; pub mod shard_and_committee; +pub mod transition; pub mod validator_record; diff --git a/lighthouse/state/transition/helpers.rs b/lighthouse/state/transition/helpers.rs new file mode 100644 index 000000000..c8e98316e --- /dev/null +++ b/lighthouse/state/transition/helpers.rs @@ -0,0 +1,68 @@ +use super::Hash256; +use super::TransitionError; + +pub fn get_signed_parent_hashes( + cycle_length: u64, + block_slot: u64, + attestation_slot: u64, + current_hashes: Vec, + oblique_hashes: Vec) + -> Result, TransitionError> +{ + let start = cycle_length.checked_add(attestation_slot) + .and_then(|x| x.checked_sub(block_slot)) + .ok_or(TransitionError::IntWrapping)?; + let start = start as usize; + + let end = cycle_length.checked_mul(2) + .and_then(|x| x.checked_add(attestation_slot)) + .and_then(|x| x.checked_sub(block_slot)) + .and_then(|x| x.checked_sub(oblique_hashes.len() as u64)) + .ok_or(TransitionError::IntWrapping)?; + let end = end as usize; + + println!("start: {}, end: {}", start, end); + + if end >= current_hashes.len() { + return Err(TransitionError::OutOfBounds); + } + if start > end { + return Err(TransitionError::InvalidInput("cats")); + } + + let mut hashes = Vec::new(); + + hashes.extend_from_slice( + ¤t_hashes[start..end]); + hashes.append(&mut oblique_hashes.clone()); + + Ok(hashes) +} + + +#[cfg(test)] +mod tests { + use super::*; + + fn get_n_hashes(value: &[u8], n: usize) -> Vec { + (0..n).map(|_| Hash256::from_slice(value)).collect() + } + + #[test] + fn test_get_signed_hashes() { + let cycle_length: u64 = 8; + let block_slot: u64 = 500; + let attestation_slot: u64 = 498; + let current_hashes = + get_n_hashes(b"0", 100); + let oblique_hashes = get_n_hashes(b"1", 2); + let result = get_signed_parent_hashes( + cycle_length, + block_slot, + attestation_slot, + current_hashes, + oblique_hashes); + // TODO: complete testing + assert!(result.is_ok()); + } +} diff --git a/lighthouse/state/transition/mod.rs b/lighthouse/state/transition/mod.rs new file mode 100644 index 000000000..676266d95 --- /dev/null +++ b/lighthouse/state/transition/mod.rs @@ -0,0 +1,11 @@ +use super::super::utils::types::Hash256; + +pub mod helpers; + +#[derive(Debug)] +pub enum TransitionError { + IntWrapping, + OutOfBounds, + InvalidInput(&str), +} +