Merge branch 'fix-fork-choice' into interop

This commit is contained in:
Paul Hauner 2019-08-29 14:33:29 +10:00
commit bf5a55c97c
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF

View File

@ -311,6 +311,7 @@ where
/// become redundant and removed from the reduced tree.
fn remove_latest_message(&mut self, validator_index: usize) -> Result<()> {
if let Some(vote) = *self.latest_votes.get(validator_index) {
if self.nodes.contains_key(&vote.hash) {
self.get_mut_node(vote.hash)?.remove_voter(validator_index);
let node = self.get_node(vote.hash)?.clone();
@ -357,6 +358,7 @@ where
self.latest_votes.insert(validator_index, Some(vote));
}
}
Ok(())
}
@ -370,7 +372,8 @@ where
/// - it does not have any votes.
fn maybe_delete_node(&mut self, hash: Hash256) -> Result<()> {
let should_delete = {
let node = self.get_node(hash)?.clone();
if let Ok(node) = self.get_node(hash) {
let node = node.clone();
if let Some(parent_hash) = node.parent_hash {
if (node.children.len() == 1) && !node.has_votes() {
@ -391,6 +394,10 @@ where
// A node without a parent is the genesis node and should not be deleted.
false
}
} else {
// No need to delete a node that does not exist.
false
}
};
if should_delete {
@ -463,6 +470,7 @@ where
// descendant of both `node` and `prev_in_tree`.
if self
.iter_ancestors(child_hash)?
.take_while(|(_, slot)| *slot >= self.root_slot())
.any(|(ancestor, _slot)| ancestor == node.block_hash)
{
let child = self.get_mut_node(child_hash)?;
@ -548,6 +556,7 @@ where
fn find_prev_in_tree(&mut self, hash: Hash256) -> Option<Hash256> {
self.iter_ancestors(hash)
.ok()?
.take_while(|(_, slot)| *slot >= self.root_slot())
.find(|(root, _slot)| self.nodes.contains_key(root))
.and_then(|(root, _slot)| Some(root))
}
@ -555,8 +564,12 @@ where
/// For the two given block roots (`a_root` and `b_root`), find the first block they share in
/// the tree. Viz, find the block that these two distinct blocks forked from.
fn find_highest_common_ancestor(&self, a_root: Hash256, b_root: Hash256) -> Result<Hash256> {
let mut a_iter = self.iter_ancestors(a_root)?;
let mut b_iter = self.iter_ancestors(b_root)?;
let mut a_iter = self
.iter_ancestors(a_root)?
.take_while(|(_, slot)| *slot >= self.root_slot());
let mut b_iter = self
.iter_ancestors(b_root)?
.take_while(|(_, slot)| *slot >= self.root_slot());
// Combines the `next()` fns on the `a_iter` and `b_iter` and returns the roots of two
// blocks at the same slot, or `None` if we have gone past genesis or the root of this tree.