From 0627b658f17a38daf29ffcf92a8c527e032bca30 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sat, 8 Dec 2018 21:11:10 -0600 Subject: [PATCH 01/35] struct updated accoding to spec --- beacon_chain/types/src/validator_record.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 3a15baeec..44d5b60df 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -14,13 +14,13 @@ pub enum ValidatorStatus { #[derive(Debug, Clone, PartialEq)] pub struct ValidatorRecord { pub pubkey: PublicKey, - pub withdrawal_shard: u16, - pub withdrawal_address: Address, - pub randao_commitment: Hash256, - pub randao_last_change: u64, + pub withdrawal_credentials: Hash32, + pub randao_commitment: Hash32, + pub randao_skips: u64, pub balance: u64, - pub status: u8, - pub exit_slot: u64, + pub status: u64, + pub latest_status_change_slot: u64, + pub exit_count: u64 } impl ValidatorRecord { From c63ef6d03230cdfa5582cf8dd48763f86530c3c7 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sun, 9 Dec 2018 22:13:57 -0600 Subject: [PATCH 02/35] ValidatorRecord initialization updated to reflect struct --- beacon_chain/types/src/validator_record.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 44d5b60df..e7ad8fecf 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -1,5 +1,5 @@ use super::bls::{Keypair, PublicKey}; -use super::{Address, Hash256}; +use super::{Hash256}; #[derive(Debug, PartialEq, Clone, Copy)] pub enum ValidatorStatus { @@ -14,8 +14,8 @@ pub enum ValidatorStatus { #[derive(Debug, Clone, PartialEq)] pub struct ValidatorRecord { pub pubkey: PublicKey, - pub withdrawal_credentials: Hash32, - pub randao_commitment: Hash32, + pub withdrawal_credentials: Hash256, + pub randao_commitment: Hash256, pub randao_skips: u64, pub balance: u64, pub status: u64, @@ -32,13 +32,13 @@ impl ValidatorRecord { let keypair = Keypair::random(); let s = Self { pubkey: keypair.pk.clone(), - withdrawal_shard: 0, - withdrawal_address: Address::zero(), + withdrawal_credentials: Hash256::zero(), randao_commitment: Hash256::zero(), - randao_last_change: 0, + randao_skips: 0, balance: 0, status: 0, - exit_slot: 0, + latest_status_change_slot: 0, + exit_count: 0 }; (s, keypair) } From 1d48aa280b6e51ab7b3ecc0fb5862c3d16c936f1 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sun, 9 Dec 2018 22:22:08 -0600 Subject: [PATCH 03/35] test passing in validator_record.rs --- beacon_chain/types/src/validator_record.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index e7ad8fecf..b1110c855 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -51,12 +51,12 @@ mod tests { #[test] fn test_validator_record_zero_rand_keypair() { let (v, _kp) = ValidatorRecord::zero_with_thread_rand_keypair(); - assert_eq!(v.withdrawal_shard, 0); - assert!(v.withdrawal_address.is_zero()); + assert!(v.withdrawal_credentials.is_zero()); assert!(v.randao_commitment.is_zero()); - assert_eq!(v.randao_last_change, 0); + assert_eq!(v.randao_skips, 0); assert_eq!(v.balance, 0); assert_eq!(v.status, 0); - assert_eq!(v.exit_slot, 0); + assert_eq!(v.latest_status_change_slot, 0); + assert_eq!(v.exit_count, 0); } } From 071e099203a8ed86a447fb712830c1f4e989211f Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Wed, 12 Dec 2018 17:56:44 -0600 Subject: [PATCH 04/35] validator registration removed --- beacon_chain/types/src/chain_config.rs | 5 ++-- beacon_chain/types/src/lib.rs | 2 -- .../types/src/validator_registration.rs | 26 ------------------- 3 files changed, 3 insertions(+), 30 deletions(-) delete mode 100644 beacon_chain/types/src/validator_registration.rs diff --git a/beacon_chain/types/src/chain_config.rs b/beacon_chain/types/src/chain_config.rs index bcc565a75..68a2f5406 100644 --- a/beacon_chain/types/src/chain_config.rs +++ b/beacon_chain/types/src/chain_config.rs @@ -1,4 +1,4 @@ -use super::ValidatorRegistration; +use super::ValidatorRecord; #[derive(Debug, Clone, PartialEq)] pub struct ChainConfig { @@ -9,7 +9,8 @@ pub struct ChainConfig { pub max_validator_churn_quotient: u64, pub genesis_time: u64, pub slot_duration_millis: u64, - pub initial_validators: Vec, + // TODO: revisit this + pub initial_validators: Vec, } /* diff --git a/beacon_chain/types/src/lib.rs b/beacon_chain/types/src/lib.rs index 6ef752e04..4e883668c 100644 --- a/beacon_chain/types/src/lib.rs +++ b/beacon_chain/types/src/lib.rs @@ -18,7 +18,6 @@ pub mod shard_and_committee; pub mod shard_reassignment_record; pub mod special_record; pub mod validator_record; -pub mod validator_registration; use self::ethereum_types::{H160, H256, U256}; use std::collections::HashMap; @@ -36,7 +35,6 @@ pub use pending_attestation_record::PendingAttestationRecord; pub use shard_and_committee::ShardAndCommittee; pub use special_record::{SpecialRecord, SpecialRecordKind}; pub use validator_record::{ValidatorRecord, ValidatorStatus}; -pub use validator_registration::ValidatorRegistration; pub type Hash256 = H256; pub type Address = H160; diff --git a/beacon_chain/types/src/validator_registration.rs b/beacon_chain/types/src/validator_registration.rs deleted file mode 100644 index 139885b1c..000000000 --- a/beacon_chain/types/src/validator_registration.rs +++ /dev/null @@ -1,26 +0,0 @@ -use super::{Address, Hash256}; -use bls::{create_proof_of_possession, Keypair, PublicKey, Signature}; - -/// The information gathered from the PoW chain validator registration function. -#[derive(Debug, Clone, PartialEq)] -pub struct ValidatorRegistration { - pub pubkey: PublicKey, - pub withdrawal_shard: u16, - pub withdrawal_address: Address, - pub randao_commitment: Hash256, - pub proof_of_possession: Signature, -} - -impl ValidatorRegistration { - pub fn random() -> Self { - let keypair = Keypair::random(); - - Self { - pubkey: keypair.pk.clone(), - withdrawal_shard: 0, - withdrawal_address: Address::random(), - randao_commitment: Hash256::random(), - proof_of_possession: create_proof_of_possession(&keypair), - } - } -} From c7f7bfaab4b2a0d06aacefe54fd228817bded815 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Wed, 12 Dec 2018 17:57:14 -0600 Subject: [PATCH 05/35] deposit and deposit_parameters types added --- beacon_chain/types/src/deposit.rs | 14 ++++++++++++++ beacon_chain/types/src/deposit_parameters.rs | 9 +++++++++ 2 files changed, 23 insertions(+) create mode 100644 beacon_chain/types/src/deposit.rs create mode 100644 beacon_chain/types/src/deposit_parameters.rs diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs new file mode 100644 index 000000000..778ebbe8b --- /dev/null +++ b/beacon_chain/types/src/deposit.rs @@ -0,0 +1,14 @@ +use super::deposit_parameters::DepositParameters; +use super::{Hash256}; + +pub struct Deposit { + pub merkle_branch: Hash256, + pub merkle_tree_index: u64, + pub deposit_data: DepositDate +} + +pub struct DepositDate { + pub deposit_parameters: DepositParameters, + pub value: u64, + pub timestamp: u64 +} diff --git a/beacon_chain/types/src/deposit_parameters.rs b/beacon_chain/types/src/deposit_parameters.rs new file mode 100644 index 000000000..9ff121fb9 --- /dev/null +++ b/beacon_chain/types/src/deposit_parameters.rs @@ -0,0 +1,9 @@ +use super::bls::{Keypair, PublicKey, AggregateSignature}; +use super::{Hash256}; + +pub struct DepositParameters { + pub pubkey: PublicKey, + pub proof_of_possession: AggregateSignature, + pub withdrawal_credentials: Hash256, + pub randao_commitment: Hash256 +} From 045f7b5b980caae04eef685957d286403bf17c07 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Wed, 12 Dec 2018 17:59:50 -0600 Subject: [PATCH 06/35] typo --- beacon_chain/types/src/deposit.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs index 778ebbe8b..b10352a45 100644 --- a/beacon_chain/types/src/deposit.rs +++ b/beacon_chain/types/src/deposit.rs @@ -4,10 +4,10 @@ use super::{Hash256}; pub struct Deposit { pub merkle_branch: Hash256, pub merkle_tree_index: u64, - pub deposit_data: DepositDate + pub deposit_data: DepositData } -pub struct DepositDate { +pub struct DepositData { pub deposit_parameters: DepositParameters, pub value: u64, pub timestamp: u64 From 6089b7c108ca4528c08cae46f46f579fbe727987 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Wed, 12 Dec 2018 19:55:30 -0600 Subject: [PATCH 07/35] chain config TODO --- beacon_chain/types/src/chain_config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon_chain/types/src/chain_config.rs b/beacon_chain/types/src/chain_config.rs index e48aabe1e..51becf519 100644 --- a/beacon_chain/types/src/chain_config.rs +++ b/beacon_chain/types/src/chain_config.rs @@ -10,6 +10,7 @@ pub struct ChainConfig { pub max_validator_churn_quotient: u64, pub genesis_time: u64, pub slot_duration_millis: u64, + // TODO: Come back to this pub initial_validators: Vec, // New constants From 5fa251943bdb011074ce523b15ebda0580e9d226 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Fri, 14 Dec 2018 17:32:01 -0600 Subject: [PATCH 08/35] updated deposit structs and validator record accrding to spec --- beacon_chain/types/src/deposit.rs | 9 +-------- beacon_chain/types/src/deposit_data.rs | 7 +++++++ .../src/{deposit_parameters.rs => deposit_input.rs} | 4 ++-- beacon_chain/types/src/validator_record.rs | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 beacon_chain/types/src/deposit_data.rs rename beacon_chain/types/src/{deposit_parameters.rs => deposit_input.rs} (68%) diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs index b10352a45..58e61c9cf 100644 --- a/beacon_chain/types/src/deposit.rs +++ b/beacon_chain/types/src/deposit.rs @@ -1,14 +1,7 @@ -use super::deposit_parameters::DepositParameters; use super::{Hash256}; pub struct Deposit { - pub merkle_branch: Hash256, + pub merkle_branch: Vec, pub merkle_tree_index: u64, pub deposit_data: DepositData } - -pub struct DepositData { - pub deposit_parameters: DepositParameters, - pub value: u64, - pub timestamp: u64 -} diff --git a/beacon_chain/types/src/deposit_data.rs b/beacon_chain/types/src/deposit_data.rs new file mode 100644 index 000000000..32a705d27 --- /dev/null +++ b/beacon_chain/types/src/deposit_data.rs @@ -0,0 +1,7 @@ +use super::deposit_parameters::DepositParameters; + +pub struct DepositData { + pub deposit_parameter: DepositInput, + pub value: u64, + pub timestamp: u64 +} diff --git a/beacon_chain/types/src/deposit_parameters.rs b/beacon_chain/types/src/deposit_input.rs similarity index 68% rename from beacon_chain/types/src/deposit_parameters.rs rename to beacon_chain/types/src/deposit_input.rs index 9ff121fb9..d2db350ef 100644 --- a/beacon_chain/types/src/deposit_parameters.rs +++ b/beacon_chain/types/src/deposit_input.rs @@ -3,7 +3,7 @@ use super::{Hash256}; pub struct DepositParameters { pub pubkey: PublicKey, - pub proof_of_possession: AggregateSignature, pub withdrawal_credentials: Hash256, - pub randao_commitment: Hash256 + pub randao_commitment: Hash256, + pub proof_of_possession: AggregateSignature } diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index c4e57d335..c68830f60 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -31,7 +31,7 @@ pub struct ValidatorRecord { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, pub randao_commitment: Hash256, - pub randao_skips: u64, + pub randao_layers: u64, pub balance: u64, pub status: u64, pub latest_status_change_slot: u64, @@ -49,7 +49,7 @@ impl ValidatorRecord { pubkey: keypair.pk.clone(), withdrawal_credentials: Hash256::zero(), randao_commitment: Hash256::zero(), - randao_skips: 0, + randao_layers: 0, balance: 0, status: 0, latest_status_change_slot: 0, @@ -72,7 +72,7 @@ mod tests { let (v, _kp) = ValidatorRecord::zero_with_thread_rand_keypair(); assert!(v.withdrawal_credentials.is_zero()); assert!(v.randao_commitment.is_zero()); - assert_eq!(v.randao_skips, 0); + assert_eq!(v.randao_layers, 0); assert_eq!(v.balance, 0); assert_eq!(v.status, 0); assert_eq!(v.latest_status_change_slot, 0); From 7065454b5c25b9b32e9b79bf996b2fb80020fbe2 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sat, 15 Dec 2018 23:18:23 -0600 Subject: [PATCH 09/35] deposit and validator_record type changes --- beacon_chain/types/src/deposit.rs | 1 + beacon_chain/types/src/deposit_data.rs | 4 ++-- beacon_chain/types/src/deposit_input.rs | 6 +++--- beacon_chain/types/src/lib.rs | 6 ++++++ beacon_chain/types/src/validator_record.rs | 6 +++--- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs index 58e61c9cf..77724000f 100644 --- a/beacon_chain/types/src/deposit.rs +++ b/beacon_chain/types/src/deposit.rs @@ -1,4 +1,5 @@ use super::{Hash256}; +use super::deposit_data::DepositData; pub struct Deposit { pub merkle_branch: Vec, diff --git a/beacon_chain/types/src/deposit_data.rs b/beacon_chain/types/src/deposit_data.rs index 32a705d27..ce187d310 100644 --- a/beacon_chain/types/src/deposit_data.rs +++ b/beacon_chain/types/src/deposit_data.rs @@ -1,7 +1,7 @@ -use super::deposit_parameters::DepositParameters; +use super::deposit_input::DepositInput; pub struct DepositData { - pub deposit_parameter: DepositInput, + pub deposit_input: DepositInput, pub value: u64, pub timestamp: u64 } diff --git a/beacon_chain/types/src/deposit_input.rs b/beacon_chain/types/src/deposit_input.rs index d2db350ef..16526cec1 100644 --- a/beacon_chain/types/src/deposit_input.rs +++ b/beacon_chain/types/src/deposit_input.rs @@ -1,9 +1,9 @@ -use super::bls::{Keypair, PublicKey, AggregateSignature}; +use super::bls::{PublicKey, Signature}; use super::{Hash256}; -pub struct DepositParameters { +pub struct DepositInput { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, pub randao_commitment: Hash256, - pub proof_of_possession: AggregateSignature + pub proof_of_possession: Signature } diff --git a/beacon_chain/types/src/lib.rs b/beacon_chain/types/src/lib.rs index f87619fd5..d3a3543fa 100644 --- a/beacon_chain/types/src/lib.rs +++ b/beacon_chain/types/src/lib.rs @@ -12,6 +12,9 @@ pub mod candidate_pow_receipt_root_record; pub mod chain_config; pub mod crosslink_record; pub mod crystallized_state; +pub mod deposit; +pub mod deposit_data; +pub mod deposit_input; pub mod fork_data; pub mod pending_attestation_record; pub mod shard_and_committee; @@ -30,6 +33,9 @@ pub use beacon_state::BeaconState; pub use chain_config::ChainConfig; pub use crosslink_record::CrosslinkRecord; pub use crystallized_state::CrystallizedState; +pub use deposit::Deposit; +pub use deposit_data::DepositData; +pub use deposit_input::DepositInput; pub use fork_data::ForkData; pub use pending_attestation_record::PendingAttestationRecord; pub use shard_and_committee::ShardAndCommittee; diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index c68830f60..b8834131f 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -33,7 +33,7 @@ pub struct ValidatorRecord { pub randao_commitment: Hash256, pub randao_layers: u64, pub balance: u64, - pub status: u64, + pub status: ValidatorStatus, pub latest_status_change_slot: u64, pub exit_count: u64 } @@ -51,7 +51,7 @@ impl ValidatorRecord { randao_commitment: Hash256::zero(), randao_layers: 0, balance: 0, - status: 0, + status: From::from(0), latest_status_change_slot: 0, exit_count: 0 }; @@ -74,7 +74,7 @@ mod tests { assert!(v.randao_commitment.is_zero()); assert_eq!(v.randao_layers, 0); assert_eq!(v.balance, 0); - assert_eq!(v.status, 0); + assert_eq!(v.status, From::from(0)); assert_eq!(v.latest_status_change_slot, 0); assert_eq!(v.exit_count, 0); } From a05364cb49d75c524e45c2d60241c85f1f9adeab Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sat, 15 Dec 2018 23:19:15 -0600 Subject: [PATCH 10/35] first pass at updating inductor with deposit structs - lots of compilation errors --- .../validator_induction/src/inductor.rs | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 816d8e356..63022b6ba 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -1,5 +1,5 @@ use bls::verify_proof_of_possession; -use types::{ValidatorRecord, ValidatorRegistration, ValidatorStatus}; +use types::{ValidatorRecord, Deposit, ValidatorStatus, BeaconState}; /// The size of a validators deposit in GWei. pub const DEPOSIT_GWEI: u64 = 32_000_000_000; @@ -8,7 +8,7 @@ pub const DEPOSIT_GWEI: u64 = 32_000_000_000; pub struct ValidatorInductor { pub current_slot: u64, pub shard_count: u16, - validators: Vec, + beacon_state: BeaconState, empty_validator_start: usize, } @@ -19,11 +19,11 @@ pub enum ValidatorInductionError { } impl ValidatorInductor { - pub fn new(current_slot: u64, shard_count: u16, validators: Vec) -> Self { + pub fn new(current_slot: u64, shard_count: u16, beacon_state: BeaconState) -> Self { Self { current_slot, shard_count, - validators, + beacon_state, empty_validator_start: 0, } } @@ -34,50 +34,55 @@ impl ValidatorInductor { /// validator in `CrystallizedState.validators`. pub fn induct( &mut self, - rego: &ValidatorRegistration, + deposit: &Deposit, status: ValidatorStatus, ) -> Result { - let v = self.process_registration(rego, status)?; + let v = self.process_registration(deposit, status)?; Ok(self.add_validator(v)) } /// Verify a `ValidatorRegistration` and return a `ValidatorRecord` if valid. - fn process_registration( + fn process_deposit( &self, - r: &ValidatorRegistration, + deposit: &Deposit, status: ValidatorStatus, ) -> Result { + let deposit_input = &deposit.deposit_data.deposit_input; /* * Ensure withdrawal shard is not too high. */ + /* if r.withdrawal_shard > self.shard_count { return Err(ValidatorInductionError::InvalidShard); } + */ /* * Prove validator has knowledge of their secret key. */ - if !verify_proof_of_possession(&r.proof_of_possession, &r.pubkey) { + if !verify_proof_of_possession(&deposit_input.proof_of_possession, &deposit_input.pubkey) { return Err(ValidatorInductionError::InvaidProofOfPossession); } Ok(ValidatorRecord { - pubkey: r.pubkey.clone(), - withdrawal_shard: r.withdrawal_shard, - withdrawal_address: r.withdrawal_address, - randao_commitment: r.randao_commitment, - randao_last_change: self.current_slot, + pubkey: deposit_input.pubkey.clone(), + withdrawal_credentials: deposit_input.withdrawal_credentials, + randao_commitment: deposit_input.randao_commitment, + // TODO: revisit this + randao_layers: 0, balance: DEPOSIT_GWEI, status: status, - exit_slot: 0, + latest_status_change_slot: self.beacon_state.validator_registry_latest_change_slot, + exit_count: self.beacon_state.validator_registry_exit_count }) } /// Returns the index of the first `ValidatorRecord` in the `CrystallizedState` where /// `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 { + let validators = self.beacon_state.validator_registry; + for i in self.empty_validator_start..validators.len() { + if validators[i].status == ValidatorStatus::Withdrawn { self.empty_validator_start = i + 1; return Some(i); } @@ -91,18 +96,18 @@ impl ValidatorInductor { fn add_validator(&mut self, v: ValidatorRecord) -> usize { match self.first_withdrawn_validator() { Some(i) => { - self.validators[i] = v; + self.beacon_state.validator_registry[i] = v; i } None => { - self.validators.push(v); - self.validators.len() - 1 + self.beacon_state.validator_registry.push(v); + self.beacon_state.validator_registry.len() - 1 } } } pub fn to_vec(self) -> Vec { - self.validators + self.beacon_state.validator_registry } } @@ -114,6 +119,7 @@ mod tests { use hashing::proof_of_possession_hash; use types::{Address, Hash256}; + /* fn registration_equals_record(reg: &ValidatorRegistration, rec: &ValidatorRecord) -> bool { (reg.pubkey == rec.pubkey) & (reg.withdrawal_shard == rec.withdrawal_shard) @@ -121,6 +127,7 @@ mod tests { & (reg.randao_commitment == rec.randao_commitment) & (verify_proof_of_possession(®.proof_of_possession, &rec.pubkey)) } + */ /// Generate a proof of possession for some keypair. fn get_proof_of_possession(kp: &Keypair) -> Signature { @@ -151,7 +158,7 @@ mod tests { let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 0); - assert!(registration_equals_record(&r, &validators[0])); + //assert!(registration_equals_record(&r, &validators[0])); assert_eq!(validators.len(), 1); } @@ -187,7 +194,7 @@ mod tests { let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 5); - assert!(registration_equals_record(&r, &validators[5])); + //assert!(registration_equals_record(&r, &validators[5])); assert_eq!(validators.len(), 6); } @@ -210,7 +217,7 @@ mod tests { let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 1); - assert!(registration_equals_record(&r, &validators[1])); + //assert!(registration_equals_record(&r, &validators[1])); assert_eq!(validators.len(), 5); } @@ -231,7 +238,7 @@ mod tests { let result = inductor.induct(&r, ValidatorStatus::PendingActivation); let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 0); - assert!(registration_equals_record(&r, &validators[0])); + //assert!(registration_equals_record(&r, &validators[0])); /* * Ensure the second validator gets the 1'st slot @@ -241,7 +248,7 @@ mod tests { let result = inductor.induct(&r_two, ValidatorStatus::PendingActivation); let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 1); - assert!(registration_equals_record(&r_two, &validators[1])); + //assert!(registration_equals_record(&r_two, &validators[1])); assert_eq!(validators.len(), 5); } From eef3627c92c80db9237059daf2fea1dc2dc5238a Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sun, 16 Dec 2018 17:00:53 -0600 Subject: [PATCH 11/35] one test passing --- beacon_chain/types/src/beacon_state.rs | 2 +- beacon_chain/types/src/fork_data.rs | 2 +- .../validator_induction/src/inductor.old.rs | 281 ++++++++++++++++++ .../validator_induction/src/inductor.rs | 37 ++- 4 files changed, 301 insertions(+), 21 deletions(-) create mode 100644 beacon_chain/validator_induction/src/inductor.old.rs diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index 2f5581d57..d0e46f1b5 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -7,7 +7,7 @@ use super::shard_reassignment_record::ShardReassignmentRecord; use super::validator_record::ValidatorRecord; use super::Hash256; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Default)] pub struct BeaconState { pub validator_registry: Vec, pub validator_registry_latest_change_slot: u64, diff --git a/beacon_chain/types/src/fork_data.rs b/beacon_chain/types/src/fork_data.rs index 4c3541c3f..ce3cf77e2 100644 --- a/beacon_chain/types/src/fork_data.rs +++ b/beacon_chain/types/src/fork_data.rs @@ -1,4 +1,4 @@ -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct ForkData { pub pre_fork_version: u64, pub post_fork_version: u64, diff --git a/beacon_chain/validator_induction/src/inductor.old.rs b/beacon_chain/validator_induction/src/inductor.old.rs new file mode 100644 index 000000000..816d8e356 --- /dev/null +++ b/beacon_chain/validator_induction/src/inductor.old.rs @@ -0,0 +1,281 @@ +use bls::verify_proof_of_possession; +use types::{ValidatorRecord, ValidatorRegistration, ValidatorStatus}; + +/// The size of a validators deposit in GWei. +pub const DEPOSIT_GWEI: u64 = 32_000_000_000; + +/// Inducts validators into a `CrystallizedState`. +pub struct ValidatorInductor { + pub current_slot: u64, + pub shard_count: u16, + validators: Vec, + empty_validator_start: usize, +} + +#[derive(Debug, PartialEq, Clone)] +pub enum ValidatorInductionError { + InvalidShard, + InvaidProofOfPossession, +} + +impl ValidatorInductor { + pub fn new(current_slot: u64, shard_count: u16, validators: Vec) -> Self { + Self { + current_slot, + shard_count, + validators, + empty_validator_start: 0, + } + } + + /// Attempt to induct a validator into the CrystallizedState. + /// + /// Returns an error if the registration is invalid, otherwise returns the index of the + /// validator in `CrystallizedState.validators`. + pub fn induct( + &mut self, + rego: &ValidatorRegistration, + status: ValidatorStatus, + ) -> Result { + let v = self.process_registration(rego, status)?; + Ok(self.add_validator(v)) + } + + /// Verify a `ValidatorRegistration` and return a `ValidatorRecord` if valid. + fn process_registration( + &self, + r: &ValidatorRegistration, + status: ValidatorStatus, + ) -> Result { + /* + * Ensure withdrawal shard is not too high. + */ + if r.withdrawal_shard > self.shard_count { + return Err(ValidatorInductionError::InvalidShard); + } + + /* + * Prove validator has knowledge of their secret key. + */ + if !verify_proof_of_possession(&r.proof_of_possession, &r.pubkey) { + return Err(ValidatorInductionError::InvaidProofOfPossession); + } + + Ok(ValidatorRecord { + pubkey: r.pubkey.clone(), + withdrawal_shard: r.withdrawal_shard, + withdrawal_address: r.withdrawal_address, + randao_commitment: r.randao_commitment, + randao_last_change: self.current_slot, + balance: DEPOSIT_GWEI, + status: status, + exit_slot: 0, + }) + } + + /// Returns the index of the first `ValidatorRecord` in the `CrystallizedState` where + /// `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 { + self.empty_validator_start = i + 1; + return Some(i); + } + } + None + } + + /// Adds a `ValidatorRecord` to the `CrystallizedState` by replacing first validator where + /// `validator.status == Withdraw`. If no such withdrawn validator exists, adds the new + /// validator to the end of the list. + fn add_validator(&mut self, v: ValidatorRecord) -> usize { + match self.first_withdrawn_validator() { + Some(i) => { + self.validators[i] = v; + i + } + None => { + self.validators.push(v); + self.validators.len() - 1 + } + } + } + + pub fn to_vec(self) -> Vec { + self.validators + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use bls::{Keypair, Signature}; + use hashing::proof_of_possession_hash; + use types::{Address, Hash256}; + + fn registration_equals_record(reg: &ValidatorRegistration, rec: &ValidatorRecord) -> bool { + (reg.pubkey == rec.pubkey) + & (reg.withdrawal_shard == rec.withdrawal_shard) + & (reg.withdrawal_address == rec.withdrawal_address) + & (reg.randao_commitment == rec.randao_commitment) + & (verify_proof_of_possession(®.proof_of_possession, &rec.pubkey)) + } + + /// Generate a proof of possession for some keypair. + fn get_proof_of_possession(kp: &Keypair) -> Signature { + let pop_message = proof_of_possession_hash(&kp.pk.as_bytes()); + Signature::new_hashed(&pop_message, &kp.sk) + } + + /// Generate a basic working ValidatorRegistration for use in tests. + fn get_registration() -> ValidatorRegistration { + let kp = Keypair::random(); + ValidatorRegistration { + pubkey: kp.pk.clone(), + withdrawal_shard: 0, + withdrawal_address: Address::zero(), + randao_commitment: Hash256::zero(), + proof_of_possession: get_proof_of_possession(&kp), + } + } + + #[test] + fn test_validator_inductor_valid_empty_validators() { + let validators = vec![]; + + let r = get_registration(); + + let mut inductor = ValidatorInductor::new(0, 1024, validators); + let result = inductor.induct(&r, ValidatorStatus::PendingActivation); + let validators = inductor.to_vec(); + + assert_eq!(result.unwrap(), 0); + assert!(registration_equals_record(&r, &validators[0])); + assert_eq!(validators.len(), 1); + } + + #[test] + fn test_validator_inductor_status() { + let validators = vec![]; + + let r = get_registration(); + + let mut inductor = ValidatorInductor::new(0, 1024, validators); + let _ = inductor.induct(&r, ValidatorStatus::PendingActivation); + let _ = inductor.induct(&r, ValidatorStatus::Active); + let validators = inductor.to_vec(); + + assert!(validators[0].status == ValidatorStatus::PendingActivation); + assert!(validators[1].status == ValidatorStatus::Active); + assert_eq!(validators.len(), 2); + } + + #[test] + fn test_validator_inductor_valid_all_active_validators() { + let mut validators = vec![]; + for _ in 0..5 { + let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); + v.status = ValidatorStatus::Active; + validators.push(v); + } + + let r = get_registration(); + + let mut inductor = ValidatorInductor::new(0, 1024, validators); + let result = inductor.induct(&r, ValidatorStatus::PendingActivation); + let validators = inductor.to_vec(); + + assert_eq!(result.unwrap(), 5); + assert!(registration_equals_record(&r, &validators[5])); + assert_eq!(validators.len(), 6); + } + + #[test] + 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; + validators.push(v); + for _ in 0..4 { + let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); + v.status = ValidatorStatus::Withdrawn; + validators.push(v); + } + + let r = get_registration(); + + let mut inductor = ValidatorInductor::new(0, 1024, validators); + let result = inductor.induct(&r, ValidatorStatus::PendingActivation); + let validators = inductor.to_vec(); + + assert_eq!(result.unwrap(), 1); + assert!(registration_equals_record(&r, &validators[1])); + assert_eq!(validators.len(), 5); + } + + #[test] + fn test_validator_inductor_valid_all_withdrawn_validators() { + let mut validators = vec![]; + for _ in 0..5 { + let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); + v.status = ValidatorStatus::Withdrawn; + validators.push(v); + } + + /* + * Ensure the first validator gets the 0'th slot + */ + let r = get_registration(); + let mut inductor = ValidatorInductor::new(0, 1024, validators); + let result = inductor.induct(&r, ValidatorStatus::PendingActivation); + let validators = inductor.to_vec(); + assert_eq!(result.unwrap(), 0); + assert!(registration_equals_record(&r, &validators[0])); + + /* + * Ensure the second validator gets the 1'st slot + */ + let r_two = get_registration(); + let mut inductor = ValidatorInductor::new(0, 1024, validators); + let result = inductor.induct(&r_two, ValidatorStatus::PendingActivation); + let validators = inductor.to_vec(); + assert_eq!(result.unwrap(), 1); + assert!(registration_equals_record(&r_two, &validators[1])); + assert_eq!(validators.len(), 5); + } + + #[test] + fn test_validator_inductor_shard_too_high() { + let validators = vec![]; + + let mut r = get_registration(); + r.withdrawal_shard = 1025; + + let mut inductor = ValidatorInductor::new(0, 1024, validators); + let result = inductor.induct(&r, ValidatorStatus::PendingActivation); + let validators = inductor.to_vec(); + + assert_eq!(result, Err(ValidatorInductionError::InvalidShard)); + assert_eq!(validators.len(), 0); + } + + #[test] + fn test_validator_inductor_shard_proof_of_possession_failure() { + let validators = vec![]; + + let mut r = get_registration(); + let kp = Keypair::random(); + r.proof_of_possession = get_proof_of_possession(&kp); + + let mut inductor = ValidatorInductor::new(0, 1024, validators); + let result = inductor.induct(&r, ValidatorStatus::PendingActivation); + let validators = inductor.to_vec(); + + assert_eq!( + result, + Err(ValidatorInductionError::InvaidProofOfPossession) + ); + assert_eq!(validators.len(), 0); + } +} diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 63022b6ba..6a455a74c 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -1,5 +1,5 @@ use bls::verify_proof_of_possession; -use types::{ValidatorRecord, Deposit, ValidatorStatus, BeaconState}; +use types::{ValidatorRecord, DepositInput, ValidatorStatus, BeaconState}; /// The size of a validators deposit in GWei. pub const DEPOSIT_GWEI: u64 = 32_000_000_000; @@ -34,20 +34,19 @@ impl ValidatorInductor { /// validator in `CrystallizedState.validators`. pub fn induct( &mut self, - deposit: &Deposit, + deposit_input: &DepositInput, status: ValidatorStatus, ) -> Result { - let v = self.process_registration(deposit, status)?; + let v = self.process_deposit(deposit_input, status)?; Ok(self.add_validator(v)) } /// Verify a `ValidatorRegistration` and return a `ValidatorRecord` if valid. fn process_deposit( &self, - deposit: &Deposit, + deposit_input: &DepositInput, status: ValidatorStatus, ) -> Result { - let deposit_input = &deposit.deposit_data.deposit_input; /* * Ensure withdrawal shard is not too high. */ @@ -80,9 +79,8 @@ impl ValidatorInductor { /// Returns the index of the first `ValidatorRecord` in the `CrystallizedState` where /// `validator.status == Withdrawn`. If no such record exists, `None` is returned. fn first_withdrawn_validator(&mut self) -> Option { - let validators = self.beacon_state.validator_registry; - for i in self.empty_validator_start..validators.len() { - if validators[i].status == ValidatorStatus::Withdrawn { + for i in self.empty_validator_start..self.beacon_state.validator_registry.len() { + if self.beacon_state.validator_registry[i].status == ValidatorStatus::Withdrawn { self.empty_validator_start = i + 1; return Some(i); } @@ -117,7 +115,7 @@ mod tests { use bls::{Keypair, Signature}; use hashing::proof_of_possession_hash; - use types::{Address, Hash256}; + use types::{Hash256}; /* fn registration_equals_record(reg: &ValidatorRegistration, rec: &ValidatorRecord) -> bool { @@ -135,26 +133,25 @@ mod tests { Signature::new_hashed(&pop_message, &kp.sk) } - /// Generate a basic working ValidatorRegistration for use in tests. - fn get_registration() -> ValidatorRegistration { + /// Generate a basic working Deposit for use in tests. + fn get_deposit_input() -> DepositInput { let kp = Keypair::random(); - ValidatorRegistration { + DepositInput { pubkey: kp.pk.clone(), - withdrawal_shard: 0, - withdrawal_address: Address::zero(), + withdrawal_credentials: Hash256::zero(), randao_commitment: Hash256::zero(), - proof_of_possession: get_proof_of_possession(&kp), + proof_of_possession: get_proof_of_possession(&kp) } } #[test] fn test_validator_inductor_valid_empty_validators() { - let validators = vec![]; + let state = BeaconState::default(); - let r = get_registration(); + let d = get_deposit_input(); - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); + let mut inductor = ValidatorInductor::new(0, 1024, state); + let result = inductor.induct(&d, ValidatorStatus::PendingActivation); let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 0); @@ -162,6 +159,7 @@ mod tests { assert_eq!(validators.len(), 1); } + /* #[test] fn test_validator_inductor_status() { let validators = vec![]; @@ -285,4 +283,5 @@ mod tests { ); assert_eq!(validators.len(), 0); } + */ } From 8adf5f79900d6a36f1a5e5757b347411884f3ff1 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sun, 16 Dec 2018 20:32:19 -0600 Subject: [PATCH 12/35] exit count changed to 0 --- beacon_chain/validator_induction/src/inductor.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 6a455a74c..08a738158 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -67,12 +67,11 @@ impl ValidatorInductor { pubkey: deposit_input.pubkey.clone(), withdrawal_credentials: deposit_input.withdrawal_credentials, randao_commitment: deposit_input.randao_commitment, - // TODO: revisit this randao_layers: 0, balance: DEPOSIT_GWEI, status: status, latest_status_change_slot: self.beacon_state.validator_registry_latest_change_slot, - exit_count: self.beacon_state.validator_registry_exit_count + exit_count: 0 }) } From e93eb55dcd46577b912957851b76933e741757af Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sun, 16 Dec 2018 20:35:15 -0600 Subject: [PATCH 13/35] removed inductor.old.rs --- .../validator_induction/src/inductor.old.rs | 281 ------------------ 1 file changed, 281 deletions(-) delete mode 100644 beacon_chain/validator_induction/src/inductor.old.rs diff --git a/beacon_chain/validator_induction/src/inductor.old.rs b/beacon_chain/validator_induction/src/inductor.old.rs deleted file mode 100644 index 816d8e356..000000000 --- a/beacon_chain/validator_induction/src/inductor.old.rs +++ /dev/null @@ -1,281 +0,0 @@ -use bls::verify_proof_of_possession; -use types::{ValidatorRecord, ValidatorRegistration, ValidatorStatus}; - -/// The size of a validators deposit in GWei. -pub const DEPOSIT_GWEI: u64 = 32_000_000_000; - -/// Inducts validators into a `CrystallizedState`. -pub struct ValidatorInductor { - pub current_slot: u64, - pub shard_count: u16, - validators: Vec, - empty_validator_start: usize, -} - -#[derive(Debug, PartialEq, Clone)] -pub enum ValidatorInductionError { - InvalidShard, - InvaidProofOfPossession, -} - -impl ValidatorInductor { - pub fn new(current_slot: u64, shard_count: u16, validators: Vec) -> Self { - Self { - current_slot, - shard_count, - validators, - empty_validator_start: 0, - } - } - - /// Attempt to induct a validator into the CrystallizedState. - /// - /// Returns an error if the registration is invalid, otherwise returns the index of the - /// validator in `CrystallizedState.validators`. - pub fn induct( - &mut self, - rego: &ValidatorRegistration, - status: ValidatorStatus, - ) -> Result { - let v = self.process_registration(rego, status)?; - Ok(self.add_validator(v)) - } - - /// Verify a `ValidatorRegistration` and return a `ValidatorRecord` if valid. - fn process_registration( - &self, - r: &ValidatorRegistration, - status: ValidatorStatus, - ) -> Result { - /* - * Ensure withdrawal shard is not too high. - */ - if r.withdrawal_shard > self.shard_count { - return Err(ValidatorInductionError::InvalidShard); - } - - /* - * Prove validator has knowledge of their secret key. - */ - if !verify_proof_of_possession(&r.proof_of_possession, &r.pubkey) { - return Err(ValidatorInductionError::InvaidProofOfPossession); - } - - Ok(ValidatorRecord { - pubkey: r.pubkey.clone(), - withdrawal_shard: r.withdrawal_shard, - withdrawal_address: r.withdrawal_address, - randao_commitment: r.randao_commitment, - randao_last_change: self.current_slot, - balance: DEPOSIT_GWEI, - status: status, - exit_slot: 0, - }) - } - - /// Returns the index of the first `ValidatorRecord` in the `CrystallizedState` where - /// `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 { - self.empty_validator_start = i + 1; - return Some(i); - } - } - None - } - - /// Adds a `ValidatorRecord` to the `CrystallizedState` by replacing first validator where - /// `validator.status == Withdraw`. If no such withdrawn validator exists, adds the new - /// validator to the end of the list. - fn add_validator(&mut self, v: ValidatorRecord) -> usize { - match self.first_withdrawn_validator() { - Some(i) => { - self.validators[i] = v; - i - } - None => { - self.validators.push(v); - self.validators.len() - 1 - } - } - } - - pub fn to_vec(self) -> Vec { - self.validators - } -} - -#[cfg(test)] -mod tests { - use super::*; - - use bls::{Keypair, Signature}; - use hashing::proof_of_possession_hash; - use types::{Address, Hash256}; - - fn registration_equals_record(reg: &ValidatorRegistration, rec: &ValidatorRecord) -> bool { - (reg.pubkey == rec.pubkey) - & (reg.withdrawal_shard == rec.withdrawal_shard) - & (reg.withdrawal_address == rec.withdrawal_address) - & (reg.randao_commitment == rec.randao_commitment) - & (verify_proof_of_possession(®.proof_of_possession, &rec.pubkey)) - } - - /// Generate a proof of possession for some keypair. - fn get_proof_of_possession(kp: &Keypair) -> Signature { - let pop_message = proof_of_possession_hash(&kp.pk.as_bytes()); - Signature::new_hashed(&pop_message, &kp.sk) - } - - /// Generate a basic working ValidatorRegistration for use in tests. - fn get_registration() -> ValidatorRegistration { - let kp = Keypair::random(); - ValidatorRegistration { - pubkey: kp.pk.clone(), - withdrawal_shard: 0, - withdrawal_address: Address::zero(), - randao_commitment: Hash256::zero(), - proof_of_possession: get_proof_of_possession(&kp), - } - } - - #[test] - fn test_validator_inductor_valid_empty_validators() { - let validators = vec![]; - - let r = get_registration(); - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - - assert_eq!(result.unwrap(), 0); - assert!(registration_equals_record(&r, &validators[0])); - assert_eq!(validators.len(), 1); - } - - #[test] - fn test_validator_inductor_status() { - let validators = vec![]; - - let r = get_registration(); - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let _ = inductor.induct(&r, ValidatorStatus::PendingActivation); - let _ = inductor.induct(&r, ValidatorStatus::Active); - let validators = inductor.to_vec(); - - assert!(validators[0].status == ValidatorStatus::PendingActivation); - assert!(validators[1].status == ValidatorStatus::Active); - assert_eq!(validators.len(), 2); - } - - #[test] - fn test_validator_inductor_valid_all_active_validators() { - let mut validators = vec![]; - for _ in 0..5 { - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Active; - validators.push(v); - } - - let r = get_registration(); - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - - assert_eq!(result.unwrap(), 5); - assert!(registration_equals_record(&r, &validators[5])); - assert_eq!(validators.len(), 6); - } - - #[test] - 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; - validators.push(v); - for _ in 0..4 { - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Withdrawn; - validators.push(v); - } - - let r = get_registration(); - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - - assert_eq!(result.unwrap(), 1); - assert!(registration_equals_record(&r, &validators[1])); - assert_eq!(validators.len(), 5); - } - - #[test] - fn test_validator_inductor_valid_all_withdrawn_validators() { - let mut validators = vec![]; - for _ in 0..5 { - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Withdrawn; - validators.push(v); - } - - /* - * Ensure the first validator gets the 0'th slot - */ - let r = get_registration(); - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - assert_eq!(result.unwrap(), 0); - assert!(registration_equals_record(&r, &validators[0])); - - /* - * Ensure the second validator gets the 1'st slot - */ - let r_two = get_registration(); - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r_two, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - assert_eq!(result.unwrap(), 1); - assert!(registration_equals_record(&r_two, &validators[1])); - assert_eq!(validators.len(), 5); - } - - #[test] - fn test_validator_inductor_shard_too_high() { - let validators = vec![]; - - let mut r = get_registration(); - r.withdrawal_shard = 1025; - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - - assert_eq!(result, Err(ValidatorInductionError::InvalidShard)); - assert_eq!(validators.len(), 0); - } - - #[test] - fn test_validator_inductor_shard_proof_of_possession_failure() { - let validators = vec![]; - - let mut r = get_registration(); - let kp = Keypair::random(); - r.proof_of_possession = get_proof_of_possession(&kp); - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - - assert_eq!( - result, - Err(ValidatorInductionError::InvaidProofOfPossession) - ); - assert_eq!(validators.len(), 0); - } -} From faaba18799750d83d76181a7d2ceb13c9c8bcce4 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Thu, 20 Dec 2018 20:22:08 -0600 Subject: [PATCH 14/35] types updated against spec and process_deposit routine partially implemented --- beacon_chain/types/src/beacon_state.rs | 12 +- beacon_chain/types/src/validator_record.rs | 2 - beacon_chain/validator_induction/Cargo.toml | 1 + .../validator_induction/src/inductor.rs | 202 +++++++----------- beacon_chain/validator_induction/src/lib.rs | 3 +- 5 files changed, 92 insertions(+), 128 deletions(-) diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index d0e46f1b5..76c23ac85 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -9,7 +9,11 @@ use super::Hash256; #[derive(Debug, PartialEq, Default)] pub struct BeaconState { + pub slot: u64, + pub genesis_time: u64, + pub fork_data: ForkData, pub validator_registry: Vec, + pub validator_balances: Vec, pub validator_registry_latest_change_slot: u64, pub validator_registry_exit_count: u64, pub validator_registry_delta_chain_tip: Hash256, @@ -20,15 +24,13 @@ pub struct BeaconState { pub persistent_committee_reassignments: Vec, pub previous_justified_slot: u64, pub justified_slot: u64, - pub justified_slot_bitfield: u64, + pub justification_bitfield: u64, pub finalized_slot: u64, pub latest_crosslinks: Vec, - pub latest_state_recalculation_slot: u64, - pub latest_block_hashes: Vec, + pub latest_block_roots: Vec, pub latest_penalized_exit_balances: Vec, pub latest_attestations: Vec, + pub batched_block_roots: Vec, pub processed_pow_receipt_root: Hash256, pub candidate_pow_receipt_roots: Vec, - pub genesis_time: u64, - pub fork_data: ForkData, } diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index b8834131f..af0e75d39 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -32,7 +32,6 @@ pub struct ValidatorRecord { pub withdrawal_credentials: Hash256, pub randao_commitment: Hash256, pub randao_layers: u64, - pub balance: u64, pub status: ValidatorStatus, pub latest_status_change_slot: u64, pub exit_count: u64 @@ -50,7 +49,6 @@ impl ValidatorRecord { withdrawal_credentials: Hash256::zero(), randao_commitment: Hash256::zero(), randao_layers: 0, - balance: 0, status: From::from(0), latest_status_change_slot: 0, exit_count: 0 diff --git a/beacon_chain/validator_induction/Cargo.toml b/beacon_chain/validator_induction/Cargo.toml index 9a443133e..2236b0f63 100644 --- a/beacon_chain/validator_induction/Cargo.toml +++ b/beacon_chain/validator_induction/Cargo.toml @@ -7,3 +7,4 @@ authors = ["Paul Hauner "] bls = { path = "../utils/bls" } hashing = { path = "../utils/hashing" } types = { path = "../types" } +spec = { path = "../spec" } diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 08a738158..36bce4284 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -1,111 +1,76 @@ -use bls::verify_proof_of_possession; -use types::{ValidatorRecord, DepositInput, ValidatorStatus, BeaconState}; +use bls::{PublicKey, verify_proof_of_possession}; +use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus}; +use spec::ChainSpec; /// The size of a validators deposit in GWei. pub const DEPOSIT_GWEI: u64 = 32_000_000_000; -/// Inducts validators into a `CrystallizedState`. -pub struct ValidatorInductor { - pub current_slot: u64, - pub shard_count: u16, - beacon_state: BeaconState, - empty_validator_start: usize, -} - #[derive(Debug, PartialEq, Clone)] pub enum ValidatorInductionError { InvalidShard, InvaidProofOfPossession, + InvalidWithdrawalCredentials } -impl ValidatorInductor { - pub fn new(current_slot: u64, shard_count: u16, beacon_state: BeaconState) -> Self { - Self { - current_slot, - shard_count, - beacon_state, - empty_validator_start: 0, - } - } - - /// Attempt to induct a validator into the CrystallizedState. - /// - /// Returns an error if the registration is invalid, otherwise returns the index of the - /// validator in `CrystallizedState.validators`. - pub fn induct( - &mut self, - deposit_input: &DepositInput, - status: ValidatorStatus, - ) -> Result { - let v = self.process_deposit(deposit_input, status)?; - Ok(self.add_validator(v)) - } - - /// Verify a `ValidatorRegistration` and return a `ValidatorRecord` if valid. - fn process_deposit( - &self, - deposit_input: &DepositInput, - status: ValidatorStatus, - ) -> Result { - /* - * Ensure withdrawal shard is not too high. - */ - /* - if r.withdrawal_shard > self.shard_count { - return Err(ValidatorInductionError::InvalidShard); - } - */ - - /* - * Prove validator has knowledge of their secret key. - */ - if !verify_proof_of_possession(&deposit_input.proof_of_possession, &deposit_input.pubkey) { - return Err(ValidatorInductionError::InvaidProofOfPossession); - } - - Ok(ValidatorRecord { - pubkey: deposit_input.pubkey.clone(), - withdrawal_credentials: deposit_input.withdrawal_credentials, - randao_commitment: deposit_input.randao_commitment, - randao_layers: 0, - balance: DEPOSIT_GWEI, - status: status, - latest_status_change_slot: self.beacon_state.validator_registry_latest_change_slot, - exit_count: 0 - }) - } - - /// Returns the index of the first `ValidatorRecord` in the `CrystallizedState` where - /// `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.beacon_state.validator_registry.len() { - if self.beacon_state.validator_registry[i].status == ValidatorStatus::Withdrawn { - self.empty_validator_start = i + 1; - return Some(i); +pub fn process_deposit( + state: &mut BeaconState, + deposit: &Deposit, + spec: &ChainSpec) +-> Result { + let deposit_input = &deposit.deposit_data.deposit_input; + let validator_index = state.validator_registry.iter() + .position(|validator| validator.pubkey == deposit_input.pubkey); + + match validator_index { + // replace withdrawn validator + Some(i) => { + if state.validator_registry[i].withdrawal_credentials == deposit_input.withdrawal_credentials { + state.validator_balances[i] += DEPOSIT_GWEI; + return Ok(i); } - } - None - } - - /// Adds a `ValidatorRecord` to the `CrystallizedState` by replacing first validator where - /// `validator.status == Withdraw`. If no such withdrawn validator exists, adds the new - /// validator to the end of the list. - fn add_validator(&mut self, v: ValidatorRecord) -> usize { - match self.first_withdrawn_validator() { - Some(i) => { - self.beacon_state.validator_registry[i] = v; - i - } - None => { - self.beacon_state.validator_registry.push(v); - self.beacon_state.validator_registry.len() - 1 + + Err(ValidatorInductionError::InvalidWithdrawalCredentials) + }, + // no withdrawn validators; push a new one on + None => { + let validator = ValidatorRecord { + pubkey: deposit_input.pubkey.clone(), + withdrawal_credentials: deposit_input.withdrawal_credentials, + randao_commitment: deposit_input.randao_commitment, + randao_layers: 0, + status: ValidatorStatus::PendingActivation, + latest_status_change_slot: state.validator_registry_latest_change_slot.clone(), + exit_count: 0 + }; + + match min_empty_validator_index(state, spec) { + Some(i) => { + state.validator_registry[i] = validator; + state.validator_balances[i] = DEPOSIT_GWEI; + Ok(i) + }, + None => { + state.validator_registry.push(validator); + state.validator_balances.push(DEPOSIT_GWEI); + Ok(state.validator_registry.len() - 1) + } } } } +} - pub fn to_vec(self) -> Vec { - self.beacon_state.validator_registry +fn min_empty_validator_index( + state: &BeaconState, + spec: &ChainSpec +) -> Option { + for i in 0..state.validator_registry.len() { + if state.validator_balances[i] == 0 + && state.validator_registry[i].latest_status_change_slot + + spec.zero_balance_validator_ttl <= state.slot { + return Some(i); + } } + None } #[cfg(test)] @@ -114,17 +79,27 @@ mod tests { use bls::{Keypair, Signature}; use hashing::proof_of_possession_hash; - use types::{Hash256}; - - /* - fn registration_equals_record(reg: &ValidatorRegistration, rec: &ValidatorRecord) -> bool { - (reg.pubkey == rec.pubkey) - & (reg.withdrawal_shard == rec.withdrawal_shard) - & (reg.withdrawal_address == rec.withdrawal_address) - & (reg.randao_commitment == rec.randao_commitment) - & (verify_proof_of_possession(®.proof_of_possession, &rec.pubkey)) + use types::{Hash256, DepositData, DepositInput}; + + fn get_deposit() -> Deposit { + let kp = Keypair::random(); + let deposit_input = DepositInput { + pubkey: kp.pk.clone(), + withdrawal_credentials: Hash256::zero(), + randao_commitment: Hash256::zero(), + proof_of_possession: get_proof_of_possession(&kp) + }; + let deposit_data = DepositData { + deposit_input: deposit_input, + value: 0, + timestamp: 0 + }; + Deposit { + merkle_branch: Vec::new(), + merkle_tree_index: 0, + deposit_data: deposit_data + } } - */ /// Generate a proof of possession for some keypair. fn get_proof_of_possession(kp: &Keypair) -> Signature { @@ -132,30 +107,17 @@ mod tests { Signature::new_hashed(&pop_message, &kp.sk) } - /// Generate a basic working Deposit for use in tests. - fn get_deposit_input() -> DepositInput { - let kp = Keypair::random(); - DepositInput { - pubkey: kp.pk.clone(), - withdrawal_credentials: Hash256::zero(), - randao_commitment: Hash256::zero(), - proof_of_possession: get_proof_of_possession(&kp) - } - } - #[test] fn test_validator_inductor_valid_empty_validators() { - let state = BeaconState::default(); + let mut state = BeaconState::default(); + let deposit = get_deposit(); + let spec = ChainSpec::foundation(); - let d = get_deposit_input(); - - let mut inductor = ValidatorInductor::new(0, 1024, state); - let result = inductor.induct(&d, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); + let result = process_deposit(&mut state, &deposit, &spec); assert_eq!(result.unwrap(), 0); //assert!(registration_equals_record(&r, &validators[0])); - assert_eq!(validators.len(), 1); + //assert_eq!(validators.len(), 1); } /* diff --git a/beacon_chain/validator_induction/src/lib.rs b/beacon_chain/validator_induction/src/lib.rs index 6ea6265ba..582155555 100644 --- a/beacon_chain/validator_induction/src/lib.rs +++ b/beacon_chain/validator_induction/src/lib.rs @@ -1,7 +1,8 @@ extern crate bls; extern crate hashing; extern crate types; +extern crate spec; mod inductor; -pub use inductor::{ValidatorInductionError, ValidatorInductor}; +pub use inductor::{ValidatorInductionError}; From 537c5e2846365d20d1cac04fd77ec246ce109111 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Fri, 21 Dec 2018 18:41:13 -0600 Subject: [PATCH 15/35] more testing around deposit processing --- .../validator_induction/src/inductor.rs | 45 ++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 36bce4284..826794d70 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -22,7 +22,6 @@ pub fn process_deposit( .position(|validator| validator.pubkey == deposit_input.pubkey); match validator_index { - // replace withdrawn validator Some(i) => { if state.validator_registry[i].withdrawal_credentials == deposit_input.withdrawal_credentials { state.validator_balances[i] += DEPOSIT_GWEI; @@ -31,7 +30,6 @@ pub fn process_deposit( Err(ValidatorInductionError::InvalidWithdrawalCredentials) }, - // no withdrawn validators; push a new one on None => { let validator = ValidatorRecord { pubkey: deposit_input.pubkey.clone(), @@ -101,6 +99,13 @@ mod tests { } } + fn deposit_equals_record(dep: &Deposit, rec: &ValidatorRecord) -> bool { + (dep.deposit_data.deposit_input.pubkey == rec.pubkey) + & (dep.deposit_data.deposit_input.withdrawal_credentials == rec.withdrawal_credentials) + & (dep.deposit_data.deposit_input.randao_commitment == rec.randao_commitment) + //& (verify_proof_of_possession(®.proof_of_possession, &rec.pubkey)) + } + /// Generate a proof of possession for some keypair. fn get_proof_of_possession(kp: &Keypair) -> Signature { let pop_message = proof_of_possession_hash(&kp.pk.as_bytes()); @@ -108,7 +113,7 @@ mod tests { } #[test] - fn test_validator_inductor_valid_empty_validators() { + fn test_process_deposit_valid_empty_validators() { let mut state = BeaconState::default(); let deposit = get_deposit(); let spec = ChainSpec::foundation(); @@ -116,8 +121,38 @@ mod tests { let result = process_deposit(&mut state, &deposit, &spec); assert_eq!(result.unwrap(), 0); - //assert!(registration_equals_record(&r, &validators[0])); - //assert_eq!(validators.len(), 1); + assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); + assert_eq!(state.validator_registry.len(), 1); + assert_eq!(state.validator_balances.len(), 1); + } + + #[test] + fn test_process_deposit_empty_validators() { + let mut state = BeaconState::default(); + let deposit = get_deposit(); + let spec = ChainSpec::foundation(); + + let result = process_deposit(&mut state, &deposit, &spec); + + assert_eq!(result.unwrap(), 0); + assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); + assert_eq!(state.validator_registry.len(), 1); + assert_eq!(state.validator_balances.len(), 1); + } + + #[test] + fn test_process_deposits_empty_validators() { + let mut state = BeaconState::default(); + let spec = ChainSpec::foundation(); + + for i in 0..5 { + let deposit = get_deposit(); + let result = process_deposit(&mut state, &deposit, &spec); + assert_eq!(result.unwrap(), i); + assert!(deposit_equals_record(&deposit, &state.validator_registry[i])); + assert_eq!(state.validator_registry.len(), i + 1); + assert_eq!(state.validator_balances.len(), i + 1); + } } /* From 50767c7bfdfaea35039dc0481d896e0eb9249b3e Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Fri, 21 Dec 2018 22:45:35 -0600 Subject: [PATCH 16/35] testing around balance increase and zero balance replacement --- .../validator_induction/src/inductor.rs | 188 +++++------------- 1 file changed, 49 insertions(+), 139 deletions(-) diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 826794d70..e342f0b98 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -18,13 +18,19 @@ pub fn process_deposit( spec: &ChainSpec) -> Result { let deposit_input = &deposit.deposit_data.deposit_input; + let deposit_data = &deposit.deposit_data; + + if !verify_proof_of_possession(&deposit_input.proof_of_possession, &deposit_input.pubkey) { + return Err(ValidatorInductionError::InvaidProofOfPossession); + } + let validator_index = state.validator_registry.iter() .position(|validator| validator.pubkey == deposit_input.pubkey); match validator_index { Some(i) => { if state.validator_registry[i].withdrawal_credentials == deposit_input.withdrawal_credentials { - state.validator_balances[i] += DEPOSIT_GWEI; + state.validator_balances[i] += deposit_data.value; return Ok(i); } @@ -44,12 +50,12 @@ pub fn process_deposit( match min_empty_validator_index(state, spec) { Some(i) => { state.validator_registry[i] = validator; - state.validator_balances[i] = DEPOSIT_GWEI; + state.validator_balances[i] = deposit_data.value; Ok(i) }, None => { state.validator_registry.push(validator); - state.validator_balances.push(DEPOSIT_GWEI); + state.validator_balances.push(deposit_data.value); Ok(state.validator_registry.len() - 1) } } @@ -89,7 +95,7 @@ mod tests { }; let deposit_data = DepositData { deposit_input: deposit_input, - value: 0, + value: DEPOSIT_GWEI, timestamp: 0 }; Deposit { @@ -126,20 +132,6 @@ mod tests { assert_eq!(state.validator_balances.len(), 1); } - #[test] - fn test_process_deposit_empty_validators() { - let mut state = BeaconState::default(); - let deposit = get_deposit(); - let spec = ChainSpec::foundation(); - - let result = process_deposit(&mut state, &deposit, &spec); - - assert_eq!(result.unwrap(), 0); - assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); - assert_eq!(state.validator_registry.len(), 1); - assert_eq!(state.validator_balances.len(), 1); - } - #[test] fn test_process_deposits_empty_validators() { let mut state = BeaconState::default(); @@ -154,130 +146,48 @@ mod tests { assert_eq!(state.validator_balances.len(), i + 1); } } - - /* + #[test] - fn test_validator_inductor_status() { - let validators = vec![]; - - let r = get_registration(); - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let _ = inductor.induct(&r, ValidatorStatus::PendingActivation); - let _ = inductor.induct(&r, ValidatorStatus::Active); - let validators = inductor.to_vec(); - - assert!(validators[0].status == ValidatorStatus::PendingActivation); - assert!(validators[1].status == ValidatorStatus::Active); - assert_eq!(validators.len(), 2); - } - - #[test] - fn test_validator_inductor_valid_all_active_validators() { - let mut validators = vec![]; - for _ in 0..5 { - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Active; - validators.push(v); - } - - let r = get_registration(); - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - - assert_eq!(result.unwrap(), 5); - //assert!(registration_equals_record(&r, &validators[5])); - assert_eq!(validators.len(), 6); - } - - #[test] - 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; - validators.push(v); - for _ in 0..4 { - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Withdrawn; - validators.push(v); - } - - let r = get_registration(); - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - - assert_eq!(result.unwrap(), 1); - //assert!(registration_equals_record(&r, &validators[1])); - assert_eq!(validators.len(), 5); - } - - #[test] - fn test_validator_inductor_valid_all_withdrawn_validators() { - let mut validators = vec![]; - for _ in 0..5 { - let (mut v, _) = ValidatorRecord::zero_with_thread_rand_keypair(); - v.status = ValidatorStatus::Withdrawn; - validators.push(v); - } - - /* - * Ensure the first validator gets the 0'th slot - */ - let r = get_registration(); - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); + fn test_process_deposit_top_out() { + let mut state = BeaconState::default(); + let spec = ChainSpec::foundation(); + + let deposit = get_deposit(); + let (mut validator, _) = ValidatorRecord::zero_with_thread_rand_keypair(); + validator.pubkey = deposit.deposit_data.deposit_input.pubkey.clone(); + validator.withdrawal_credentials = deposit.deposit_data.deposit_input.withdrawal_credentials; + validator.randao_commitment = deposit.deposit_data.deposit_input.randao_commitment; + + state.validator_registry.push(validator); + state.validator_balances.push(DEPOSIT_GWEI); + + let result = process_deposit(&mut state, &deposit, &spec); + assert_eq!(result.unwrap(), 0); - //assert!(registration_equals_record(&r, &validators[0])); - - /* - * Ensure the second validator gets the 1'st slot - */ - let r_two = get_registration(); - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r_two, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - assert_eq!(result.unwrap(), 1); - //assert!(registration_equals_record(&r_two, &validators[1])); - assert_eq!(validators.len(), 5); + assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); + assert_eq!(state.validator_balances[0], DEPOSIT_GWEI * 2); + assert_eq!(state.validator_registry.len(), 1); + assert_eq!(state.validator_balances.len(), 1); } - + #[test] - fn test_validator_inductor_shard_too_high() { - let validators = vec![]; - - let mut r = get_registration(); - r.withdrawal_shard = 1025; - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - - assert_eq!(result, Err(ValidatorInductionError::InvalidShard)); - assert_eq!(validators.len(), 0); + fn test_process_deposit_replace_validator() { + let mut state = BeaconState::default(); + let spec = ChainSpec::foundation(); + + let (mut validator, _) = ValidatorRecord::zero_with_thread_rand_keypair(); + state.validator_registry.push(validator); + state.validator_balances.push(0); + + let deposit = get_deposit(); + state.slot = spec.zero_balance_validator_ttl; + + let result = process_deposit(&mut state, &deposit, &spec); + + assert_eq!(result.unwrap(), 0); + assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); + assert_eq!(state.validator_balances[0], DEPOSIT_GWEI); + assert_eq!(state.validator_registry.len(), 1); + assert_eq!(state.validator_balances.len(), 1); } - - #[test] - fn test_validator_inductor_shard_proof_of_possession_failure() { - let validators = vec![]; - - let mut r = get_registration(); - let kp = Keypair::random(); - r.proof_of_possession = get_proof_of_possession(&kp); - - let mut inductor = ValidatorInductor::new(0, 1024, validators); - let result = inductor.induct(&r, ValidatorStatus::PendingActivation); - let validators = inductor.to_vec(); - - assert_eq!( - result, - Err(ValidatorInductionError::InvaidProofOfPossession) - ); - assert_eq!(validators.len(), 0); - } - */ } From 77838734b5761e0f8f2ba50f7e14f3f381cf3560 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Tue, 25 Dec 2018 12:19:20 -0600 Subject: [PATCH 17/35] tests passing again --- beacon_chain/validator_induction/src/inductor.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index e342f0b98..4b370dcfc 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -43,7 +43,7 @@ pub fn process_deposit( randao_commitment: deposit_input.randao_commitment, randao_layers: 0, status: ValidatorStatus::PendingActivation, - latest_status_change_slot: state.validator_registry_latest_change_slot.clone(), + latest_status_change_slot: state.validator_registry_latest_change_slot, exit_count: 0 }; @@ -81,8 +81,8 @@ fn min_empty_validator_index( mod tests { use super::*; - use bls::{Keypair, Signature}; - use hashing::proof_of_possession_hash; + use bls::{Keypair, Signature, create_proof_of_possession}; + use hashing::canonical_hash; use types::{Hash256, DepositData, DepositInput}; fn get_deposit() -> Deposit { @@ -91,7 +91,7 @@ mod tests { pubkey: kp.pk.clone(), withdrawal_credentials: Hash256::zero(), randao_commitment: Hash256::zero(), - proof_of_possession: get_proof_of_possession(&kp) + proof_of_possession: create_proof_of_possession(&kp) }; let deposit_data = DepositData { deposit_input: deposit_input, @@ -112,12 +112,6 @@ mod tests { //& (verify_proof_of_possession(®.proof_of_possession, &rec.pubkey)) } - /// Generate a proof of possession for some keypair. - fn get_proof_of_possession(kp: &Keypair) -> Signature { - let pop_message = proof_of_possession_hash(&kp.pk.as_bytes()); - Signature::new_hashed(&pop_message, &kp.sk) - } - #[test] fn test_process_deposit_valid_empty_validators() { let mut state = BeaconState::default(); From 2b818d0c70e9131a71fc88f95421f917d00d4331 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Wed, 26 Dec 2018 12:10:01 -0600 Subject: [PATCH 18/35] deleted deposit types --- beacon_chain/types/src/deposit.rs | 8 -------- beacon_chain/types/src/deposit_data.rs | 7 ------- beacon_chain/types/src/deposit_input.rs | 9 --------- 3 files changed, 24 deletions(-) delete mode 100644 beacon_chain/types/src/deposit.rs delete mode 100644 beacon_chain/types/src/deposit_data.rs delete mode 100644 beacon_chain/types/src/deposit_input.rs diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs deleted file mode 100644 index 77724000f..000000000 --- a/beacon_chain/types/src/deposit.rs +++ /dev/null @@ -1,8 +0,0 @@ -use super::{Hash256}; -use super::deposit_data::DepositData; - -pub struct Deposit { - pub merkle_branch: Vec, - pub merkle_tree_index: u64, - pub deposit_data: DepositData -} diff --git a/beacon_chain/types/src/deposit_data.rs b/beacon_chain/types/src/deposit_data.rs deleted file mode 100644 index ce187d310..000000000 --- a/beacon_chain/types/src/deposit_data.rs +++ /dev/null @@ -1,7 +0,0 @@ -use super::deposit_input::DepositInput; - -pub struct DepositData { - pub deposit_input: DepositInput, - pub value: u64, - pub timestamp: u64 -} diff --git a/beacon_chain/types/src/deposit_input.rs b/beacon_chain/types/src/deposit_input.rs deleted file mode 100644 index 16526cec1..000000000 --- a/beacon_chain/types/src/deposit_input.rs +++ /dev/null @@ -1,9 +0,0 @@ -use super::bls::{PublicKey, Signature}; -use super::{Hash256}; - -pub struct DepositInput { - pub pubkey: PublicKey, - pub withdrawal_credentials: Hash256, - pub randao_commitment: Hash256, - pub proof_of_possession: Signature -} From 9040fcf43726b982fd3da4572b565e136d305e4c Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Wed, 26 Dec 2018 12:13:47 -0600 Subject: [PATCH 19/35] fixed conflicts --- beacon_chain/types/src/lib.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/beacon_chain/types/src/lib.rs b/beacon_chain/types/src/lib.rs index f91325db7..f2904b3c5 100644 --- a/beacon_chain/types/src/lib.rs +++ b/beacon_chain/types/src/lib.rs @@ -19,10 +19,7 @@ pub mod crystallized_state; pub mod deposit; pub mod deposit_data; pub mod deposit_input; -<<<<<<< HEAD -======= pub mod exit; ->>>>>>> master pub mod fork_data; pub mod pending_attestation_record; pub mod proposal_signed_data; @@ -49,10 +46,7 @@ pub use crate::crystallized_state::CrystallizedState; pub use crate::deposit::Deposit; pub use crate::deposit_data::DepositData; pub use crate::deposit_input::DepositInput; -<<<<<<< HEAD -======= pub use crate::exit::Exit; ->>>>>>> master pub use crate::fork_data::ForkData; pub use crate::pending_attestation_record::PendingAttestationRecord; pub use crate::proposal_signed_data::ProposalSignedData; From da508fd82623cc8bd9539c834aa91467981c0658 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Wed, 26 Dec 2018 12:45:04 -0600 Subject: [PATCH 20/35] 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); - } -} From 65917a696a85407e1c69e40ff45388f48f8f6a3f Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Wed, 26 Dec 2018 14:43:09 -0600 Subject: [PATCH 21/35] beacon_state, deposit_input, and validator_record updated again beacon_state, deposit_input, and validator_record updated again --- beacon_chain/chain/src/genesis.rs | 5 ++++- beacon_chain/types/src/beacon_state.rs | 8 +++++--- beacon_chain/types/src/deposit_input.rs | 5 +++++ beacon_chain/types/src/validator_record.rs | 14 +++++++++++--- beacon_chain/validator_induction/src/inductor.rs | 6 +++++- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/beacon_chain/chain/src/genesis.rs b/beacon_chain/chain/src/genesis.rs index d9b634062..e1b39cd23 100644 --- a/beacon_chain/chain/src/genesis.rs +++ b/beacon_chain/chain/src/genesis.rs @@ -1,6 +1,5 @@ use super::{ActiveState, ChainConfig, CrystallizedState}; use types::ValidatorStatus; -use validator_induction::ValidatorInductor; use validator_shuffling::{shard_and_committees_for_cycle, ValidatorAssignmentError}; #[derive(Debug, PartialEq)] @@ -24,6 +23,8 @@ pub fn genesis_states(config: &ChainConfig) -> Result<(ActiveState, Crystallized * * Ignore any records which fail proof-of-possession or are invalid. */ + /* + TODO: refactor this let validators = { let mut inductor = ValidatorInductor::new(0, config.shard_count, vec![]); for registration in &config.initial_validators { @@ -31,6 +32,8 @@ pub fn genesis_states(config: &ChainConfig) -> Result<(ActiveState, Crystallized } inductor.to_vec() }; + */ + let validators = vec![]; /* * Assign the validators to shards, using all zeros as the seed. diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index 76c23ac85..c90595da3 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -17,9 +17,9 @@ pub struct BeaconState { pub validator_registry_latest_change_slot: u64, pub validator_registry_exit_count: u64, pub validator_registry_delta_chain_tip: Hash256, - pub randao_mix: Hash256, - pub next_seed: Hash256, - pub shard_and_committee_for_slots: Vec>, + pub latest_randao_mixes: Vec, + pub latest_vdf_outputs: Vec, + pub shard_committees_at_slots: Vec>, pub persistent_committees: Vec>, pub persistent_committee_reassignments: Vec, pub previous_justified_slot: u64, @@ -27,6 +27,8 @@ pub struct BeaconState { pub justification_bitfield: u64, pub finalized_slot: u64, pub latest_crosslinks: Vec, + // TODO: remove this, it's no longer in the spec + pub latest_state_recalculation_slot: u64, pub latest_block_roots: Vec, pub latest_penalized_exit_balances: Vec, pub latest_attestations: Vec, diff --git a/beacon_chain/types/src/deposit_input.rs b/beacon_chain/types/src/deposit_input.rs index 41b579cda..d280fa7ea 100644 --- a/beacon_chain/types/src/deposit_input.rs +++ b/beacon_chain/types/src/deposit_input.rs @@ -9,6 +9,7 @@ pub struct DepositInput { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, pub randao_commitment: Hash256, + pub poc_commitment: Hash256, pub proof_of_possession: Signature, } @@ -17,6 +18,7 @@ impl Encodable for DepositInput { s.append_vec(&self.pubkey.as_bytes()); s.append(&self.withdrawal_credentials); s.append(&self.randao_commitment); + s.append(&self.poc_commitment); s.append(&self.proof_of_possession); } } @@ -27,6 +29,7 @@ impl Decodable for DepositInput { let pubkey = PublicKey::from_bytes(&pubkey_bytes).map_err(|_| DecodeError::TooShort)?; let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?; let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?; + let (poc_commitment, i) = <_>::ssz_decode(bytes, i)?; let (proof_of_possession, i) = <_>::ssz_decode(bytes, i)?; Ok(( @@ -34,6 +37,7 @@ impl Decodable for DepositInput { pubkey, withdrawal_credentials, randao_commitment, + poc_commitment, proof_of_possession, }, i, @@ -47,6 +51,7 @@ impl TestRandom for DepositInput { pubkey: <_>::random_for_test(rng), withdrawal_credentials: <_>::random_for_test(rng), randao_commitment: <_>::random_for_test(rng), + poc_commitment: <_>::random_for_test(rng), proof_of_possession: <_>::random_for_test(rng), } } diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index af0e75d39..42dc45811 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -34,7 +34,10 @@ pub struct ValidatorRecord { pub randao_layers: u64, pub status: ValidatorStatus, pub latest_status_change_slot: u64, - pub exit_count: u64 + pub exit_count: u64, + pub poc_commitment: Hash256, + pub last_poc_change_slot: u64, + pub second_last_poc_slot: u64 } impl ValidatorRecord { @@ -51,7 +54,10 @@ impl ValidatorRecord { randao_layers: 0, status: From::from(0), latest_status_change_slot: 0, - exit_count: 0 + exit_count: 0, + poc_commitment: Hash256::zero(), + last_poc_change_slot: 0, + second_last_poc_slot: 0 }; (s, keypair) } @@ -71,9 +77,11 @@ mod tests { assert!(v.withdrawal_credentials.is_zero()); assert!(v.randao_commitment.is_zero()); assert_eq!(v.randao_layers, 0); - assert_eq!(v.balance, 0); assert_eq!(v.status, From::from(0)); assert_eq!(v.latest_status_change_slot, 0); assert_eq!(v.exit_count, 0); + assert!(v.poc_commitment.is_zero()); + assert_eq!(v.last_poc_change_slot, 0); + assert_eq!(v.second_last_poc_slot, 0); } } diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 4b370dcfc..ab40bd8cd 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -44,7 +44,10 @@ pub fn process_deposit( randao_layers: 0, status: ValidatorStatus::PendingActivation, latest_status_change_slot: state.validator_registry_latest_change_slot, - exit_count: 0 + exit_count: 0, + poc_commitment: deposit_input.poc_commitment, + last_poc_change_slot: 0, + second_last_poc_slot: 0 }; match min_empty_validator_index(state, spec) { @@ -91,6 +94,7 @@ mod tests { pubkey: kp.pk.clone(), withdrawal_credentials: Hash256::zero(), randao_commitment: Hash256::zero(), + poc_commitment: Hash256::zero(), proof_of_possession: create_proof_of_possession(&kp) }; let deposit_data = DepositData { From a470cb53692a33a64b955050023b3259c6196c22 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Thu, 27 Dec 2018 19:50:05 -0600 Subject: [PATCH 22/35] testing around BeaconState and ChainSpec passing --- beacon_chain/genesis/src/beacon_state.rs | 23 ++++++--------------- beacon_chain/spec/src/foundation.rs | 26 +++++++++++++++--------- beacon_chain/spec/src/lib.rs | 5 +++-- beacon_chain/types/src/beacon_state.rs | 2 +- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index fda94552f..a858baaca 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -1,6 +1,5 @@ use spec::ChainSpec; -use types::{BeaconState, CrosslinkRecord, ForkData, ValidatorStatus}; -use validator_induction::ValidatorInductor; +use types::{BeaconState, CrosslinkRecord, ForkData}; use validator_shuffling::{shard_and_committees_for_cycle, ValidatorAssignmentError}; #[derive(Debug, PartialEq)] @@ -11,26 +10,13 @@ pub enum Error { } pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { - /* - * Parse the ValidatorRegistrations into ValidatorRecords and induct them. - * - * Ignore any records which fail proof-of-possession or are invalid. - */ - let validators = { - let mut inductor = ValidatorInductor::new(0, spec.shard_count, vec![]); - for registration in &spec.initial_validators { - let _ = inductor.induct(®istration, ValidatorStatus::Active); - } - inductor.to_vec() - }; - /* * Assign the validators to shards, using all zeros as the seed. * * Crystallizedstate stores two cycles, so we simply repeat the same assignment twice. */ let _shard_and_committee_for_slots = { - let mut a = shard_and_committees_for_cycle(&[0; 32], &validators, 0, &spec)?; + let mut a = shard_and_committees_for_cycle(&[0; 32], &spec.initial_validators, 0, &spec)?; let mut b = a.clone(); a.append(&mut b); a @@ -55,7 +41,8 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { /* * Validator registry */ - validator_registry: validators, + validator_registry: spec.initial_validators.clone(), + validator_balances: spec.initial_balances.clone(), validator_registry_latest_change_slot: spec.initial_slot_number, validator_registry_exit_count: 0, validator_registry_delta_chain_tip: spec.zero_hash, @@ -118,6 +105,7 @@ mod tests { ); } + /* #[test] fn test_genesis_bad_validator() { let mut spec = ChainSpec::foundation(); @@ -132,4 +120,5 @@ mod tests { spec.initial_validators.len() - 1 ); } + */ } diff --git a/beacon_chain/spec/src/foundation.rs b/beacon_chain/spec/src/foundation.rs index b80187281..ba437cef3 100644 --- a/beacon_chain/spec/src/foundation.rs +++ b/beacon_chain/spec/src/foundation.rs @@ -1,7 +1,7 @@ use super::ChainSpec; use bls::{create_proof_of_possession, Keypair, PublicKey, SecretKey}; -use types::{Address, Hash256, ValidatorRegistration}; +use types::{Address, Hash256, ValidatorRecord}; impl ChainSpec { /// Returns a `ChainSpec` compatible with the specification from Ethereum Foundation. @@ -64,14 +64,15 @@ impl ChainSpec { * Intialization parameters */ initial_validators: initial_validators_for_testing(), + initial_balances: vec![0,0,0,0], genesis_time: 1544672897, processed_pow_receipt_root: Hash256::from("pow_root".as_bytes()), } } } -/// Generate a set of validator registrations to use with testing until the real chain starts. -fn initial_validators_for_testing() -> Vec { +/// Generate a set of validator records to use with testing until the real chain starts. +fn initial_validators_for_testing() -> Vec { // Some dummy private keys to start with. let key_strings = vec![ "jzjxxgjajfjrmgodszzsgqccmhnyvetcuxobhtynojtpdtbj", @@ -94,14 +95,19 @@ fn initial_validators_for_testing() -> Vec { pk: public_key, } }; - let validator_registration = ValidatorRegistration { - pubkey: keypair.pk.clone(), - withdrawal_shard: 0, - withdrawal_address: Address::random(), - randao_commitment: Hash256::random(), - proof_of_possession: create_proof_of_possession(&keypair), + let validator_record = ValidatorRecord { + pubkey: keypair.pk.clone(), + withdrawal_credentials: Hash256::zero(), + randao_commitment: Hash256::zero(), + randao_layers: 0, + status: From::from(0), + latest_status_change_slot: 0, + exit_count: 0, + poc_commitment: Hash256::zero(), + last_poc_change_slot: 0, + second_last_poc_slot: 0 }; - initial_validators.push(validator_registration); + initial_validators.push(validator_record); } initial_validators diff --git a/beacon_chain/spec/src/lib.rs b/beacon_chain/spec/src/lib.rs index 3a05f2696..786cf326b 100644 --- a/beacon_chain/spec/src/lib.rs +++ b/beacon_chain/spec/src/lib.rs @@ -3,7 +3,7 @@ extern crate types; mod foundation; -use types::{Address, Hash256, ValidatorRegistration}; +use types::{Address, Hash256, ValidatorRecord}; #[derive(PartialEq, Debug)] pub struct ChainSpec { @@ -60,7 +60,8 @@ pub struct ChainSpec { /* * Intialization parameters */ - pub initial_validators: Vec, + pub initial_validators: Vec, + pub initial_balances: Vec, pub genesis_time: u64, pub processed_pow_receipt_root: Hash256, } diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index 4bf1bca55..a82894dcc 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -7,7 +7,7 @@ use super::shard_reassignment_record::ShardReassignmentRecord; use super::validator_record::ValidatorRecord; use super::Hash256; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Default)] pub struct BeaconState { // Misc pub slot: u64, From d61ab50f45faad3b9bdbbcf02b6d47c835c7caf9 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sat, 29 Dec 2018 12:17:51 -0600 Subject: [PATCH 23/35] ValidatorRecord serialization fixed --- beacon_chain/types/src/validator_record.rs | 50 +++++++++++++--------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 6a3ecbfb0..7df39f931 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -1,5 +1,5 @@ use super::bls::PublicKey; -use super::{Address, Hash256}; +use super::{Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; use ssz::{Decodable, DecodeError, Encodable, SszStream}; @@ -96,37 +96,43 @@ impl TestRandom for ValidatorStatus { impl Encodable for ValidatorRecord { fn ssz_append(&self, s: &mut SszStream) { s.append(&self.pubkey); - s.append(&self.withdrawal_shard); - s.append(&self.withdrawal_address); + s.append(&self.withdrawal_credentials); s.append(&self.randao_commitment); - s.append(&self.randao_last_change); - s.append(&self.balance); + s.append(&self.randao_layers); s.append(&self.status); - s.append(&self.exit_slot); + s.append(&self.latest_status_change_slot); + s.append(&self.exit_count); + s.append(&self.poc_commitment); + s.append(&self.last_poc_change_slot); + s.append(&self.second_last_poc_slot); } } impl Decodable for ValidatorRecord { fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { let (pubkey, i) = <_>::ssz_decode(bytes, i)?; - let (withdrawal_shard, i) = <_>::ssz_decode(bytes, i)?; - let (withdrawal_address, i) = <_>::ssz_decode(bytes, i)?; + let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?; let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?; - let (randao_last_change, i) = <_>::ssz_decode(bytes, i)?; - let (balance, i) = <_>::ssz_decode(bytes, i)?; + let (randao_layers, i) = <_>::ssz_decode(bytes, i)?; let (status, i) = <_>::ssz_decode(bytes, i)?; - let (exit_slot, i) = <_>::ssz_decode(bytes, i)?; + let (latest_status_change_slot, i) = <_>::ssz_decode(bytes, i)?; + let (exit_count, i) = <_>::ssz_decode(bytes, i)?; + let (poc_commitment, i) = <_>::ssz_decode(bytes, i)?; + let (last_poc_change_slot, i) = <_>::ssz_decode(bytes, i)?; + let (second_last_poc_slot, i) = <_>::ssz_decode(bytes, i)?; Ok(( Self { pubkey, - withdrawal_shard, - withdrawal_address, + withdrawal_credentials, randao_commitment, - randao_last_change, - balance, + randao_layers, status, - exit_slot, + latest_status_change_slot, + exit_count, + poc_commitment, + last_poc_change_slot, + second_last_poc_slot }, i, )) @@ -137,13 +143,15 @@ impl TestRandom for ValidatorRecord { fn random_for_test(rng: &mut T) -> Self { Self { pubkey: <_>::random_for_test(rng), - withdrawal_shard: <_>::random_for_test(rng), - withdrawal_address: <_>::random_for_test(rng), + withdrawal_credentials: <_>::random_for_test(rng), randao_commitment: <_>::random_for_test(rng), - randao_last_change: <_>::random_for_test(rng), - balance: <_>::random_for_test(rng), + randao_layers: <_>::random_for_test(rng), status: <_>::random_for_test(rng), - exit_slot: <_>::random_for_test(rng), + latest_status_change_slot: <_>::random_for_test(rng), + exit_count: <_>::random_for_test(rng), + poc_commitment: <_>::random_for_test(rng), + last_poc_change_slot: <_>::random_for_test(rng), + second_last_poc_slot: <_>::random_for_test(rng), } } } From 598562da7304924788f1c5590b078817dbe7f714 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sat, 29 Dec 2018 16:22:14 -0600 Subject: [PATCH 24/35] BaconState serialization fixed and reorganiztion around induction and deposits --- beacon_chain/genesis/src/beacon_state.rs | 1 - beacon_chain/types/src/beacon_state.rs | 4 ++ beacon_chain/types/src/deposit.rs | 26 ++++++++- beacon_chain/types/src/fork_data.rs | 2 +- beacon_chain/types/src/validator_record.rs | 19 ++++++- .../validator_induction/src/inductor.rs | 53 ++++++------------- 6 files changed, 65 insertions(+), 40 deletions(-) diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index a858baaca..b6a42db5b 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -87,7 +87,6 @@ mod tests { extern crate bls; extern crate validator_induction; - use self::bls::{create_proof_of_possession, Keypair}; use super::*; // TODO: enhance these tests. diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index 15ebde5fb..065e87c02 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -62,6 +62,7 @@ impl Encodable for BeaconState { s.append(&self.genesis_time); s.append(&self.fork_data); s.append(&self.validator_registry); + s.append(&self.validator_balances); s.append(&self.validator_registry_latest_change_slot); s.append(&self.validator_registry_exit_count); s.append(&self.validator_registry_delta_chain_tip); @@ -89,6 +90,7 @@ impl Decodable for BeaconState { let (genesis_time, i) = <_>::ssz_decode(bytes, i)?; let (fork_data, i) = <_>::ssz_decode(bytes, i)?; let (validator_registry, i) = <_>::ssz_decode(bytes, i)?; + let (validator_balances, i) = <_>::ssz_decode(bytes, i)?; let (validator_registry_latest_change_slot, i) = <_>::ssz_decode(bytes, i)?; let (validator_registry_exit_count, i) = <_>::ssz_decode(bytes, i)?; let (validator_registry_delta_chain_tip, i) = <_>::ssz_decode(bytes, i)?; @@ -114,6 +116,7 @@ impl Decodable for BeaconState { genesis_time, fork_data, validator_registry, + validator_balances, validator_registry_latest_change_slot, validator_registry_exit_count, validator_registry_delta_chain_tip, @@ -145,6 +148,7 @@ impl TestRandom for BeaconState { genesis_time: <_>::random_for_test(rng), fork_data: <_>::random_for_test(rng), validator_registry: <_>::random_for_test(rng), + validator_balances: <_>::random_for_test(rng), validator_registry_latest_change_slot: <_>::random_for_test(rng), validator_registry_exit_count: <_>::random_for_test(rng), validator_registry_delta_chain_tip: <_>::random_for_test(rng), diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs index 9d84bc278..eb26e2b10 100644 --- a/beacon_chain/types/src/deposit.rs +++ b/beacon_chain/types/src/deposit.rs @@ -1,7 +1,8 @@ use super::ssz::{Decodable, DecodeError, Encodable, SszStream}; -use super::{DepositData, Hash256}; +use super::{DepositData, DepositInput, Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; +use bls::{Keypair, create_proof_of_possession}; #[derive(Debug, PartialEq, Clone)] pub struct Deposit { @@ -10,6 +11,29 @@ pub struct Deposit { pub deposit_data: DepositData, } +impl Deposit { + pub fn zero_with_rand_keypair() -> Self{ + let kp = Keypair::random(); + let deposit_input = DepositInput { + pubkey: kp.pk.clone(), + withdrawal_credentials: Hash256::zero(), + randao_commitment: Hash256::zero(), + poc_commitment: Hash256::zero(), + proof_of_possession: create_proof_of_possession(&kp) + }; + let deposit_data = DepositData { + deposit_input: deposit_input, + value: 0, + timestamp: 0 + }; + Self { + merkle_branch: Vec::new(), + merkle_tree_index: 0, + deposit_data: deposit_data + } + } +} + impl Encodable for Deposit { fn ssz_append(&self, s: &mut SszStream) { s.append_vec(&self.merkle_branch); diff --git a/beacon_chain/types/src/fork_data.rs b/beacon_chain/types/src/fork_data.rs index e779c797e..4e797bc34 100644 --- a/beacon_chain/types/src/fork_data.rs +++ b/beacon_chain/types/src/fork_data.rs @@ -2,7 +2,7 @@ use super::ssz::{Decodable, DecodeError, Encodable, SszStream}; use crate::test_utils::TestRandom; use rand::RngCore; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct ForkData { pub pre_fork_version: u64, pub post_fork_version: u64, diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 7df39f931..579509945 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -1,4 +1,4 @@ -use super::bls::PublicKey; +use super::bls::{Keypair, PublicKey}; use super::{Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; @@ -43,6 +43,23 @@ pub struct ValidatorRecord { pub second_last_poc_slot: u64 } +impl ValidatorRecord { + pub fn zero_with_rand_keypair() -> Self { + Self { + pubkey: Keypair::random().pk, + withdrawal_credentials: Hash256::zero(), + randao_commitment: Hash256::zero(), + randao_layers: 0, + status: ValidatorStatus::from(0), + latest_status_change_slot: 0, + exit_count: 0, + poc_commitment: Hash256::zero(), + last_poc_change_slot: 0, + second_last_poc_slot: 0 + } + } +} + impl ValidatorRecord { pub fn status_is(&self, status: ValidatorStatus) -> bool { self.status == status diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index ab40bd8cd..84d7928d0 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -1,4 +1,4 @@ -use bls::{PublicKey, verify_proof_of_possession}; +use bls::{verify_proof_of_possession}; use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus}; use spec::ChainSpec; @@ -84,44 +84,22 @@ fn min_empty_validator_index( mod tests { use super::*; - use bls::{Keypair, Signature, create_proof_of_possession}; - use hashing::canonical_hash; use types::{Hash256, DepositData, DepositInput}; - fn get_deposit() -> Deposit { - let kp = Keypair::random(); - let deposit_input = DepositInput { - pubkey: kp.pk.clone(), - withdrawal_credentials: Hash256::zero(), - randao_commitment: Hash256::zero(), - poc_commitment: Hash256::zero(), - proof_of_possession: create_proof_of_possession(&kp) - }; - let deposit_data = DepositData { - deposit_input: deposit_input, - value: DEPOSIT_GWEI, - timestamp: 0 - }; - Deposit { - merkle_branch: Vec::new(), - merkle_tree_index: 0, - deposit_data: deposit_data - } - } - - fn deposit_equals_record(dep: &Deposit, rec: &ValidatorRecord) -> bool { - (dep.deposit_data.deposit_input.pubkey == rec.pubkey) - & (dep.deposit_data.deposit_input.withdrawal_credentials == rec.withdrawal_credentials) - & (dep.deposit_data.deposit_input.randao_commitment == rec.randao_commitment) - //& (verify_proof_of_possession(®.proof_of_possession, &rec.pubkey)) + fn deposit_equals_record(dep: &Deposit, val: &ValidatorRecord) -> bool { + (dep.deposit_data.deposit_input.pubkey == val.pubkey) + & (dep.deposit_data.deposit_input.withdrawal_credentials == val.withdrawal_credentials) + & (dep.deposit_data.deposit_input.randao_commitment == val.randao_commitment) + & (verify_proof_of_possession(&dep.deposit_data.deposit_input.proof_of_possession, &val.pubkey)) } #[test] fn test_process_deposit_valid_empty_validators() { let mut state = BeaconState::default(); - let deposit = get_deposit(); + let mut deposit = Deposit::zero_with_rand_keypair(); let spec = ChainSpec::foundation(); - + deposit.deposit_data.value = DEPOSIT_GWEI; + let result = process_deposit(&mut state, &deposit, &spec); assert_eq!(result.unwrap(), 0); @@ -136,8 +114,9 @@ mod tests { let spec = ChainSpec::foundation(); for i in 0..5 { - let deposit = get_deposit(); + let mut deposit = Deposit::zero_with_rand_keypair(); let result = process_deposit(&mut state, &deposit, &spec); + deposit.deposit_data.value = DEPOSIT_GWEI; assert_eq!(result.unwrap(), i); assert!(deposit_equals_record(&deposit, &state.validator_registry[i])); assert_eq!(state.validator_registry.len(), i + 1); @@ -150,8 +129,9 @@ mod tests { let mut state = BeaconState::default(); let spec = ChainSpec::foundation(); - let deposit = get_deposit(); - let (mut validator, _) = ValidatorRecord::zero_with_thread_rand_keypair(); + let mut deposit = Deposit::zero_with_rand_keypair(); + let mut validator = ValidatorRecord::zero_with_rand_keypair(); + deposit.deposit_data.value = DEPOSIT_GWEI; validator.pubkey = deposit.deposit_data.deposit_input.pubkey.clone(); validator.withdrawal_credentials = deposit.deposit_data.deposit_input.withdrawal_credentials; validator.randao_commitment = deposit.deposit_data.deposit_input.randao_commitment; @@ -173,11 +153,12 @@ mod tests { let mut state = BeaconState::default(); let spec = ChainSpec::foundation(); - let (mut validator, _) = ValidatorRecord::zero_with_thread_rand_keypair(); + let validator = ValidatorRecord::zero_with_rand_keypair(); state.validator_registry.push(validator); state.validator_balances.push(0); - let deposit = get_deposit(); + let mut deposit = Deposit::zero_with_rand_keypair(); + deposit.deposit_data.value = DEPOSIT_GWEI; state.slot = spec.zero_balance_validator_ttl; let result = process_deposit(&mut state, &deposit, &spec); From c180c6c94fd19443064efbfb1e9bd38eca9e8d4a Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sat, 29 Dec 2018 17:53:48 -0600 Subject: [PATCH 25/35] BaconState cleanup, warnings fixed, and invalid proof of possession induction test added --- beacon_chain/genesis/src/beacon_state.rs | 17 ------------- beacon_chain/spec/src/foundation.rs | 11 ++++++-- beacon_chain/types/src/chain_config.rs | 1 - .../validator_induction/src/inductor.rs | 25 +++++++++++++++---- beacon_chain/validator_induction/src/lib.rs | 2 +- 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index b6a42db5b..9724800f8 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -103,21 +103,4 @@ mod tests { spec.initial_validators.len() ); } - - /* - #[test] - fn test_genesis_bad_validator() { - let mut spec = ChainSpec::foundation(); - - let random_kp = Keypair::random(); - spec.initial_validators[4].proof_of_possession = create_proof_of_possession(&random_kp); - - let state = genesis_beacon_state(&spec).unwrap(); - - assert_eq!( - state.validator_registry.len(), - spec.initial_validators.len() - 1 - ); - } - */ } diff --git a/beacon_chain/spec/src/foundation.rs b/beacon_chain/spec/src/foundation.rs index ba437cef3..930e414e8 100644 --- a/beacon_chain/spec/src/foundation.rs +++ b/beacon_chain/spec/src/foundation.rs @@ -1,8 +1,11 @@ use super::ChainSpec; -use bls::{create_proof_of_possession, Keypair, PublicKey, SecretKey}; +use bls::{Keypair, PublicKey, SecretKey}; use types::{Address, Hash256, ValidatorRecord}; +/// The size of a validators deposit in GWei. +pub const DEPOSIT_GWEI: u64 = 32_000_000_000; + impl ChainSpec { /// Returns a `ChainSpec` compatible with the specification from Ethereum Foundation. /// @@ -64,7 +67,7 @@ impl ChainSpec { * Intialization parameters */ initial_validators: initial_validators_for_testing(), - initial_balances: vec![0,0,0,0], + initial_balances: initial_balances_for_testing(), genesis_time: 1544672897, processed_pow_receipt_root: Hash256::from("pow_root".as_bytes()), } @@ -113,6 +116,10 @@ fn initial_validators_for_testing() -> Vec { initial_validators } +fn initial_balances_for_testing() -> Vec { + vec![DEPOSIT_GWEI; 4] +} + #[cfg(test)] mod tests { use super::*; diff --git a/beacon_chain/types/src/chain_config.rs b/beacon_chain/types/src/chain_config.rs index 51becf519..e48aabe1e 100644 --- a/beacon_chain/types/src/chain_config.rs +++ b/beacon_chain/types/src/chain_config.rs @@ -10,7 +10,6 @@ pub struct ChainConfig { pub max_validator_churn_quotient: u64, pub genesis_time: u64, pub slot_duration_millis: u64, - // TODO: Come back to this pub initial_validators: Vec, // New constants diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 84d7928d0..000cb228b 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -2,9 +2,6 @@ use bls::{verify_proof_of_possession}; use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus}; use spec::ChainSpec; -/// The size of a validators deposit in GWei. -pub const DEPOSIT_GWEI: u64 = 32_000_000_000; - #[derive(Debug, PartialEq, Clone)] pub enum ValidatorInductionError { InvalidShard, @@ -83,8 +80,11 @@ fn min_empty_validator_index( #[cfg(test)] mod tests { use super::*; - - use types::{Hash256, DepositData, DepositInput}; + + use bls::{create_proof_of_possession, Keypair}; + + /// The size of a validators deposit in GWei. + pub const DEPOSIT_GWEI: u64 = 32_000_000_000; fn deposit_equals_record(dep: &Deposit, val: &ValidatorRecord) -> bool { (dep.deposit_data.deposit_input.pubkey == val.pubkey) @@ -169,4 +169,19 @@ mod tests { assert_eq!(state.validator_registry.len(), 1); assert_eq!(state.validator_balances.len(), 1); } + + #[test] + fn test_process_deposit_invalid_proof_of_possession() { + let mut state = BeaconState::default(); + let mut deposit = Deposit::zero_with_rand_keypair(); + let spec = ChainSpec::foundation(); + deposit.deposit_data.value = DEPOSIT_GWEI; + deposit.deposit_data.deposit_input.proof_of_possession = create_proof_of_possession(&Keypair::random()); + + let result = process_deposit(&mut state, &deposit, &spec); + + assert_eq!(result, Err(ValidatorInductionError::InvaidProofOfPossession)); + assert_eq!(state.validator_registry.len(), 0); + assert_eq!(state.validator_balances.len(), 0); + } } diff --git a/beacon_chain/validator_induction/src/lib.rs b/beacon_chain/validator_induction/src/lib.rs index c9f4dc67f..f6dec3cfa 100644 --- a/beacon_chain/validator_induction/src/lib.rs +++ b/beacon_chain/validator_induction/src/lib.rs @@ -5,4 +5,4 @@ extern crate spec; mod inductor; -pub use crate::inductor::{ValidatorInductionError}; +pub use crate::inductor::{ValidatorInductionError, process_deposit}; From a8a36fef74534828db51b0d620a9a17b1df2b85a Mon Sep 17 00:00:00 2001 From: Stan Drozd Date: Mon, 17 Dec 2018 17:36:43 +0100 Subject: [PATCH 26/35] vec_shuffle: Add a keccak testsuite This refreshes the vec shuffle testsuite with a freshly generated keccak-based variety --- beacon_chain/utils/vec_shuffle/src/lib.rs | 36 +++++++-------- .../src/specs/shuffle_test_vectors.yaml | 44 +++++++++---------- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/beacon_chain/utils/vec_shuffle/src/lib.rs b/beacon_chain/utils/vec_shuffle/src/lib.rs index b3d540174..f5c2b7ebd 100644 --- a/beacon_chain/utils/vec_shuffle/src/lib.rs +++ b/beacon_chain/utils/vec_shuffle/src/lib.rs @@ -42,40 +42,40 @@ mod tests { extern crate yaml_rust; use self::yaml_rust::yaml; - use super::hashing::canonical_hash; - use super::*; - use std::fs::File; - use std::io::prelude::*; - // TODO: update test vectors to use keccak instead of blake. - // https://github.com/sigp/lighthouse/issues/121 + use std::{fs::File, io::prelude::*, path::PathBuf}; + + use super::{hashing::canonical_hash, *}; + #[test] - #[should_panic] fn test_shuffling() { - let mut file = File::open("./src/specs/shuffle_test_vectors.yaml").unwrap(); + let mut file = { + let mut file_path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + file_path_buf.push("src/specs/shuffle_test_vectors.yaml"); + + File::open(file_path_buf).unwrap() + }; + let mut yaml_str = String::new(); file.read_to_string(&mut yaml_str).unwrap(); let docs = yaml::YamlLoader::load_from_str(&yaml_str).unwrap(); let doc = &docs[0]; - let test_cases = doc["test_cases"].as_vec(); + let test_cases = doc["test_cases"].as_vec().unwrap(); - for test_case in test_cases.unwrap() { + for test_case in test_cases { let input = test_case["input"].clone().into_vec().unwrap(); let output = test_case["output"].clone().into_vec().unwrap(); let seed_bytes = test_case["seed"].as_str().unwrap().as_bytes(); - let mut seed; - if seed_bytes.len() > 0 { - seed = canonical_hash(seed_bytes); + let seed = if seed_bytes.len() > 0 { + canonical_hash(seed_bytes) } else { - seed = vec![]; - } + vec![] + }; - let mut s = shuffle(&seed, input).unwrap(); - - assert_eq!(s, output); + assert_eq!(shuffle(&seed, input).unwrap(), output); } } } diff --git a/beacon_chain/utils/vec_shuffle/src/specs/shuffle_test_vectors.yaml b/beacon_chain/utils/vec_shuffle/src/specs/shuffle_test_vectors.yaml index c97d6d328..2571f0804 100644 --- a/beacon_chain/utils/vec_shuffle/src/specs/shuffle_test_vectors.yaml +++ b/beacon_chain/utils/vec_shuffle/src/specs/shuffle_test_vectors.yaml @@ -1,5 +1,3 @@ -# This file was generated with sigp/shuffling_sandbox -# python3 sandbox.py test_vectors title: Shuffling Algorithm Tests summary: Test vectors for shuffling a list based upon a seed. test_suite: Shuffling @@ -15,13 +13,13 @@ test_cases: output: [255] seed: '' - input: [4, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [1, 6, 4, 1, 6, 6, 2, 2, 4, 5] + output: [2, 1, 1, 5, 6, 6, 6, 2, 4, 4] seed: '' - input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] - output: [4, 7, 10, 13, 3, 1, 2, 9, 12, 6, 11, 8, 5] + output: [4, 9, 6, 8, 13, 3, 2, 11, 5, 1, 12, 7, 10] seed: '' - input: [65, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [1, 6, 65, 1, 6, 6, 2, 2, 4, 5] + output: [2, 1, 1, 5, 6, 6, 6, 2, 4, 65] seed: '' - input: [] output: [] @@ -33,13 +31,13 @@ test_cases: output: [255] seed: 4kn4driuctg8 - input: [4, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [6, 4, 2, 5, 4, 2, 6, 6, 1, 1] + output: [2, 4, 4, 2, 1, 1, 6, 5, 6, 6] seed: 4kn4driuctg8 - input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] - output: [13, 1, 9, 8, 3, 10, 6, 2, 5, 12, 11, 4, 7] + output: [7, 6, 3, 12, 11, 1, 8, 13, 10, 5, 9, 4, 2] seed: 4kn4driuctg8 - input: [65, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [6, 65, 2, 5, 4, 2, 6, 6, 1, 1] + output: [2, 4, 65, 2, 1, 1, 6, 5, 6, 6] seed: 4kn4driuctg8 - input: [] output: [] @@ -51,13 +49,13 @@ test_cases: output: [255] seed: ytre1p - input: [4, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [6, 2, 5, 1, 6, 4, 1, 2, 4, 6] + output: [6, 1, 1, 5, 6, 2, 6, 2, 4, 4] seed: ytre1p - input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] - output: [3, 8, 10, 4, 7, 11, 6, 1, 2, 5, 13, 9, 12] + output: [6, 2, 3, 4, 8, 5, 12, 9, 7, 11, 10, 1, 13] seed: ytre1p - input: [65, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [6, 2, 5, 1, 6, 4, 1, 2, 65, 6] + output: [6, 1, 1, 5, 6, 2, 6, 2, 4, 65] seed: ytre1p - input: [] output: [] @@ -69,13 +67,13 @@ test_cases: output: [255] seed: mytobcffnkvj - input: [4, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [5, 6, 2, 1, 6, 4, 6, 4, 1, 2] + output: [2, 4, 1, 1, 6, 4, 6, 5, 6, 2] seed: mytobcffnkvj - input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] - output: [12, 4, 11, 6, 13, 10, 9, 2, 3, 7, 8, 1, 5] + output: [11, 5, 9, 7, 2, 4, 12, 10, 8, 1, 6, 3, 13] seed: mytobcffnkvj - input: [65, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [5, 6, 2, 1, 6, 65, 6, 4, 1, 2] + output: [2, 65, 1, 1, 6, 4, 6, 5, 6, 2] seed: mytobcffnkvj - input: [] output: [] @@ -87,13 +85,13 @@ test_cases: output: [255] seed: myzu3g7evxp5nkvj - input: [4, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [6, 2, 6, 5, 4, 4, 1, 6, 2, 1] + output: [6, 2, 1, 4, 2, 6, 5, 6, 4, 1] seed: myzu3g7evxp5nkvj - input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] - output: [10, 12, 13, 3, 7, 11, 2, 4, 9, 8, 6, 5, 1] + output: [2, 1, 11, 3, 9, 7, 8, 13, 4, 10, 5, 6, 12] seed: myzu3g7evxp5nkvj - input: [65, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [6, 2, 6, 5, 65, 4, 1, 6, 2, 1] + output: [6, 2, 1, 4, 2, 6, 5, 6, 65, 1] seed: myzu3g7evxp5nkvj - input: [] output: [] @@ -105,13 +103,13 @@ test_cases: output: [255] seed: xdpli1jsx5xb - input: [4, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [6, 2, 4, 1, 2, 6, 5, 1, 6, 4] + output: [2, 1, 2, 4, 6, 6, 5, 6, 1, 4] seed: xdpli1jsx5xb - input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] - output: [11, 8, 12, 9, 2, 1, 10, 4, 13, 5, 7, 3, 6] + output: [5, 8, 12, 9, 11, 4, 7, 13, 1, 3, 2, 10, 6] seed: xdpli1jsx5xb - input: [65, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [6, 2, 65, 1, 2, 6, 5, 1, 6, 4] + output: [2, 1, 2, 65, 6, 6, 5, 6, 1, 4] seed: xdpli1jsx5xb - input: [] output: [] @@ -123,11 +121,11 @@ test_cases: output: [255] seed: oab3mbb3xe8qsx5xb - input: [4, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [2, 5, 1, 6, 1, 2, 6, 6, 4, 4] + output: [6, 2, 1, 1, 6, 2, 4, 4, 6, 5] seed: oab3mbb3xe8qsx5xb - input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] - output: [5, 13, 9, 7, 11, 10, 12, 2, 6, 8, 3, 1, 4] + output: [1, 8, 5, 13, 2, 10, 7, 11, 12, 6, 3, 4, 9] seed: oab3mbb3xe8qsx5xb - input: [65, 6, 2, 6, 1, 4, 6, 2, 1, 5] - output: [2, 5, 1, 6, 1, 2, 6, 6, 65, 4] + output: [6, 2, 1, 1, 6, 2, 4, 65, 6, 5] seed: oab3mbb3xe8qsx5xb From ec20493f01f8df332acaae5b7f60f8e203a8e04a Mon Sep 17 00:00:00 2001 From: Stan Drozd Date: Fri, 28 Dec 2018 09:28:11 +0100 Subject: [PATCH 27/35] types: delete ChainConfig --- beacon_chain/types/src/chain_config.rs | 72 -------------------------- beacon_chain/types/src/lib.rs | 2 - 2 files changed, 74 deletions(-) delete mode 100644 beacon_chain/types/src/chain_config.rs diff --git a/beacon_chain/types/src/chain_config.rs b/beacon_chain/types/src/chain_config.rs deleted file mode 100644 index 8428945bb..000000000 --- a/beacon_chain/types/src/chain_config.rs +++ /dev/null @@ -1,72 +0,0 @@ -use super::ValidatorRegistration; - -#[derive(Debug, Clone, PartialEq)] -pub struct ChainConfig { - // Old, potentially outdated constants - pub cycle_length: u8, - pub deposit_size_gwei: u64, - pub shard_count: u16, - pub min_committee_size: u64, - pub max_validator_churn_quotient: u64, - pub genesis_time: u64, - pub slot_duration_millis: u64, - pub initial_validators: Vec, - - // New constants - pub epoch_length: u64, - pub min_attestation_inclusion_delay: u64, -} - -/* - * Presently this is just some arbitrary time in Sept 2018. - */ -const TEST_GENESIS_TIME: u64 = 1_537_488_655; - -impl ChainConfig { - pub fn standard() -> Self { - Self { - cycle_length: 64, - deposit_size_gwei: 32 * (10 ^ 9), - shard_count: 1024, - min_committee_size: 128, - max_validator_churn_quotient: 32, - genesis_time: TEST_GENESIS_TIME, - slot_duration_millis: 16 * 1000, - initial_validators: vec![], - - // New - epoch_length: 64, - min_attestation_inclusion_delay: 4, - } - } - - pub fn validate(&self) -> bool { - // criteria that ensure the config is valid - - // shard_count / cycle_length > 0 otherwise validator delegation - // will fail. - if self.shard_count / u16::from(self.cycle_length) == 0 { - return false; - } - - true - } - - #[cfg(test)] - pub fn super_fast_tests() -> Self { - Self { - cycle_length: 2, - deposit_size_gwei: 32 * (10 ^ 9), - shard_count: 2, - min_committee_size: 2, - max_validator_churn_quotient: 32, - genesis_time: TEST_GENESIS_TIME, // arbitrary - slot_duration_millis: 16 * 1000, - initial_validators: vec![], - - // New constants - epoch_length: 64, - min_attestation_inclusion_delay: 4, - } - } -} diff --git a/beacon_chain/types/src/lib.rs b/beacon_chain/types/src/lib.rs index 08b871c1c..b1bc03c31 100644 --- a/beacon_chain/types/src/lib.rs +++ b/beacon_chain/types/src/lib.rs @@ -13,7 +13,6 @@ pub mod beacon_block_body; pub mod beacon_state; pub mod candidate_pow_receipt_root_record; pub mod casper_slashing; -pub mod chain_config; pub mod crosslink_record; pub mod crystallized_state; pub mod deposit; @@ -41,7 +40,6 @@ pub use crate::beacon_block::BeaconBlock; pub use crate::beacon_block_body::BeaconBlockBody; pub use crate::beacon_state::BeaconState; pub use crate::casper_slashing::CasperSlashing; -pub use crate::chain_config::ChainConfig; pub use crate::crosslink_record::CrosslinkRecord; pub use crate::crystallized_state::CrystallizedState; pub use crate::deposit::Deposit; From 9d57f72c9a7bd7fc46376e89a3e0efd82a6b1bb2 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Fri, 4 Jan 2019 20:32:10 -0600 Subject: [PATCH 28/35] removed chanin_config.rs --- beacon_chain/types/src/chain_config.rs | 72 -------------------------- 1 file changed, 72 deletions(-) delete mode 100644 beacon_chain/types/src/chain_config.rs diff --git a/beacon_chain/types/src/chain_config.rs b/beacon_chain/types/src/chain_config.rs deleted file mode 100644 index e48aabe1e..000000000 --- a/beacon_chain/types/src/chain_config.rs +++ /dev/null @@ -1,72 +0,0 @@ -use super::ValidatorRecord; - -#[derive(Debug, Clone, PartialEq)] -pub struct ChainConfig { - // Old, potentially outdated constants - pub cycle_length: u8, - pub deposit_size_gwei: u64, - pub shard_count: u16, - pub min_committee_size: u64, - pub max_validator_churn_quotient: u64, - pub genesis_time: u64, - pub slot_duration_millis: u64, - pub initial_validators: Vec, - - // New constants - pub epoch_length: u64, - pub min_attestation_inclusion_delay: u64, -} - -/* - * Presently this is just some arbitrary time in Sept 2018. - */ -const TEST_GENESIS_TIME: u64 = 1_537_488_655; - -impl ChainConfig { - pub fn standard() -> Self { - Self { - cycle_length: 64, - deposit_size_gwei: 32 * (10 ^ 9), - shard_count: 1024, - min_committee_size: 128, - max_validator_churn_quotient: 32, - genesis_time: TEST_GENESIS_TIME, - slot_duration_millis: 16 * 1000, - initial_validators: vec![], - - // New - epoch_length: 64, - min_attestation_inclusion_delay: 4, - } - } - - pub fn validate(&self) -> bool { - // criteria that ensure the config is valid - - // shard_count / cycle_length > 0 otherwise validator delegation - // will fail. - if self.shard_count / u16::from(self.cycle_length) == 0 { - return false; - } - - true - } - - #[cfg(test)] - pub fn super_fast_tests() -> Self { - Self { - cycle_length: 2, - deposit_size_gwei: 32 * (10 ^ 9), - shard_count: 2, - min_committee_size: 2, - max_validator_churn_quotient: 32, - genesis_time: TEST_GENESIS_TIME, // arbitrary - slot_duration_millis: 16 * 1000, - initial_validators: vec![], - - // New constants - epoch_length: 64, - min_attestation_inclusion_delay: 4, - } - } -} From 246584f1a557cc3a59b8e9300d488f99cc816a1c Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Fri, 4 Jan 2019 20:46:25 -0600 Subject: [PATCH 29/35] TODO added for deposit signature validation --- beacon_chain/validator_induction/src/inductor.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 000cb228b..2f69f6f01 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -17,6 +17,7 @@ pub fn process_deposit( let deposit_input = &deposit.deposit_data.deposit_input; let deposit_data = &deposit.deposit_data; + // TODO: Update the signature validation as defined in the spec once issues #91 and #70 are completed if !verify_proof_of_possession(&deposit_input.proof_of_possession, &deposit_input.pubkey) { return Err(ValidatorInductionError::InvaidProofOfPossession); } From c1474976fece25aa61105de42b9e48b32567865b Mon Sep 17 00:00:00 2001 From: Stan Drozd Date: Sun, 6 Jan 2019 11:11:19 +0100 Subject: [PATCH 30/35] types: remove [Active|Crystallized]State --- beacon_chain/types/src/active_state.rs | 17 ------------ beacon_chain/types/src/crystallized_state.rs | 28 -------------------- beacon_chain/types/src/lib.rs | 4 --- 3 files changed, 49 deletions(-) delete mode 100644 beacon_chain/types/src/active_state.rs delete mode 100644 beacon_chain/types/src/crystallized_state.rs diff --git a/beacon_chain/types/src/active_state.rs b/beacon_chain/types/src/active_state.rs deleted file mode 100644 index 5661fd60e..000000000 --- a/beacon_chain/types/src/active_state.rs +++ /dev/null @@ -1,17 +0,0 @@ -use super::Hash256; -use super::{Attestation, SpecialRecord}; - -#[derive(Debug, PartialEq)] -pub struct ActiveState { - pub pending_attestations: Vec, - pub pending_specials: Vec, - pub recent_block_hashes: Vec, - pub randao_mix: Hash256, -} - -impl ActiveState { - // TODO: implement this. - pub fn canonical_root(&self) -> Hash256 { - Hash256::zero() - } -} diff --git a/beacon_chain/types/src/crystallized_state.rs b/beacon_chain/types/src/crystallized_state.rs deleted file mode 100644 index 454454a17..000000000 --- a/beacon_chain/types/src/crystallized_state.rs +++ /dev/null @@ -1,28 +0,0 @@ -use super::crosslink_record::CrosslinkRecord; -use super::shard_committee::ShardCommittee; -use super::validator_record::ValidatorRecord; -use super::Hash256; - -#[derive(Debug, PartialEq)] -pub struct CrystallizedState { - pub validator_set_change_slot: u64, - pub validators: Vec, - pub crosslinks: Vec, - pub last_state_recalculation_slot: u64, - pub last_finalized_slot: u64, - pub last_justified_slot: u64, - pub justified_streak: u64, - pub shard_and_committee_for_slots: Vec>, - pub deposits_penalized_in_period: Vec, - pub validator_set_delta_hash_chain: Hash256, - pub pre_fork_version: u32, - pub post_fork_version: u32, - pub fork_slot_number: u32, -} - -impl CrystallizedState { - // TODO: implement this. - pub fn canonical_root(&self) -> Hash256 { - Hash256::zero() - } -} diff --git a/beacon_chain/types/src/lib.rs b/beacon_chain/types/src/lib.rs index b1bc03c31..3ffa9ff2d 100644 --- a/beacon_chain/types/src/lib.rs +++ b/beacon_chain/types/src/lib.rs @@ -5,7 +5,6 @@ extern crate ssz; pub mod test_utils; -pub mod active_state; pub mod attestation_data; pub mod attestation; pub mod beacon_block; @@ -14,7 +13,6 @@ pub mod beacon_state; pub mod candidate_pow_receipt_root_record; pub mod casper_slashing; pub mod crosslink_record; -pub mod crystallized_state; pub mod deposit; pub mod deposit_data; pub mod deposit_input; @@ -33,7 +31,6 @@ pub mod validator_registration; use self::ethereum_types::{H160, H256, U256}; use std::collections::HashMap; -pub use crate::active_state::ActiveState; pub use crate::attestation_data::AttestationData; pub use crate::attestation::Attestation; pub use crate::beacon_block::BeaconBlock; @@ -41,7 +38,6 @@ pub use crate::beacon_block_body::BeaconBlockBody; pub use crate::beacon_state::BeaconState; pub use crate::casper_slashing::CasperSlashing; pub use crate::crosslink_record::CrosslinkRecord; -pub use crate::crystallized_state::CrystallizedState; pub use crate::deposit::Deposit; pub use crate::deposit_data::DepositData; pub use crate::deposit_input::DepositInput; From c00dc0a96b197f6d42d06bb78085f675a6c6aa4f Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Mon, 7 Jan 2019 19:35:30 -0600 Subject: [PATCH 31/35] removed crystallized state ref --- beacon_chain/genesis/src/beacon_state.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index 9724800f8..f9c2ef327 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -12,8 +12,6 @@ pub enum Error { pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { /* * Assign the validators to shards, using all zeros as the seed. - * - * Crystallizedstate stores two cycles, so we simply repeat the same assignment twice. */ let _shard_and_committee_for_slots = { let mut a = shard_and_committees_for_cycle(&[0; 32], &spec.initial_validators, 0, &spec)?; From a3ef9231a64fc089843d338b7febf2161a289b96 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Mon, 7 Jan 2019 21:37:30 -0600 Subject: [PATCH 32/35] inductor tests use deposit::random_for_test --- beacon_chain/types/src/deposit.rs | 23 ------------------- .../validator_induction/src/inductor.rs | 21 +++++++++++++---- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs index eb26e2b10..27b8a66cc 100644 --- a/beacon_chain/types/src/deposit.rs +++ b/beacon_chain/types/src/deposit.rs @@ -11,29 +11,6 @@ pub struct Deposit { pub deposit_data: DepositData, } -impl Deposit { - pub fn zero_with_rand_keypair() -> Self{ - let kp = Keypair::random(); - let deposit_input = DepositInput { - pubkey: kp.pk.clone(), - withdrawal_credentials: Hash256::zero(), - randao_commitment: Hash256::zero(), - poc_commitment: Hash256::zero(), - proof_of_possession: create_proof_of_possession(&kp) - }; - let deposit_data = DepositData { - deposit_input: deposit_input, - value: 0, - timestamp: 0 - }; - Self { - merkle_branch: Vec::new(), - merkle_tree_index: 0, - deposit_data: deposit_data - } - } -} - impl Encodable for Deposit { fn ssz_append(&self, s: &mut SszStream) { s.append_vec(&self.merkle_branch); diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 2f69f6f01..159986666 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -81,12 +81,23 @@ fn min_empty_validator_index( #[cfg(test)] mod tests { use super::*; + use types::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use bls::{create_proof_of_possession, Keypair}; /// The size of a validators deposit in GWei. pub const DEPOSIT_GWEI: u64 = 32_000_000_000; + fn get_deposit() -> Deposit { + let mut rng = XorShiftRng::from_seed([42; 16]); + let mut deposit = Deposit::random_for_test(&mut rng); + + let kp = Keypair::random(); + deposit.deposit_data.deposit_input.pubkey = kp.pk.clone(); + deposit.deposit_data.deposit_input.proof_of_possession = create_proof_of_possession(&kp); + deposit + } + fn deposit_equals_record(dep: &Deposit, val: &ValidatorRecord) -> bool { (dep.deposit_data.deposit_input.pubkey == val.pubkey) & (dep.deposit_data.deposit_input.withdrawal_credentials == val.withdrawal_credentials) @@ -97,7 +108,7 @@ mod tests { #[test] fn test_process_deposit_valid_empty_validators() { let mut state = BeaconState::default(); - let mut deposit = Deposit::zero_with_rand_keypair(); + let mut deposit = get_deposit(); let spec = ChainSpec::foundation(); deposit.deposit_data.value = DEPOSIT_GWEI; @@ -115,7 +126,7 @@ mod tests { let spec = ChainSpec::foundation(); for i in 0..5 { - let mut deposit = Deposit::zero_with_rand_keypair(); + let mut deposit = get_deposit(); let result = process_deposit(&mut state, &deposit, &spec); deposit.deposit_data.value = DEPOSIT_GWEI; assert_eq!(result.unwrap(), i); @@ -130,7 +141,7 @@ mod tests { let mut state = BeaconState::default(); let spec = ChainSpec::foundation(); - let mut deposit = Deposit::zero_with_rand_keypair(); + let mut deposit = get_deposit(); let mut validator = ValidatorRecord::zero_with_rand_keypair(); deposit.deposit_data.value = DEPOSIT_GWEI; validator.pubkey = deposit.deposit_data.deposit_input.pubkey.clone(); @@ -158,7 +169,7 @@ mod tests { state.validator_registry.push(validator); state.validator_balances.push(0); - let mut deposit = Deposit::zero_with_rand_keypair(); + let mut deposit = get_deposit(); deposit.deposit_data.value = DEPOSIT_GWEI; state.slot = spec.zero_balance_validator_ttl; @@ -174,7 +185,7 @@ mod tests { #[test] fn test_process_deposit_invalid_proof_of_possession() { let mut state = BeaconState::default(); - let mut deposit = Deposit::zero_with_rand_keypair(); + let mut deposit = get_deposit(); let spec = ChainSpec::foundation(); deposit.deposit_data.value = DEPOSIT_GWEI; deposit.deposit_data.deposit_input.proof_of_possession = create_proof_of_possession(&Keypair::random()); From d2a62fa2110216e75b92437d39fa4309eafcfc74 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Mon, 7 Jan 2019 21:49:57 -0600 Subject: [PATCH 33/35] removed ValidatorRecord::zero_with_rand_keypair() --- beacon_chain/types/src/validator_record.rs | 17 ----------------- .../validator_induction/src/inductor.rs | 11 +++++++++-- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 579509945..c1eb9e605 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -43,23 +43,6 @@ pub struct ValidatorRecord { pub second_last_poc_slot: u64 } -impl ValidatorRecord { - pub fn zero_with_rand_keypair() -> Self { - Self { - pubkey: Keypair::random().pk, - withdrawal_credentials: Hash256::zero(), - randao_commitment: Hash256::zero(), - randao_layers: 0, - status: ValidatorStatus::from(0), - latest_status_change_slot: 0, - exit_count: 0, - poc_commitment: Hash256::zero(), - last_poc_change_slot: 0, - second_last_poc_slot: 0 - } - } -} - impl ValidatorRecord { pub fn status_is(&self, status: ValidatorStatus) -> bool { self.status == status diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 159986666..856bb5e71 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -98,6 +98,11 @@ mod tests { deposit } + fn get_validator() -> ValidatorRecord { + let mut rng = XorShiftRng::from_seed([42; 16]); + ValidatorRecord::random_for_test(&mut rng) + } + fn deposit_equals_record(dep: &Deposit, val: &ValidatorRecord) -> bool { (dep.deposit_data.deposit_input.pubkey == val.pubkey) & (dep.deposit_data.deposit_input.withdrawal_credentials == val.withdrawal_credentials) @@ -142,7 +147,8 @@ mod tests { let spec = ChainSpec::foundation(); let mut deposit = get_deposit(); - let mut validator = ValidatorRecord::zero_with_rand_keypair(); + let mut validator = get_validator(); + deposit.deposit_data.value = DEPOSIT_GWEI; validator.pubkey = deposit.deposit_data.deposit_input.pubkey.clone(); validator.withdrawal_credentials = deposit.deposit_data.deposit_input.withdrawal_credentials; @@ -165,7 +171,8 @@ mod tests { let mut state = BeaconState::default(); let spec = ChainSpec::foundation(); - let validator = ValidatorRecord::zero_with_rand_keypair(); + let mut validator = get_validator(); + validator.latest_status_change_slot = 0; state.validator_registry.push(validator); state.validator_balances.push(0); From 0a1c29920611d6f0e57715d24cea6a519d3aeb8b Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Mon, 7 Jan 2019 21:57:35 -0600 Subject: [PATCH 34/35] custody names refactored --- beacon_chain/spec/src/foundation.rs | 6 ++-- beacon_chain/types/src/deposit.rs | 3 +- beacon_chain/types/src/deposit_input.rs | 10 +++--- beacon_chain/types/src/validator_record.rs | 32 +++++++++---------- .../validator_induction/src/inductor.rs | 6 ++-- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/beacon_chain/spec/src/foundation.rs b/beacon_chain/spec/src/foundation.rs index 930e414e8..c69c57e6d 100644 --- a/beacon_chain/spec/src/foundation.rs +++ b/beacon_chain/spec/src/foundation.rs @@ -106,9 +106,9 @@ fn initial_validators_for_testing() -> Vec { status: From::from(0), latest_status_change_slot: 0, exit_count: 0, - poc_commitment: Hash256::zero(), - last_poc_change_slot: 0, - second_last_poc_slot: 0 + custody_commitment: Hash256::zero(), + latest_custody_reseed_slot: 0, + penultimate_custody_reseed_slot: 0 }; initial_validators.push(validator_record); } diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs index 27b8a66cc..9d84bc278 100644 --- a/beacon_chain/types/src/deposit.rs +++ b/beacon_chain/types/src/deposit.rs @@ -1,8 +1,7 @@ use super::ssz::{Decodable, DecodeError, Encodable, SszStream}; -use super::{DepositData, DepositInput, Hash256}; +use super::{DepositData, Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; -use bls::{Keypair, create_proof_of_possession}; #[derive(Debug, PartialEq, Clone)] pub struct Deposit { diff --git a/beacon_chain/types/src/deposit_input.rs b/beacon_chain/types/src/deposit_input.rs index 2808e98bc..636da0d35 100644 --- a/beacon_chain/types/src/deposit_input.rs +++ b/beacon_chain/types/src/deposit_input.rs @@ -9,7 +9,7 @@ pub struct DepositInput { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, pub randao_commitment: Hash256, - pub poc_commitment: Hash256, + pub custody_commitment: Hash256, pub proof_of_possession: Signature, } @@ -18,7 +18,7 @@ impl Encodable for DepositInput { s.append(&self.pubkey); s.append(&self.withdrawal_credentials); s.append(&self.randao_commitment); - s.append(&self.poc_commitment); + s.append(&self.custody_commitment); s.append(&self.proof_of_possession); } } @@ -28,7 +28,7 @@ impl Decodable for DepositInput { let (pubkey, i) = <_>::ssz_decode(bytes, i)?; let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?; let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?; - let (poc_commitment, i) = <_>::ssz_decode(bytes, i)?; + let (custody_commitment, i) = <_>::ssz_decode(bytes, i)?; let (proof_of_possession, i) = <_>::ssz_decode(bytes, i)?; Ok(( @@ -36,7 +36,7 @@ impl Decodable for DepositInput { pubkey, withdrawal_credentials, randao_commitment, - poc_commitment, + custody_commitment, proof_of_possession, }, i, @@ -50,7 +50,7 @@ impl TestRandom for DepositInput { pubkey: <_>::random_for_test(rng), withdrawal_credentials: <_>::random_for_test(rng), randao_commitment: <_>::random_for_test(rng), - poc_commitment: <_>::random_for_test(rng), + custody_commitment: <_>::random_for_test(rng), proof_of_possession: <_>::random_for_test(rng), } } diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index c1eb9e605..fcc93fb07 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -1,4 +1,4 @@ -use super::bls::{Keypair, PublicKey}; +use super::bls::PublicKey; use super::{Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; @@ -38,9 +38,9 @@ pub struct ValidatorRecord { pub status: ValidatorStatus, pub latest_status_change_slot: u64, pub exit_count: u64, - pub poc_commitment: Hash256, - pub last_poc_change_slot: u64, - pub second_last_poc_slot: u64 + pub custody_commitment: Hash256, + pub latest_custody_reseed_slot: u64, + pub penultimate_custody_reseed_slot: u64 } impl ValidatorRecord { @@ -102,9 +102,9 @@ impl Encodable for ValidatorRecord { s.append(&self.status); s.append(&self.latest_status_change_slot); s.append(&self.exit_count); - s.append(&self.poc_commitment); - s.append(&self.last_poc_change_slot); - s.append(&self.second_last_poc_slot); + s.append(&self.custody_commitment); + s.append(&self.latest_custody_reseed_slot); + s.append(&self.penultimate_custody_reseed_slot); } } @@ -117,9 +117,9 @@ impl Decodable for ValidatorRecord { let (status, i) = <_>::ssz_decode(bytes, i)?; let (latest_status_change_slot, i) = <_>::ssz_decode(bytes, i)?; let (exit_count, i) = <_>::ssz_decode(bytes, i)?; - let (poc_commitment, i) = <_>::ssz_decode(bytes, i)?; - let (last_poc_change_slot, i) = <_>::ssz_decode(bytes, i)?; - let (second_last_poc_slot, i) = <_>::ssz_decode(bytes, i)?; + let (custody_commitment, i) = <_>::ssz_decode(bytes, i)?; + let (latest_custody_reseed_slot, i) = <_>::ssz_decode(bytes, i)?; + let (penultimate_custody_reseed_slot, i) = <_>::ssz_decode(bytes, i)?; Ok(( Self { @@ -130,9 +130,9 @@ impl Decodable for ValidatorRecord { status, latest_status_change_slot, exit_count, - poc_commitment, - last_poc_change_slot, - second_last_poc_slot + custody_commitment, + latest_custody_reseed_slot, + penultimate_custody_reseed_slot }, i, )) @@ -149,9 +149,9 @@ impl TestRandom for ValidatorRecord { status: <_>::random_for_test(rng), latest_status_change_slot: <_>::random_for_test(rng), exit_count: <_>::random_for_test(rng), - poc_commitment: <_>::random_for_test(rng), - last_poc_change_slot: <_>::random_for_test(rng), - second_last_poc_slot: <_>::random_for_test(rng), + custody_commitment: <_>::random_for_test(rng), + latest_custody_reseed_slot: <_>::random_for_test(rng), + penultimate_custody_reseed_slot: <_>::random_for_test(rng), } } } diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 856bb5e71..a9b10a0f0 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -43,9 +43,9 @@ pub fn process_deposit( status: ValidatorStatus::PendingActivation, latest_status_change_slot: state.validator_registry_latest_change_slot, exit_count: 0, - poc_commitment: deposit_input.poc_commitment, - last_poc_change_slot: 0, - second_last_poc_slot: 0 + custody_commitment: deposit_input.custody_commitment, + latest_custody_reseed_slot: 0, + penultimate_custody_reseed_slot: 0 }; match min_empty_validator_index(state, spec) { From cdb8e602e8a4d419c1f715a8ca079a24f3ef3cc4 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Mon, 7 Jan 2019 22:16:15 -0600 Subject: [PATCH 35/35] bad whitespace removed --- beacon_chain/spec/src/foundation.rs | 2 +- .../validator_induction/src/inductor.rs | 68 +++++++++---------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/beacon_chain/spec/src/foundation.rs b/beacon_chain/spec/src/foundation.rs index c69c57e6d..a07077364 100644 --- a/beacon_chain/spec/src/foundation.rs +++ b/beacon_chain/spec/src/foundation.rs @@ -99,7 +99,7 @@ fn initial_validators_for_testing() -> Vec { } }; let validator_record = ValidatorRecord { - pubkey: keypair.pk.clone(), + pubkey: keypair.pk.clone(), withdrawal_credentials: Hash256::zero(), randao_commitment: Hash256::zero(), randao_layers: 0, diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index a9b10a0f0..720e38fa4 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -12,29 +12,29 @@ pub enum ValidatorInductionError { pub fn process_deposit( state: &mut BeaconState, deposit: &Deposit, - spec: &ChainSpec) + spec: &ChainSpec) -> Result { let deposit_input = &deposit.deposit_data.deposit_input; let deposit_data = &deposit.deposit_data; - - // TODO: Update the signature validation as defined in the spec once issues #91 and #70 are completed + + // TODO: Update the signature validation as defined in the spec once issues #91 and #70 are completed if !verify_proof_of_possession(&deposit_input.proof_of_possession, &deposit_input.pubkey) { return Err(ValidatorInductionError::InvaidProofOfPossession); } - + let validator_index = state.validator_registry.iter() .position(|validator| validator.pubkey == deposit_input.pubkey); - + match validator_index { Some(i) => { if state.validator_registry[i].withdrawal_credentials == deposit_input.withdrawal_credentials { state.validator_balances[i] += deposit_data.value; return Ok(i); } - + Err(ValidatorInductionError::InvalidWithdrawalCredentials) }, - None => { + None => { let validator = ValidatorRecord { pubkey: deposit_input.pubkey.clone(), withdrawal_credentials: deposit_input.withdrawal_credentials, @@ -47,7 +47,7 @@ pub fn process_deposit( latest_custody_reseed_slot: 0, penultimate_custody_reseed_slot: 0 }; - + match min_empty_validator_index(state, spec) { Some(i) => { state.validator_registry[i] = validator; @@ -67,10 +67,10 @@ pub fn process_deposit( fn min_empty_validator_index( state: &BeaconState, spec: &ChainSpec -) -> Option { +) -> Option { for i in 0..state.validator_registry.len() { - if state.validator_balances[i] == 0 - && state.validator_registry[i].latest_status_change_slot + if state.validator_balances[i] == 0 + && state.validator_registry[i].latest_status_change_slot + spec.zero_balance_validator_ttl <= state.slot { return Some(i); } @@ -82,27 +82,27 @@ fn min_empty_validator_index( mod tests { use super::*; use types::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - + use bls::{create_proof_of_possession, Keypair}; - + /// The size of a validators deposit in GWei. pub const DEPOSIT_GWEI: u64 = 32_000_000_000; - + fn get_deposit() -> Deposit { let mut rng = XorShiftRng::from_seed([42; 16]); let mut deposit = Deposit::random_for_test(&mut rng); - + let kp = Keypair::random(); deposit.deposit_data.deposit_input.pubkey = kp.pk.clone(); deposit.deposit_data.deposit_input.proof_of_possession = create_proof_of_possession(&kp); deposit } - - fn get_validator() -> ValidatorRecord { + + fn get_validator() -> ValidatorRecord { let mut rng = XorShiftRng::from_seed([42; 16]); ValidatorRecord::random_for_test(&mut rng) } - + fn deposit_equals_record(dep: &Deposit, val: &ValidatorRecord) -> bool { (dep.deposit_data.deposit_input.pubkey == val.pubkey) & (dep.deposit_data.deposit_input.withdrawal_credentials == val.withdrawal_credentials) @@ -116,7 +116,7 @@ mod tests { let mut deposit = get_deposit(); let spec = ChainSpec::foundation(); deposit.deposit_data.value = DEPOSIT_GWEI; - + let result = process_deposit(&mut state, &deposit, &spec); assert_eq!(result.unwrap(), 0); @@ -140,63 +140,63 @@ mod tests { assert_eq!(state.validator_balances.len(), i + 1); } } - + #[test] - fn test_process_deposit_top_out() { + fn test_process_deposit_top_out() { let mut state = BeaconState::default(); let spec = ChainSpec::foundation(); - + let mut deposit = get_deposit(); let mut validator = get_validator(); - + deposit.deposit_data.value = DEPOSIT_GWEI; validator.pubkey = deposit.deposit_data.deposit_input.pubkey.clone(); validator.withdrawal_credentials = deposit.deposit_data.deposit_input.withdrawal_credentials; validator.randao_commitment = deposit.deposit_data.deposit_input.randao_commitment; - + state.validator_registry.push(validator); state.validator_balances.push(DEPOSIT_GWEI); - + let result = process_deposit(&mut state, &deposit, &spec); - + assert_eq!(result.unwrap(), 0); assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); assert_eq!(state.validator_balances[0], DEPOSIT_GWEI * 2); assert_eq!(state.validator_registry.len(), 1); assert_eq!(state.validator_balances.len(), 1); } - + #[test] fn test_process_deposit_replace_validator() { let mut state = BeaconState::default(); let spec = ChainSpec::foundation(); - + let mut validator = get_validator(); validator.latest_status_change_slot = 0; state.validator_registry.push(validator); state.validator_balances.push(0); - + let mut deposit = get_deposit(); deposit.deposit_data.value = DEPOSIT_GWEI; state.slot = spec.zero_balance_validator_ttl; - + let result = process_deposit(&mut state, &deposit, &spec); - + assert_eq!(result.unwrap(), 0); assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); assert_eq!(state.validator_balances[0], DEPOSIT_GWEI); assert_eq!(state.validator_registry.len(), 1); assert_eq!(state.validator_balances.len(), 1); } - + #[test] - fn test_process_deposit_invalid_proof_of_possession() { + fn test_process_deposit_invalid_proof_of_possession() { let mut state = BeaconState::default(); let mut deposit = get_deposit(); let spec = ChainSpec::foundation(); deposit.deposit_data.value = DEPOSIT_GWEI; deposit.deposit_data.deposit_input.proof_of_possession = create_proof_of_possession(&Keypair::random()); - + let result = process_deposit(&mut state, &deposit, &spec); assert_eq!(result, Err(ValidatorInductionError::InvaidProofOfPossession));