Fix failing tree hash test

This commit is contained in:
Paul Hauner
2019-04-14 13:54:04 +10:00
parent e038bd18b5
commit 737e6b9a86
3 changed files with 39 additions and 24 deletions

View File

@@ -113,25 +113,31 @@ where
) -> Result<usize, Error> {
let offset_handler = OffsetHandler::new(self, chunk)?;
// Check to see if the length of the list has changed length beyond a power-of-two
// boundary. In such a case we need to resize the merkle tree bytes.
if self.len().next_power_of_two() > other.len().next_power_of_two() {
let old_offset_handler = OffsetHandler::new(other, chunk)?;
dbg!(old_offset_handler.node_range());
// Get slices of the exsiting tree from the cache.
let (old_bytes, old_flags) = cache
.slices(offset_handler.node_range()?)
.slices(old_offset_handler.node_range())
.ok_or_else(|| Error::UnableToObtainSlices)?;
// From the existing slices build new, expanded Vecs.
let (new_bytes, new_flags) = grow_merkle_cache(
old_bytes,
old_flags,
other.len().next_power_of_two().leading_zeros() as usize,
self.len().next_power_of_two().leading_zeros() as usize,
old_offset_handler.height(),
offset_handler.height(),
).ok_or_else(|| Error::UnableToGrowMerkleTree)?;
// Create a `TreeHashCache` from the raw elements.
let expanded_cache = TreeHashCache::from_elems(new_bytes, new_flags);
// Splice the newly created `TreeHashCache` over the existing, smaller elements.
cache.splice(offset_handler.node_range()?, expanded_cache);
cache.splice(old_offset_handler.node_range(), expanded_cache);
//
} else if self.len().next_power_of_two() < other.len().next_power_of_two() {
panic!("shrinking below power of two is not implemented")