BaconState serialization fixed and reorganiztion around induction and deposits

This commit is contained in:
Grant Wuerker 2018-12-29 16:22:14 -06:00
parent d61ab50f45
commit 598562da73
6 changed files with 65 additions and 40 deletions

View File

@ -87,7 +87,6 @@ mod tests {
extern crate bls; extern crate bls;
extern crate validator_induction; extern crate validator_induction;
use self::bls::{create_proof_of_possession, Keypair};
use super::*; use super::*;
// TODO: enhance these tests. // TODO: enhance these tests.

View File

@ -62,6 +62,7 @@ impl Encodable for BeaconState {
s.append(&self.genesis_time); s.append(&self.genesis_time);
s.append(&self.fork_data); s.append(&self.fork_data);
s.append(&self.validator_registry); s.append(&self.validator_registry);
s.append(&self.validator_balances);
s.append(&self.validator_registry_latest_change_slot); s.append(&self.validator_registry_latest_change_slot);
s.append(&self.validator_registry_exit_count); s.append(&self.validator_registry_exit_count);
s.append(&self.validator_registry_delta_chain_tip); 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 (genesis_time, i) = <_>::ssz_decode(bytes, i)?;
let (fork_data, i) = <_>::ssz_decode(bytes, i)?; let (fork_data, i) = <_>::ssz_decode(bytes, i)?;
let (validator_registry, 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_latest_change_slot, i) = <_>::ssz_decode(bytes, i)?;
let (validator_registry_exit_count, 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)?; let (validator_registry_delta_chain_tip, i) = <_>::ssz_decode(bytes, i)?;
@ -114,6 +116,7 @@ impl Decodable for BeaconState {
genesis_time, genesis_time,
fork_data, fork_data,
validator_registry, validator_registry,
validator_balances,
validator_registry_latest_change_slot, validator_registry_latest_change_slot,
validator_registry_exit_count, validator_registry_exit_count,
validator_registry_delta_chain_tip, validator_registry_delta_chain_tip,
@ -145,6 +148,7 @@ impl<T: RngCore> TestRandom<T> for BeaconState {
genesis_time: <_>::random_for_test(rng), genesis_time: <_>::random_for_test(rng),
fork_data: <_>::random_for_test(rng), fork_data: <_>::random_for_test(rng),
validator_registry: <_>::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_latest_change_slot: <_>::random_for_test(rng),
validator_registry_exit_count: <_>::random_for_test(rng), validator_registry_exit_count: <_>::random_for_test(rng),
validator_registry_delta_chain_tip: <_>::random_for_test(rng), validator_registry_delta_chain_tip: <_>::random_for_test(rng),

View File

@ -1,7 +1,8 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream}; use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::{DepositData, Hash256}; use super::{DepositData, DepositInput, Hash256};
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use bls::{Keypair, create_proof_of_possession};
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct Deposit { pub struct Deposit {
@ -10,6 +11,29 @@ pub struct Deposit {
pub deposit_data: DepositData, 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 { impl Encodable for Deposit {
fn ssz_append(&self, s: &mut SszStream) { fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(&self.merkle_branch); s.append_vec(&self.merkle_branch);

View File

@ -2,7 +2,7 @@ use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Default)]
pub struct ForkData { pub struct ForkData {
pub pre_fork_version: u64, pub pre_fork_version: u64,
pub post_fork_version: u64, pub post_fork_version: u64,

View File

@ -1,4 +1,4 @@
use super::bls::PublicKey; use super::bls::{Keypair, PublicKey};
use super::{Hash256}; use super::{Hash256};
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
@ -43,6 +43,23 @@ pub struct ValidatorRecord {
pub second_last_poc_slot: u64 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 { impl ValidatorRecord {
pub fn status_is(&self, status: ValidatorStatus) -> bool { pub fn status_is(&self, status: ValidatorStatus) -> bool {
self.status == status self.status == status

View File

@ -1,4 +1,4 @@
use bls::{PublicKey, verify_proof_of_possession}; use bls::{verify_proof_of_possession};
use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus}; use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus};
use spec::ChainSpec; use spec::ChainSpec;
@ -84,44 +84,22 @@ fn min_empty_validator_index(
mod tests { mod tests {
use super::*; use super::*;
use bls::{Keypair, Signature, create_proof_of_possession};
use hashing::canonical_hash;
use types::{Hash256, DepositData, DepositInput}; use types::{Hash256, DepositData, DepositInput};
fn get_deposit() -> Deposit { fn deposit_equals_record(dep: &Deposit, val: &ValidatorRecord) -> bool {
let kp = Keypair::random(); (dep.deposit_data.deposit_input.pubkey == val.pubkey)
let deposit_input = DepositInput { & (dep.deposit_data.deposit_input.withdrawal_credentials == val.withdrawal_credentials)
pubkey: kp.pk.clone(), & (dep.deposit_data.deposit_input.randao_commitment == val.randao_commitment)
withdrawal_credentials: Hash256::zero(), & (verify_proof_of_possession(&dep.deposit_data.deposit_input.proof_of_possession, &val.pubkey))
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(&reg.proof_of_possession, &rec.pubkey))
} }
#[test] #[test]
fn test_process_deposit_valid_empty_validators() { fn test_process_deposit_valid_empty_validators() {
let mut state = BeaconState::default(); let mut state = BeaconState::default();
let deposit = get_deposit(); let mut deposit = Deposit::zero_with_rand_keypair();
let spec = ChainSpec::foundation(); let spec = ChainSpec::foundation();
deposit.deposit_data.value = DEPOSIT_GWEI;
let result = process_deposit(&mut state, &deposit, &spec); let result = process_deposit(&mut state, &deposit, &spec);
assert_eq!(result.unwrap(), 0); assert_eq!(result.unwrap(), 0);
@ -136,8 +114,9 @@ mod tests {
let spec = ChainSpec::foundation(); let spec = ChainSpec::foundation();
for i in 0..5 { 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); let result = process_deposit(&mut state, &deposit, &spec);
deposit.deposit_data.value = DEPOSIT_GWEI;
assert_eq!(result.unwrap(), i); assert_eq!(result.unwrap(), i);
assert!(deposit_equals_record(&deposit, &state.validator_registry[i])); assert!(deposit_equals_record(&deposit, &state.validator_registry[i]));
assert_eq!(state.validator_registry.len(), i + 1); assert_eq!(state.validator_registry.len(), i + 1);
@ -150,8 +129,9 @@ mod tests {
let mut state = BeaconState::default(); let mut state = BeaconState::default();
let spec = ChainSpec::foundation(); let spec = ChainSpec::foundation();
let deposit = get_deposit(); let mut deposit = Deposit::zero_with_rand_keypair();
let (mut validator, _) = ValidatorRecord::zero_with_thread_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.pubkey = deposit.deposit_data.deposit_input.pubkey.clone();
validator.withdrawal_credentials = deposit.deposit_data.deposit_input.withdrawal_credentials; validator.withdrawal_credentials = deposit.deposit_data.deposit_input.withdrawal_credentials;
validator.randao_commitment = deposit.deposit_data.deposit_input.randao_commitment; validator.randao_commitment = deposit.deposit_data.deposit_input.randao_commitment;
@ -173,11 +153,12 @@ mod tests {
let mut state = BeaconState::default(); let mut state = BeaconState::default();
let spec = ChainSpec::foundation(); 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_registry.push(validator);
state.validator_balances.push(0); 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; state.slot = spec.zero_balance_validator_ttl;
let result = process_deposit(&mut state, &deposit, &spec); let result = process_deposit(&mut state, &deposit, &spec);