Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
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
47 changes: 22 additions & 25 deletions client/api/src/leaves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ impl<H, N: Ord> FinalizationDisplaced<H, N> {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LeafSet<H, N> {
storage: BTreeMap<Reverse<N>, Vec<H>>,
pending_added: Vec<(H, N)>,
pending_removed: Vec<H>,
}

impl<H, N> LeafSet<H, N>
Expand All @@ -78,7 +76,7 @@ where
{
/// Construct a new, blank leaf set.
pub fn new() -> Self {
Self { storage: BTreeMap::new(), pending_added: Vec::new(), pending_removed: Vec::new() }
Self { storage: BTreeMap::new() }
}

/// Read the leaf list from the DB, using given prefix for keys.
Expand All @@ -97,21 +95,21 @@ where
},
None => {},
}
Ok(Self { storage, pending_added: Vec::new(), pending_removed: Vec::new() })
Ok(Self { storage })
}

/// update the leaf list on import. returns a displaced leaf if there was one.
/// Update the leaf list on import.
/// Returns a displaced leaf if there was one.
pub fn import(&mut self, hash: H, number: N, parent_hash: H) -> Option<ImportDisplaced<H, N>> {
// avoid underflow for genesis.
let displaced = if number != N::zero() {
let new_number = Reverse(number.clone() - N::one());
let was_displaced = self.remove_leaf(&new_number, &parent_hash);
let parent_number = Reverse(number.clone() - N::one());
let was_displaced = self.remove_leaf(&parent_number, &parent_hash);

if was_displaced {
self.pending_removed.push(parent_hash.clone());
Some(ImportDisplaced {
new_hash: hash.clone(),
displaced: LeafSetItem { hash: parent_hash, number: new_number },
displaced: LeafSetItem { hash: parent_hash, number: parent_number },
})
} else {
None
Expand All @@ -121,7 +119,6 @@ where
};

self.insert_leaf(Reverse(number.clone()), hash.clone());
self.pending_added.push((hash, number));
displaced
}

Expand All @@ -140,8 +137,6 @@ where
};

let below_boundary = self.storage.split_off(&Reverse(boundary));
self.pending_removed
.extend(below_boundary.values().flat_map(|h| h.iter()).cloned());
FinalizationDisplaced { leaves: below_boundary }
}

Expand Down Expand Up @@ -188,8 +183,6 @@ where
self.remove_leaf(number, hash),
"item comes from an iterator over storage; qed",
);

self.pending_removed.push(hash.clone());
}
}

Expand All @@ -203,7 +196,6 @@ where
// this is an invariant of regular block import.
if !leaves_contains_best {
self.insert_leaf(best_number.clone(), best_hash.clone());
self.pending_added.push((best_hash, best_number.0));
}
}

Expand All @@ -227,8 +219,6 @@ where
) {
let leaves: Vec<_> = self.storage.iter().map(|(n, h)| (n.0.clone(), h.clone())).collect();
tx.set_from_vec(column, prefix, leaves.encode());
self.pending_added.clear();
self.pending_removed.clear();
}

/// Check if given block is a leaf.
Expand All @@ -242,7 +232,7 @@ where
self.storage.entry(number).or_insert_with(Vec::new).push(hash);
}

// returns true if this leaf was contained, false otherwise.
// Returns true if this leaf was contained, false otherwise.
fn remove_leaf(&mut self, number: &Reverse<N>, hash: &H) -> bool {
let mut empty = false;
let removed = self.storage.get_mut(number).map_or(false, |leaves| {
Expand Down Expand Up @@ -294,13 +284,6 @@ where
}
}

impl<'a, H: 'a, N: 'a> Drop for Undo<'a, H, N> {
fn drop(&mut self) {
self.inner.pending_added.clear();
self.inner.pending_removed.clear();
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -391,6 +374,20 @@ mod tests {
assert_eq!(set, set2);
}

#[test]
fn undo_import() {
let mut set = LeafSet::new();
set.import(10_1u32, 10u32, 0u32);
set.import(11_1, 11, 10_1);
set.import(11_2, 11, 10_1);

let displaced = set.import(12_1, 12, 11_2).unwrap();
assert!(set.contains(12, 12_1));

set.undo().undo_import(displaced);
assert!(!set.contains(12, 12_1));
Comment thread
davxy marked this conversation as resolved.
Outdated
}

#[test]
fn undo_finalization() {
let mut set = LeafSet::new();
Expand Down