diff --git a/beacon_chain/genesis/Cargo.toml b/beacon_chain/genesis/Cargo.toml index 499333979..dbfd7e1fc 100644 --- a/beacon_chain/genesis/Cargo.toml +++ b/beacon_chain/genesis/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dependencies] bls = { path = "../utils/bls" } +bls-aggregates = { git = "https://github.com/sigp/signature-schemes" } spec = { path = "../spec" } ssz = { path = "../utils/ssz" } types = { path = "../types" } diff --git a/beacon_chain/genesis/src/beacon_block.rs b/beacon_chain/genesis/src/beacon_block.rs index 696ac6499..3ec321237 100644 --- a/beacon_chain/genesis/src/beacon_block.rs +++ b/beacon_chain/genesis/src/beacon_block.rs @@ -47,4 +47,58 @@ mod tests { // This only checks that the function runs without panic. genesis_beacon_block(state_root, &spec); } + + // Tests parent_root, randao_reveal, deposit_root are the zero hash after creation and slot == 0 + #[test] + fn test_zero_items() { + let spec = ChainSpec::foundation(); + + // Note: state_root will not be available without a state (test in beacon_state) + let state_root = Hash256::zero(); + + let genesis_block = genesis_beacon_block(state_root, &spec); + + assert!(genesis_block.parent_root.is_zero()); + assert!(genesis_block.randao_reveal.is_zero()); + assert!(genesis_block.slot == 0); + // assert!(genesis_block.depsoit_root.is_zero()); + + } + + #[test] + fn test_beacon_body() { + let spec = ChainSpec::foundation(); + + // Note: state_root will not be available without a state (test in beacon_state) + let state_root = Hash256::zero(); + + let genesis_block = genesis_beacon_block(state_root, &spec); + + 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()); + // Specs have changed to include 3 more variables in BeaconBody to be added later + } + + #[test] + fn test_signature() { + let spec = ChainSpec::foundation(); + + // Note: state_root will not be available without a state (test in beacon_state) + 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(); + + assert!(raw_sig_bytes.len() == 97); + 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 f9c2ef327..3ebd3dba7 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -86,12 +86,13 @@ mod tests { extern crate validator_induction; use super::*; + use types::Hash256; // TODO: enhance these tests. // https://github.com/sigp/lighthouse/issues/117 #[test] - fn test_genesis() { + fn test_gen_state() { let spec = ChainSpec::foundation(); let state = genesis_beacon_state(&spec).unwrap(); @@ -101,4 +102,106 @@ mod tests { spec.initial_validators.len() ); } + + #[test] + fn test_gen_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_gen_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_gen_state_randomness_committees() { + let spec = ChainSpec::foundation(); + + let state = genesis_beacon_state(&spec).unwrap(); + + // Note: specs now have randao_mixes containing 8,192 zero hashes + assert_eq!(state.randao_mix, Hash256::zero()); + + // Note: next_seed has changed to latest_vdf_outputs[8,192]8,192] + assert_eq!(state.next_seed, 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()); + } + + #[test] + fn test_gen_state_custody_finanilty() { + let spec = ChainSpec::foundation(); + + let state = genesis_beacon_state(&spec).unwrap(); + + // Note: custody_challenges are not included yet but are in Eth2.0 specs + + 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_gen_state_recent_state() { + let spec = ChainSpec::foundation(); + + let state = genesis_beacon_state(&spec).unwrap(); + + + // Test latest_crosslinks + assert_eq!(state.latest_crosslinks.len(), 1024); + 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(), 64); + for block in state.latest_block_roots.iter() { + assert_eq!(*block, Hash256::zero()); + } + + // Test latest_penalized_exit_balances + // Note: Eth2.0 specs says this should be an array of length LATEST_PENALIZE_EXIT_LENGTH + // = (8,192) + assert!(state.latest_penalized_exit_balances.is_empty()); + + // Test latest_attestations + assert!(state.latest_attestations.is_empty()); + + // Note: missing batched_block_roots in new spec + + } + + // Note: here we refer to it as pow_reciept in the Eth2.0 specs it is called deposit + #[test] + fn test_gen_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()); + } }