Update fork choice for v0.9.1

This commit is contained in:
Michael Sproul
2019-11-18 12:12:44 +11:00
parent d9467a8ebb
commit 0eb24bb198
8 changed files with 377 additions and 103 deletions

View File

@@ -45,7 +45,9 @@ struct ForkedHarness {
pub genesis_block: BeaconBlock<TestEthSpec>,
pub honest_head: RootAndSlot,
pub faulty_head: RootAndSlot,
/// Honest roots in reverse order (slot high to low)
pub honest_roots: Vec<RootAndSlot>,
/// Faulty roots in reverse order (slot high to low)
pub faulty_roots: Vec<RootAndSlot>,
}
@@ -222,7 +224,7 @@ fn single_voter_persistent_instance_reverse_order() {
"New tree should have integrity"
);
for (root, slot) in harness.honest_roots.iter().rev() {
for (root, slot) in &harness.honest_roots {
lmd.process_attestation(0, *root, *slot)
.expect("fork choice should accept attestations to honest roots in reverse");
@@ -234,11 +236,15 @@ fn single_voter_persistent_instance_reverse_order() {
}
// The honest head should be selected.
let (head_root, head_slot) = harness.honest_roots.first().unwrap();
let (finalized_root, _) = harness.honest_roots.last().unwrap();
let (head_root, _) = harness.honest_roots.first().unwrap();
let (finalized_root, finalized_slot) = harness.honest_roots.last().unwrap();
assert_eq!(
lmd.find_head(*head_slot, *finalized_root, ForkedHarness::weight_function),
lmd.find_head(
*finalized_slot,
*finalized_root,
ForkedHarness::weight_function
),
Ok(*head_root),
"Honest head should be selected"
);
@@ -250,7 +256,7 @@ fn single_voter_persistent_instance_reverse_order() {
fn single_voter_many_instance_honest_blocks_voting_forwards() {
let harness = &FORKED_HARNESS;
for (root, slot) in &harness.honest_roots {
for (root, slot) in harness.honest_roots.iter().rev() {
let lmd = harness.new_fork_choice();
lmd.process_attestation(0, *root, *slot)
.expect("fork choice should accept attestations to honest roots");
@@ -269,7 +275,7 @@ fn single_voter_many_instance_honest_blocks_voting_in_reverse() {
let harness = &FORKED_HARNESS;
// Same as above, but in reverse order (votes on the highest honest block first).
for (root, slot) in harness.honest_roots.iter().rev() {
for (root, slot) in &harness.honest_roots {
let lmd = harness.new_fork_choice();
lmd.process_attestation(0, *root, *slot)
.expect("fork choice should accept attestations to honest roots in reverse");
@@ -288,7 +294,7 @@ fn single_voter_many_instance_honest_blocks_voting_in_reverse() {
fn single_voter_many_instance_faulty_blocks_voting_forwards() {
let harness = &FORKED_HARNESS;
for (root, slot) in &harness.faulty_roots {
for (root, slot) in harness.faulty_roots.iter().rev() {
let lmd = harness.new_fork_choice();
lmd.process_attestation(0, *root, *slot)
.expect("fork choice should accept attestations to faulty roots");
@@ -306,7 +312,7 @@ fn single_voter_many_instance_faulty_blocks_voting_forwards() {
fn single_voter_many_instance_faulty_blocks_voting_in_reverse() {
let harness = &FORKED_HARNESS;
for (root, slot) in harness.faulty_roots.iter().rev() {
for (root, slot) in &harness.faulty_roots {
let lmd = harness.new_fork_choice();
lmd.process_attestation(0, *root, *slot)
.expect("fork choice should accept attestations to faulty roots in reverse");
@@ -319,6 +325,49 @@ fn single_voter_many_instance_faulty_blocks_voting_in_reverse() {
}
}
/// Ensure that votes with slots before the justified slot are not counted.
#[test]
fn discard_votes_before_justified_slot() {
let harness = &FORKED_HARNESS;
let lmd = harness.new_fork_choice();
let (genesis_root, genesis_slot) = *harness.honest_roots.last().unwrap();
dbg!(&harness.honest_roots);
// Add attestations from all validators for all honest blocks.
for (root, slot) in harness.honest_roots.iter().rev() {
for i in 0..VALIDATOR_COUNT {
lmd.process_attestation(i, *root, *slot)
.expect("should accept attestations in increasing order");
}
// Head starting from 0 checkpoint (genesis) should be current root
assert_eq!(
lmd.find_head(genesis_slot, genesis_root, ForkedHarness::weight_function),
Ok(*root),
"Honest head should be selected"
);
assert_eq!(
lmd.find_head(
genesis_slot + 1,
genesis_root,
ForkedHarness::weight_function
),
Ok(genesis_root)
);
}
let head = harness.harness.chain.head();
dbg!(&harness.honest_roots);
dbg!(head.beacon_state.current_justified_checkpoint);
// assert!(false);
}
/// Ensures that the finalized root can be set to all values in `roots`.
fn test_update_finalized_root(roots: &[(Hash256, Slot)]) {
let harness = &FORKED_HARNESS;