Add slow LMD Ghost working tests.

This commit is contained in:
Age Manning 2019-02-19 15:08:55 +11:00
parent 846cbdd7f7
commit bd66a02cb3
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
4 changed files with 64 additions and 12 deletions

View File

@ -54,7 +54,6 @@ where
// Note: Votes are weighted by min(balance, MAX_DEPOSIT_AMOUNT) // // Note: Votes are weighted by min(balance, MAX_DEPOSIT_AMOUNT) //
// FORK_CHOICE_BALANCE_INCREMENT // FORK_CHOICE_BALANCE_INCREMENT
// build a hashmap of block_hash to weighted votes // build a hashmap of block_hash to weighted votes
trace!("FORKCHOICE: Getting the latest votes");
let mut latest_votes: HashMap<Hash256, u64> = HashMap::new(); let mut latest_votes: HashMap<Hash256, u64> = HashMap::new();
// gets the current weighted votes // gets the current weighted votes
let current_state = self let current_state = self
@ -66,10 +65,6 @@ where
&current_state.validator_registry[..], &current_state.validator_registry[..],
block_slot.epoch(spec.epoch_length), block_slot.epoch(spec.epoch_length),
); );
trace!(
"FORKCHOICE: Active validator indicies: {:?}",
active_validator_indices
);
for index in active_validator_indices { for index in active_validator_indices {
let balance = std::cmp::min( let balance = std::cmp::min(
@ -101,12 +96,12 @@ where
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*block_root))? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*block_root))?
.slot(); .slot();
for (target_hash, votes) in latest_votes.iter() { for (vote_hash, votes) in latest_votes.iter() {
let (root_at_slot, _) = self let (root_at_slot, _) = self
.block_store .block_store
.block_at_slot(&block_root, block_slot)? .block_at_slot(&vote_hash, block_slot)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*block_root))?; .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*block_root))?;
if root_at_slot == *target_hash { if root_at_slot == *block_root {
count += votes; count += votes;
} }
} }
@ -142,12 +137,21 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
) -> Result<(), ForkChoiceError> { ) -> Result<(), ForkChoiceError> {
// simply add the attestation to the latest_attestation_target if the block_height is // simply add the attestation to the latest_attestation_target if the block_height is
// larger // larger
trace!(
"FORKCHOICE: Adding attestation of validator: {:?} for block: {}",
validator_index,
target_block_root
);
let attestation_target = self let attestation_target = self
.latest_attestation_targets .latest_attestation_targets
.entry(validator_index) .entry(validator_index)
.or_insert_with(|| *target_block_root); .or_insert_with(|| *target_block_root);
// if we already have a value // if we already have a value
if attestation_target != target_block_root { if attestation_target != target_block_root {
trace!(
"FORKCHOICE: Old attestation found: {:?}",
attestation_target
);
// get the height of the target block // get the height of the target block
let block_height = self let block_height = self
.block_store .block_store
@ -165,6 +169,7 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
.height(spec.genesis_slot); .height(spec.genesis_slot);
// update the attestation only if the new target is higher // update the attestation only if the new target is higher
if past_block_height < block_height { if past_block_height < block_height {
trace!("FORKCHOICE: Updating old attestation");
*attestation_target = *target_block_root; *attestation_target = *target_block_root;
} }
} }
@ -177,6 +182,7 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
justified_block_start: &Hash256, justified_block_start: &Hash256,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<Hash256, ForkChoiceError> { ) -> Result<Hash256, ForkChoiceError> {
debug!("FORKCHOICE: Running LMD Ghost Fork-choice rule");
let start = self let start = self
.block_store .block_store
.get_deserialized(&justified_block_start)? .get_deserialized(&justified_block_start)?
@ -189,7 +195,7 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
let mut head_hash = Hash256::zero(); let mut head_hash = Hash256::zero();
loop { loop {
let mut head_vote_count = 0; debug!("FORKCHOICE: Iteration for block: {}", head_hash);
let children = match self.children.get(&head_hash) { let children = match self.children.get(&head_hash) {
Some(children) => children, Some(children) => children,
@ -197,8 +203,22 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
None => break, None => break,
}; };
// if we only have one child, use it
if children.len() == 1 {
trace!("FORKCHOICE: Single child found.");
head_hash = children[0];
continue;
}
trace!("FORKCHOICE: Children found: {:?}", children);
let mut head_vote_count = 0;
for child_hash in children { for child_hash in children {
let vote_count = self.get_vote_count(&latest_votes, &child_hash)?; let vote_count = self.get_vote_count(&latest_votes, &child_hash)?;
trace!(
"FORKCHOICE: Vote count for child: {} is: {}",
child_hash,
vote_count
);
if vote_count > head_vote_count { if vote_count > head_vote_count {
head_hash = *child_hash; head_hash = *child_hash;

View File

@ -0,0 +1,21 @@
title: Fork-choice Tests
summary: A collection of abstract fork-choice tests for lmd ghost.
test_suite: Fork-Choice
test_cases:
- blocks:
- id: 'b0'
parent: 'b0'
- id: 'b1'
parent: 'b0'
- id: 'b2'
parent: 'b1'
- id: 'b3'
parent: 'b1'
weights:
- b0: 0
- b1: 0
- b2: 5
- b3: 10
heads:
- id: 'b3'

View File

@ -1,5 +1,5 @@
title: Fork-choice Tests title: Fork-choice Tests
summary: A collection of abstract fork-choice tests. summary: A collection of abstract fork-choice tests for bitwise lmd ghost.
test_suite: Fork-Choice test_suite: Fork-Choice
test_cases: test_cases:

View File

@ -25,13 +25,23 @@ use types::{
}; };
use yaml_rust::yaml; use yaml_rust::yaml;
// run tests
#[test] #[test]
fn test_optimised_lmd_ghost() { fn test_optimised_lmd_ghost() {
test_yaml_vectors( test_yaml_vectors(
ForkChoiceAlgorithm::OptimisedLMDGhost, ForkChoiceAlgorithm::OptimisedLMDGhost,
"tests/optimised_lmd_ghost_test_vectors.yaml", "tests/optimised_lmd_ghost_test_vectors.yaml",
100, 100,
"debug",
);
}
#[test]
fn test_slow_lmd_ghost() {
test_yaml_vectors(
ForkChoiceAlgorithm::SlowLMDGhost,
"tests/lmd_ghost_test_vectors.yaml",
100,
"debug",
); );
} }
@ -40,9 +50,10 @@ fn test_yaml_vectors(
fork_choice_algo: ForkChoiceAlgorithm, fork_choice_algo: ForkChoiceAlgorithm,
yaml_file_path: &str, yaml_file_path: &str,
max_validators: usize, max_validators: usize,
log_level: &str,
) { ) {
// set up logging // set up logging
Builder::from_env(Env::default().default_filter_or("debug")).init(); Builder::from_env(Env::default().default_filter_or(log_level)).init();
// load test cases from yaml // load test cases from yaml
let test_cases = load_test_cases_from_yaml(yaml_file_path); let test_cases = load_test_cases_from_yaml(yaml_file_path);