Merge branch 'master' into chain-update

This commit is contained in:
Paul Hauner
2019-01-09 10:11:41 +11:00
17 changed files with 266 additions and 461 deletions

View File

@@ -1,17 +0,0 @@
use super::Hash256;
use super::{Attestation, SpecialRecord};
#[derive(Debug, PartialEq)]
pub struct ActiveState {
pub pending_attestations: Vec<Attestation>,
pub pending_specials: Vec<SpecialRecord>,
pub recent_block_hashes: Vec<Hash256>,
pub randao_mix: Hash256,
}
impl ActiveState {
// TODO: implement this.
pub fn canonical_root(&self) -> Hash256 {
Hash256::zero()
}
}

View File

@@ -11,7 +11,7 @@ use hashing::canonical_hash;
use rand::RngCore;
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Default)]
pub struct BeaconState {
// Misc
pub slot: u64,
@@ -20,6 +20,7 @@ pub struct BeaconState {
// Validator registry
pub validator_registry: Vec<ValidatorRecord>,
pub validator_balances: Vec<u64>,
pub validator_registry_latest_change_slot: u64,
pub validator_registry_exit_count: u64,
pub validator_registry_delta_chain_tip: Hash256,
@@ -62,6 +63,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 +91,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 +117,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 +149,7 @@ impl<T: RngCore> TestRandom<T> 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),

View File

@@ -1,72 +0,0 @@
use super::ValidatorRegistration;
#[derive(Debug, Clone, PartialEq)]
pub struct ChainConfig {
// Old, potentially outdated constants
pub cycle_length: u8,
pub deposit_size_gwei: u64,
pub shard_count: u16,
pub min_committee_size: u64,
pub max_validator_churn_quotient: u64,
pub genesis_time: u64,
pub slot_duration_millis: u64,
pub initial_validators: Vec<ValidatorRegistration>,
// New constants
pub epoch_length: u64,
pub min_attestation_inclusion_delay: u64,
}
/*
* Presently this is just some arbitrary time in Sept 2018.
*/
const TEST_GENESIS_TIME: u64 = 1_537_488_655;
impl ChainConfig {
pub fn standard() -> Self {
Self {
cycle_length: 64,
deposit_size_gwei: 32 * (10 ^ 9),
shard_count: 1024,
min_committee_size: 128,
max_validator_churn_quotient: 32,
genesis_time: TEST_GENESIS_TIME,
slot_duration_millis: 16 * 1000,
initial_validators: vec![],
// New
epoch_length: 64,
min_attestation_inclusion_delay: 4,
}
}
pub fn validate(&self) -> bool {
// criteria that ensure the config is valid
// shard_count / cycle_length > 0 otherwise validator delegation
// will fail.
if self.shard_count / u16::from(self.cycle_length) == 0 {
return false;
}
true
}
#[cfg(test)]
pub fn super_fast_tests() -> Self {
Self {
cycle_length: 2,
deposit_size_gwei: 32 * (10 ^ 9),
shard_count: 2,
min_committee_size: 2,
max_validator_churn_quotient: 32,
genesis_time: TEST_GENESIS_TIME, // arbitrary
slot_duration_millis: 16 * 1000,
initial_validators: vec![],
// New constants
epoch_length: 64,
min_attestation_inclusion_delay: 4,
}
}
}

View File

@@ -1,28 +0,0 @@
use super::crosslink_record::CrosslinkRecord;
use super::shard_committee::ShardCommittee;
use super::validator_record::ValidatorRecord;
use super::Hash256;
#[derive(Debug, PartialEq)]
pub struct CrystallizedState {
pub validator_set_change_slot: u64,
pub validators: Vec<ValidatorRecord>,
pub crosslinks: Vec<CrosslinkRecord>,
pub last_state_recalculation_slot: u64,
pub last_finalized_slot: u64,
pub last_justified_slot: u64,
pub justified_streak: u64,
pub shard_and_committee_for_slots: Vec<Vec<ShardCommittee>>,
pub deposits_penalized_in_period: Vec<u32>,
pub validator_set_delta_hash_chain: Hash256,
pub pre_fork_version: u32,
pub post_fork_version: u32,
pub fork_slot_number: u32,
}
impl CrystallizedState {
// TODO: implement this.
pub fn canonical_root(&self) -> Hash256 {
Hash256::zero()
}
}

View File

@@ -9,6 +9,7 @@ pub struct DepositInput {
pub pubkey: PublicKey,
pub withdrawal_credentials: Hash256,
pub randao_commitment: Hash256,
pub custody_commitment: Hash256,
pub proof_of_possession: Signature,
}
@@ -17,6 +18,7 @@ impl Encodable for DepositInput {
s.append(&self.pubkey);
s.append(&self.withdrawal_credentials);
s.append(&self.randao_commitment);
s.append(&self.custody_commitment);
s.append(&self.proof_of_possession);
}
}
@@ -26,6 +28,7 @@ impl Decodable for DepositInput {
let (pubkey, i) = <_>::ssz_decode(bytes, i)?;
let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?;
let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
let (custody_commitment, i) = <_>::ssz_decode(bytes, i)?;
let (proof_of_possession, i) = <_>::ssz_decode(bytes, i)?;
Ok((
@@ -33,6 +36,7 @@ impl Decodable for DepositInput {
pubkey,
withdrawal_credentials,
randao_commitment,
custody_commitment,
proof_of_possession,
},
i,
@@ -46,6 +50,7 @@ impl<T: RngCore> TestRandom<T> for DepositInput {
pubkey: <_>::random_for_test(rng),
withdrawal_credentials: <_>::random_for_test(rng),
randao_commitment: <_>::random_for_test(rng),
custody_commitment: <_>::random_for_test(rng),
proof_of_possession: <_>::random_for_test(rng),
}
}

View File

@@ -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,

View File

@@ -5,7 +5,6 @@ extern crate ssz;
pub mod test_utils;
pub mod active_state;
pub mod attestation;
pub mod attestation_data;
pub mod beacon_block;
@@ -13,9 +12,7 @@ pub mod beacon_block_body;
pub mod beacon_state;
pub mod candidate_pow_receipt_root_record;
pub mod casper_slashing;
pub mod chain_config;
pub mod crosslink_record;
pub mod crystallized_state;
pub mod deposit;
pub mod deposit_data;
pub mod deposit_input;
@@ -29,23 +26,19 @@ pub mod shard_reassignment_record;
pub mod slashable_vote_data;
pub mod special_record;
pub mod validator_record;
pub mod validator_registration;
pub mod readers;
use self::ethereum_types::{H160, H256, U256};
use std::collections::HashMap;
pub use crate::active_state::ActiveState;
pub use crate::attestation::Attestation;
pub use crate::attestation_data::AttestationData;
pub use crate::beacon_block::BeaconBlock;
pub use crate::beacon_block_body::BeaconBlockBody;
pub use crate::beacon_state::BeaconState;
pub use crate::casper_slashing::CasperSlashing;
pub use crate::chain_config::ChainConfig;
pub use crate::crosslink_record::CrosslinkRecord;
pub use crate::crystallized_state::CrystallizedState;
pub use crate::deposit::Deposit;
pub use crate::deposit_data::DepositData;
pub use crate::deposit_input::DepositInput;
@@ -58,7 +51,6 @@ pub use crate::shard_committee::ShardCommittee;
pub use crate::slashable_vote_data::SlashableVoteData;
pub use crate::special_record::{SpecialRecord, SpecialRecordKind};
pub use crate::validator_record::{ValidatorRecord, ValidatorStatus};
pub use crate::validator_registration::ValidatorRegistration;
pub type Hash256 = H256;
pub type Address = H160;

View File

@@ -1,5 +1,5 @@
use super::bls::PublicKey;
use super::{Address, Hash256};
use super::{Hash256};
use crate::test_utils::TestRandom;
use rand::RngCore;
use ssz::{Decodable, DecodeError, Encodable, SszStream};
@@ -32,13 +32,15 @@ impl convert::From<u8> for ValidatorStatus {
#[derive(Debug, Clone, PartialEq)]
pub struct ValidatorRecord {
pub pubkey: PublicKey,
pub withdrawal_shard: u64,
pub withdrawal_address: Address,
pub withdrawal_credentials: Hash256,
pub randao_commitment: Hash256,
pub randao_last_change: u64,
pub balance: u64,
pub randao_layers: u64,
pub status: ValidatorStatus,
pub exit_slot: u64,
pub latest_status_change_slot: u64,
pub exit_count: u64,
pub custody_commitment: Hash256,
pub latest_custody_reseed_slot: u64,
pub penultimate_custody_reseed_slot: u64
}
impl ValidatorRecord {
@@ -94,37 +96,43 @@ impl<T: RngCore> TestRandom<T> for ValidatorStatus {
impl Encodable for ValidatorRecord {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.pubkey);
s.append(&self.withdrawal_shard);
s.append(&self.withdrawal_address);
s.append(&self.withdrawal_credentials);
s.append(&self.randao_commitment);
s.append(&self.randao_last_change);
s.append(&self.balance);
s.append(&self.randao_layers);
s.append(&self.status);
s.append(&self.exit_slot);
s.append(&self.latest_status_change_slot);
s.append(&self.exit_count);
s.append(&self.custody_commitment);
s.append(&self.latest_custody_reseed_slot);
s.append(&self.penultimate_custody_reseed_slot);
}
}
impl Decodable for ValidatorRecord {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (pubkey, i) = <_>::ssz_decode(bytes, i)?;
let (withdrawal_shard, i) = <_>::ssz_decode(bytes, i)?;
let (withdrawal_address, i) = <_>::ssz_decode(bytes, i)?;
let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?;
let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
let (randao_last_change, i) = <_>::ssz_decode(bytes, i)?;
let (balance, i) = <_>::ssz_decode(bytes, i)?;
let (randao_layers, i) = <_>::ssz_decode(bytes, i)?;
let (status, i) = <_>::ssz_decode(bytes, i)?;
let (exit_slot, i) = <_>::ssz_decode(bytes, i)?;
let (latest_status_change_slot, i) = <_>::ssz_decode(bytes, i)?;
let (exit_count, i) = <_>::ssz_decode(bytes, i)?;
let (custody_commitment, i) = <_>::ssz_decode(bytes, i)?;
let (latest_custody_reseed_slot, i) = <_>::ssz_decode(bytes, i)?;
let (penultimate_custody_reseed_slot, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
pubkey,
withdrawal_shard,
withdrawal_address,
withdrawal_credentials,
randao_commitment,
randao_last_change,
balance,
randao_layers,
status,
exit_slot,
latest_status_change_slot,
exit_count,
custody_commitment,
latest_custody_reseed_slot,
penultimate_custody_reseed_slot
},
i,
))
@@ -135,13 +143,15 @@ impl<T: RngCore> TestRandom<T> for ValidatorRecord {
fn random_for_test(rng: &mut T) -> Self {
Self {
pubkey: <_>::random_for_test(rng),
withdrawal_shard: <_>::random_for_test(rng),
withdrawal_address: <_>::random_for_test(rng),
withdrawal_credentials: <_>::random_for_test(rng),
randao_commitment: <_>::random_for_test(rng),
randao_last_change: <_>::random_for_test(rng),
balance: <_>::random_for_test(rng),
randao_layers: <_>::random_for_test(rng),
status: <_>::random_for_test(rng),
exit_slot: <_>::random_for_test(rng),
latest_status_change_slot: <_>::random_for_test(rng),
exit_count: <_>::random_for_test(rng),
custody_commitment: <_>::random_for_test(rng),
latest_custody_reseed_slot: <_>::random_for_test(rng),
penultimate_custody_reseed_slot: <_>::random_for_test(rng),
}
}
}