Merge pull request #305 from sigp/fork-choice-correction
Correct all fork choice rules for children with no votes.
This commit is contained in:
commit
e44f2fca8c
@ -409,11 +409,23 @@ impl<T: ClientDB + Sized> ForkChoice for BitwiseLMDGhost<T> {
|
||||
*child_votes.entry(child).or_insert_with(|| 0) += vote;
|
||||
}
|
||||
}
|
||||
// given the votes on the children, find the best child
|
||||
current_head = self
|
||||
.choose_best_child(&child_votes)
|
||||
.ok_or(ForkChoiceError::CannotFindBestChild)?;
|
||||
trace!("Best child found: {}", current_head);
|
||||
// check if we have votes of children, if not select the smallest hash child
|
||||
if child_votes.is_empty() {
|
||||
current_head = *children
|
||||
.iter()
|
||||
.min_by(|child1, child2| child1.cmp(child2))
|
||||
.expect("Must be children here");
|
||||
trace!(
|
||||
"Children have no votes - smallest hash chosen: {}",
|
||||
current_head
|
||||
);
|
||||
} else {
|
||||
// given the votes on the children, find the best child
|
||||
current_head = self
|
||||
.choose_best_child(&child_votes)
|
||||
.ok_or(ForkChoiceError::CannotFindBestChild)?;
|
||||
trace!("Best child found: {}", current_head);
|
||||
}
|
||||
}
|
||||
|
||||
// didn't find head yet, proceed to next iteration
|
||||
|
@ -380,11 +380,23 @@ impl<T: ClientDB + Sized> ForkChoice for OptimizedLMDGhost<T> {
|
||||
*child_votes.entry(child).or_insert_with(|| 0) += vote;
|
||||
}
|
||||
}
|
||||
// given the votes on the children, find the best child
|
||||
current_head = self
|
||||
.choose_best_child(&child_votes)
|
||||
.ok_or(ForkChoiceError::CannotFindBestChild)?;
|
||||
trace!("Best child found: {}", current_head);
|
||||
// check if we have votes of children, if not select the smallest hash child
|
||||
if child_votes.is_empty() {
|
||||
current_head = *children
|
||||
.iter()
|
||||
.min_by(|child1, child2| child1.cmp(child2))
|
||||
.expect("Must be children here");
|
||||
trace!(
|
||||
"Children have no votes - smallest hash chosen: {}",
|
||||
current_head
|
||||
);
|
||||
} else {
|
||||
// given the votes on the children, find the best child
|
||||
current_head = self
|
||||
.choose_best_child(&child_votes)
|
||||
.ok_or(ForkChoiceError::CannotFindBestChild)?;
|
||||
trace!("Best child found: {}", current_head);
|
||||
}
|
||||
}
|
||||
|
||||
// didn't find head yet, proceed to next iteration
|
||||
|
@ -1 +0,0 @@
|
||||
|
@ -210,6 +210,7 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
|
||||
trace!("Children found: {:?}", children);
|
||||
|
||||
let mut head_vote_count = 0;
|
||||
head_hash = children[0];
|
||||
for child_hash in children {
|
||||
let vote_count = self.get_vote_count(&latest_votes, &child_hash)?;
|
||||
trace!("Vote count for child: {} is: {}", child_hash, vote_count);
|
||||
@ -218,6 +219,12 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
|
||||
head_hash = *child_hash;
|
||||
head_vote_count = vote_count;
|
||||
}
|
||||
// resolve ties - choose smaller hash
|
||||
else if vote_count == head_vote_count {
|
||||
if *child_hash < head_hash {
|
||||
head_hash = *child_hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(head_hash)
|
||||
|
@ -129,3 +129,16 @@ test_cases:
|
||||
- b9: 2
|
||||
heads:
|
||||
- id: 'b7'
|
||||
- blocks:
|
||||
- id: 'b0'
|
||||
parent: 'b0'
|
||||
- id: 'b1'
|
||||
parent: 'b0'
|
||||
- id: 'b2'
|
||||
parent: 'b0'
|
||||
weights:
|
||||
- b1: 0
|
||||
- b2: 0
|
||||
heads:
|
||||
- id: 'b1'
|
||||
|
||||
|
@ -51,3 +51,15 @@ test_cases:
|
||||
- b3: 6
|
||||
heads:
|
||||
- id: 'b2'
|
||||
- blocks:
|
||||
- id: 'b0'
|
||||
parent: 'b0'
|
||||
- id: 'b1'
|
||||
parent: 'b0'
|
||||
- id: 'b2'
|
||||
parent: 'b0'
|
||||
weights:
|
||||
- b1: 0
|
||||
- b2: 0
|
||||
heads:
|
||||
- id: 'b1'
|
||||
|
Loading…
Reference in New Issue
Block a user