Add tree shrinking for u64 vec
This commit is contained in:
parent
9bc0519092
commit
da74c4ce74
@ -1,4 +1,4 @@
|
|||||||
use super::resize::grow_merkle_cache;
|
use super::resize::{grow_merkle_cache, shrink_merkle_cache};
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::ssz_encode;
|
use crate::ssz_encode;
|
||||||
|
|
||||||
@ -112,35 +112,41 @@ where
|
|||||||
chunk: usize,
|
chunk: usize,
|
||||||
) -> Result<usize, Error> {
|
) -> Result<usize, Error> {
|
||||||
let offset_handler = OffsetHandler::new(self, chunk)?;
|
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)?;
|
let old_offset_handler = OffsetHandler::new(other, chunk)?;
|
||||||
|
|
||||||
dbg!(old_offset_handler.node_range());
|
if offset_handler.num_leaf_nodes != old_offset_handler.num_leaf_nodes {
|
||||||
|
let old_offset_handler = OffsetHandler::new(other, chunk)?;
|
||||||
|
|
||||||
// Get slices of the exsiting tree from the cache.
|
// Get slices of the exsiting tree from the cache.
|
||||||
let (old_bytes, old_flags) = cache
|
let (old_bytes, old_flags) = cache
|
||||||
.slices(old_offset_handler.node_range())
|
.slices(old_offset_handler.node_range())
|
||||||
.ok_or_else(|| Error::UnableToObtainSlices)?;
|
.ok_or_else(|| Error::UnableToObtainSlices)?;
|
||||||
|
|
||||||
// From the existing slices build new, expanded Vecs.
|
let (new_bytes, new_flags) =
|
||||||
let (new_bytes, new_flags) = grow_merkle_cache(
|
if offset_handler.num_leaf_nodes > old_offset_handler.num_leaf_nodes {
|
||||||
|
grow_merkle_cache(
|
||||||
old_bytes,
|
old_bytes,
|
||||||
old_flags,
|
old_flags,
|
||||||
old_offset_handler.height(),
|
old_offset_handler.height(),
|
||||||
offset_handler.height(),
|
offset_handler.height(),
|
||||||
).ok_or_else(|| Error::UnableToGrowMerkleTree)?;
|
)
|
||||||
|
.ok_or_else(|| Error::UnableToGrowMerkleTree)?
|
||||||
|
} else {
|
||||||
|
shrink_merkle_cache(
|
||||||
|
old_bytes,
|
||||||
|
old_flags,
|
||||||
|
old_offset_handler.height(),
|
||||||
|
offset_handler.height(),
|
||||||
|
offset_handler.total_nodes(),
|
||||||
|
)
|
||||||
|
.ok_or_else(|| Error::UnableToGrowMerkleTree)?
|
||||||
|
};
|
||||||
|
|
||||||
// Create a `TreeHashCache` from the raw elements.
|
// Create a `TreeHashCache` from the raw elements.
|
||||||
let expanded_cache = TreeHashCache::from_elems(new_bytes, new_flags);
|
let expanded_cache = TreeHashCache::from_elems(new_bytes, new_flags);
|
||||||
|
|
||||||
// Splice the newly created `TreeHashCache` over the existing, smaller elements.
|
// Splice the newly created `TreeHashCache` over the existing, smaller elements.
|
||||||
cache.splice(old_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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match T::item_type() {
|
match T::item_type() {
|
||||||
|
@ -396,6 +396,15 @@ fn shortened_u64_vec_len_within_pow_2_boundary() {
|
|||||||
test_u64_vec_modifications(original_vec, modified_vec);
|
test_u64_vec_modifications(original_vec, modified_vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn shortened_u64_vec_len_outside_pow_2_boundary() {
|
||||||
|
let original_vec: Vec<u64> = (0..2_u64.pow(6)).collect();
|
||||||
|
|
||||||
|
let modified_vec: Vec<u64> = (0..2_u64.pow(5)).collect();
|
||||||
|
|
||||||
|
test_u64_vec_modifications(original_vec, modified_vec);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn extended_u64_vec_len_within_pow_2_boundary() {
|
fn extended_u64_vec_len_within_pow_2_boundary() {
|
||||||
let n: u64 = 2_u64.pow(5) - 2;
|
let n: u64 = 2_u64.pow(5) - 2;
|
||||||
|
Loading…
Reference in New Issue
Block a user