Merge pull request #268 from sigp/fork-choice-corrections

Correct bitwise fork-choice rule.
This commit is contained in:
Paul Hauner 2019-03-01 18:34:44 +13:00 committed by GitHub
commit 8671c5790c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 3 deletions

View File

@ -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<T: ClientDB + Sized> ForkChoice for BitwiseLMDGhost<T> {
// if there are no children, we are done, return the current_head
let children = match self.children.get(&current_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<T: ClientDB + Sized> ForkChoice for BitwiseLMDGhost<T> {
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 {

View File

@ -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'