Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9062f94
value ranges in consensus cache
svyatonik Aug 2, 2019
ce451c3
skip values in cache
svyatonik Aug 2, 2019
2efc6a4
read epoch0 + epoch1 data from genesis in babe
svyatonik Aug 2, 2019
d9d75bc
sync authorities + session validators at genesis
svyatonik Aug 4, 2019
aa491d0
removed some debug printlns
svyatonik Aug 4, 2019
de097ee
fixed cache encoding
svyatonik Aug 5, 2019
4bbebde
Revert "skip values in cache"
svyatonik Aug 5, 2019
2897a83
Revert "value ranges in consensus cache"
svyatonik Aug 5, 2019
2c72a87
get rid of cache::AUTHORITIES in Babe
svyatonik Aug 5, 2019
1c7b2cd
cleaning up
svyatonik Aug 5, 2019
cb2851f
cleaning up
svyatonik Aug 5, 2019
e5c7bd8
update spec version
svyatonik Aug 5, 2019
a838e9b
lost changes
svyatonik Aug 5, 2019
200a638
fixed tests
svyatonik Aug 5, 2019
3c420ff
Merge branch 'master' into fix_flaming_fir_light_sync
svyatonik Aug 5, 2019
c4c6788
Update node/runtime/src/lib.rs
svyatonik Aug 6, 2019
04f73d5
Merge branch 'master' into fix_flaming_fir_light_sync
svyatonik Aug 6, 2019
f30f0fb
fix once-per-block condition
svyatonik Aug 6, 2019
773982b
fix standalone babe + temp_storage in BuildGenesis
svyatonik Aug 6, 2019
c922fad
fix benhes compilation
svyatonik Aug 6, 2019
69fe38a
fixed comment
svyatonik Aug 6, 2019
b5e223e
Merge branch 'master' into fix_flaming_fir_light_sync
svyatonik Aug 8, 2019
54fd261
Merge branch 'master' into fix_flaming_fir_light_sync
svyatonik Aug 12, 2019
5991212
re-added light nodes to integration tests
svyatonik Aug 12, 2019
d24bb0b
Merge branch 'master' into fix_flaming_fir_light_sync
Demi-Marie Aug 14, 2019
51b79fc
finalize_with_ancestors from extra_requests
svyatonik Aug 14, 2019
651d8ad
Merge branch 'fix_flaming_fir_light_sync' of https://github.com/parit…
svyatonik Aug 14, 2019
8f106ae
post-merge fix
svyatonik Aug 14, 2019
b4dd1bd
aaand removed debug code
svyatonik Aug 14, 2019
d593290
(another one)
svyatonik Aug 14, 2019
a7adc1c
fix warn in logs (do not call ForkTree::finalize twice for the same b…
svyatonik Aug 14, 2019
707b288
sync digest.next_authorities with actual next authorities
svyatonik Aug 14, 2019
f1d0e41
more docs
svyatonik Aug 15, 2019
515587c
Merge branch 'master' into fix_flaming_fir_light_sync
svyatonik Aug 15, 2019
b328acb
reverting all commits affecting storage
svyatonik Aug 16, 2019
4786ea7
Merge branch 'master' into fix_flaming_fir_light_sync
svyatonik Aug 16, 2019
e33fd0f
also remove keys from babe trait
svyatonik Aug 16, 2019
e7644e4
fixed warnings
svyatonik Aug 16, 2019
f70bbcc
Merge branch 'master' into fix_flaming_fir_light_sync
svyatonik Aug 16, 2019
89447ee
post-merge fixes
svyatonik Aug 16, 2019
320534d
reverted some redundant changes
svyatonik Aug 16, 2019
5bf8db9
reverted more changes
svyatonik Aug 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/network/src/protocol/sync/extra_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<B: BlockT> ExtraRequests<B> {
return Ok(())
}

self.tree.finalize(best_finalized_hash, best_finalized_number, &is_descendent_of)?;
self.tree.finalize_with_ancestors(best_finalized_hash, best_finalized_number, &is_descendent_of)?;

let roots = self.tree.roots().collect::<HashSet<_>>();

Expand Down
178 changes: 166 additions & 12 deletions core/utils/fork-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,16 @@ impl<H, N, V> ForkTree<H, N, V> where
/// with the given hash exists. All other roots are pruned, and the children
/// of the finalized node become the new roots.
pub fn finalize_root(&mut self, hash: &H) -> Option<V> {
if let Some(position) = self.roots.iter().position(|node| node.hash == *hash) {
let node = self.roots.swap_remove(position);
self.roots = node.children;
self.best_finalized_number = Some(node.number);
return Some(node.data);
}
self.roots.iter().position(|node| node.hash == *hash)
.map(|position| self.finalize_root_at(position))
}

None
/// Finalize root at given positiion. See `finalize_root` comment for details.
fn finalize_root_at(&mut self, position: usize) -> V {
let node = self.roots.swap_remove(position);
self.roots = node.children;
self.best_finalized_number = Some(node.number);
return node.data;
}

/// Finalize a node in the tree. This method will make sure that the node
Expand Down Expand Up @@ -305,6 +307,79 @@ impl<H, N, V> ForkTree<H, N, V> where
}
}

/// Finalize a node in the tree and all its ancestors. The given function
/// `is_descendent_of` should return `true` if the second hash (target) is
// a descendent of the first hash (base).
pub fn finalize_with_ancestors<F, E>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is very similar to what we do in prune method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. If I call prune(), for example, for block N in test storage, it leaves M node. If called for H, the H node is left in the storage. This isn't what expected from finalize_with_ancestors(). Probably it could be refactored to support both cases (or we could call prune() from within finalize_with_ancestors()), but I haven't found a straightforward way to do that.

&mut self,
hash: &H,
number: N,
is_descendent_of: &F,
) -> Result<FinalizationResult<V>, Error<E>>
where E: std::error::Error,
F: Fn(&H, &H) -> Result<bool, E>
{
if let Some(ref best_finalized_number) = self.best_finalized_number {
if number <= *best_finalized_number {
return Err(Error::Revert);
}
}

// check if one of the current roots is being finalized
if let Some(root) = self.finalize_root(hash) {
return Ok(FinalizationResult::Changed(Some(root)));
}

// we need to:
// 1) remove all roots that are not ancestors AND not descendants of finalized block;
// 2) if node is descendant - just leave it;
// 3) if node is ancestor - 'open it'
let mut changed = false;
let mut idx = 0;
while idx != self.roots.len() {
let (is_finalized, is_descendant, is_ancestor) = {
let root = &self.roots[idx];
let is_finalized = root.hash == *hash;
let is_descendant = !is_finalized
&& root.number > number && is_descendent_of(hash, &root.hash).unwrap_or(false);
let is_ancestor = !is_finalized && !is_descendant
&& root.number < number && is_descendent_of(&root.hash, hash).unwrap_or(false);
(is_finalized, is_descendant, is_ancestor)
};

// if we have met finalized root - open it and return
if is_finalized {
return Ok(FinalizationResult::Changed(Some(self.finalize_root_at(idx))));
}

// if node is descendant of finalized block - just leave it as is
if is_descendant {
idx += 1;
continue;
}

// if node is ancestor of finalized block - remove it and continue with children
if is_ancestor {
let root = self.roots.swap_remove(idx);
self.roots.extend(root.children);
changed = true;
continue;
}

// if node is neither ancestor, nor descendant of the finalized block - remove it
self.roots.swap_remove(idx);
changed = true;
}

self.best_finalized_number = Some(number);

if changed {
Ok(FinalizationResult::Changed(None))
} else {
Ok(FinalizationResult::Unchanged)
}
}

/// Checks if any node in the tree is finalized by either finalizing the
/// node itself or a child node that's not in the tree, guaranteeing that
/// the node being finalized isn't a descendent of any of the node's
Expand Down Expand Up @@ -580,23 +655,32 @@ mod test {
// / - G
// / /
// A - F - H - I
// \
// - L - M - N
// \
// - O
// \
// — J - K
//
// (where N is not a part of fork tree)
let is_descendent_of = |base: &&str, block: &&str| -> Result<bool, TestError> {
let letters = vec!["B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
let letters = vec!["B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"];
match (*base, *block) {
("A", b) => Ok(letters.into_iter().any(|n| n == b)),
("B", b) => Ok(b == "C" || b == "D" || b == "E"),
("C", b) => Ok(b == "D" || b == "E"),
("D", b) => Ok(b == "E"),
("E", _) => Ok(false),
("F", b) => Ok(b == "G" || b == "H" || b == "I"),
("F", b) => Ok(b == "G" || b == "H" || b == "I" || b == "L" || b == "M" || b == "N" || b == "O"),
("G", _) => Ok(false),
("H", b) => Ok(b == "I"),
("H", b) => Ok(b == "I" || b == "L" || b == "M" || b == "O"),
("I", _) => Ok(false),
("J", b) => Ok(b == "K"),
("K", _) => Ok(false),
("L", b) => Ok(b == "M" || b == "O" || b == "N"),
("M", b) => Ok(b == "N"),
("N", _) => Ok(false),
("O", _) => Ok(false),
("0", _) => Ok(true),
_ => Ok(false),
}
Expand All @@ -614,6 +698,9 @@ mod test {

tree.import("H", 3, (), &is_descendent_of).unwrap();
tree.import("I", 4, (), &is_descendent_of).unwrap();
tree.import("L", 4, (), &is_descendent_of).unwrap();
tree.import("M", 5, (), &is_descendent_of).unwrap();
tree.import("O", 5, (), &is_descendent_of).unwrap();

tree.import("J", 2, (), &is_descendent_of).unwrap();
tree.import("K", 3, (), &is_descendent_of).unwrap();
Expand Down Expand Up @@ -770,7 +857,7 @@ mod test {

assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("I", 4)],
vec![("I", 4), ("L", 4)],
);

// finalizing a node from another fork that isn't part of the tree clears the tree
Expand All @@ -782,6 +869,71 @@ mod test {
assert!(tree.roots.is_empty());
}

#[test]
fn finalize_with_ancestor_works() {
let (mut tree, is_descendent_of) = test_fork_tree();

let original_roots = tree.roots.clone();

// finalizing a block prior to any in the node doesn't change the tree
assert_eq!(
tree.finalize_with_ancestors(&"0", 0, &is_descendent_of),
Ok(FinalizationResult::Unchanged),
);

assert_eq!(tree.roots, original_roots);

// finalizing "A" opens up three possible forks
assert_eq!(
tree.finalize_with_ancestors(&"A", 1, &is_descendent_of),
Ok(FinalizationResult::Changed(Some(()))),
);

assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("B", 2), ("F", 2), ("J", 2)],
);

// finalizing H:
// 1) removes roots that are not ancestors/descendants of H (B, J)
// 2) opens root that is ancestor of H (F -> G+H)
// 3) finalizes the just opened root H (H -> I + L)
assert_eq!(
tree.finalize_with_ancestors(&"H", 3, &is_descendent_of),
Ok(FinalizationResult::Changed(Some(()))),
);

assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("I", 4), ("L", 4)],
);

assert_eq!(
tree.best_finalized_number,
Some(3),
);

// finalizing N (which is not a part of the tree):
// 1) removes roots that are not ancestors/descendants of N (I)
// 2) opens root that is ancestor of N (L -> M+O)
// 3) removes roots that are not ancestors/descendants of N (O)
// 4) opens root that is ancestor of N (M -> {})
assert_eq!(
tree.finalize_with_ancestors(&"N", 6, &is_descendent_of),
Ok(FinalizationResult::Changed(None)),
);

assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![],
);

assert_eq!(
tree.best_finalized_number,
Some(6),
);
}

#[test]
fn finalize_with_descendent_works() {
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -927,7 +1079,9 @@ mod test {
vec![
("A", 1),
("J", 2), ("K", 3),
("F", 2), ("H", 3), ("I", 4),
("F", 2), ("H", 3), ("L", 4), ("O", 5),
("M", 5),
("I", 4),
("G", 3),
("B", 2), ("C", 3), ("D", 4), ("E", 5),
],
Expand Down