Replace genesis
crate with on-type defs
The methods in the `gensis` crate have been moved to `genesis` methods on `BeaconState` and `BeaconBlock`.
This commit is contained in:
parent
5e37e8a33a
commit
36f441c968
@ -2,7 +2,6 @@
|
||||
members = [
|
||||
"eth2/attester",
|
||||
"eth2/block_producer",
|
||||
"eth2/genesis",
|
||||
"eth2/naive_fork_choice",
|
||||
"eth2/state_processing",
|
||||
"eth2/types",
|
||||
|
@ -14,7 +14,6 @@ clap = "2.32.0"
|
||||
db = { path = "db" }
|
||||
dirs = "1.0.3"
|
||||
futures = "0.1.23"
|
||||
genesis = { path = "../eth2/genesis" }
|
||||
slog = "^2.2.3"
|
||||
slot_clock = { path = "../eth2/utils/slot_clock" }
|
||||
slog-term = "^2.4.0"
|
||||
|
@ -11,7 +11,6 @@ boolean-bitfield = { path = "../../eth2/utils/boolean-bitfield" }
|
||||
db = { path = "../db" }
|
||||
failure = "0.1"
|
||||
failure_derive = "0.1"
|
||||
genesis = { path = "../../eth2/genesis" }
|
||||
hashing = { path = "../../eth2/utils/hashing" }
|
||||
parking_lot = "0.7"
|
||||
log = "0.4"
|
||||
|
@ -21,7 +21,6 @@ db = { path = "../../db" }
|
||||
parking_lot = "0.7"
|
||||
failure = "0.1"
|
||||
failure_derive = "0.1"
|
||||
genesis = { path = "../../../eth2/genesis" }
|
||||
hashing = { path = "../../../eth2/utils/hashing" }
|
||||
log = "0.4"
|
||||
env_logger = "0.6.0"
|
||||
|
@ -1,10 +0,0 @@
|
||||
[package]
|
||||
name = "genesis"
|
||||
version = "0.1.0"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bls = { path = "../utils/bls" }
|
||||
ssz = { path = "../utils/ssz" }
|
||||
types = { path = "../types" }
|
@ -1,93 +0,0 @@
|
||||
use types::{BeaconBlock, BeaconBlockBody, ChainSpec, Eth1Data, Hash256};
|
||||
|
||||
/// Generate a genesis BeaconBlock.
|
||||
pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBlock {
|
||||
BeaconBlock {
|
||||
slot: spec.genesis_slot,
|
||||
parent_root: spec.zero_hash,
|
||||
state_root,
|
||||
randao_reveal: spec.empty_signature.clone(),
|
||||
eth1_data: Eth1Data {
|
||||
deposit_root: spec.zero_hash,
|
||||
block_hash: 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_state_root() {
|
||||
let spec = ChainSpec::foundation();
|
||||
let state_root = Hash256::from("cats".as_bytes());
|
||||
|
||||
let block = genesis_beacon_block(state_root, &spec);
|
||||
|
||||
assert_eq!(block.state_root, state_root);
|
||||
}
|
||||
|
||||
#[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_eq!(genesis_block.randao_reveal, Signature::empty_signature());
|
||||
assert!(genesis_block.eth1_data.deposit_root.is_zero());
|
||||
assert!(genesis_block.eth1_data.block_hash.is_zero());
|
||||
}
|
||||
|
||||
#[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());
|
||||
}
|
||||
}
|
@ -1,194 +0,0 @@
|
||||
use types::{BeaconState, ChainSpec, Crosslink, Fork};
|
||||
|
||||
pub fn genesis_beacon_state(spec: &ChainSpec) -> BeaconState {
|
||||
let initial_crosslink = Crosslink {
|
||||
slot: spec.genesis_slot,
|
||||
shard_block_root: spec.zero_hash,
|
||||
};
|
||||
|
||||
BeaconState {
|
||||
/*
|
||||
* Misc
|
||||
*/
|
||||
slot: spec.genesis_slot,
|
||||
genesis_time: spec.genesis_time,
|
||||
fork_data: Fork {
|
||||
pre_fork_version: spec.genesis_fork_version,
|
||||
post_fork_version: spec.genesis_fork_version,
|
||||
fork_slot: spec.genesis_slot,
|
||||
},
|
||||
/*
|
||||
* Validator registry
|
||||
*/
|
||||
validator_registry: spec.initial_validators.clone(),
|
||||
validator_balances: spec.initial_balances.clone(),
|
||||
validator_registry_update_slot: spec.genesis_slot,
|
||||
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,
|
||||
current_epoch_calculation_slot: spec.genesis_slot,
|
||||
previous_epoch_seed: spec.zero_hash,
|
||||
current_epoch_seed: spec.zero_hash,
|
||||
/*
|
||||
* Custody challenges
|
||||
*/
|
||||
custody_challenges: vec![],
|
||||
/*
|
||||
* Finality
|
||||
*/
|
||||
previous_justified_slot: spec.genesis_slot,
|
||||
justified_slot: spec.genesis_slot,
|
||||
justification_bitfield: 0,
|
||||
finalized_slot: spec.genesis_slot,
|
||||
/*
|
||||
* 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_balances: vec![0; spec.latest_penalized_exit_length as usize],
|
||||
latest_attestations: vec![],
|
||||
batched_block_roots: vec![],
|
||||
/*
|
||||
* PoW receipt root
|
||||
*/
|
||||
latest_eth1_data: spec.intial_eth1_data.clone(),
|
||||
eth1_data_votes: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use types::Hash256;
|
||||
|
||||
#[test]
|
||||
fn test_genesis_state() {
|
||||
let spec = ChainSpec::foundation();
|
||||
|
||||
let state = genesis_beacon_state(&spec);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
assert_eq!(state.validator_registry, spec.initial_validators);
|
||||
assert_eq!(state.validator_balances, spec.initial_balances);
|
||||
assert!(state.validator_registry_update_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);
|
||||
|
||||
// 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);
|
||||
|
||||
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);
|
||||
|
||||
// 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_balances
|
||||
assert_eq!(state.latest_penalized_balances.len(), 8_192);
|
||||
for item in state.latest_penalized_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);
|
||||
|
||||
assert_eq!(&state.latest_eth1_data, &spec.intial_eth1_data);
|
||||
assert!(state.eth1_data_votes.is_empty());
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
mod beacon_block;
|
||||
mod beacon_state;
|
||||
|
||||
pub use crate::beacon_block::genesis_beacon_block;
|
||||
pub use crate::beacon_state::genesis_beacon_state;
|
Loading…
Reference in New Issue
Block a user