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 bls::verify_proof_of_possession;
use types::{ValidatorRecord, ValidatorRegistration, ValidatorStatus}; use types::{ValidatorRecord, Deposit, ValidatorStatus, BeaconState};
/// 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;
@ -8,7 +8,7 @@ pub const DEPOSIT_GWEI: u64 = 32_000_000_000;
pub struct ValidatorInductor { pub struct ValidatorInductor {
pub current_slot: u64, pub current_slot: u64,
pub shard_count: u16, pub shard_count: u16,
validators: Vec<ValidatorRecord>, beacon_state: BeaconState,
empty_validator_start: usize, empty_validator_start: usize,
} }
@ -19,11 +19,11 @@ pub enum ValidatorInductionError {
} }
impl ValidatorInductor { 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 { Self {
current_slot, current_slot,
shard_count, shard_count,
validators, beacon_state,
empty_validator_start: 0, empty_validator_start: 0,
} }
} }
@ -34,50 +34,55 @@ impl ValidatorInductor {
/// validator in `CrystallizedState.validators`. /// validator in `CrystallizedState.validators`.
pub fn induct( pub fn induct(
&mut self, &mut self,
rego: &ValidatorRegistration, deposit: &Deposit,
status: ValidatorStatus, status: ValidatorStatus,
) -> Result<usize, ValidatorInductionError> { ) -> Result<usize, ValidatorInductionError> {
let v = self.process_registration(rego, status)?; let v = self.process_registration(deposit, status)?;
Ok(self.add_validator(v)) Ok(self.add_validator(v))
} }
/// Verify a `ValidatorRegistration` and return a `ValidatorRecord` if valid. /// Verify a `ValidatorRegistration` and return a `ValidatorRecord` if valid.
fn process_registration( fn process_deposit(
&self, &self,
r: &ValidatorRegistration, deposit: &Deposit,
status: ValidatorStatus, status: ValidatorStatus,
) -> Result<ValidatorRecord, ValidatorInductionError> { ) -> Result<ValidatorRecord, ValidatorInductionError> {
let deposit_input = &deposit.deposit_data.deposit_input;
/* /*
* Ensure withdrawal shard is not too high. * Ensure withdrawal shard is not too high.
*/ */
/*
if r.withdrawal_shard > self.shard_count { if r.withdrawal_shard > self.shard_count {
return Err(ValidatorInductionError::InvalidShard); return Err(ValidatorInductionError::InvalidShard);
} }
*/
/* /*
* Prove validator has knowledge of their secret key. * 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); return Err(ValidatorInductionError::InvaidProofOfPossession);
} }
Ok(ValidatorRecord { Ok(ValidatorRecord {
pubkey: r.pubkey.clone(), pubkey: deposit_input.pubkey.clone(),
withdrawal_shard: r.withdrawal_shard, withdrawal_credentials: deposit_input.withdrawal_credentials,
withdrawal_address: r.withdrawal_address, randao_commitment: deposit_input.randao_commitment,
randao_commitment: r.randao_commitment, // TODO: revisit this
randao_last_change: self.current_slot, randao_layers: 0,
balance: DEPOSIT_GWEI, balance: DEPOSIT_GWEI,
status: status, 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 /// Returns the index of the first `ValidatorRecord` in the `CrystallizedState` where
/// `validator.status == Withdrawn`. If no such record exists, `None` is returned. /// `validator.status == Withdrawn`. If no such record exists, `None` is returned.
fn first_withdrawn_validator(&mut self) -> Option<usize> { fn first_withdrawn_validator(&mut self) -> Option<usize> {
for i in self.empty_validator_start..self.validators.len() { let validators = self.beacon_state.validator_registry;
if self.validators[i].status == ValidatorStatus::Withdrawn { for i in self.empty_validator_start..validators.len() {
if validators[i].status == ValidatorStatus::Withdrawn {
self.empty_validator_start = i + 1; self.empty_validator_start = i + 1;
return Some(i); return Some(i);
} }
@ -91,18 +96,18 @@ impl ValidatorInductor {
fn add_validator(&mut self, v: ValidatorRecord) -> usize { fn add_validator(&mut self, v: ValidatorRecord) -> usize {
match self.first_withdrawn_validator() { match self.first_withdrawn_validator() {
Some(i) => { Some(i) => {
self.validators[i] = v; self.beacon_state.validator_registry[i] = v;
i i
} }
None => { None => {
self.validators.push(v); self.beacon_state.validator_registry.push(v);
self.validators.len() - 1 self.beacon_state.validator_registry.len() - 1
} }
} }
} }
pub fn to_vec(self) -> Vec<ValidatorRecord> { 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 hashing::proof_of_possession_hash;
use types::{Address, Hash256}; use types::{Address, Hash256};
/*
fn registration_equals_record(reg: &ValidatorRegistration, rec: &ValidatorRecord) -> bool { fn registration_equals_record(reg: &ValidatorRegistration, rec: &ValidatorRecord) -> bool {
(reg.pubkey == rec.pubkey) (reg.pubkey == rec.pubkey)
& (reg.withdrawal_shard == rec.withdrawal_shard) & (reg.withdrawal_shard == rec.withdrawal_shard)
@ -121,6 +127,7 @@ mod tests {
& (reg.randao_commitment == rec.randao_commitment) & (reg.randao_commitment == rec.randao_commitment)
& (verify_proof_of_possession(&reg.proof_of_possession, &rec.pubkey)) & (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 {
@ -151,7 +158,7 @@ mod tests {
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 0); assert_eq!(result.unwrap(), 0);
assert!(registration_equals_record(&r, &validators[0])); //assert!(registration_equals_record(&r, &validators[0]));
assert_eq!(validators.len(), 1); assert_eq!(validators.len(), 1);
} }
@ -187,7 +194,7 @@ mod tests {
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 5); assert_eq!(result.unwrap(), 5);
assert!(registration_equals_record(&r, &validators[5])); //assert!(registration_equals_record(&r, &validators[5]));
assert_eq!(validators.len(), 6); assert_eq!(validators.len(), 6);
} }
@ -210,7 +217,7 @@ mod tests {
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 1); assert_eq!(result.unwrap(), 1);
assert!(registration_equals_record(&r, &validators[1])); //assert!(registration_equals_record(&r, &validators[1]));
assert_eq!(validators.len(), 5); assert_eq!(validators.len(), 5);
} }
@ -231,7 +238,7 @@ mod tests {
let result = inductor.induct(&r, ValidatorStatus::PendingActivation); let result = inductor.induct(&r, ValidatorStatus::PendingActivation);
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 0); 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 * Ensure the second validator gets the 1'st slot
@ -241,7 +248,7 @@ mod tests {
let result = inductor.induct(&r_two, ValidatorStatus::PendingActivation); let result = inductor.induct(&r_two, ValidatorStatus::PendingActivation);
let validators = inductor.to_vec(); let validators = inductor.to_vec();
assert_eq!(result.unwrap(), 1); 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); assert_eq!(validators.len(), 5);
} }