From a2b23d6ae2051ee7d3ad0a1bf167ee4861907d99 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Thu, 10 Jan 2019 14:57:34 -0600 Subject: [PATCH 1/6] run cargo fmt --- .../attestation_validation/src/lib.rs | 6 +- beacon_chain/genesis/src/lib.rs | 2 +- beacon_chain/types/src/test_utils/mod.rs | 10 +-- beacon_chain/types/src/validator_record.rs | 6 +- beacon_chain/utils/bls/src/lib.rs | 2 +- .../validator_induction/src/inductor.rs | 69 ++++++++++++------- beacon_chain/validator_induction/src/lib.rs | 4 +- lighthouse/beacon_chain/tests/chain_test.rs | 2 +- lighthouse/db/src/disk_db.rs | 3 +- .../db/src/stores/beacon_state_store.rs | 4 +- lighthouse/main.rs | 8 ++- 11 files changed, 72 insertions(+), 44 deletions(-) diff --git a/beacon_chain/attestation_validation/src/lib.rs b/beacon_chain/attestation_validation/src/lib.rs index 254d6134e0..825371ed09 100644 --- a/beacon_chain/attestation_validation/src/lib.rs +++ b/beacon_chain/attestation_validation/src/lib.rs @@ -14,9 +14,9 @@ mod justified_slot; mod shard_block; mod signature; -pub use crate::enums::{Invalid, Outcome, Error}; pub use crate::block_inclusion::validate_attestation_for_block; -pub use crate::justified_slot::validate_attestation_justified_slot; +pub use crate::enums::{Error, Invalid, Outcome}; pub use crate::justified_block::validate_attestation_justified_block_hash; -pub use crate::signature::validate_attestation_signature; +pub use crate::justified_slot::validate_attestation_justified_slot; pub use crate::shard_block::validate_attestation_data_shard_block_hash; +pub use crate::signature::validate_attestation_signature; diff --git a/beacon_chain/genesis/src/lib.rs b/beacon_chain/genesis/src/lib.rs index e590fa8be8..6f45863aa4 100644 --- a/beacon_chain/genesis/src/lib.rs +++ b/beacon_chain/genesis/src/lib.rs @@ -3,8 +3,8 @@ extern crate types; extern crate validator_induction; extern crate validator_shuffling; -mod beacon_state; mod beacon_block; +mod beacon_state; pub use crate::beacon_block::genesis_beacon_block; pub use crate::beacon_state::{genesis_beacon_state, Error as GenesisError}; diff --git a/beacon_chain/types/src/test_utils/mod.rs b/beacon_chain/types/src/test_utils/mod.rs index f3e382e6ef..eb54f2a53f 100644 --- a/beacon_chain/types/src/test_utils/mod.rs +++ b/beacon_chain/types/src/test_utils/mod.rs @@ -6,12 +6,13 @@ pub mod address; pub mod aggregate_signature; pub mod bitfield; pub mod hash256; -pub mod signature; -pub mod secret_key; pub mod public_key; +pub mod secret_key; +pub mod signature; pub trait TestRandom -where T: RngCore +where + T: RngCore, { fn random_for_test(rng: &mut T) -> Self; } @@ -35,7 +36,8 @@ impl TestRandom for usize { } impl TestRandom for Vec -where U: TestRandom +where + U: TestRandom, { fn random_for_test(rng: &mut T) -> Self { vec![ diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 5f74d28d25..3f4a432616 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -1,5 +1,5 @@ use super::bls::PublicKey; -use super::{Hash256}; +use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; use ssz::{Decodable, DecodeError, Encodable, SszStream}; @@ -40,7 +40,7 @@ pub struct ValidatorRecord { pub exit_count: u64, pub custody_commitment: Hash256, pub latest_custody_reseed_slot: u64, - pub penultimate_custody_reseed_slot: u64 + pub penultimate_custody_reseed_slot: u64, } impl ValidatorRecord { @@ -132,7 +132,7 @@ impl Decodable for ValidatorRecord { exit_count, custody_commitment, latest_custody_reseed_slot, - penultimate_custody_reseed_slot + penultimate_custody_reseed_slot, }, i, )) diff --git a/beacon_chain/utils/bls/src/lib.rs b/beacon_chain/utils/bls/src/lib.rs index 9d27d870ba..d7a3ff15dc 100644 --- a/beacon_chain/utils/bls/src/lib.rs +++ b/beacon_chain/utils/bls/src/lib.rs @@ -19,8 +19,8 @@ pub use self::bls_aggregates::AggregatePublicKey; pub const BLS_AGG_SIG_BYTE_SIZE: usize = 97; use hashing::canonical_hash; -use std::default::Default; use ssz::ssz_encode; +use std::default::Default; fn extend_if_needed(hash: &mut Vec) { // NOTE: bls_aggregates crate demands 48 bytes, this may be removed as we get closer to production diff --git a/beacon_chain/validator_induction/src/inductor.rs b/beacon_chain/validator_induction/src/inductor.rs index 720e38fa4e..f00c9ec55f 100644 --- a/beacon_chain/validator_induction/src/inductor.rs +++ b/beacon_chain/validator_induction/src/inductor.rs @@ -1,19 +1,19 @@ -use bls::{verify_proof_of_possession}; -use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus}; +use bls::verify_proof_of_possession; use spec::ChainSpec; +use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus}; #[derive(Debug, PartialEq, Clone)] pub enum ValidatorInductionError { InvalidShard, InvaidProofOfPossession, - InvalidWithdrawalCredentials + InvalidWithdrawalCredentials, } pub fn process_deposit( state: &mut BeaconState, deposit: &Deposit, - spec: &ChainSpec) --> Result { + spec: &ChainSpec, +) -> Result { let deposit_input = &deposit.deposit_data.deposit_input; let deposit_data = &deposit.deposit_data; @@ -22,18 +22,22 @@ pub fn process_deposit( return Err(ValidatorInductionError::InvaidProofOfPossession); } - let validator_index = state.validator_registry.iter() + let validator_index = state + .validator_registry + .iter() .position(|validator| validator.pubkey == deposit_input.pubkey); match validator_index { Some(i) => { - if state.validator_registry[i].withdrawal_credentials == deposit_input.withdrawal_credentials { + if state.validator_registry[i].withdrawal_credentials + == deposit_input.withdrawal_credentials + { state.validator_balances[i] += deposit_data.value; return Ok(i); } Err(ValidatorInductionError::InvalidWithdrawalCredentials) - }, + } None => { let validator = ValidatorRecord { pubkey: deposit_input.pubkey.clone(), @@ -45,7 +49,7 @@ pub fn process_deposit( exit_count: 0, custody_commitment: deposit_input.custody_commitment, latest_custody_reseed_slot: 0, - penultimate_custody_reseed_slot: 0 + penultimate_custody_reseed_slot: 0, }; match min_empty_validator_index(state, spec) { @@ -53,7 +57,7 @@ pub fn process_deposit( state.validator_registry[i] = validator; state.validator_balances[i] = deposit_data.value; Ok(i) - }, + } None => { state.validator_registry.push(validator); state.validator_balances.push(deposit_data.value); @@ -64,14 +68,13 @@ pub fn process_deposit( } } -fn min_empty_validator_index( - state: &BeaconState, - spec: &ChainSpec -) -> Option { +fn min_empty_validator_index(state: &BeaconState, spec: &ChainSpec) -> Option { for i in 0..state.validator_registry.len() { if state.validator_balances[i] == 0 && state.validator_registry[i].latest_status_change_slot - + spec.zero_balance_validator_ttl <= state.slot { + + spec.zero_balance_validator_ttl + <= state.slot + { return Some(i); } } @@ -107,7 +110,10 @@ mod tests { (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)) + & (verify_proof_of_possession( + &dep.deposit_data.deposit_input.proof_of_possession, + &val.pubkey, + )) } #[test] @@ -120,7 +126,10 @@ mod tests { let result = process_deposit(&mut state, &deposit, &spec); assert_eq!(result.unwrap(), 0); - assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); + assert!(deposit_equals_record( + &deposit, + &state.validator_registry[0] + )); assert_eq!(state.validator_registry.len(), 1); assert_eq!(state.validator_balances.len(), 1); } @@ -135,7 +144,10 @@ mod tests { 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!(deposit_equals_record( + &deposit, + &state.validator_registry[i] + )); assert_eq!(state.validator_registry.len(), i + 1); assert_eq!(state.validator_balances.len(), i + 1); } @@ -151,7 +163,8 @@ mod tests { 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.withdrawal_credentials = + deposit.deposit_data.deposit_input.withdrawal_credentials; validator.randao_commitment = deposit.deposit_data.deposit_input.randao_commitment; state.validator_registry.push(validator); @@ -160,7 +173,10 @@ mod tests { let result = process_deposit(&mut state, &deposit, &spec); assert_eq!(result.unwrap(), 0); - assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); + assert!(deposit_equals_record( + &deposit, + &state.validator_registry[0] + )); assert_eq!(state.validator_balances[0], DEPOSIT_GWEI * 2); assert_eq!(state.validator_registry.len(), 1); assert_eq!(state.validator_balances.len(), 1); @@ -183,7 +199,10 @@ mod tests { let result = process_deposit(&mut state, &deposit, &spec); assert_eq!(result.unwrap(), 0); - assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); + assert!(deposit_equals_record( + &deposit, + &state.validator_registry[0] + )); assert_eq!(state.validator_balances[0], DEPOSIT_GWEI); assert_eq!(state.validator_registry.len(), 1); assert_eq!(state.validator_balances.len(), 1); @@ -195,11 +214,15 @@ mod tests { let mut deposit = get_deposit(); let spec = ChainSpec::foundation(); deposit.deposit_data.value = DEPOSIT_GWEI; - deposit.deposit_data.deposit_input.proof_of_possession = create_proof_of_possession(&Keypair::random()); + deposit.deposit_data.deposit_input.proof_of_possession = + create_proof_of_possession(&Keypair::random()); let result = process_deposit(&mut state, &deposit, &spec); - assert_eq!(result, Err(ValidatorInductionError::InvaidProofOfPossession)); + assert_eq!( + result, + Err(ValidatorInductionError::InvaidProofOfPossession) + ); assert_eq!(state.validator_registry.len(), 0); assert_eq!(state.validator_balances.len(), 0); } diff --git a/beacon_chain/validator_induction/src/lib.rs b/beacon_chain/validator_induction/src/lib.rs index f6dec3cfae..7119d6c2eb 100644 --- a/beacon_chain/validator_induction/src/lib.rs +++ b/beacon_chain/validator_induction/src/lib.rs @@ -1,8 +1,8 @@ extern crate bls; extern crate hashing; -extern crate types; extern crate spec; +extern crate types; mod inductor; -pub use crate::inductor::{ValidatorInductionError, process_deposit}; +pub use crate::inductor::{process_deposit, ValidatorInductionError}; diff --git a/lighthouse/beacon_chain/tests/chain_test.rs b/lighthouse/beacon_chain/tests/chain_test.rs index aec6c08e66..8d2cfa501a 100644 --- a/lighthouse/beacon_chain/tests/chain_test.rs +++ b/lighthouse/beacon_chain/tests/chain_test.rs @@ -1,4 +1,4 @@ -use chain::{BlockProcessingOutcome, BeaconChain}; +use chain::{BeaconChain, BlockProcessingOutcome}; use db::{ stores::{BeaconBlockStore, BeaconStateStore}, MemoryDB, diff --git a/lighthouse/db/src/disk_db.rs b/lighthouse/db/src/disk_db.rs index b084f483f0..9d8a71bc45 100644 --- a/lighthouse/db/src/disk_db.rs +++ b/lighthouse/db/src/disk_db.rs @@ -44,7 +44,8 @@ impl DiskDB { let db = match columns { None => DB::open(&options, db_path), Some(columns) => DB::open_cf(&options, db_path, columns), - }.expect("Unable to open local database");; + } + .expect("Unable to open local database");; Self { db } } diff --git a/lighthouse/db/src/stores/beacon_state_store.rs b/lighthouse/db/src/stores/beacon_state_store.rs index 122b225094..a54e192495 100644 --- a/lighthouse/db/src/stores/beacon_state_store.rs +++ b/lighthouse/db/src/stores/beacon_state_store.rs @@ -42,10 +42,10 @@ mod tests { use super::super::super::MemoryDB; use super::*; - use std::sync::Arc; use ssz::ssz_encode; - use types::Hash256; + use std::sync::Arc; use types::test_utils::{SeedableRng, TestRandom, XorShiftRng}; + use types::Hash256; test_crud_for_store!(BeaconStateStore, DB_COLUMN); diff --git a/lighthouse/main.rs b/lighthouse/main.rs index b2c199db98..3102feaf79 100644 --- a/lighthouse/main.rs +++ b/lighthouse/main.rs @@ -12,8 +12,8 @@ mod config; use std::path::PathBuf; -use clap::{App, Arg}; use crate::config::LighthouseConfig; +use clap::{App, Arg}; use slog::Drain; fn main() { @@ -32,13 +32,15 @@ fn main() { .value_name("DIR") .help("Data directory for keys and databases.") .takes_value(true), - ).arg( + ) + .arg( Arg::with_name("port") .long("port") .value_name("PORT") .help("Network listen port for p2p connections.") .takes_value(true), - ).get_matches(); + ) + .get_matches(); let mut config = LighthouseConfig::default(); From 32d96995419dd1e0655fba308d6f2608a8157848 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Thu, 10 Jan 2019 14:59:15 -0600 Subject: [PATCH 2/6] Add `cargo fmt` as a build step to pass CI --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 3337de4209..0357e1c110 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: rust +cache: cargo script: + - cargo fmt --all -- --check - cargo build --verbose --all - cargo test --verbose --all rust: @@ -10,3 +12,6 @@ matrix: allow_failures: - rust: nightly fast_finish: true +install: + - (cargo install rustfmt || true) + - PATH=$PATH:/home/travis/.cargo/bin From bc053ba41535fbd8a56795ab76988e43d5ea70c1 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Thu, 10 Jan 2019 15:05:50 -0600 Subject: [PATCH 3/6] Tweaks for travis --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0357e1c110..8518c5d450 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ language: rust -cache: cargo script: - cargo fmt --all -- --check - cargo build --verbose --all @@ -13,5 +12,4 @@ matrix: - rust: nightly fast_finish: true install: - - (cargo install rustfmt || true) - - PATH=$PATH:/home/travis/.cargo/bin + - rustup component add rustfmt From 7979ec1635367e13ce3f7417ec37c9b4ec770e42 Mon Sep 17 00:00:00 2001 From: Kirk Baird Date: Mon, 14 Jan 2019 19:28:57 +1100 Subject: [PATCH 4/6] Modify BeaconState and BeaconBlockBody to reflect changes in the specs. Also add new variable LATEST_RANDAO_MIXES_LENGTH to foundation --- beacon_chain/genesis/src/beacon_block.rs | 3 ++ beacon_chain/genesis/src/beacon_state.rs | 11 +++-- beacon_chain/spec/src/foundation.rs | 1 + beacon_chain/spec/src/lib.rs | 1 + beacon_chain/types/src/beacon_block_body.rs | 21 +++++++++ beacon_chain/types/src/beacon_state.rs | 48 +++++++++++---------- 6 files changed, 59 insertions(+), 26 deletions(-) diff --git a/beacon_chain/genesis/src/beacon_block.rs b/beacon_chain/genesis/src/beacon_block.rs index 696ac6499f..a1bbdd3a53 100644 --- a/beacon_chain/genesis/src/beacon_block.rs +++ b/beacon_chain/genesis/src/beacon_block.rs @@ -16,6 +16,9 @@ pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBloc proposer_slashings: vec![], casper_slashings: vec![], attestations: vec![], + custody_reseeds: vec![], + custody_challenges: vec![], + custody_responses: vec![], deposits: vec![], exits: vec![], }, diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index f9c2ef3278..f8006a3c6d 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -47,11 +47,13 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { /* * Randomness and committees */ - randao_mix: spec.zero_hash, - next_seed: spec.zero_hash, + latest_randao_mixes: vec![spec.zero_hash; spec.latest_randao_mixes_length as usize], + latest_vdf_outputs: vec![spec.zero_hash; (spec.latest_randao_mixes_length / spec.epoch_length) as usize], shard_committees_at_slots: vec![], - persistent_committees: vec![], - persistent_committee_reassignments: vec![], + /* + * Custody challenges + */ + custody_challenges: vec![], /* * Finality */ @@ -66,6 +68,7 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { latest_block_roots: vec![spec.zero_hash; spec.epoch_length as usize], latest_penalized_exit_balances: vec![], latest_attestations: vec![], + batched_block_roots: vec![], /* * PoW receipt root */ diff --git a/beacon_chain/spec/src/foundation.rs b/beacon_chain/spec/src/foundation.rs index 31207f0582..1a493d72c8 100644 --- a/beacon_chain/spec/src/foundation.rs +++ b/beacon_chain/spec/src/foundation.rs @@ -24,6 +24,7 @@ impl ChainSpec { beacon_chain_shard_number: u64::max_value(), bls_withdrawal_prefix_byte: 0x00, max_casper_votes: 1_024, + latest_randao_mixes_length: 8_192, /* * Deposit contract */ diff --git a/beacon_chain/spec/src/lib.rs b/beacon_chain/spec/src/lib.rs index 786cf326b6..eb024cb6c2 100644 --- a/beacon_chain/spec/src/lib.rs +++ b/beacon_chain/spec/src/lib.rs @@ -18,6 +18,7 @@ pub struct ChainSpec { pub beacon_chain_shard_number: u64, pub bls_withdrawal_prefix_byte: u8, pub max_casper_votes: u64, + pub latest_randao_mixes_length: u64, /* * Deposit contract */ diff --git a/beacon_chain/types/src/beacon_block_body.rs b/beacon_chain/types/src/beacon_block_body.rs index 8bcf1af334..b5891d5924 100644 --- a/beacon_chain/types/src/beacon_block_body.rs +++ b/beacon_chain/types/src/beacon_block_body.rs @@ -3,11 +3,20 @@ use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing}; use crate::test_utils::TestRandom; use rand::RngCore; +// The following types are just dummy classes as they will not be defined until +// Phase 1 (Sharding phase) +type CustodyReseed = usize; +type CustodyChallenge = usize; +type CustodyResponse = usize; + #[derive(Debug, PartialEq, Clone, Default)] pub struct BeaconBlockBody { pub proposer_slashings: Vec, pub casper_slashings: Vec, pub attestations: Vec, + pub custody_reseeds: Vec, + pub custody_challenges: Vec, + pub custody_responses: Vec, pub deposits: Vec, pub exits: Vec, } @@ -17,6 +26,9 @@ impl Encodable for BeaconBlockBody { s.append_vec(&self.proposer_slashings); s.append_vec(&self.casper_slashings); s.append_vec(&self.attestations); + s.append_vec(&self.custody_reseeds); + s.append_vec(&self.custody_challenges); + s.append_vec(&self.custody_responses); s.append_vec(&self.deposits); s.append_vec(&self.exits); } @@ -27,6 +39,9 @@ impl Decodable for BeaconBlockBody { let (proposer_slashings, i) = <_>::ssz_decode(bytes, i)?; let (casper_slashings, i) = <_>::ssz_decode(bytes, i)?; let (attestations, i) = <_>::ssz_decode(bytes, i)?; + let (custody_reseeds, i) = <_>::ssz_decode(bytes, i)?; + let (custody_challenges, i) = <_>::ssz_decode(bytes, i)?; + let (custody_responses, i) = <_>::ssz_decode(bytes, i)?; let (deposits, i) = <_>::ssz_decode(bytes, i)?; let (exits, i) = <_>::ssz_decode(bytes, i)?; @@ -35,6 +50,9 @@ impl Decodable for BeaconBlockBody { proposer_slashings, casper_slashings, attestations, + custody_reseeds, + custody_challenges, + custody_responses, deposits, exits, }, @@ -49,6 +67,9 @@ impl TestRandom for BeaconBlockBody { proposer_slashings: <_>::random_for_test(rng), casper_slashings: <_>::random_for_test(rng), attestations: <_>::random_for_test(rng), + custody_reseeds: <_>::random_for_test(rng), + custody_challenges: <_>::random_for_test(rng), + custody_responses: <_>::random_for_test(rng), deposits: <_>::random_for_test(rng), exits: <_>::random_for_test(rng), } diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index 588cfb6788..b9994bdddb 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -3,7 +3,6 @@ use super::crosslink_record::CrosslinkRecord; use super::fork_data::ForkData; use super::pending_attestation_record::PendingAttestationRecord; use super::shard_committee::ShardCommittee; -use super::shard_reassignment_record::ShardReassignmentRecord; use super::validator_record::ValidatorRecord; use super::Hash256; use crate::test_utils::TestRandom; @@ -11,6 +10,9 @@ use hashing::canonical_hash; use rand::RngCore; use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; +// Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummay class used. +type CustodyChallenge = usize; + #[derive(Debug, PartialEq, Clone, Default)] pub struct BeaconState { // Misc @@ -26,11 +28,12 @@ pub struct BeaconState { pub validator_registry_delta_chain_tip: Hash256, // Randomness and committees - pub randao_mix: Hash256, - pub next_seed: Hash256, + pub latest_randao_mixes: Vec, + pub latest_vdf_outputs: Vec, pub shard_committees_at_slots: Vec>, - pub persistent_committees: Vec>, - pub persistent_committee_reassignments: Vec, + + // Custody challenges + pub custody_challenges: Vec, // Finality pub previous_justified_slot: u64, @@ -43,8 +46,9 @@ pub struct BeaconState { pub latest_block_roots: Vec, pub latest_penalized_exit_balances: Vec, pub latest_attestations: Vec, + pub batched_block_roots: Vec, - // PoW receipt root + // PoW receipt root (a.k.a. deposit root) pub processed_pow_receipt_root: Hash256, pub candidate_pow_receipt_roots: Vec, } @@ -67,11 +71,10 @@ impl Encodable for BeaconState { s.append(&self.validator_registry_latest_change_slot); s.append(&self.validator_registry_exit_count); s.append(&self.validator_registry_delta_chain_tip); - s.append(&self.randao_mix); - s.append(&self.next_seed); + s.append(&self.latest_randao_mixes); + s.append(&self.latest_vdf_outputs); s.append(&self.shard_committees_at_slots); - s.append(&self.persistent_committees); - s.append(&self.persistent_committee_reassignments); + s.append(&self.custody_challenges); s.append(&self.previous_justified_slot); s.append(&self.justified_slot); s.append(&self.justification_bitfield); @@ -80,6 +83,7 @@ impl Encodable for BeaconState { s.append(&self.latest_block_roots); s.append(&self.latest_penalized_exit_balances); s.append(&self.latest_attestations); + s.append(&self.batched_block_roots); s.append(&self.processed_pow_receipt_root); s.append(&self.candidate_pow_receipt_roots); } @@ -95,11 +99,10 @@ impl Decodable for BeaconState { 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)?; - let (randao_mix, i) = <_>::ssz_decode(bytes, i)?; - let (next_seed, i) = <_>::ssz_decode(bytes, i)?; + let (latest_randao_mixes, i) = <_>::ssz_decode(bytes, i)?; + let (latest_vdf_outputs, i) = <_>::ssz_decode(bytes, i)?; let (shard_committees_at_slots, i) = <_>::ssz_decode(bytes, i)?; - let (persistent_committees, i) = <_>::ssz_decode(bytes, i)?; - let (persistent_committee_reassignments, i) = <_>::ssz_decode(bytes, i)?; + let (custody_challenges, i) = <_>::ssz_decode(bytes, i)?; let (previous_justified_slot, i) = <_>::ssz_decode(bytes, i)?; let (justified_slot, i) = <_>::ssz_decode(bytes, i)?; let (justification_bitfield, i) = <_>::ssz_decode(bytes, i)?; @@ -108,6 +111,7 @@ impl Decodable for BeaconState { let (latest_block_roots, i) = <_>::ssz_decode(bytes, i)?; let (latest_penalized_exit_balances, i) = <_>::ssz_decode(bytes, i)?; let (latest_attestations, i) = <_>::ssz_decode(bytes, i)?; + let (batched_block_roots, i) = <_>::ssz_decode(bytes, i)?; let (processed_pow_receipt_root, i) = <_>::ssz_decode(bytes, i)?; let (candidate_pow_receipt_roots, i) = <_>::ssz_decode(bytes, i)?; @@ -121,11 +125,10 @@ impl Decodable for BeaconState { validator_registry_latest_change_slot, validator_registry_exit_count, validator_registry_delta_chain_tip, - randao_mix, - next_seed, + latest_randao_mixes, + latest_vdf_outputs, shard_committees_at_slots, - persistent_committees, - persistent_committee_reassignments, + custody_challenges, previous_justified_slot, justified_slot, justification_bitfield, @@ -134,6 +137,7 @@ impl Decodable for BeaconState { latest_block_roots, latest_penalized_exit_balances, latest_attestations, + batched_block_roots, processed_pow_receipt_root, candidate_pow_receipt_roots, }, @@ -153,11 +157,10 @@ impl TestRandom for BeaconState { 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), - randao_mix: <_>::random_for_test(rng), - next_seed: <_>::random_for_test(rng), + latest_randao_mixes: <_>::random_for_test(rng), + latest_vdf_outputs: <_>::random_for_test(rng), shard_committees_at_slots: <_>::random_for_test(rng), - persistent_committees: <_>::random_for_test(rng), - persistent_committee_reassignments: <_>::random_for_test(rng), + custody_challenges: <_>::random_for_test(rng), previous_justified_slot: <_>::random_for_test(rng), justified_slot: <_>::random_for_test(rng), justification_bitfield: <_>::random_for_test(rng), @@ -166,6 +169,7 @@ impl TestRandom for BeaconState { latest_block_roots: <_>::random_for_test(rng), latest_penalized_exit_balances: <_>::random_for_test(rng), latest_attestations: <_>::random_for_test(rng), + batched_block_roots: <_>::random_for_test(rng), processed_pow_receipt_root: <_>::random_for_test(rng), candidate_pow_receipt_roots: <_>::random_for_test(rng), } From ca28c187097d4bbb56f22a629048c84f97047363 Mon Sep 17 00:00:00 2001 From: Kirk Baird Date: Mon, 14 Jan 2019 19:28:57 +1100 Subject: [PATCH 5/6] Modify BeaconState and BeaconBlockBody to reflect changes in the specs. Also add new variable LATEST_RANDAO_MIXES_LENGTH to foundation Signed-off-by: Kirk Baird --- beacon_chain/genesis/src/beacon_block.rs | 3 ++ beacon_chain/genesis/src/beacon_state.rs | 11 +++-- beacon_chain/spec/src/foundation.rs | 1 + beacon_chain/spec/src/lib.rs | 1 + beacon_chain/types/src/beacon_block_body.rs | 21 +++++++++ beacon_chain/types/src/beacon_state.rs | 48 +++++++++++---------- 6 files changed, 59 insertions(+), 26 deletions(-) diff --git a/beacon_chain/genesis/src/beacon_block.rs b/beacon_chain/genesis/src/beacon_block.rs index 696ac6499f..a1bbdd3a53 100644 --- a/beacon_chain/genesis/src/beacon_block.rs +++ b/beacon_chain/genesis/src/beacon_block.rs @@ -16,6 +16,9 @@ pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBloc proposer_slashings: vec![], casper_slashings: vec![], attestations: vec![], + custody_reseeds: vec![], + custody_challenges: vec![], + custody_responses: vec![], deposits: vec![], exits: vec![], }, diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index f9c2ef3278..f8006a3c6d 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -47,11 +47,13 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { /* * Randomness and committees */ - randao_mix: spec.zero_hash, - next_seed: spec.zero_hash, + latest_randao_mixes: vec![spec.zero_hash; spec.latest_randao_mixes_length as usize], + latest_vdf_outputs: vec![spec.zero_hash; (spec.latest_randao_mixes_length / spec.epoch_length) as usize], shard_committees_at_slots: vec![], - persistent_committees: vec![], - persistent_committee_reassignments: vec![], + /* + * Custody challenges + */ + custody_challenges: vec![], /* * Finality */ @@ -66,6 +68,7 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { latest_block_roots: vec![spec.zero_hash; spec.epoch_length as usize], latest_penalized_exit_balances: vec![], latest_attestations: vec![], + batched_block_roots: vec![], /* * PoW receipt root */ diff --git a/beacon_chain/spec/src/foundation.rs b/beacon_chain/spec/src/foundation.rs index 31207f0582..1a493d72c8 100644 --- a/beacon_chain/spec/src/foundation.rs +++ b/beacon_chain/spec/src/foundation.rs @@ -24,6 +24,7 @@ impl ChainSpec { beacon_chain_shard_number: u64::max_value(), bls_withdrawal_prefix_byte: 0x00, max_casper_votes: 1_024, + latest_randao_mixes_length: 8_192, /* * Deposit contract */ diff --git a/beacon_chain/spec/src/lib.rs b/beacon_chain/spec/src/lib.rs index 786cf326b6..eb024cb6c2 100644 --- a/beacon_chain/spec/src/lib.rs +++ b/beacon_chain/spec/src/lib.rs @@ -18,6 +18,7 @@ pub struct ChainSpec { pub beacon_chain_shard_number: u64, pub bls_withdrawal_prefix_byte: u8, pub max_casper_votes: u64, + pub latest_randao_mixes_length: u64, /* * Deposit contract */ diff --git a/beacon_chain/types/src/beacon_block_body.rs b/beacon_chain/types/src/beacon_block_body.rs index 8bcf1af334..b5891d5924 100644 --- a/beacon_chain/types/src/beacon_block_body.rs +++ b/beacon_chain/types/src/beacon_block_body.rs @@ -3,11 +3,20 @@ use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing}; use crate::test_utils::TestRandom; use rand::RngCore; +// The following types are just dummy classes as they will not be defined until +// Phase 1 (Sharding phase) +type CustodyReseed = usize; +type CustodyChallenge = usize; +type CustodyResponse = usize; + #[derive(Debug, PartialEq, Clone, Default)] pub struct BeaconBlockBody { pub proposer_slashings: Vec, pub casper_slashings: Vec, pub attestations: Vec, + pub custody_reseeds: Vec, + pub custody_challenges: Vec, + pub custody_responses: Vec, pub deposits: Vec, pub exits: Vec, } @@ -17,6 +26,9 @@ impl Encodable for BeaconBlockBody { s.append_vec(&self.proposer_slashings); s.append_vec(&self.casper_slashings); s.append_vec(&self.attestations); + s.append_vec(&self.custody_reseeds); + s.append_vec(&self.custody_challenges); + s.append_vec(&self.custody_responses); s.append_vec(&self.deposits); s.append_vec(&self.exits); } @@ -27,6 +39,9 @@ impl Decodable for BeaconBlockBody { let (proposer_slashings, i) = <_>::ssz_decode(bytes, i)?; let (casper_slashings, i) = <_>::ssz_decode(bytes, i)?; let (attestations, i) = <_>::ssz_decode(bytes, i)?; + let (custody_reseeds, i) = <_>::ssz_decode(bytes, i)?; + let (custody_challenges, i) = <_>::ssz_decode(bytes, i)?; + let (custody_responses, i) = <_>::ssz_decode(bytes, i)?; let (deposits, i) = <_>::ssz_decode(bytes, i)?; let (exits, i) = <_>::ssz_decode(bytes, i)?; @@ -35,6 +50,9 @@ impl Decodable for BeaconBlockBody { proposer_slashings, casper_slashings, attestations, + custody_reseeds, + custody_challenges, + custody_responses, deposits, exits, }, @@ -49,6 +67,9 @@ impl TestRandom for BeaconBlockBody { proposer_slashings: <_>::random_for_test(rng), casper_slashings: <_>::random_for_test(rng), attestations: <_>::random_for_test(rng), + custody_reseeds: <_>::random_for_test(rng), + custody_challenges: <_>::random_for_test(rng), + custody_responses: <_>::random_for_test(rng), deposits: <_>::random_for_test(rng), exits: <_>::random_for_test(rng), } diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index 588cfb6788..b9994bdddb 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -3,7 +3,6 @@ use super::crosslink_record::CrosslinkRecord; use super::fork_data::ForkData; use super::pending_attestation_record::PendingAttestationRecord; use super::shard_committee::ShardCommittee; -use super::shard_reassignment_record::ShardReassignmentRecord; use super::validator_record::ValidatorRecord; use super::Hash256; use crate::test_utils::TestRandom; @@ -11,6 +10,9 @@ use hashing::canonical_hash; use rand::RngCore; use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; +// Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummay class used. +type CustodyChallenge = usize; + #[derive(Debug, PartialEq, Clone, Default)] pub struct BeaconState { // Misc @@ -26,11 +28,12 @@ pub struct BeaconState { pub validator_registry_delta_chain_tip: Hash256, // Randomness and committees - pub randao_mix: Hash256, - pub next_seed: Hash256, + pub latest_randao_mixes: Vec, + pub latest_vdf_outputs: Vec, pub shard_committees_at_slots: Vec>, - pub persistent_committees: Vec>, - pub persistent_committee_reassignments: Vec, + + // Custody challenges + pub custody_challenges: Vec, // Finality pub previous_justified_slot: u64, @@ -43,8 +46,9 @@ pub struct BeaconState { pub latest_block_roots: Vec, pub latest_penalized_exit_balances: Vec, pub latest_attestations: Vec, + pub batched_block_roots: Vec, - // PoW receipt root + // PoW receipt root (a.k.a. deposit root) pub processed_pow_receipt_root: Hash256, pub candidate_pow_receipt_roots: Vec, } @@ -67,11 +71,10 @@ impl Encodable for BeaconState { s.append(&self.validator_registry_latest_change_slot); s.append(&self.validator_registry_exit_count); s.append(&self.validator_registry_delta_chain_tip); - s.append(&self.randao_mix); - s.append(&self.next_seed); + s.append(&self.latest_randao_mixes); + s.append(&self.latest_vdf_outputs); s.append(&self.shard_committees_at_slots); - s.append(&self.persistent_committees); - s.append(&self.persistent_committee_reassignments); + s.append(&self.custody_challenges); s.append(&self.previous_justified_slot); s.append(&self.justified_slot); s.append(&self.justification_bitfield); @@ -80,6 +83,7 @@ impl Encodable for BeaconState { s.append(&self.latest_block_roots); s.append(&self.latest_penalized_exit_balances); s.append(&self.latest_attestations); + s.append(&self.batched_block_roots); s.append(&self.processed_pow_receipt_root); s.append(&self.candidate_pow_receipt_roots); } @@ -95,11 +99,10 @@ impl Decodable for BeaconState { 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)?; - let (randao_mix, i) = <_>::ssz_decode(bytes, i)?; - let (next_seed, i) = <_>::ssz_decode(bytes, i)?; + let (latest_randao_mixes, i) = <_>::ssz_decode(bytes, i)?; + let (latest_vdf_outputs, i) = <_>::ssz_decode(bytes, i)?; let (shard_committees_at_slots, i) = <_>::ssz_decode(bytes, i)?; - let (persistent_committees, i) = <_>::ssz_decode(bytes, i)?; - let (persistent_committee_reassignments, i) = <_>::ssz_decode(bytes, i)?; + let (custody_challenges, i) = <_>::ssz_decode(bytes, i)?; let (previous_justified_slot, i) = <_>::ssz_decode(bytes, i)?; let (justified_slot, i) = <_>::ssz_decode(bytes, i)?; let (justification_bitfield, i) = <_>::ssz_decode(bytes, i)?; @@ -108,6 +111,7 @@ impl Decodable for BeaconState { let (latest_block_roots, i) = <_>::ssz_decode(bytes, i)?; let (latest_penalized_exit_balances, i) = <_>::ssz_decode(bytes, i)?; let (latest_attestations, i) = <_>::ssz_decode(bytes, i)?; + let (batched_block_roots, i) = <_>::ssz_decode(bytes, i)?; let (processed_pow_receipt_root, i) = <_>::ssz_decode(bytes, i)?; let (candidate_pow_receipt_roots, i) = <_>::ssz_decode(bytes, i)?; @@ -121,11 +125,10 @@ impl Decodable for BeaconState { validator_registry_latest_change_slot, validator_registry_exit_count, validator_registry_delta_chain_tip, - randao_mix, - next_seed, + latest_randao_mixes, + latest_vdf_outputs, shard_committees_at_slots, - persistent_committees, - persistent_committee_reassignments, + custody_challenges, previous_justified_slot, justified_slot, justification_bitfield, @@ -134,6 +137,7 @@ impl Decodable for BeaconState { latest_block_roots, latest_penalized_exit_balances, latest_attestations, + batched_block_roots, processed_pow_receipt_root, candidate_pow_receipt_roots, }, @@ -153,11 +157,10 @@ impl TestRandom for BeaconState { 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), - randao_mix: <_>::random_for_test(rng), - next_seed: <_>::random_for_test(rng), + latest_randao_mixes: <_>::random_for_test(rng), + latest_vdf_outputs: <_>::random_for_test(rng), shard_committees_at_slots: <_>::random_for_test(rng), - persistent_committees: <_>::random_for_test(rng), - persistent_committee_reassignments: <_>::random_for_test(rng), + custody_challenges: <_>::random_for_test(rng), previous_justified_slot: <_>::random_for_test(rng), justified_slot: <_>::random_for_test(rng), justification_bitfield: <_>::random_for_test(rng), @@ -166,6 +169,7 @@ impl TestRandom for BeaconState { latest_block_roots: <_>::random_for_test(rng), latest_penalized_exit_balances: <_>::random_for_test(rng), latest_attestations: <_>::random_for_test(rng), + batched_block_roots: <_>::random_for_test(rng), processed_pow_receipt_root: <_>::random_for_test(rng), candidate_pow_receipt_roots: <_>::random_for_test(rng), } From 730281ed7d7fd69373cf954874c473098793c8e7 Mon Sep 17 00:00:00 2001 From: Kirk Baird Date: Tue, 15 Jan 2019 10:37:08 +1100 Subject: [PATCH 6/6] modify formatting --- beacon_chain/genesis/src/beacon_state.rs | 5 ++++- beacon_chain/types/src/beacon_block_body.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index f8006a3c6d..f8c7eb5397 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -48,7 +48,10 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { * Randomness and committees */ latest_randao_mixes: vec![spec.zero_hash; spec.latest_randao_mixes_length as usize], - latest_vdf_outputs: vec![spec.zero_hash; (spec.latest_randao_mixes_length / spec.epoch_length) as usize], + latest_vdf_outputs: vec![ + spec.zero_hash; + (spec.latest_randao_mixes_length / spec.epoch_length) as usize + ], shard_committees_at_slots: vec![], /* * Custody challenges diff --git a/beacon_chain/types/src/beacon_block_body.rs b/beacon_chain/types/src/beacon_block_body.rs index b5891d5924..67fa34d917 100644 --- a/beacon_chain/types/src/beacon_block_body.rs +++ b/beacon_chain/types/src/beacon_block_body.rs @@ -3,7 +3,7 @@ use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing}; use crate::test_utils::TestRandom; use rand::RngCore; -// The following types are just dummy classes as they will not be defined until +// The following types are just dummy classes as they will not be defined until // Phase 1 (Sharding phase) type CustodyReseed = usize; type CustodyChallenge = usize;