From 8b320ab24f5ffff5eae887c0222cf2463bd92978 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 17 Jul 2018 17:54:14 +1000 Subject: [PATCH] Implement process_recent_proposers() --- src/state/transition/mod.rs | 1 + src/state/transition/proposers.rs | 54 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/state/transition/proposers.rs diff --git a/src/state/transition/mod.rs b/src/state/transition/mod.rs index 84ae6682b..52a171204 100644 --- a/src/state/transition/mod.rs +++ b/src/state/transition/mod.rs @@ -12,6 +12,7 @@ use super::validator_record; pub mod crosslinks; pub mod deposits; pub mod ffg; +pub mod proposers; pub mod shuffling; pub mod validators; pub mod attestors; diff --git a/src/state/transition/proposers.rs b/src/state/transition/proposers.rs new file mode 100644 index 000000000..8ff89c84f --- /dev/null +++ b/src/state/transition/proposers.rs @@ -0,0 +1,54 @@ +use super::crystallized_state::CrystallizedState; +use super::recent_proposer_record::RecentPropserRecord; + + +pub fn process_recent_proposers( + cry_state: &CrystallizedState, + recent_proposers: &Vec) + -> Vec +{ + let mut deltas: Vec = vec![0; cry_state.num_active_validators()]; + for p in recent_proposers { + deltas[p.index] += p.balance_delta; + } + deltas +} + +#[cfg(test)] +mod tests { + use super::*; + use super::super::utils::types::Sha256Digest; + use super::super::validator_record::ValidatorRecord; + + #[test] + fn test_process_recent_proposers() { + let mut cry_state = CrystallizedState::zero(); + let validator_count = 20; + + let mut recent_proposers: Vec = vec![]; + + for i in 0..validator_count { + cry_state.active_validators + .push(ValidatorRecord::zero_with_thread_rand_pub_key()); + if i % 2 == 0 { + recent_proposers.push(RecentPropserRecord { + index: i, + randao_commitment: Sha256Digest::zero(), + balance_delta: 10 + }); + } + } + + let d = process_recent_proposers( + &cry_state, + &recent_proposers); + + for i in 0..validator_count { + if i % 2 == 0 { + assert_eq!(d[i], 10); + } else { + assert_eq!(d[i], 0); + } + } + } +}