mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2024-12-25 13:07:18 +00:00
Add first pass of grow cache algo
This commit is contained in:
parent
a124042e30
commit
75177837d0
@ -6,6 +6,7 @@ use std::ops::Range;
|
||||
use std::vec::Splice;
|
||||
|
||||
mod impls;
|
||||
mod resize;
|
||||
mod tests;
|
||||
|
||||
const BYTES_PER_CHUNK: usize = 32;
|
||||
|
@ -170,23 +170,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// New vec is bigger than old vec.
|
||||
fn grow_merkle_cache(cache: Vec<u8>, to: usize) -> Vec<u8> {
|
||||
let new = Vec::with_capacity(to * HASHSIZE);
|
||||
|
||||
let i = cache.len() / HASHSIZE;
|
||||
let j = to;
|
||||
|
||||
assert_eq!(i.next_power_of_two(), i);
|
||||
assert_eq!(j.next_power_of_two(), j);
|
||||
|
||||
while i > 0 {
|
||||
|
||||
}
|
||||
|
||||
new
|
||||
}
|
||||
|
||||
fn get_packed_leaves<T>(vec: &Vec<T>) -> Vec<u8>
|
||||
where
|
||||
T: CachedTreeHash<T>,
|
||||
|
33
eth2/utils/ssz/src/cached_tree_hash/resize.rs
Normal file
33
eth2/utils/ssz/src/cached_tree_hash/resize.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use super::*;
|
||||
|
||||
/// New vec is bigger than old vec.
|
||||
fn grow_merkle_cache(old_bytes: &[u8], old_flags: &[bool], to: usize) -> Option<Vec<u8>> {
|
||||
let mut bytes = Vec::with_capacity(to * HASHSIZE);
|
||||
let mut flags = Vec::with_capacity(to);
|
||||
|
||||
let from = old_bytes.len() / HASHSIZE;
|
||||
let to = to;
|
||||
|
||||
let distance = (from.leading_zeros() - to.leading_zeros()) as usize;
|
||||
|
||||
let leading_zero_chunks = 1 >> distance;
|
||||
|
||||
bytes.resize(leading_zero_chunks * HASHSIZE, 0);
|
||||
flags.resize(leading_zero_chunks, true); // all new chunks are modified by default.
|
||||
|
||||
for i in 0..to.leading_zeros() as usize {
|
||||
let new_slice = bytes.get_mut(1 >> i + distance..1 >> i + distance + 1)?;
|
||||
let old_slice = old_bytes.get(1 >> i..1 >> i + 1)?;
|
||||
new_slice.copy_from_slice(old_slice);
|
||||
}
|
||||
|
||||
Some(bytes)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
#[test]
|
||||
fn can_grow() {
|
||||
// TODO
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user