From da508fd82623cc8bd9539c834aa91467981c0658 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Wed, 26 Dec 2018 12:45:04 -0600 Subject: [PATCH] deleted validator_change --- Cargo.toml | 1 - beacon_chain/validator_change/Cargo.toml | 10 -- beacon_chain/validator_change/src/lib.rs | 141 ----------------------- 3 files changed, 152 deletions(-) delete mode 100644 beacon_chain/validator_change/Cargo.toml delete mode 100644 beacon_chain/validator_change/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index e14bd336c..ee19d1277 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,6 @@ members = [ "beacon_chain/utils/slot-clock", "beacon_chain/utils/ssz", "beacon_chain/utils/vec_shuffle", - "beacon_chain/validator_change", "beacon_chain/validator_induction", "beacon_chain/validator_shuffling", "lighthouse/db", diff --git a/beacon_chain/validator_change/Cargo.toml b/beacon_chain/validator_change/Cargo.toml deleted file mode 100644 index 4574a115f..000000000 --- a/beacon_chain/validator_change/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "validator_change" -version = "0.1.0" -authors = ["Paul Hauner "] -edition = "2018" - -[dependencies] -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 deleted file mode 100644 index 99687e30a..000000000 --- a/beacon_chain/validator_change/src/lib.rs +++ /dev/null @@ -1,141 +0,0 @@ -extern crate bytes; -extern crate hashing; -extern crate types; - -use bytes::{BufMut, BytesMut}; -use hashing::canonical_hash; -use std::cmp::max; -use types::{Hash256, ValidatorRecord, ValidatorStatus}; - -pub enum UpdateValidatorSetError { - ArithmeticOverflow, -} - -const VALIDATOR_FLAG_ENTRY: u8 = 0; -const VALIDATOR_FLAG_EXIT: u8 = 1; - -pub fn update_validator_set( - validators: &mut Vec, - hash_chain: Hash256, - present_slot: u64, - deposit_size_gwei: u64, - max_validator_churn_quotient: u64, -) -> Result<(), UpdateValidatorSetError> { - /* - * Total balance of all active validators. - * - * Return an error if an overflow occurs. - */ - let total_balance = { - let mut bal: u64 = 0; - for v in validators.iter() { - if v.status_is(ValidatorStatus::Active) { - bal = bal - .checked_add(v.balance) - .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?; - } - } - bal - }; - - /* - * Note: this is not the maximum allowable change, it can actually be higher. - */ - let max_allowable_change = { - let double_deposit_size = deposit_size_gwei - .checked_mul(2) - .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?; - max( - double_deposit_size, - total_balance / max_validator_churn_quotient, - ) - }; - - let mut hasher = ValidatorChangeHashChain { - bytes: hash_chain.to_vec(), - }; - let mut total_changed: u64 = 0; - for (i, v) in validators.iter_mut().enumerate() { - match v.status { - /* - * Validator is pending activiation. - */ - ValidatorStatus::PendingActivation => { - let new_total_changed = total_changed - .checked_add(deposit_size_gwei) - .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?; - /* - * If entering this validator would not exceed the max balance delta, - * activate the validator. - */ - if new_total_changed <= max_allowable_change { - v.status = ValidatorStatus::Active; - hasher.extend(i, &v.pubkey.as_bytes(), VALIDATOR_FLAG_ENTRY); - total_changed = new_total_changed; - } else { - // Entering the validator would exceed the balance delta. - break; - } - } - /* - * Validator is pending exit. - */ - ValidatorStatus::PendingExit => { - let new_total_changed = total_changed - .checked_add(v.balance) - .ok_or(UpdateValidatorSetError::ArithmeticOverflow)?; - /* - * If exiting this validator would not exceed the max balance delta, - * exit the validator - */ - if new_total_changed <= max_allowable_change { - v.status = ValidatorStatus::PendingWithdraw; - v.exit_slot = present_slot; - hasher.extend(i, &v.pubkey.as_bytes(), VALIDATOR_FLAG_EXIT); - total_changed = new_total_changed; - } else { - // Exiting the validator would exceed the balance delta. - break; - } - } - _ => (), - }; - if total_changed >= max_allowable_change { - break; - } - } - Ok(()) -} - -pub struct ValidatorChangeHashChain { - bytes: Vec, -} - -impl ValidatorChangeHashChain { - pub fn extend(&mut self, index: usize, pubkey: &Vec, flag: u8) { - let mut message = self.bytes.clone(); - message.append(&mut serialize_validator_change_record(index, pubkey, flag)); - self.bytes = canonical_hash(&message); - } -} - -fn serialize_validator_change_record(index: usize, pubkey: &Vec, flag: u8) -> Vec { - let mut buf = BytesMut::with_capacity(68); - buf.put_u8(flag); - let index_bytes = { - let mut buf = BytesMut::with_capacity(8); - buf.put_u64_be(index as u64); - buf.take()[8 - 3..8].to_vec() - }; - buf.put(index_bytes); - buf.put(pubkey); - buf.take().to_vec() -} - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -}