From d3681e876a2bcf9a1c6829362868c74f6a151060 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Tue, 11 Dec 2018 15:16:25 -0800 Subject: [PATCH 1/7] add method to determine validator status --- beacon_chain/types/src/validator_record.rs | 4 ++++ beacon_chain/utils/active-validators/src/lib.rs | 13 +++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 3a15baeec..257df3ac1 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -42,6 +42,10 @@ impl ValidatorRecord { }; (s, keypair) } + + pub fn status_is(&self, status: ValidatorStatus) -> bool { + self.status == status as u8 + } } #[cfg(test)] diff --git a/beacon_chain/utils/active-validators/src/lib.rs b/beacon_chain/utils/active-validators/src/lib.rs index 75ad2daac..e96b230a3 100644 --- a/beacon_chain/utils/active-validators/src/lib.rs +++ b/beacon_chain/utils/active-validators/src/lib.rs @@ -2,28 +2,29 @@ extern crate types; use types::{ValidatorRecord, ValidatorStatus}; -pub fn validator_is_active(v: &ValidatorRecord) -> bool { - v.status == ValidatorStatus::Active as u8 -} - /// Returns the indicies of each active validator in a given vec of validators. pub fn active_validator_indices(validators: &[ValidatorRecord]) -> Vec { validators .iter() .enumerate() .filter_map(|(i, validator)| { - if validator_is_active(&validator) { + if validator.status_is(ValidatorStatus::Active) { Some(i) } else { None } - }).collect() + }) + .collect() } #[cfg(test)] mod tests { use super::*; + pub fn validator_is_active(v: &ValidatorRecord) -> bool { + v.status_is(ValidatorStatus::Active) + } + #[test] fn test_active_validator() { let mut validators = vec![]; From fa3d9bdb074bd093d3b7d0a6e508bf7e8a8ec0d7 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Tue, 11 Dec 2018 15:17:55 -0800 Subject: [PATCH 2/7] begin the transition to using ValidatorStatus as a distinct type --- beacon_chain/types/src/validator_record.rs | 21 ++++++++++++++++--- .../utils/active-validators/src/lib.rs | 12 +++++------ beacon_chain/validator_change/src/lib.rs | 8 +++---- .../validator_induction/src/inductor.rs | 16 +++++++------- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 257df3ac1..d08f77c53 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -1,5 +1,6 @@ use super::bls::{Keypair, PublicKey}; use super::{Address, Hash256}; +use std::convert; #[derive(Debug, PartialEq, Clone, Copy)] pub enum ValidatorStatus { @@ -11,6 +12,20 @@ pub enum ValidatorStatus { Penalized = 127, } +impl convert::From for ValidatorStatus { + fn from(status: u8) -> Self { + match status { + 0 => ValidatorStatus::PendingActivation, + 1 => ValidatorStatus::Active, + 2 => ValidatorStatus::PendingExit, + 3 => ValidatorStatus::PendingWithdraw, + 5 => ValidatorStatus::Withdrawn, + 127 => ValidatorStatus::Penalized, + _ => unreachable!(), + } + } +} + #[derive(Debug, Clone, PartialEq)] pub struct ValidatorRecord { pub pubkey: PublicKey, @@ -19,7 +34,7 @@ pub struct ValidatorRecord { pub randao_commitment: Hash256, pub randao_last_change: u64, pub balance: u64, - pub status: u8, + pub status: ValidatorStatus, pub exit_slot: u64, } @@ -37,7 +52,7 @@ impl ValidatorRecord { randao_commitment: Hash256::zero(), randao_last_change: 0, balance: 0, - status: 0, + status: From::from(0), exit_slot: 0, }; (s, keypair) @@ -60,7 +75,7 @@ mod tests { assert!(v.randao_commitment.is_zero()); assert_eq!(v.randao_last_change, 0); assert_eq!(v.balance, 0); - assert_eq!(v.status, 0); + assert_eq!(v.status, From::from(0)); assert_eq!(v.exit_slot, 0); } } diff --git a/beacon_chain/utils/active-validators/src/lib.rs b/beacon_chain/utils/active-validators/src/lib.rs index e96b230a3..495d1f85a 100644 --- a/beacon_chain/utils/active-validators/src/lib.rs +++ b/beacon_chain/utils/active-validators/src/lib.rs @@ -30,32 +30,32 @@ mod tests { let mut validators = vec![]; let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Active as u8; + v.status = ValidatorStatus::Active; assert!(validator_is_active(&v)); validators.push(v); let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::PendingActivation as u8; + v.status = ValidatorStatus::PendingActivation; assert!(!validator_is_active(&v)); validators.push(v); let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::PendingExit as u8; + v.status = ValidatorStatus::PendingExit; assert!(!validator_is_active(&v)); validators.push(v); let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::PendingWithdraw as u8; + v.status = ValidatorStatus::PendingWithdraw; assert!(!validator_is_active(&v)); validators.push(v); let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Withdrawn as u8; + v.status = ValidatorStatus::Withdrawn; assert!(!validator_is_active(&v)); validators.push(v); let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Penalized as u8; + v.status = ValidatorStatus::Penalized; assert!(!validator_is_active(&v)); validators.push(v); diff --git a/beacon_chain/validator_change/src/lib.rs b/beacon_chain/validator_change/src/lib.rs index 99226debb..a2299b930 100644 --- a/beacon_chain/validator_change/src/lib.rs +++ b/beacon_chain/validator_change/src/lib.rs @@ -62,7 +62,7 @@ pub fn update_validator_set( /* * Validator is pending activiation. */ - x if x == ValidatorStatus::PendingActivation as u8 => { + x if x == ValidatorStatus::PendingActivation => { let new_total_changed = total_changed .checked_add(deposit_size_gwei) .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?; @@ -71,7 +71,7 @@ pub fn update_validator_set( * activate the validator. */ if new_total_changed <= max_allowable_change { - v.status = ValidatorStatus::Active as u8; + v.status = ValidatorStatus::Active; hasher.extend(i, &v.pubkey.as_bytes(), VALIDATOR_FLAG_ENTRY); total_changed = new_total_changed; } else { @@ -82,7 +82,7 @@ pub fn update_validator_set( /* * Validator is pending exit. */ - x if x == ValidatorStatus::PendingExit as u8 => { + x if x == ValidatorStatus::PendingExit => { let new_total_changed = total_changed .checked_add(v.balance) .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?; @@ -91,7 +91,7 @@ pub fn update_validator_set( * exit the validator */ if new_total_changed <= max_allowable_change { - v.status = ValidatorStatus::PendingWithdraw as u8; + v.status = ValidatorStatus::PendingWithdraw; v.exit_slot = present_slot; hasher.extend(i, &v.pubkey.as_bytes(), VALIDATOR_FLAG_EXIT); total_changed = new_total_changed; diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index c86fce9e6..816d8e356 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -68,7 +68,7 @@ impl ValidatorInductor { randao_commitment: r.randao_commitment, randao_last_change: self.current_slot, balance: DEPOSIT_GWEI, - status: status as u8, + status: status, exit_slot: 0, }) } @@ -77,7 +77,7 @@ impl ValidatorInductor { /// `validator.status == Withdrawn`. If no such record exists, `None` is returned. fn first_withdrawn_validator(&mut self) -> Option { for i in self.empty_validator_start..self.validators.len() { - if self.validators[i].status == ValidatorStatus::Withdrawn as u8 { + if self.validators[i].status == ValidatorStatus::Withdrawn { self.empty_validator_start = i + 1; return Some(i); } @@ -166,8 +166,8 @@ mod tests { let _ = inductor.induct(&r, ValidatorStatus::Active); let validators = inductor.to_vec(); - assert!(validators[0].status == ValidatorStatus::PendingActivation as u8); - assert!(validators[1].status == ValidatorStatus::Active as u8); + assert!(validators[0].status == ValidatorStatus::PendingActivation); + assert!(validators[1].status == ValidatorStatus::Active); assert_eq!(validators.len(), 2); } @@ -176,7 +176,7 @@ mod tests { let mut validators = vec![]; for _ in 0..5 { let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Active as u8; + v.status = ValidatorStatus::Active; validators.push(v); } @@ -195,11 +195,11 @@ mod tests { fn test_validator_inductor_valid_all_second_validator_withdrawn() { let mut validators = vec![]; let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Active as u8; + v.status = ValidatorStatus::Active; validators.push(v); for _ in 0..4 { let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Withdrawn as u8; + v.status = ValidatorStatus::Withdrawn; validators.push(v); } @@ -219,7 +219,7 @@ mod tests { let mut validators = vec![]; for _ in 0..5 { let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Withdrawn as u8; + v.status = ValidatorStatus::Withdrawn; validators.push(v); } From 1ff47720f1d5b1f62bca144da15f4c4a556cbc2c Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 12 Dec 2018 19:05:32 -0800 Subject: [PATCH 3/7] Clean up representation of ValidatorStatus --- beacon_chain/types/src/validator_record.rs | 16 ++++++++-------- beacon_chain/validator_change/src/lib.rs | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index d08f77c53..537c91723 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -2,14 +2,14 @@ use super::bls::{Keypair, PublicKey}; use super::{Address, Hash256}; use std::convert; -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Clone)] pub enum ValidatorStatus { - PendingActivation = 0, - Active = 1, - PendingExit = 2, - PendingWithdraw = 3, - Withdrawn = 5, - Penalized = 127, + PendingActivation, + Active, + PendingExit, + PendingWithdraw, + Withdrawn, + Penalized, } impl convert::From for ValidatorStatus { @@ -59,7 +59,7 @@ impl ValidatorRecord { } pub fn status_is(&self, status: ValidatorStatus) -> bool { - self.status == status as u8 + self.status == status } } diff --git a/beacon_chain/validator_change/src/lib.rs b/beacon_chain/validator_change/src/lib.rs index a2299b930..a2d9daeff 100644 --- a/beacon_chain/validator_change/src/lib.rs +++ b/beacon_chain/validator_change/src/lib.rs @@ -62,7 +62,7 @@ pub fn update_validator_set( /* * Validator is pending activiation. */ - x if x == ValidatorStatus::PendingActivation => { + ValidatorStatus::PendingActivation => { let new_total_changed = total_changed .checked_add(deposit_size_gwei) .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?; @@ -82,7 +82,7 @@ pub fn update_validator_set( /* * Validator is pending exit. */ - x if x == ValidatorStatus::PendingExit => { + ValidatorStatus::PendingExit => { let new_total_changed = total_changed .checked_add(v.balance) .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?; From 5eabdad0dc59d44a0823a178374190cd4eee5d2d Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 12 Dec 2018 19:22:44 -0800 Subject: [PATCH 4/7] remove active_validators crate --- .../utils/active-validators/Cargo.toml | 7 -- .../utils/active-validators/src/lib.rs | 64 ------------------- beacon_chain/validator_change/Cargo.toml | 1 - beacon_chain/validator_change/src/lib.rs | 4 +- beacon_chain/validator_shuffling/src/lib.rs | 1 - .../validator_shuffling/src/shuffle.rs | 15 ++++- 6 files changed, 13 insertions(+), 79 deletions(-) delete mode 100644 beacon_chain/utils/active-validators/Cargo.toml delete mode 100644 beacon_chain/utils/active-validators/src/lib.rs diff --git a/beacon_chain/utils/active-validators/Cargo.toml b/beacon_chain/utils/active-validators/Cargo.toml deleted file mode 100644 index 4729747d9..000000000 --- a/beacon_chain/utils/active-validators/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "active-validators" -version = "0.1.0" -authors = ["Paul Hauner "] - -[dependencies] -types = { path = "../../types" } diff --git a/beacon_chain/utils/active-validators/src/lib.rs b/beacon_chain/utils/active-validators/src/lib.rs deleted file mode 100644 index 495d1f85a..000000000 --- a/beacon_chain/utils/active-validators/src/lib.rs +++ /dev/null @@ -1,64 +0,0 @@ -extern crate types; - -use types::{ValidatorRecord, ValidatorStatus}; - -/// Returns the indicies of each active validator in a given vec of validators. -pub fn active_validator_indices(validators: &[ValidatorRecord]) -> Vec { - validators - .iter() - .enumerate() - .filter_map(|(i, validator)| { - if validator.status_is(ValidatorStatus::Active) { - Some(i) - } else { - None - } - }) - .collect() -} - -#[cfg(test)] -mod tests { - use super::*; - - pub fn validator_is_active(v: &ValidatorRecord) -> bool { - v.status_is(ValidatorStatus::Active) - } - - #[test] - fn test_active_validator() { - let mut validators = vec![]; - - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Active; - assert!(validator_is_active(&v)); - validators.push(v); - - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::PendingActivation; - assert!(!validator_is_active(&v)); - validators.push(v); - - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::PendingExit; - assert!(!validator_is_active(&v)); - validators.push(v); - - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::PendingWithdraw; - assert!(!validator_is_active(&v)); - validators.push(v); - - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Withdrawn; - assert!(!validator_is_active(&v)); - validators.push(v); - - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Penalized; - assert!(!validator_is_active(&v)); - validators.push(v); - - assert_eq!(active_validator_indices(&validators), vec![0]); - } -} diff --git a/beacon_chain/validator_change/Cargo.toml b/beacon_chain/validator_change/Cargo.toml index 88e78c9f8..0705033dc 100644 --- a/beacon_chain/validator_change/Cargo.toml +++ b/beacon_chain/validator_change/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.0" authors = ["Paul Hauner "] [dependencies] -active-validators = { path = "../utils/active-validators" } bytes = "0.4.10" hashing = { path = "../utils/hashing" } types = { path = "../types" } diff --git a/beacon_chain/validator_change/src/lib.rs b/beacon_chain/validator_change/src/lib.rs index a2d9daeff..99687e30a 100644 --- a/beacon_chain/validator_change/src/lib.rs +++ b/beacon_chain/validator_change/src/lib.rs @@ -1,9 +1,7 @@ -extern crate active_validators; extern crate bytes; extern crate hashing; extern crate types; -use active_validators::validator_is_active; use bytes::{BufMut, BytesMut}; use hashing::canonical_hash; use std::cmp::max; @@ -31,7 +29,7 @@ pub fn update_validator_set( let total_balance = { let mut bal: u64 = 0; for v in validators.iter() { - if validator_is_active(&v) { + if v.status_is(ValidatorStatus::Active) { bal = bal .checked_add(v.balance) .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?; diff --git a/beacon_chain/validator_shuffling/src/lib.rs b/beacon_chain/validator_shuffling/src/lib.rs index fe2447fc0..90077279f 100644 --- a/beacon_chain/validator_shuffling/src/lib.rs +++ b/beacon_chain/validator_shuffling/src/lib.rs @@ -1,4 +1,3 @@ -extern crate active_validators; extern crate honey_badger_split; extern crate types; extern crate vec_shuffle; diff --git a/beacon_chain/validator_shuffling/src/shuffle.rs b/beacon_chain/validator_shuffling/src/shuffle.rs index 3009f4cbd..17d3d6420 100644 --- a/beacon_chain/validator_shuffling/src/shuffle.rs +++ b/beacon_chain/validator_shuffling/src/shuffle.rs @@ -1,8 +1,7 @@ use std::cmp::min; -use active_validators::active_validator_indices; use honey_badger_split::SplitExt; -use types::{ChainConfig, ShardAndCommittee, ValidatorRecord}; +use types::{ChainConfig, ShardAndCommittee, ValidatorRecord, ValidatorStatus}; use vec_shuffle::{shuffle, ShuffleErr}; type DelegatedCycle = Vec>; @@ -24,7 +23,17 @@ pub fn shard_and_committees_for_cycle( config: &ChainConfig, ) -> Result { let shuffled_validator_indices = { - let mut validator_indices = active_validator_indices(validators); + let mut validator_indices = validators + .iter() + .enumerate() + .filter_map(|(i, validator)| { + if validator.status_is(ValidatorStatus::Active) { + Some(i) + } else { + None + } + }) + .collect(); shuffle(seed, validator_indices)? }; let shard_indices: Vec = (0_usize..config.shard_count as usize).into_iter().collect(); From ca3aa82f4e3ac2f0b6819b89032207a32e66c027 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 12 Dec 2018 19:22:52 -0800 Subject: [PATCH 5/7] rustfmt changes --- beacon_chain/validator_shuffling/src/shuffle.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/beacon_chain/validator_shuffling/src/shuffle.rs b/beacon_chain/validator_shuffling/src/shuffle.rs index 17d3d6420..577668a8f 100644 --- a/beacon_chain/validator_shuffling/src/shuffle.rs +++ b/beacon_chain/validator_shuffling/src/shuffle.rs @@ -96,8 +96,10 @@ fn generate_cycle( .map(|(j, shard_indices)| ShardAndCommittee { shard: ((shard_start + j) % shard_count) as u16, committee: shard_indices.to_vec(), - }).collect() - }).collect(); + }) + .collect() + }) + .collect(); Ok(cycle) } From 7d77ebd1b5bbb7ab2e8b182b5aeda53736adc7a2 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 12 Dec 2018 19:24:15 -0800 Subject: [PATCH 6/7] remove active-validators crate from deps desc --- Cargo.toml | 1 - beacon_chain/validator_shuffling/Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f2fe2aa7d..31a63dd4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,6 @@ members = [ "beacon_chain/naive_fork_choice", "beacon_chain/state-transition", "beacon_chain/types", - "beacon_chain/utils/active-validators", "beacon_chain/utils/bls", "beacon_chain/utils/boolean-bitfield", "beacon_chain/utils/hashing", diff --git a/beacon_chain/validator_shuffling/Cargo.toml b/beacon_chain/validator_shuffling/Cargo.toml index 269b5c557..ba99841b8 100644 --- a/beacon_chain/validator_shuffling/Cargo.toml +++ b/beacon_chain/validator_shuffling/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.0" authors = ["Paul Hauner "] [dependencies] -active-validators = { path = "../utils/active-validators" } honey-badger-split = { path = "../utils/honey-badger-split" } types = { path = "../types" } vec_shuffle = { path = "../utils/vec_shuffle" } From de54d40841f8401bf2078bbadcd57cf0cb5453c8 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 12 Dec 2018 19:28:54 -0800 Subject: [PATCH 7/7] fix build by patching up import and usage --- beacon_chain/validator_change/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/beacon_chain/validator_change/src/lib.rs b/beacon_chain/validator_change/src/lib.rs index a2d9daeff..352585cbd 100644 --- a/beacon_chain/validator_change/src/lib.rs +++ b/beacon_chain/validator_change/src/lib.rs @@ -3,7 +3,6 @@ extern crate bytes; extern crate hashing; extern crate types; -use active_validators::validator_is_active; use bytes::{BufMut, BytesMut}; use hashing::canonical_hash; use std::cmp::max; @@ -31,7 +30,7 @@ pub fn update_validator_set( let total_balance = { let mut bal: u64 = 0; for v in validators.iter() { - if validator_is_active(&v) { + if v.status_is(ValidatorStatus::Active) { bal = bal .checked_add(v.balance) .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?;