diff --git a/beacon_chain/genesis/src/beacon_block.rs b/beacon_chain/genesis/src/beacon_block.rs index a1bbdd3a5..78968b7a6 100644 --- a/beacon_chain/genesis/src/beacon_block.rs +++ b/beacon_chain/genesis/src/beacon_block.rs @@ -39,9 +39,6 @@ fn genesis_signature() -> Signature { mod tests { use super::*; - // TODO: enhance these tests. - // https://github.com/sigp/lighthouse/issues/117 - #[test] fn test_genesis() { let spec = ChainSpec::foundation(); @@ -50,4 +47,53 @@ mod tests { // 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); + } + } } diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index f8c7eb539..58feca840 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -68,8 +68,8 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { * Recent state */ latest_crosslinks: vec![initial_crosslink; spec.shard_count as usize], - latest_block_roots: vec![spec.zero_hash; spec.epoch_length as usize], - latest_penalized_exit_balances: vec![], + 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![], /* @@ -88,16 +88,11 @@ impl From for Error { #[cfg(test)] mod tests { - extern crate bls; - extern crate validator_induction; - use super::*; - - // TODO: enhance these tests. - // https://github.com/sigp/lighthouse/issues/117 + use types::Hash256; #[test] - fn test_genesis() { + fn test_genesis_state() { let spec = ChainSpec::foundation(); let state = genesis_beacon_state(&spec).unwrap(); @@ -107,4 +102,116 @@ mod tests { 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()); + } } diff --git a/beacon_chain/spec/src/foundation.rs b/beacon_chain/spec/src/foundation.rs index 315be168e..c2794e44b 100644 --- a/beacon_chain/spec/src/foundation.rs +++ b/beacon_chain/spec/src/foundation.rs @@ -24,7 +24,10 @@ impl ChainSpec { beacon_chain_shard_number: u64::max_value(), bls_withdrawal_prefix_byte: 0x00, max_casper_votes: 1_024, + latest_block_roots_length: 8_192, latest_randao_mixes_length: 8_192, + latest_penalized_exit_length: 8_192, + max_withdrawals_per_epoch: 4, /* * Deposit contract */ diff --git a/beacon_chain/spec/src/lib.rs b/beacon_chain/spec/src/lib.rs index eb024cb6c..5b7bdfae3 100644 --- a/beacon_chain/spec/src/lib.rs +++ b/beacon_chain/spec/src/lib.rs @@ -18,7 +18,10 @@ pub struct ChainSpec { pub beacon_chain_shard_number: u64, pub bls_withdrawal_prefix_byte: u8, pub max_casper_votes: u64, + pub latest_block_roots_length: u64, pub latest_randao_mixes_length: u64, + pub latest_penalized_exit_length: u64, + pub max_withdrawals_per_epoch: u64, /* * Deposit contract */ diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index b9994bddd..cbcb00d19 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -10,7 +10,7 @@ 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. +// Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummy class used. type CustodyChallenge = usize; #[derive(Debug, PartialEq, Clone, Default)]