Skip to content

Commit

Permalink
Fix beacon chain tests non-altair (#2305)
Browse files Browse the repository at this point in the history
  • Loading branch information
realbigsean authored Apr 13, 2021
1 parent 8ffeb33 commit d5f19dc
Show file tree
Hide file tree
Showing 29 changed files with 409 additions and 324 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 17 additions & 10 deletions beacon_node/beacon_chain/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ mod test {
use store::config::StoreConfig;
use store::{HotColdDB, MemoryStore};
use tempfile::tempdir;
use types::{EthSpec, MinimalEthSpec, Slot};
use types::{init_fork_schedule, EthSpec, ForkSchedule, MinimalEthSpec, Slot};

type TestEthSpec = MinimalEthSpec;

Expand All @@ -686,6 +686,11 @@ mod test {

#[test]
fn recent_genesis() {
//TODO: handle altair
init_fork_schedule(ForkSchedule {
altair_fork_slot: None,
});

let validator_count = 1;
let genesis_time = 13_371_337;

Expand Down Expand Up @@ -728,9 +733,10 @@ mod test {
let state = head.beacon_state;
let block = head.beacon_block;

assert_eq!(state.slot, Slot::new(0), "should start from genesis");
assert_eq!(state.slot(), Slot::new(0), "should start from genesis");
assert_eq!(
state.genesis_time, 13_371_337,
state.genesis_time(),
13_371_337,
"should have the correct genesis time"
);
assert_eq!(
Expand All @@ -748,7 +754,7 @@ mod test {
"should store genesis block under zero hash alias"
);
assert_eq!(
state.validators.len(),
state.validators().len(),
validator_count,
"should have correct validator count"
);
Expand All @@ -771,24 +777,25 @@ mod test {
.expect("should build state");

assert_eq!(
state.eth1_data.block_hash,
state.eth1_data().block_hash,
Hash256::from_slice(&[0x42; 32]),
"eth1 block hash should be co-ordinated junk"
);

assert_eq!(
state.genesis_time, genesis_time,
state.genesis_time(),
genesis_time,
"genesis time should be as specified"
);

for b in &state.balances {
for b in state.balances() {
assert_eq!(
*b, spec.max_effective_balance,
"validator balances should be max effective balance"
);
}

for v in &state.validators {
for v in state.validators() {
let creds = v.withdrawal_credentials.as_bytes();
assert_eq!(
creds[0], spec.bls_withdrawal_prefix_byte,
Expand All @@ -802,13 +809,13 @@ mod test {
}

assert_eq!(
state.balances.len(),
state.balances().len(),
validator_count,
"validator balances len should be correct"
);

assert_eq!(
state.validators.len(),
state.validators().len(),
validator_count,
"validator count should be correct"
);
Expand Down
52 changes: 27 additions & 25 deletions beacon_node/beacon_chain/src/eth1_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ fn is_candidate_block(block: &Eth1Block, period_start: u64, spec: &ChainSpec) ->
mod test {
use super::*;
use environment::null_logger;
use types::{test_utils::DepositTestTask, MinimalEthSpec};
use types::{DepositData, MinimalEthSpec, Signature};

type E = MinimalEthSpec;

Expand All @@ -682,9 +682,9 @@ mod test {

fn get_voting_period_start_seconds(state: &BeaconState<E>, spec: &ChainSpec) -> u64 {
let period = <E as EthSpec>::SlotsPerEth1VotingPeriod::to_u64();
let voting_period_start_slot = (state.slot / period) * period;
let voting_period_start_slot = (state.slot() / period) * period;
slot_start_seconds::<E>(
state.genesis_time,
state.genesis_time(),
spec.seconds_per_slot,
voting_period_start_slot,
)
Expand Down Expand Up @@ -725,10 +725,7 @@ mod test {
mod eth1_chain_json_backend {
use super::*;
use eth1::DepositLog;
use types::{
test_utils::{generate_deterministic_keypair, TestingDepositBuilder},
EthSpec, MainnetEthSpec,
};
use types::{test_utils::generate_deterministic_keypair, EthSpec, MainnetEthSpec};

fn get_eth1_chain() -> Eth1Chain<CachingEth1Backend<E>, E> {
let eth1_config = Eth1Config {
Expand All @@ -745,13 +742,17 @@ mod test {

fn get_deposit_log(i: u64, spec: &ChainSpec) -> DepositLog {
let keypair = generate_deterministic_keypair(i as usize);
let mut builder =
TestingDepositBuilder::new(keypair.pk.clone(), spec.max_effective_balance);
builder.sign(DepositTestTask::Valid, &keypair, spec);
let deposit_data = builder.build().data;
let mut deposit = DepositData {
pubkey: keypair.pk.into(),
withdrawal_credentials: Hash256::zero(),
amount: spec.max_effective_balance,
signature: Signature::empty().into(),
};

deposit.signature = deposit.create_signature(&keypair.sk, &E::default_spec());

DepositLog {
deposit_data,
deposit_data: deposit,
block_number: i,
index: i,
signature_is_valid: true,
Expand All @@ -770,8 +771,8 @@ mod test {
);

let mut state: BeaconState<E> = BeaconState::new(0, get_eth1_data(0), &spec);
state.eth1_deposit_index = 0;
state.eth1_data.deposit_count = 0;
*state.eth1_deposit_index_mut() = 0;
state.eth1_data_mut().deposit_count = 0;

assert!(
eth1_chain
Expand All @@ -780,7 +781,7 @@ mod test {
"should succeed if cache is empty but no deposits are required"
);

state.eth1_data.deposit_count = 1;
state.eth1_data_mut().deposit_count = 1;

assert!(
eth1_chain
Expand Down Expand Up @@ -823,8 +824,8 @@ mod test {
);

let mut state: BeaconState<E> = BeaconState::new(0, get_eth1_data(0), &spec);
state.eth1_deposit_index = 0;
state.eth1_data.deposit_count = 0;
*state.eth1_deposit_index_mut() = 0;
state.eth1_data_mut().deposit_count = 0;

assert!(
eth1_chain
Expand All @@ -834,10 +835,10 @@ mod test {
);

(0..3).for_each(|initial_deposit_index| {
state.eth1_deposit_index = initial_deposit_index as u64;
*state.eth1_deposit_index_mut() = initial_deposit_index as u64;

(initial_deposit_index..deposits.len()).for_each(|i| {
state.eth1_data.deposit_count = i as u64;
state.eth1_data_mut().deposit_count = i as u64;

let deposits_for_inclusion = eth1_chain
.deposits_for_block_inclusion(&state, &Eth1Data::default(), spec)
Expand Down Expand Up @@ -890,7 +891,8 @@ mod test {
.eth1_data_for_block_production(&state, &spec)
.expect("should produce default eth1 data vote");
assert_eq!(
a, state.eth1_data,
a,
*state.eth1_data(),
"default vote should be same as state.eth1_data"
);
}
Expand All @@ -910,7 +912,7 @@ mod test {

let mut state: BeaconState<E> = BeaconState::new(0, get_eth1_data(0), &spec);

state.slot = Slot::from(slots_per_eth1_voting_period * 10);
*state.slot_mut() = Slot::from(slots_per_eth1_voting_period * 10);
let follow_distance_seconds = eth1_follow_distance * spec.seconds_per_eth1_block;
let voting_period_start = get_voting_period_start_seconds(&state, &spec);
let start_eth1_block = voting_period_start - follow_distance_seconds * 2;
Expand Down Expand Up @@ -976,8 +978,8 @@ mod test {
let eth1_follow_distance = spec.eth1_follow_distance;

let mut state: BeaconState<E> = BeaconState::new(0, get_eth1_data(0), &spec);
state.genesis_time = 0;
state.slot = Slot::from(slots_per_eth1_voting_period * 10);
*state.genesis_time_mut() = 0;
*state.slot_mut() = Slot::from(slots_per_eth1_voting_period * 10);

let follow_distance_seconds = eth1_follow_distance * spec.seconds_per_eth1_block;
let voting_period_start = get_voting_period_start_seconds(&state, &spec);
Expand Down Expand Up @@ -1057,7 +1059,7 @@ mod test {

let votes_to_consider = get_eth1_data_vec(slots, 0);

state.eth1_data_votes = votes_to_consider[0..slots as usize / 4]
*state.eth1_data_votes_mut() = votes_to_consider[0..slots as usize / 4]
.iter()
.map(|(eth1_data, _)| eth1_data)
.cloned()
Expand Down Expand Up @@ -1086,7 +1088,7 @@ mod test {
.expect("should have some eth1 data")
.clone();

state.eth1_data_votes = vec![duplicate_eth1_data.clone(); 4]
*state.eth1_data_votes_mut() = vec![duplicate_eth1_data.clone(); 4]
.iter()
.map(|(eth1_data, _)| eth1_data)
.cloned()
Expand Down
12 changes: 6 additions & 6 deletions beacon_node/beacon_chain/src/head_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ mod test {
let mut block: BeaconBlock<E> = BeaconBlock::empty(spec);
let block_root = Hash256::from_low_u64_be(i);

block.slot = Slot::new(i);
block.parent_root = if i == 0 {
*block.slot_mut() = Slot::new(i);
*block.parent_root_mut() = if i == 0 {
Hash256::random()
} else {
Hash256::from_low_u64_be(i - 1)
};

head_tracker.register_block(block_root, block.parent_root, block.slot);
head_tracker.register_block(block_root, block.parent_root(), block.slot());
}

assert_eq!(
Expand All @@ -130,9 +130,9 @@ mod test {

let mut block: BeaconBlock<E> = BeaconBlock::empty(spec);
let block_root = Hash256::from_low_u64_be(42);
block.slot = Slot::new(15);
block.parent_root = Hash256::from_low_u64_be(14);
head_tracker.register_block(block_root, block.parent_root, block.slot);
*block.slot_mut() = Slot::new(15);
*block.parent_root_mut() = Hash256::from_low_u64_be(14);
head_tracker.register_block(block_root, block.parent_root(), block.slot());

let heads = head_tracker.heads();

Expand Down
Loading

0 comments on commit d5f19dc

Please sign in to comment.