From 598562da7304924788f1c5590b078817dbe7f714 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sat, 29 Dec 2018 16:22:14 -0600 Subject: [PATCH] BaconState serialization fixed and reorganiztion around induction and deposits --- beacon_chain/genesis/src/beacon_state.rs | 1 - beacon_chain/types/src/beacon_state.rs | 4 ++ beacon_chain/types/src/deposit.rs | 26 ++++++++- beacon_chain/types/src/fork_data.rs | 2 +- beacon_chain/types/src/validator_record.rs | 19 ++++++- .../validator_induction/src/inductor.rs | 53 ++++++------------- 6 files changed, 65 insertions(+), 40 deletions(-) diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index a858baaca..b6a42db5b 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -87,7 +87,6 @@ mod tests { extern crate bls; extern crate validator_induction; - use self::bls::{create_proof_of_possession, Keypair}; use super::*; // TODO: enhance these tests. diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index 15ebde5fb..065e87c02 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -62,6 +62,7 @@ impl Encodable for BeaconState { s.append(&self.genesis_time); s.append(&self.fork_data); s.append(&self.validator_registry); + s.append(&self.validator_balances); s.append(&self.validator_registry_latest_change_slot); s.append(&self.validator_registry_exit_count); s.append(&self.validator_registry_delta_chain_tip); @@ -89,6 +90,7 @@ impl Decodable for BeaconState { let (genesis_time, i) = <_>::ssz_decode(bytes, i)?; let (fork_data, i) = <_>::ssz_decode(bytes, i)?; let (validator_registry, i) = <_>::ssz_decode(bytes, i)?; + let (validator_balances, i) = <_>::ssz_decode(bytes, i)?; let (validator_registry_latest_change_slot, i) = <_>::ssz_decode(bytes, i)?; let (validator_registry_exit_count, i) = <_>::ssz_decode(bytes, i)?; let (validator_registry_delta_chain_tip, i) = <_>::ssz_decode(bytes, i)?; @@ -114,6 +116,7 @@ impl Decodable for BeaconState { genesis_time, fork_data, validator_registry, + validator_balances, validator_registry_latest_change_slot, validator_registry_exit_count, validator_registry_delta_chain_tip, @@ -145,6 +148,7 @@ impl TestRandom for BeaconState { genesis_time: <_>::random_for_test(rng), fork_data: <_>::random_for_test(rng), validator_registry: <_>::random_for_test(rng), + validator_balances: <_>::random_for_test(rng), validator_registry_latest_change_slot: <_>::random_for_test(rng), validator_registry_exit_count: <_>::random_for_test(rng), validator_registry_delta_chain_tip: <_>::random_for_test(rng), diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs index 9d84bc278..eb26e2b10 100644 --- a/beacon_chain/types/src/deposit.rs +++ b/beacon_chain/types/src/deposit.rs @@ -1,7 +1,8 @@ use super::ssz::{Decodable, DecodeError, Encodable, SszStream}; -use super::{DepositData, Hash256}; +use super::{DepositData, DepositInput, Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; +use bls::{Keypair, create_proof_of_possession}; #[derive(Debug, PartialEq, Clone)] pub struct Deposit { @@ -10,6 +11,29 @@ pub struct Deposit { pub deposit_data: DepositData, } +impl Deposit { + pub fn zero_with_rand_keypair() -> Self{ + let kp = Keypair::random(); + let deposit_input = DepositInput { + pubkey: kp.pk.clone(), + withdrawal_credentials: Hash256::zero(), + randao_commitment: Hash256::zero(), + poc_commitment: Hash256::zero(), + proof_of_possession: create_proof_of_possession(&kp) + }; + let deposit_data = DepositData { + deposit_input: deposit_input, + value: 0, + timestamp: 0 + }; + Self { + merkle_branch: Vec::new(), + merkle_tree_index: 0, + deposit_data: deposit_data + } + } +} + impl Encodable for Deposit { fn ssz_append(&self, s: &mut SszStream) { s.append_vec(&self.merkle_branch); diff --git a/beacon_chain/types/src/fork_data.rs b/beacon_chain/types/src/fork_data.rs index e779c797e..4e797bc34 100644 --- a/beacon_chain/types/src/fork_data.rs +++ b/beacon_chain/types/src/fork_data.rs @@ -2,7 +2,7 @@ use super::ssz::{Decodable, DecodeError, Encodable, SszStream}; use crate::test_utils::TestRandom; use rand::RngCore; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct ForkData { pub pre_fork_version: u64, pub post_fork_version: u64, diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 7df39f931..579509945 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -1,4 +1,4 @@ -use super::bls::PublicKey; +use super::bls::{Keypair, PublicKey}; use super::{Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; @@ -43,6 +43,23 @@ pub struct ValidatorRecord { pub second_last_poc_slot: u64 } +impl ValidatorRecord { + pub fn zero_with_rand_keypair() -> Self { + Self { + pubkey: Keypair::random().pk, + withdrawal_credentials: Hash256::zero(), + randao_commitment: Hash256::zero(), + randao_layers: 0, + status: ValidatorStatus::from(0), + latest_status_change_slot: 0, + exit_count: 0, + poc_commitment: Hash256::zero(), + last_poc_change_slot: 0, + second_last_poc_slot: 0 + } + } +} + impl ValidatorRecord { pub fn status_is(&self, status: ValidatorStatus) -> bool { self.status == status diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index ab40bd8cd..84d7928d0 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -1,4 +1,4 @@ -use bls::{PublicKey, verify_proof_of_possession}; +use bls::{verify_proof_of_possession}; use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus}; use spec::ChainSpec; @@ -84,44 +84,22 @@ fn min_empty_validator_index( mod tests { use super::*; - use bls::{Keypair, Signature, create_proof_of_possession}; - use hashing::canonical_hash; use types::{Hash256, DepositData, DepositInput}; - fn get_deposit() -> Deposit { - let kp = Keypair::random(); - let deposit_input = DepositInput { - pubkey: kp.pk.clone(), - withdrawal_credentials: Hash256::zero(), - randao_commitment: Hash256::zero(), - poc_commitment: Hash256::zero(), - proof_of_possession: create_proof_of_possession(&kp) - }; - let deposit_data = DepositData { - deposit_input: deposit_input, - value: DEPOSIT_GWEI, - timestamp: 0 - }; - Deposit { - merkle_branch: Vec::new(), - merkle_tree_index: 0, - deposit_data: deposit_data - } - } - - fn deposit_equals_record(dep: &Deposit, rec: &ValidatorRecord) -> bool { - (dep.deposit_data.deposit_input.pubkey == rec.pubkey) - & (dep.deposit_data.deposit_input.withdrawal_credentials == rec.withdrawal_credentials) - & (dep.deposit_data.deposit_input.randao_commitment == rec.randao_commitment) - //& (verify_proof_of_possession(®.proof_of_possession, &rec.pubkey)) + fn deposit_equals_record(dep: &Deposit, val: &ValidatorRecord) -> bool { + (dep.deposit_data.deposit_input.pubkey == val.pubkey) + & (dep.deposit_data.deposit_input.withdrawal_credentials == val.withdrawal_credentials) + & (dep.deposit_data.deposit_input.randao_commitment == val.randao_commitment) + & (verify_proof_of_possession(&dep.deposit_data.deposit_input.proof_of_possession, &val.pubkey)) } #[test] fn test_process_deposit_valid_empty_validators() { let mut state = BeaconState::default(); - let deposit = get_deposit(); + let mut deposit = Deposit::zero_with_rand_keypair(); let spec = ChainSpec::foundation(); - + deposit.deposit_data.value = DEPOSIT_GWEI; + let result = process_deposit(&mut state, &deposit, &spec); assert_eq!(result.unwrap(), 0); @@ -136,8 +114,9 @@ mod tests { let spec = ChainSpec::foundation(); for i in 0..5 { - let deposit = get_deposit(); + let mut deposit = Deposit::zero_with_rand_keypair(); let result = process_deposit(&mut state, &deposit, &spec); + deposit.deposit_data.value = DEPOSIT_GWEI; assert_eq!(result.unwrap(), i); assert!(deposit_equals_record(&deposit, &state.validator_registry[i])); assert_eq!(state.validator_registry.len(), i + 1); @@ -150,8 +129,9 @@ mod tests { let mut state = BeaconState::default(); let spec = ChainSpec::foundation(); - let deposit = get_deposit(); - let (mut validator, _) = ValidatorRecord::zero_with_thread_rand_keypair(); + let mut deposit = Deposit::zero_with_rand_keypair(); + let mut validator = ValidatorRecord::zero_with_rand_keypair(); + deposit.deposit_data.value = DEPOSIT_GWEI; validator.pubkey = deposit.deposit_data.deposit_input.pubkey.clone(); validator.withdrawal_credentials = deposit.deposit_data.deposit_input.withdrawal_credentials; validator.randao_commitment = deposit.deposit_data.deposit_input.randao_commitment; @@ -173,11 +153,12 @@ mod tests { let mut state = BeaconState::default(); let spec = ChainSpec::foundation(); - let (mut validator, _) = ValidatorRecord::zero_with_thread_rand_keypair(); + let validator = ValidatorRecord::zero_with_rand_keypair(); state.validator_registry.push(validator); state.validator_balances.push(0); - let deposit = get_deposit(); + let mut deposit = Deposit::zero_with_rand_keypair(); + deposit.deposit_data.value = DEPOSIT_GWEI; state.slot = spec.zero_balance_validator_ttl; let result = process_deposit(&mut state, &deposit, &spec);