Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from all commits
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
30 changes: 24 additions & 6 deletions core/client/src/leaves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub struct DisplacedLeaf<H, N> {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LeafSet<H, N> {
storage: BTreeSet<LeafSetItem<H, N>>,
pending_added: Vec<LeafSetItem<H, N>>,
pending_removed: Vec<H>,
}

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

Expand All @@ -94,20 +98,25 @@ impl<H, N> LeafSet<H, N> where
};
storage.insert(LeafSetItem { hash, number });
}
Ok(Self { storage })
Ok(Self {
storage,
pending_added: Vec::new(),
pending_removed: Vec::new(),
})
}

/// 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<DisplacedLeaf<H, N>> {
// avoid underflow for genesis.
let displaced = if number != N::zero() {
let displaced = LeafSetItem {
hash: parent_hash,
hash: parent_hash.clone(),
number: number.clone() - N::one(),
};
let was_displaced = self.storage.remove(&displaced);

if was_displaced {
self.pending_removed.push(parent_hash);
Some(DisplacedLeaf {
new_hash: hash.clone(),
displaced,
Expand All @@ -119,7 +128,9 @@ impl<H, N> LeafSet<H, N> where
None
};

self.storage.insert(LeafSetItem { hash, number });
let item = LeafSetItem { hash, number };
self.storage.insert(item.clone());
self.pending_added.push(item);
displaced
}

Expand All @@ -128,6 +139,8 @@ impl<H, N> LeafSet<H, N> where
let new_number = displaced.displaced.number.clone() + N::one();
self.storage.remove(&LeafSetItem { hash: displaced.new_hash, number: new_number });
self.storage.insert(displaced.displaced);
self.pending_added.clear();
self.pending_removed.clear();
}

/// currently since revert only affects the canonical chain
Expand All @@ -148,13 +161,18 @@ impl<H, N> LeafSet<H, N> where
}

/// Write the leaf list to the database transaction.
pub fn prepare_transaction(&self, tx: &mut DBTransaction, column: Option<u32>, prefix: &[u8]) {
pub fn prepare_transaction(&mut self, tx: &mut DBTransaction, column: Option<u32>, prefix: &[u8]) {
let mut buf = prefix.to_vec();
for &LeafSetItem { ref hash, ref number } in &self.storage {
for LeafSetItem { hash, number } in self.pending_added.drain(..) {
hash.using_encoded(|s| buf.extend(s));
tx.put_vec(column, &buf[..], number.encode());
buf.truncate(prefix.len()); // reuse allocation.
}
for hash in self.pending_removed.drain(..) {
hash.using_encoded(|s| buf.extend(s));
tx.delete(column, &buf[..]);
buf.truncate(prefix.len()); // reuse allocation.
}
}
}

Expand Down