Use BABE instead of AuRa in node (#3171)

* babe: add babe module trait

* babe: track current slot and epoch start slot

* babe: implement ShouldEndSession based on epochs

* babe: rename weight type to avoid ambiguities

* babe: expose epoch start slot in Epoch digest

* babe: use epoch start for validating epoch transitions

* babe: make the epoch duration a parameter type

* babe: remove unused fields from config

* node: update runtime to use babe instead of aura

* node: use babe instead of aura

* core: generate sr25519 keys from seed and add to keystore

* core: remove AuthorityKeyring

* node: remove unused primitive types related to babe crypto

* uniform babe primitives crate import name

* wrap long lines

* babe: fix find_epoch_digest

* fork-tree: fix find_node_where

* node: set babe epoch duration to "10 minutes"

* babe: cleanup import key cache if authorities don't change

* node: make integration test compile (but fail)

* node: bump spec_version

* node: fix import

* babe: don't use constants in storage fields array sizes

* babe: account for first epoch slot way in the past

* babe: signal next epoch change (not current)

* babe: calculate next epoch randomness with next epoch index

* babe: track next epoch in node

* babe: cache current epoch and authorities separately

* babe: generate valid babe vrf proofs in integration test

* babe: cleanup claim_slot

* babe: perform threshold calculation according to spec

* babe: compute relative weight in threshold

* babe: more precise threshold calculation

* babe: use floats for threshold exponent calculation

* babe: update constant c
This commit is contained in:
André Silva
2019-07-24 20:53:04 +01:00
committed by DemiMarie-parity
parent 407970406d
commit 9f50c8fce4
33 changed files with 784 additions and 429 deletions
+48 -6
View File
@@ -228,7 +228,7 @@ impl<H, N, V> ForkTree<H, N, V> where
let node = root.find_node_where(hash, number, is_descendent_of, predicate)?;
// found the node, early exit
if node.is_some() {
if let Some(node) = node {
return Ok(node);
}
}
@@ -498,7 +498,7 @@ mod node_implementation {
number: &N,
is_descendent_of: &F,
predicate: &P,
) -> Result<Option<&Node<H, N, V>>, Error<E>>
) -> Result<Option<Option<&Node<H, N, V>>>, Error<E>>
where E: std::error::Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
@@ -519,10 +519,18 @@ mod node_implementation {
}
// node not found in any of the descendents, if the node we're
// searching for is a descendent of this node and it passes the
// predicate, then it is this one.
if predicate(&self.data) && is_descendent_of(&self.hash, hash)? {
Ok(Some(self))
// searching for is a descendent of this node then we will stop the
// search here, since there aren't any more children and we found
// the correct node so we don't want to backtrack.
if is_descendent_of(&self.hash, hash)? {
// if the predicate passes we return the node
if predicate(&self.data) {
Ok(Some(Some(self)))
// otherwise we stop the search returning `None`
} else {
Ok(Some(None))
}
} else {
Ok(None)
}
@@ -1033,4 +1041,38 @@ mod test {
vec!["C", "D", "E"],
);
}
#[test]
fn find_node_doesnt_backtrack_after_finding_highest_descending_node() {
let mut tree = ForkTree::new();
//
// A - B
// \
// — C
//
let is_descendent_of = |base: &&str, block: &&str| -> Result<bool, TestError> {
match (*base, *block) {
("A", b) => Ok(b == "B" || b == "C" || b == "D"),
("B", b) | ("C", b) => Ok(b == "D"),
("0", _) => Ok(true),
_ => Ok(false),
}
};
tree.import("A", 1, 1, &is_descendent_of).unwrap();
tree.import("B", 2, 4, &is_descendent_of).unwrap();
tree.import("C", 2, 4, &is_descendent_of).unwrap();
// when searching the tree we reach both node `B` and `C`, but the
// predicate doesn't pass. still, we should not backtrack to node `A`.
let node = tree.find_node_where(
&"D",
&3,
&is_descendent_of,
&|data| *data < 3,
).unwrap();
assert_eq!(node, None);
}
}