diff --git a/beacon_chain/types/src/lib.rs b/beacon_chain/types/src/lib.rs index a00e904e9..887a2b1ce 100644 --- a/beacon_chain/types/src/lib.rs +++ b/beacon_chain/types/src/lib.rs @@ -12,6 +12,7 @@ pub mod crosslink_record; pub mod shard_and_committee; pub mod special_record; pub mod validator_record; +pub mod validator_registration; use self::ethereum_types::{ H256, @@ -30,6 +31,7 @@ pub use crosslink_record::CrosslinkRecord; 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/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 9b3027ed2..75a32c2b5 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -1,10 +1,10 @@ use types::{ ValidatorRecord, ValidatorStatus, + ValidatorRegistration, }; use super::proof_of_possession::verify_proof_of_possession; -use super::registration::ValidatorRegistration; /// The size of a validators deposit in GWei. pub const DEPOSIT_GWEI: u64 = 32_000_000_000; @@ -130,6 +130,16 @@ mod tests { }; use hashing::proof_of_possession_hash; + 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()); @@ -159,7 +169,7 @@ mod tests { let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 0); - assert_eq!(r, validators[0]); + assert!(registration_equals_record(&r, &validators[0])); assert_eq!(validators.len(), 1); } @@ -179,7 +189,7 @@ mod tests { let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 5); - assert_eq!(r, validators[5]); + assert!(registration_equals_record(&r, &validators[5])); assert_eq!(validators.len(), 6); } @@ -202,7 +212,7 @@ mod tests { let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 1); - assert_eq!(r, validators[1]); + assert!(registration_equals_record(&r, &validators[1])); assert_eq!(validators.len(), 5); } @@ -223,18 +233,17 @@ mod tests { let result = inductor.induct(&r); let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 0); - assert_eq!(r, validators[0]); - assert_eq!(validators.len(), 5); + 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); + let result = inductor.induct(&r_two); let validators = inductor.to_vec(); assert_eq!(result.unwrap(), 1); - assert_eq!(r_two, validators[1]); + assert!(registration_equals_record(&r_two, &validators[1])); assert_eq!(validators.len(), 5); } diff --git a/beacon_chain/validator_induction/src/lib.rs b/beacon_chain/validator_induction/src/lib.rs index 6c5bf11a9..f35548480 100644 --- a/beacon_chain/validator_induction/src/lib.rs +++ b/beacon_chain/validator_induction/src/lib.rs @@ -4,9 +4,12 @@ extern crate types; mod inductor; mod proof_of_possession; -mod registration; pub use inductor::{ ValidatorInductor, ValidatorInductionError, }; + +pub use proof_of_possession::{ + create_proof_of_possession, +}; diff --git a/beacon_chain/validator_induction/src/proof_of_possession.rs b/beacon_chain/validator_induction/src/proof_of_possession.rs index 85719c014..391625e85 100644 --- a/beacon_chain/validator_induction/src/proof_of_possession.rs +++ b/beacon_chain/validator_induction/src/proof_of_possession.rs @@ -1,6 +1,7 @@ use bls::{ - Signature, + Keypair, PublicKey, + Signature, }; use hashing::proof_of_possession_hash; @@ -12,3 +13,10 @@ pub fn verify_proof_of_possession(sig: &Signature, pubkey: &PublicKey) let hash = proof_of_possession_hash(&pubkey.as_bytes()); sig.verify_hashed(&hash, &pubkey) } + +pub fn create_proof_of_possession(keypair: &Keypair) + -> Signature +{ + let hash = proof_of_possession_hash(&keypair.pk.as_bytes()); + Signature::new_hashed(&hash, &keypair.sk) +} diff --git a/beacon_chain/validator_induction/src/registration.rs b/beacon_chain/validator_induction/src/registration.rs deleted file mode 100644 index 0cb62c6bd..000000000 --- a/beacon_chain/validator_induction/src/registration.rs +++ /dev/null @@ -1,105 +0,0 @@ -use bls::{ - PublicKey, - Signature, -}; -use types::{ - Address, - Hash256, - ValidatorRecord, -}; - -use super::proof_of_possession::verify_proof_of_possession; - - -/// 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 PartialEq for ValidatorRegistration { - fn eq(&self, v: &ValidatorRecord) -> bool { - (self.pubkey == v.pubkey) & - (self.withdrawal_shard == v.withdrawal_shard) & - (self.withdrawal_address == v.withdrawal_address) & - (self.randao_commitment == v.randao_commitment) & - (verify_proof_of_possession(&self.proof_of_possession, &v.pubkey)) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - use bls::{ - Keypair, - Signature, - }; - use types::{ - Address, - Hash256, - ValidatorRecord, - }; - use hashing::proof_of_possession_hash; - - 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) - } - - fn get_equal_validator_registrations_and_records() - -> (ValidatorRegistration, ValidatorRecord) - { - let kp = Keypair::random(); - let rego = ValidatorRegistration { - pubkey: kp.pk.clone(), - withdrawal_shard: 0, - withdrawal_address: Address::zero(), - randao_commitment: Hash256::zero(), - proof_of_possession: get_proof_of_possession(&kp), - }; - let record = ValidatorRecord { - pubkey: rego.pubkey.clone(), - withdrawal_shard: rego.withdrawal_shard, - withdrawal_address: rego.withdrawal_address.clone(), - randao_commitment: rego.randao_commitment.clone(), - randao_last_change: 0, - balance: 0, - status: 0, - exit_slot: 0, - }; - (rego, record) - } - - #[test] - fn test_validator_registration_and_record_partial_eq() { - let (rego, record) = get_equal_validator_registrations_and_records(); - assert!(rego == record); - - let (mut rego, record) = get_equal_validator_registrations_and_records(); - let kp = Keypair::random(); - rego.pubkey = kp.pk.clone(); - assert!(rego != record); - - let (mut rego, record) = get_equal_validator_registrations_and_records(); - rego.withdrawal_shard = record.withdrawal_shard + 1; - assert!(rego != record); - - let (mut rego, record) = get_equal_validator_registrations_and_records(); - rego.withdrawal_address = Address::from(42); - assert!(rego != record); - - let (mut rego, record) = get_equal_validator_registrations_and_records(); - rego.randao_commitment = Hash256::from(42); - assert!(rego != record); - - let (mut rego, record) = get_equal_validator_registrations_and_records(); - let kp = Keypair::random(); - rego.proof_of_possession = get_proof_of_possession(&kp); - assert!(rego != record); - } -}