Guard reduced tree from errors

This commit is contained in:
Paul Hauner 2019-08-27 18:09:31 +10:00
parent ed6c39e25a
commit 6bb3a65189
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 {