lighthouse-pulse/lighthouse/state/validator_record.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

2018-07-13 05:35:48 +00:00
extern crate rand;
2018-08-10 00:48:03 +00:00
use super::utils::types::{ Hash256, Address, U256 };
2018-07-13 05:35:48 +00:00
use super::utils::bls::{ PublicKey, Keypair };
2018-07-06 07:54:07 +00:00
2018-07-13 05:35:48 +00:00
use self::rand::thread_rng;
2018-07-06 07:54:07 +00:00
pub struct ValidatorRecord {
pub pubkey: PublicKey,
pub withdrawal_shard: u16,
pub withdrawal_address: Address,
2018-08-10 00:48:03 +00:00
pub randao_commitment: Hash256,
pub balance: U256,
2018-08-10 00:48:03 +00:00
pub start_dynasty: u64,
pub end_dynasty: u64,
2018-07-06 07:54:07 +00:00
}
impl ValidatorRecord {
2018-08-10 00:48:03 +00:00
/// Generates a new instance where the keypair is generated using
/// `rand::thread_rng` entropy and all other fields are set to zero.
///
/// Returns the new instance and new keypair.
pub fn zero_with_thread_rand_keypair() -> (Self, Keypair) {
let mut rng = thread_rng();
let keypair = Keypair::generate(&mut rng);
let s = Self {
pubkey: keypair.public.clone(),
withdrawal_shard: 0,
withdrawal_address: Address::zero(),
2018-08-10 00:48:03 +00:00
randao_commitment: Hash256::zero(),
balance: U256::zero(),
2018-08-10 00:48:03 +00:00
start_dynasty: 0,
end_dynasty: 0,
};
(s, keypair)
}
2018-07-06 07:54:07 +00:00
}
impl Clone for ValidatorRecord {
fn clone(&self) -> ValidatorRecord {
ValidatorRecord {
pubkey: self.pubkey.clone(),
..*self
}
}
}
2018-07-06 07:54:07 +00:00
#[cfg(test)]
mod tests {
use super::*;
2018-07-06 07:54:07 +00:00
#[test]
2018-08-10 00:48:03 +00:00
fn test_validator_record_zero_rand_keypair() {
2018-08-10 01:22:15 +00:00
let (v, _kp) = ValidatorRecord::zero_with_thread_rand_keypair();
2018-08-10 00:48:03 +00:00
// TODO: check keys
assert_eq!(v.withdrawal_shard, 0);
assert!(v.withdrawal_address.is_zero());
assert!(v.randao_commitment.is_zero());
assert!(v.balance.is_zero());
assert_eq!(v.start_dynasty, 0);
assert_eq!(v.end_dynasty, 0);
}
2018-07-06 07:54:07 +00:00
}