mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-22 06:14:38 +00:00
Allow for building cached vec
This commit is contained in:
@@ -4,6 +4,10 @@ use crate::{ssz_encode, Encodable};
|
||||
impl CachedTreeHash for u64 {
|
||||
type Item = Self;
|
||||
|
||||
fn item_type() -> ItemType {
|
||||
ItemType::Basic
|
||||
}
|
||||
|
||||
fn build_tree_hash_cache(&self) -> Result<TreeHashCache, Error> {
|
||||
Ok(TreeHashCache::from_bytes(merkleize(ssz_encode(self)))?)
|
||||
}
|
||||
@@ -20,6 +24,10 @@ impl CachedTreeHash for u64 {
|
||||
0
|
||||
}
|
||||
|
||||
fn packed_encoding(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
|
||||
fn cached_hash_tree_root(
|
||||
&self,
|
||||
other: &Self,
|
||||
@@ -35,38 +43,73 @@ impl CachedTreeHash for u64 {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
impl<T> CachedTreeHash for Vec<T>
|
||||
where
|
||||
T: CachedTreeHash + Encodable,
|
||||
T: CachedTreeHash,
|
||||
{
|
||||
type Item = Self;
|
||||
|
||||
fn build_cache_bytes(&self) -> Vec<u8> {
|
||||
let num_packed_bytes = self.num_bytes();
|
||||
let num_leaves = num_sanitized_leaves(num_packed_bytes);
|
||||
fn item_type() -> ItemType {
|
||||
ItemType::List
|
||||
}
|
||||
|
||||
let mut packed = Vec::with_capacity(num_leaves * HASHSIZE);
|
||||
fn build_tree_hash_cache(&self) -> Result<TreeHashCache, Error> {
|
||||
match T::item_type() {
|
||||
ItemType::Basic => {
|
||||
let num_packed_bytes = self.num_bytes();
|
||||
let num_leaves = num_sanitized_leaves(num_packed_bytes);
|
||||
|
||||
let mut packed = Vec::with_capacity(num_leaves * HASHSIZE);
|
||||
|
||||
for item in self {
|
||||
packed.append(&mut item.packed_encoding());
|
||||
}
|
||||
|
||||
let packed = sanitise_bytes(packed);
|
||||
|
||||
TreeHashCache::from_bytes(merkleize(packed))
|
||||
}
|
||||
ItemType::Composite | ItemType::List => {
|
||||
let subtrees = self
|
||||
.iter()
|
||||
.map(|item| TreeHashCache::new(item))
|
||||
.collect::<Result<Vec<TreeHashCache>, _>>()?;
|
||||
|
||||
TreeHashCache::from_leaves_and_subtrees(self, subtrees)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn offsets(&self) -> Result<Vec<usize>, Error> {
|
||||
let mut offsets = vec![];
|
||||
|
||||
for item in self {
|
||||
packed.append(&mut ssz_encode(item));
|
||||
offsets.push(item.offsets()?.iter().sum())
|
||||
}
|
||||
|
||||
let packed = sanitise_bytes(packed);
|
||||
Ok(offsets)
|
||||
}
|
||||
|
||||
merkleize(packed)
|
||||
fn num_child_nodes(&self) -> usize {
|
||||
// TODO
|
||||
42
|
||||
}
|
||||
|
||||
fn num_bytes(&self) -> usize {
|
||||
self.iter().fold(0, |acc, item| acc + item.num_bytes())
|
||||
}
|
||||
|
||||
fn packed_encoding(&self) -> Vec<u8> {
|
||||
panic!("List should never be packed")
|
||||
}
|
||||
|
||||
fn cached_hash_tree_root(
|
||||
&self,
|
||||
other: &Self::Item,
|
||||
cache: &mut TreeHashCache,
|
||||
chunk: usize,
|
||||
) -> Option<usize> {
|
||||
) -> Result<usize, Error> {
|
||||
/*
|
||||
let num_packed_bytes = self.num_bytes();
|
||||
let num_leaves = num_sanitized_leaves(num_packed_bytes);
|
||||
|
||||
@@ -103,6 +146,17 @@ where
|
||||
}
|
||||
|
||||
Some(chunk + num_nodes)
|
||||
*/
|
||||
// TODO
|
||||
Ok(42)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
fn get_packed_leaves<T>(vec: Vec<T>) -> Vec<u8>
|
||||
where
|
||||
T: Encodable,
|
||||
{
|
||||
//
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user