From 13c957bef7623fbac689330b0aa221b404443e14 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 1 Mar 2019 14:38:07 +1100 Subject: [PATCH] Correct bitwise fork-choice rule. --- eth2/fork_choice/src/bitwise_lmd_ghost.rs | 9 ++++-- .../tests/bitwise_lmd_ghost_test_vectors.yaml | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/eth2/fork_choice/src/bitwise_lmd_ghost.rs b/eth2/fork_choice/src/bitwise_lmd_ghost.rs index e1d246e92..1e66de079 100644 --- a/eth2/fork_choice/src/bitwise_lmd_ghost.rs +++ b/eth2/fork_choice/src/bitwise_lmd_ghost.rs @@ -192,7 +192,7 @@ where } // Check if there is a clear block winner at this height. If so return it. for (hash, votes) in current_votes.iter() { - if *votes >= total_vote_count / 2 { + if *votes > total_vote_count / 2 { // we have a clear winner, return it return Some(*hash); } @@ -371,7 +371,10 @@ impl ForkChoice for BitwiseLMDGhost { // if there are no children, we are done, return the current_head let children = match self.children.get(¤t_head) { Some(children) => children.clone(), - None => return Ok(current_head), + None => { + debug!("Head found: {}", current_head); + return Ok(current_head); + } }; // logarithmic lookup blocks to see if there are obvious winners, if so, @@ -391,7 +394,7 @@ impl ForkChoice for BitwiseLMDGhost { step /= 2; } if step > 0 { - trace!("Found clear winner in log lookup"); + trace!("Found clear winner: {}", current_head); } // if our skip lookup failed and we only have one child, progress to that child else if children.len() == 1 { diff --git a/eth2/fork_choice/tests/bitwise_lmd_ghost_test_vectors.yaml b/eth2/fork_choice/tests/bitwise_lmd_ghost_test_vectors.yaml index 1578673cd..3233137ab 100644 --- a/eth2/fork_choice/tests/bitwise_lmd_ghost_test_vectors.yaml +++ b/eth2/fork_choice/tests/bitwise_lmd_ghost_test_vectors.yaml @@ -35,3 +35,31 @@ test_cases: - b3: 3 heads: - id: 'b2' +- blocks: + - id: 'b0' + parent: 'b0' + - id: 'b1' + parent: 'b0' + - id: 'b2' + parent: 'b0' + - id: 'b3' + parent: 'b1' + - id: 'b4' + parent: 'b1' + - id: 'b5' + parent: 'b1' + - id: 'b6' + parent: 'b2' + - id: 'b7' + parent: 'b6' + weights: + - b0: 0 + - b1: 3 + - b2: 2 + - b3: 1 + - b4: 1 + - b5: 1 + - b6: 2 + - b7: 2 + heads: + - id: 'b4'