Merge branch 'master' into proto-array + more changes

This commit is contained in:
Paul Hauner
2020-01-15 07:46:07 +11:00
79 changed files with 2318 additions and 897 deletions

View File

@@ -25,9 +25,17 @@ pub enum Error {
InvalidNodeDelta(usize),
DeltaOverflow(usize),
IndexOverflow(&'static str),
InvalidDeltaLen { deltas: usize, indices: usize },
InvalidDeltaLen {
deltas: usize,
indices: usize,
},
RevertedFinalizedEpoch,
InvalidFindHeadStartRoot,
InvalidFindHeadStartRoot {
justified_epoch: Epoch,
finalized_epoch: Epoch,
node_justified_epoch: Epoch,
node_finalized_epoch: Epoch,
},
}
#[derive(Default, PartialEq, Clone, Encode, Decode)]
@@ -159,10 +167,6 @@ impl ProtoArrayForkChoice {
let new_balances = justified_state_balances;
proto_array
.maybe_prune(finalized_epoch, finalized_root)
.map_err(|e| format!("find_head maybe_prune failed: {:?}", e))?;
let deltas = compute_deltas(
&proto_array.indices,
&mut votes,
@@ -172,7 +176,7 @@ impl ProtoArrayForkChoice {
.map_err(|e| format!("find_head compute_deltas failed: {:?}", e))?;
proto_array
.apply_score_changes(deltas, justified_epoch)
.apply_score_changes(deltas, justified_epoch, finalized_epoch)
.map_err(|e| format!("find_head apply_score_changes failed: {:?}", e))?;
*old_balances = new_balances.to_vec();

View File

@@ -61,6 +61,7 @@ impl ProtoArray {
&mut self,
mut deltas: Vec<i64>,
justified_epoch: Epoch,
finalized_epoch: Epoch,
) -> Result<(), Error> {
if deltas.len() != self.indices.len() {
return Err(Error::InvalidDeltaLen {
@@ -73,9 +74,11 @@ impl ProtoArray {
// finalized/justified epoch of all nodes against the epochs in `self`.
//
// This behaviour is equivalent to the `filter_block_tree` function in the spec.
self.ffg_update_required = justified_epoch != self.justified_epoch;
self.ffg_update_required =
justified_epoch != self.justified_epoch || finalized_epoch != self.finalized_epoch;
if self.ffg_update_required {
self.justified_epoch = justified_epoch;
self.finalized_epoch = finalized_epoch;
}
// Iterate backwards through all indices in `self.nodes`.
@@ -238,7 +241,7 @@ impl ProtoArray {
// If the blocks justified and finalized epochs match our values, then try and see if it
// becomes the best child.
if justified_epoch == self.justified_epoch && finalized_epoch == self.finalized_epoch {
if self.node_is_viable_for_head(&node) {
if let Some(parent_index) = node.parent {
let parent = self
.nodes
@@ -285,10 +288,13 @@ impl ProtoArray {
// It is a logic error to try and find the head starting from a block that does not match
// the filter.
if justified_node.justified_epoch != self.justified_epoch
|| justified_node.finalized_epoch != self.finalized_epoch
{
return Err(Error::InvalidFindHeadStartRoot);
if !self.node_is_viable_for_head(&justified_node) {
return Err(Error::InvalidFindHeadStartRoot {
justified_epoch: self.justified_epoch,
finalized_epoch: self.finalized_epoch,
node_justified_epoch: justified_node.justified_epoch,
node_finalized_epoch: justified_node.finalized_epoch,
});
}
let best_descendant_index = justified_node
@@ -422,6 +428,8 @@ impl ProtoArray {
/// Any node that has a different finalized or justified epoch should not be viable for the
/// head.
fn node_is_viable_for_head(&self, node: &ProtoNode) -> bool {
node.justified_epoch == self.justified_epoch && node.finalized_epoch == self.finalized_epoch
(node.justified_epoch == self.justified_epoch || self.justified_epoch == Epoch::new(0))
&& (node.finalized_epoch == self.finalized_epoch
|| self.finalized_epoch == Epoch::new(0))
}
}