mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-20 13:24:44 +00:00
Rename beacon_chain/ -> eth2/
This commit is contained in:
89
eth2/genesis/src/beacon_block.rs
Normal file
89
eth2/genesis/src/beacon_block.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use spec::ChainSpec;
|
||||
use types::{BeaconBlock, BeaconBlockBody, Hash256};
|
||||
|
||||
/// Generate a genesis BeaconBlock.
|
||||
pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBlock {
|
||||
BeaconBlock {
|
||||
slot: spec.genesis_slot_number,
|
||||
parent_root: spec.zero_hash,
|
||||
state_root,
|
||||
randao_reveal: spec.zero_hash,
|
||||
candidate_pow_receipt_root: spec.zero_hash,
|
||||
signature: spec.empty_signature.clone(),
|
||||
body: BeaconBlockBody {
|
||||
proposer_slashings: vec![],
|
||||
casper_slashings: vec![],
|
||||
attestations: vec![],
|
||||
custody_reseeds: vec![],
|
||||
custody_challenges: vec![],
|
||||
custody_responses: vec![],
|
||||
deposits: vec![],
|
||||
exits: vec![],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use bls::Signature;
|
||||
|
||||
#[test]
|
||||
fn test_genesis() {
|
||||
let spec = ChainSpec::foundation();
|
||||
let state_root = Hash256::from("cats".as_bytes());
|
||||
|
||||
// This only checks that the function runs without panic.
|
||||
genesis_beacon_block(state_root, &spec);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero_items() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state_root = Hash256::zero();
|
||||
|
||||
let genesis_block = genesis_beacon_block(state_root, &spec);
|
||||
|
||||
assert!(genesis_block.slot == 0);
|
||||
assert!(genesis_block.parent_root.is_zero());
|
||||
assert!(genesis_block.randao_reveal.is_zero());
|
||||
assert!(genesis_block.candidate_pow_receipt_root.is_zero()); // aka deposit_root
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_beacon_body() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state_root = Hash256::zero();
|
||||
|
||||
let genesis_block = genesis_beacon_block(state_root, &spec);
|
||||
|
||||
// Custody items are not being implemented until phase 1 so tests to be added later
|
||||
|
||||
assert!(genesis_block.body.proposer_slashings.is_empty());
|
||||
assert!(genesis_block.body.casper_slashings.is_empty());
|
||||
assert!(genesis_block.body.attestations.is_empty());
|
||||
assert!(genesis_block.body.deposits.is_empty());
|
||||
assert!(genesis_block.body.exits.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_signature() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state_root = Hash256::zero();
|
||||
|
||||
let genesis_block = genesis_beacon_block(state_root, &spec);
|
||||
|
||||
// Signature should consist of [bytes48(0), bytes48(0)]
|
||||
// Note this is implemented using Apache Milagro BLS which requires one extra byte -> 97bytes
|
||||
let raw_sig = genesis_block.signature.as_raw();
|
||||
let raw_sig_bytes = raw_sig.as_bytes();
|
||||
|
||||
for item in raw_sig_bytes.iter() {
|
||||
assert!(*item == 0);
|
||||
}
|
||||
assert_eq!(genesis_block.signature, Signature::empty_signature());
|
||||
}
|
||||
}
|
||||
222
eth2/genesis/src/beacon_state.rs
Normal file
222
eth2/genesis/src/beacon_state.rs
Normal file
@@ -0,0 +1,222 @@
|
||||
use spec::ChainSpec;
|
||||
use types::{BeaconState, CrosslinkRecord, ForkData};
|
||||
use validator_shuffling::{shard_and_committees_for_cycle, ValidatorAssignmentError};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
NoValidators,
|
||||
ValidationAssignmentError(ValidatorAssignmentError),
|
||||
NotImplemented,
|
||||
}
|
||||
|
||||
pub fn genesis_beacon_state(spec: &ChainSpec) -> Result<BeaconState, Error> {
|
||||
/*
|
||||
* Assign the validators to shards, using all zeros as the seed.
|
||||
*/
|
||||
let _shard_and_committee_for_slots = {
|
||||
let mut a = shard_and_committees_for_cycle(&[0; 32], &spec.initial_validators, 0, &spec)?;
|
||||
let mut b = a.clone();
|
||||
a.append(&mut b);
|
||||
a
|
||||
};
|
||||
|
||||
let initial_crosslink = CrosslinkRecord {
|
||||
slot: spec.genesis_slot_number,
|
||||
shard_block_root: spec.zero_hash,
|
||||
};
|
||||
|
||||
Ok(BeaconState {
|
||||
/*
|
||||
* Misc
|
||||
*/
|
||||
slot: spec.genesis_slot_number,
|
||||
genesis_time: spec.genesis_time,
|
||||
fork_data: ForkData {
|
||||
pre_fork_version: spec.genesis_fork_version,
|
||||
post_fork_version: spec.genesis_fork_version,
|
||||
fork_slot: spec.genesis_slot_number,
|
||||
},
|
||||
/*
|
||||
* Validator registry
|
||||
*/
|
||||
validator_registry: spec.initial_validators.clone(),
|
||||
validator_balances: spec.initial_balances.clone(),
|
||||
validator_registry_latest_change_slot: spec.genesis_slot_number,
|
||||
validator_registry_exit_count: 0,
|
||||
validator_registry_delta_chain_tip: spec.zero_hash,
|
||||
/*
|
||||
* 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
|
||||
],
|
||||
previous_epoch_start_shard: spec.genesis_start_shard,
|
||||
current_epoch_start_shard: spec.genesis_start_shard,
|
||||
previous_epoch_calculation_slot: spec.genesis_slot_number,
|
||||
current_epoch_calculation_slot: spec.genesis_slot_number,
|
||||
previous_epoch_randao_mix: spec.zero_hash,
|
||||
current_epoch_randao_mix: spec.zero_hash,
|
||||
/*
|
||||
* Custody challenges
|
||||
*/
|
||||
custody_challenges: vec![],
|
||||
/*
|
||||
* Finality
|
||||
*/
|
||||
previous_justified_slot: spec.genesis_slot_number,
|
||||
justified_slot: spec.genesis_slot_number,
|
||||
justification_bitfield: 0,
|
||||
finalized_slot: spec.genesis_slot_number,
|
||||
/*
|
||||
* Recent state
|
||||
*/
|
||||
latest_crosslinks: vec![initial_crosslink; spec.shard_count as usize],
|
||||
latest_block_roots: vec![spec.zero_hash; spec.latest_block_roots_length as usize],
|
||||
latest_penalized_exit_balances: vec![0; spec.latest_penalized_exit_length as usize],
|
||||
latest_attestations: vec![],
|
||||
batched_block_roots: vec![],
|
||||
/*
|
||||
* PoW receipt root
|
||||
*/
|
||||
processed_pow_receipt_root: spec.processed_pow_receipt_root,
|
||||
candidate_pow_receipt_roots: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
impl From<ValidatorAssignmentError> for Error {
|
||||
fn from(e: ValidatorAssignmentError) -> Error {
|
||||
Error::ValidationAssignmentError(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use types::Hash256;
|
||||
|
||||
#[test]
|
||||
fn test_genesis_state() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state = genesis_beacon_state(&spec).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
state.validator_registry.len(),
|
||||
spec.initial_validators.len()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_genesis_state_misc() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state = genesis_beacon_state(&spec).unwrap();
|
||||
|
||||
assert_eq!(state.slot, 0);
|
||||
assert_eq!(state.genesis_time, spec.genesis_time);
|
||||
assert_eq!(state.fork_data.pre_fork_version, 0);
|
||||
assert_eq!(state.fork_data.post_fork_version, 0);
|
||||
assert_eq!(state.fork_data.fork_slot, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_genesis_state_validators() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state = genesis_beacon_state(&spec).unwrap();
|
||||
|
||||
assert_eq!(state.validator_registry, spec.initial_validators);
|
||||
assert_eq!(state.validator_balances, spec.initial_balances);
|
||||
assert!(state.validator_registry_latest_change_slot == 0);
|
||||
assert!(state.validator_registry_exit_count == 0);
|
||||
assert_eq!(state.validator_registry_delta_chain_tip, Hash256::zero());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_genesis_state_randomness_committees() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state = genesis_beacon_state(&spec).unwrap();
|
||||
|
||||
// Array of size 8,192 each being zero_hash
|
||||
assert_eq!(state.latest_randao_mixes.len(), 8_192);
|
||||
for item in state.latest_randao_mixes.iter() {
|
||||
assert_eq!(*item, Hash256::zero());
|
||||
}
|
||||
|
||||
// Array of size 8,192 each being a zero hash
|
||||
assert_eq!(state.latest_vdf_outputs.len(), (8_192 / 64));
|
||||
for item in state.latest_vdf_outputs.iter() {
|
||||
assert_eq!(*item, Hash256::zero());
|
||||
}
|
||||
|
||||
// TODO: Check shard and committee shuffling requires solving issue:
|
||||
// https://github.com/sigp/lighthouse/issues/151
|
||||
|
||||
// initial_shuffling = get_shuffling(Hash256::zero(), &state.validator_registry, 0, 0)
|
||||
// initial_shuffling = initial_shuffling.append(initial_shuffling.clone());
|
||||
}
|
||||
|
||||
// Custody not implemented until Phase 1
|
||||
#[test]
|
||||
fn test_genesis_state_custody() {}
|
||||
|
||||
#[test]
|
||||
fn test_genesis_state_finanilty() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state = genesis_beacon_state(&spec).unwrap();
|
||||
|
||||
assert_eq!(state.previous_justified_slot, 0);
|
||||
assert_eq!(state.justified_slot, 0);
|
||||
assert_eq!(state.justification_bitfield, 0);
|
||||
assert_eq!(state.finalized_slot, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_genesis_state_recent_state() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state = genesis_beacon_state(&spec).unwrap();
|
||||
|
||||
// Test latest_crosslinks
|
||||
assert_eq!(state.latest_crosslinks.len(), 1_024);
|
||||
for link in state.latest_crosslinks.iter() {
|
||||
assert_eq!(link.slot, 0);
|
||||
assert_eq!(link.shard_block_root, Hash256::zero());
|
||||
}
|
||||
|
||||
// Test latest_block_roots
|
||||
assert_eq!(state.latest_block_roots.len(), 8_192);
|
||||
for block in state.latest_block_roots.iter() {
|
||||
assert_eq!(*block, Hash256::zero());
|
||||
}
|
||||
|
||||
// Test latest_penalized_exit_balances
|
||||
assert_eq!(state.latest_penalized_exit_balances.len(), 8_192);
|
||||
for item in state.latest_penalized_exit_balances.iter() {
|
||||
assert!(*item == 0);
|
||||
}
|
||||
|
||||
// Test latest_attestations
|
||||
assert!(state.latest_attestations.is_empty());
|
||||
|
||||
// batched_block_roots
|
||||
assert!(state.batched_block_roots.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_genesis_state_deposit_root() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state = genesis_beacon_state(&spec).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
state.processed_pow_receipt_root,
|
||||
spec.processed_pow_receipt_root
|
||||
);
|
||||
assert!(state.candidate_pow_receipt_roots.is_empty());
|
||||
}
|
||||
}
|
||||
10
eth2/genesis/src/lib.rs
Normal file
10
eth2/genesis/src/lib.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
extern crate spec;
|
||||
extern crate types;
|
||||
extern crate validator_induction;
|
||||
extern crate validator_shuffling;
|
||||
|
||||
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};
|
||||
Reference in New Issue
Block a user