Move ValidatorRegistration into types

This commit is contained in:
Paul Hauner 2018-10-22 05:22:16 +11:00
parent f495ed845b
commit 12b5d7434c
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
5 changed files with 32 additions and 115 deletions

View File

@ -12,6 +12,7 @@ pub mod crosslink_record;
pub mod shard_and_committee; pub mod shard_and_committee;
pub mod special_record; pub mod special_record;
pub mod validator_record; pub mod validator_record;
pub mod validator_registration;
use self::ethereum_types::{ use self::ethereum_types::{
H256, H256,
@ -30,6 +31,7 @@ pub use crosslink_record::CrosslinkRecord;
pub use shard_and_committee::ShardAndCommittee; pub use shard_and_committee::ShardAndCommittee;
pub use special_record::{ SpecialRecord, SpecialRecordKind }; pub use special_record::{ SpecialRecord, SpecialRecordKind };
pub use validator_record::{ ValidatorRecord, ValidatorStatus }; pub use validator_record::{ ValidatorRecord, ValidatorStatus };
pub use validator_registration::{ ValidatorRegistration };
pub type Hash256 = H256; pub type Hash256 = H256;
pub type Address = H160; pub type Address = H160;

View File

@ -1,10 +1,10 @@
use types::{ use types::{
ValidatorRecord, ValidatorRecord,
ValidatorStatus, ValidatorStatus,
ValidatorRegistration,
}; };
use super::proof_of_possession::verify_proof_of_possession; use super::proof_of_possession::verify_proof_of_possession;
use super::registration::ValidatorRegistration;
/// The size of a validators deposit in GWei. /// The size of a validators deposit in GWei.
pub const DEPOSIT_GWEI: u64 = 32_000_000_000; pub const DEPOSIT_GWEI: u64 = 32_000_000_000;
@ -130,6 +130,16 @@ mod tests {
}; };
use hashing::proof_of_possession_hash; 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(&reg.proof_of_possession, &rec.pubkey))
}
/// Generate a proof of possession for some keypair. /// Generate a proof of possession for some keypair.
fn get_proof_of_possession(kp: &Keypair) -> Signature { fn get_proof_of_possession(kp: &Keypair) -> Signature {
let pop_message = proof_of_possession_hash(&kp.pk.as_bytes()); let pop_message = proof_of_possession_hash(&kp.pk.as_bytes());
@ -159,7 +169,7 @@ mod tests {
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 0); assert_eq!(result.unwrap(), 0);
assert_eq!(r, validators[0]); assert!(registration_equals_record(&r, &validators[0]));
assert_eq!(validators.len(), 1); assert_eq!(validators.len(), 1);
} }
@ -179,7 +189,7 @@ mod tests {
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 5); assert_eq!(result.unwrap(), 5);
assert_eq!(r, validators[5]); assert!(registration_equals_record(&r, &validators[5]));
assert_eq!(validators.len(), 6); assert_eq!(validators.len(), 6);
} }
@ -202,7 +212,7 @@ mod tests {
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 1); assert_eq!(result.unwrap(), 1);
assert_eq!(r, validators[1]); assert!(registration_equals_record(&r, &validators[1]));
assert_eq!(validators.len(), 5); assert_eq!(validators.len(), 5);
} }
@ -223,18 +233,17 @@ mod tests {
let result = inductor.induct(&r); let result = inductor.induct(&r);
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 0); assert_eq!(result.unwrap(), 0);
assert_eq!(r, validators[0]); assert!(registration_equals_record(&r, &validators[0]));
assert_eq!(validators.len(), 5);
/* /*
* Ensure the second validator gets the 1'st slot * Ensure the second validator gets the 1'st slot
*/ */
let r_two = get_registration(); let r_two = get_registration();
let mut inductor = ValidatorInductor::new(0, 1024, validators); let mut inductor = ValidatorInductor::new(0, 1024, validators);
let result = inductor.induct(&r); let result = inductor.induct(&r_two);
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 1); assert_eq!(result.unwrap(), 1);
assert_eq!(r_two, validators[1]); assert!(registration_equals_record(&r_two, &validators[1]));
assert_eq!(validators.len(), 5); assert_eq!(validators.len(), 5);
} }

View File

@ -4,9 +4,12 @@ extern crate types;
mod inductor; mod inductor;
mod proof_of_possession; mod proof_of_possession;
mod registration;
pub use inductor::{ pub use inductor::{
ValidatorInductor, ValidatorInductor,
ValidatorInductionError, ValidatorInductionError,
}; };
pub use proof_of_possession::{
create_proof_of_possession,
};

View File

@ -1,6 +1,7 @@
use bls::{ use bls::{
Signature, Keypair,
PublicKey, PublicKey,
Signature,
}; };
use hashing::proof_of_possession_hash; 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()); let hash = proof_of_possession_hash(&pubkey.as_bytes());
sig.verify_hashed(&hash, &pubkey) 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)
}

View File

@ -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<ValidatorRecord> 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);
}
}