Add genesis tests pre modifying the beancon structs

This commit is contained in:
Kirk Baird 2019-01-14 16:32:55 +11:00
parent 67a153bf4f
commit d12b5249e0
3 changed files with 159 additions and 1 deletions

View File

@ -6,6 +6,7 @@ edition = "2018"
[dependencies] [dependencies]
bls = { path = "../utils/bls" } bls = { path = "../utils/bls" }
bls-aggregates = { git = "https://github.com/sigp/signature-schemes" }
spec = { path = "../spec" } spec = { path = "../spec" }
ssz = { path = "../utils/ssz" } ssz = { path = "../utils/ssz" }
types = { path = "../types" } types = { path = "../types" }

View File

@ -47,4 +47,58 @@ mod tests {
// This only checks that the function runs without panic. // This only checks that the function runs without panic.
genesis_beacon_block(state_root, &spec); 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);
}
}
} }

View File

@ -86,12 +86,13 @@ mod tests {
extern crate validator_induction; extern crate validator_induction;
use super::*; use super::*;
use types::Hash256;
// TODO: enhance these tests. // TODO: enhance these tests.
// https://github.com/sigp/lighthouse/issues/117 // https://github.com/sigp/lighthouse/issues/117
#[test] #[test]
fn test_genesis() { fn test_gen_state() {
let spec = ChainSpec::foundation(); let spec = ChainSpec::foundation();
let state = genesis_beacon_state(&spec).unwrap(); let state = genesis_beacon_state(&spec).unwrap();
@ -101,4 +102,106 @@ mod tests {
spec.initial_validators.len() 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());
}
} }