From c5ee9294396f2b29167ab53072655b5b06e5cb21 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 7 Jan 2020 12:34:41 +1100 Subject: [PATCH] Add progress --- eth2/lmd_ghost/src/proto_array.rs | 39 ++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/eth2/lmd_ghost/src/proto_array.rs b/eth2/lmd_ghost/src/proto_array.rs index ec6bc3e20f..6893d3372d 100644 --- a/eth2/lmd_ghost/src/proto_array.rs +++ b/eth2/lmd_ghost/src/proto_array.rs @@ -125,6 +125,7 @@ impl ProtoArray { }; } + // back-prop best-child/target updates for i in (start..d.len()).rev() { if let Some(best_child) = self.get_best_child(i)? { // TODO: array access safety @@ -144,7 +145,6 @@ impl ProtoArray { if self.weights[i] > self.weights[best_child_of_parent] { self.best_child[parent] = Some(i) } - // Do thing } } else { // TODO: what is this? @@ -225,23 +225,44 @@ impl ProtoArray { // TODO: safe array access. self.best_descendant[i].saturating_sub(start); - if let Some(parent) = self.parents[i] { + self.parents[i] = if let Some(parent) = self.parents[i] { if parent < start { - parent = None + None } else { - // TODO: what happens if this becomes negative?? Safety issue. - parent -= start + Some(parent.saturating_sub(start)) } - } + } else { + None + }; - self.indices - .get_mut(n.block_root) - .ok_or_else(|| Error::NodeUnknown(n.block_root))? -= start + *self + .indices + .get_mut(&node.block_root) + .ok_or_else(|| Error::NodeUnknown(node.block_root))? -= start } Ok(()) } + pub fn head_fn(&self) -> Result { + let mut i = *self + .indices + .get(&self.dag.finalized) + .ok_or_else(|| Error::FinalizedNodeUnknown(self.dag.finalized))?; + + loop { + // TODO: safe array access. + if let Some(best_child) = self.best_child[i] { + i = best_child; + } else { + break; + } + } + + // TODO: safe array access. + Ok(self.nodes[i].block_root) + } + fn check_consistency(&self) -> Result<(), Error> { let num_nodes = self.nodes.len(); if self.best_child.len() != num_nodes {