Fix failing test, add hacky fix

This commit is contained in:
Paul Hauner
2019-04-24 14:56:39 +10:00
parent e19abee7f9
commit e12fa58e6e
5 changed files with 171 additions and 169 deletions

View File

@@ -220,7 +220,6 @@ impl TreeHashCache {
leaves.append(&mut t.root()?.to_vec());
let (mut bytes, _bools, mut t_overlays) = t.into_components();
cache.append(&mut bytes);
overlays.append(&mut t_overlays);
}
@@ -296,33 +295,40 @@ impl TreeHashCache {
) -> Result<BTreeOverlay, Error> {
let old_overlay = self.get_overlay(overlay_index, chunk_index)?;
// Get slices of the exsiting tree from the cache.
let (old_bytes, old_flags) = self
.slices(old_overlay.chunk_range())
.ok_or_else(|| Error::UnableToObtainSlices)?;
// If the merkle tree required to represent the new list is of a different size to the one
// required for the previous list, then update our cache.
//
// This grows/shrinks the bytes to accomodate the new tree, preserving as much of the tree
// as possible.
if new_overlay.num_leaf_nodes() != old_overlay.num_leaf_nodes() {
// Get slices of the exsiting tree from the cache.
let (old_bytes, old_flags) = self
.slices(old_overlay.chunk_range())
.ok_or_else(|| Error::UnableToObtainSlices)?;
let (new_bytes, new_bools) = if new_overlay.num_leaf_nodes() > old_overlay.num_leaf_nodes()
{
resize::grow_merkle_cache(
old_bytes,
old_flags,
old_overlay.height(),
new_overlay.height(),
)
.ok_or_else(|| Error::UnableToGrowMerkleTree)?
} else {
resize::shrink_merkle_cache(
old_bytes,
old_flags,
old_overlay.height(),
new_overlay.height(),
new_overlay.num_chunks(),
)
.ok_or_else(|| Error::UnableToShrinkMerkleTree)?
};
let (new_bytes, new_bools) =
if new_overlay.num_leaf_nodes() > old_overlay.num_leaf_nodes() {
resize::grow_merkle_cache(
old_bytes,
old_flags,
old_overlay.height(),
new_overlay.height(),
)
.ok_or_else(|| Error::UnableToGrowMerkleTree)?
} else {
resize::shrink_merkle_cache(
old_bytes,
old_flags,
old_overlay.height(),
new_overlay.height(),
new_overlay.num_chunks(),
)
.ok_or_else(|| Error::UnableToShrinkMerkleTree)?
};
// Splice the newly created `TreeHashCache` over the existing elements.
self.splice(old_overlay.chunk_range(), new_bytes, new_bools);
// Splice the newly created `TreeHashCache` over the existing elements.
self.splice(old_overlay.chunk_range(), new_bytes, new_bools);
}
Ok(std::mem::replace(
&mut self.overlays[overlay_index],

View File

@@ -96,10 +96,6 @@ impl BTreeOverlay {
pub fn get_leaf_node(&self, i: usize) -> Result<Option<Range<usize>>, Error> {
if i >= self.num_nodes() - self.num_padding_leaves() {
Ok(None)
/*
} else if i < self.num_internal_nodes() {
Ok(None)
*/
} else if (i == self.num_internal_nodes()) && (self.num_items == 0) {
// If this is the first leaf node and the overlay contains zero items, return `None` as
// this node must be padding.

View File

@@ -67,14 +67,7 @@ where
let old_overlay = cache.get_overlay(cache.overlay_index, cache.chunk_index)?;
let new_overlay = BTreeOverlay::new(self, cache.chunk_index, old_overlay.depth)?;
// If the merkle tree required to represent the new list is of a different size to the one
// required for the previous list, then update our cache.
//
// This grows/shrinks the bytes to accomodate the new tree, preserving as much of the tree
// as possible.
if new_overlay.num_leaf_nodes() != old_overlay.num_leaf_nodes() {
cache.replace_overlay(cache.overlay_index, cache.chunk_index, new_overlay.clone())?;
}
cache.replace_overlay(cache.overlay_index, cache.chunk_index, new_overlay.clone())?;
cache.overlay_index += 1;
@@ -120,6 +113,9 @@ where
// The item existed in the previous list and exists in the current list.
(Some(_old), Some(new)) => {
cache.chunk_index = new.start;
if cache.chunk_index + 1 < cache.chunk_modified.len() {
cache.chunk_modified[cache.chunk_index + 1] = true;
}
self[i].update_tree_hash_cache(cache)?;
}
@@ -157,11 +153,7 @@ where
// splice out the entire tree of the removed node, replacing it
// with a single padding node.
cache.splice(old, vec![0; HASHSIZE], vec![true]);
// cache.overlays.remove(cache.overlay_index);
}
// local_overlay_index += 1;
}
// The item didn't exist in the old list and doesn't exist in the new list,
// nothing to do.