diff --git a/eth2/fork_choice/src/slow_lmd_ghost.rs b/eth2/fork_choice/src/slow_lmd_ghost.rs index 3412f5473e..c7cd9bde2c 100644 --- a/eth2/fork_choice/src/slow_lmd_ghost.rs +++ b/eth2/fork_choice/src/slow_lmd_ghost.rs @@ -54,7 +54,6 @@ where // Note: Votes are weighted by min(balance, MAX_DEPOSIT_AMOUNT) // // FORK_CHOICE_BALANCE_INCREMENT // build a hashmap of block_hash to weighted votes - trace!("FORKCHOICE: Getting the latest votes"); let mut latest_votes: HashMap = HashMap::new(); // gets the current weighted votes let current_state = self @@ -66,10 +65,6 @@ where ¤t_state.validator_registry[..], block_slot.epoch(spec.epoch_length), ); - trace!( - "FORKCHOICE: Active validator indicies: {:?}", - active_validator_indices - ); for index in active_validator_indices { let balance = std::cmp::min( @@ -101,12 +96,12 @@ where .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*block_root))? .slot(); - for (target_hash, votes) in latest_votes.iter() { + for (vote_hash, votes) in latest_votes.iter() { let (root_at_slot, _) = self .block_store - .block_at_slot(&block_root, block_slot)? + .block_at_slot(&vote_hash, block_slot)? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*block_root))?; - if root_at_slot == *target_hash { + if root_at_slot == *block_root { count += votes; } } @@ -142,12 +137,21 @@ impl ForkChoice for SlowLMDGhost { ) -> Result<(), ForkChoiceError> { // simply add the attestation to the latest_attestation_target if the block_height is // larger + trace!( + "FORKCHOICE: Adding attestation of validator: {:?} for block: {}", + validator_index, + target_block_root + ); let attestation_target = self .latest_attestation_targets .entry(validator_index) .or_insert_with(|| *target_block_root); // if we already have a value if attestation_target != target_block_root { + trace!( + "FORKCHOICE: Old attestation found: {:?}", + attestation_target + ); // get the height of the target block let block_height = self .block_store @@ -165,6 +169,7 @@ impl ForkChoice for SlowLMDGhost { .height(spec.genesis_slot); // update the attestation only if the new target is higher if past_block_height < block_height { + trace!("FORKCHOICE: Updating old attestation"); *attestation_target = *target_block_root; } } @@ -177,6 +182,7 @@ impl ForkChoice for SlowLMDGhost { justified_block_start: &Hash256, spec: &ChainSpec, ) -> Result { + debug!("FORKCHOICE: Running LMD Ghost Fork-choice rule"); let start = self .block_store .get_deserialized(&justified_block_start)? @@ -189,7 +195,7 @@ impl ForkChoice for SlowLMDGhost { let mut head_hash = Hash256::zero(); loop { - let mut head_vote_count = 0; + debug!("FORKCHOICE: Iteration for block: {}", head_hash); let children = match self.children.get(&head_hash) { Some(children) => children, @@ -197,8 +203,22 @@ impl ForkChoice for SlowLMDGhost { 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 { 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 { head_hash = *child_hash; diff --git a/eth2/fork_choice/tests/lmd_ghost_test_vectors.yaml b/eth2/fork_choice/tests/lmd_ghost_test_vectors.yaml new file mode 100644 index 0000000000..3c2118222a --- /dev/null +++ b/eth2/fork_choice/tests/lmd_ghost_test_vectors.yaml @@ -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' diff --git a/eth2/fork_choice/tests/optimised_lmd_ghost_test_vectors.yaml b/eth2/fork_choice/tests/optimised_lmd_ghost_test_vectors.yaml index 5a8869e8b5..1c2f6b6968 100644 --- a/eth2/fork_choice/tests/optimised_lmd_ghost_test_vectors.yaml +++ b/eth2/fork_choice/tests/optimised_lmd_ghost_test_vectors.yaml @@ -1,5 +1,5 @@ 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_cases: diff --git a/eth2/fork_choice/tests/tests.rs b/eth2/fork_choice/tests/tests.rs index 83baa8bd8b..0eafb58ab4 100644 --- a/eth2/fork_choice/tests/tests.rs +++ b/eth2/fork_choice/tests/tests.rs @@ -25,13 +25,23 @@ use types::{ }; use yaml_rust::yaml; -// run tests #[test] fn test_optimised_lmd_ghost() { test_yaml_vectors( ForkChoiceAlgorithm::OptimisedLMDGhost, "tests/optimised_lmd_ghost_test_vectors.yaml", 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, yaml_file_path: &str, max_validators: usize, + log_level: &str, ) { // 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 let test_cases = load_test_cases_from_yaml(yaml_file_path);