first pass at updating inductor with deposit structs - lots of compilation errors

This commit is contained in:
Grant Wuerker 2018-12-15 23:19:15 -06:00
parent 7065454b5c
commit a05364cb49

View File

@ -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<ValidatorRecord>,
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<ValidatorRecord>) -> 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<usize, ValidatorInductionError> {
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<ValidatorRecord, ValidatorInductionError> {
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<usize> {
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<ValidatorRecord> {
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(&reg.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);
}