Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove MerkleCache and partial code #74

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 6 additions & 41 deletions ssz-rs/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::{
error::{Error, InstanceError},
lib::*,
merkleization::{
merkleize, mix_in_length, pack, MerkleCache, MerkleizationError, Merkleized, Node,
BYTES_PER_CHUNK,
merkleize, mix_in_length, pack, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK,
},
ser::{serialize_composite, Serialize, SerializeError},
SimpleSerialize, Sized,
Expand All @@ -18,7 +17,6 @@ use std::marker::PhantomData;
#[derive(Clone)]
pub struct List<T: SimpleSerialize, const N: usize> {
data: Vec<T>,
cache: MerkleCache,
}

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -121,8 +119,7 @@ where
let len = data.len();
Err((data, Error::Instance(InstanceError::Bounded { bound: N, provided: len })))
} else {
let leaf_count = Self::get_leaf_count(data.len());
Ok(Self { data, cache: MerkleCache::with_leaves(leaf_count) })
Ok(Self { data })
}
}
}
Expand Down Expand Up @@ -157,8 +154,6 @@ where
T: SimpleSerialize,
{
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let leaf_index = Self::get_leaf_index(index);
self.cache.invalidate(leaf_index);
&mut self.data[index]
}
}
Expand Down Expand Up @@ -209,25 +204,6 @@ impl<T, const N: usize> List<T, N>
where
T: SimpleSerialize,
{
// the number of leafs in the Merkle tree of this `Vector`
fn get_leaf_count(element_count: usize) -> usize {
if T::is_composite_type() {
element_count
} else {
let encoded_length = T::size_hint() * element_count;
(encoded_length + 31) / 32
}
}

fn get_leaf_index(index: usize) -> usize {
if T::is_composite_type() {
index
} else {
// TODO: compute correct leaf index
index + 1
}
}

fn compute_hash_tree_root(&mut self) -> Result<Node, MerkleizationError> {
if T::is_composite_type() {
let mut chunks = vec![0u8; self.len() * BYTES_PER_CHUNK];
Expand All @@ -248,31 +224,26 @@ where

pub fn push(&mut self, element: T) {
self.data.push(element);
self.cache.resize(self.len());
}

pub fn pop(&mut self) -> Option<T> {
let element = self.data.pop();
self.cache.resize(self.len());
element
self.data.pop()
}

pub fn clear(&mut self) {
self.data.clear();
self.cache.resize(0);
}

pub fn iter_mut(&mut self) -> IterMut<'_, T, N> {
IterMut { inner: self.data.iter_mut().enumerate(), cache: &mut self.cache }
IterMut { inner: self.data.iter_mut() }
}
}

pub struct IterMut<'a, T, const N: usize>
where
T: SimpleSerialize,
{
inner: Enumerate<slice::IterMut<'a, T>>,
cache: &'a mut MerkleCache,
inner: slice::IterMut<'a, T>,
}

impl<'a, T, const N: usize> Iterator for IterMut<'a, T, N>
Expand All @@ -282,13 +253,7 @@ where
type Item = &'a mut T;

fn next(&mut self) -> Option<Self::Item> {
if let Some((index, next)) = self.inner.next() {
let leaf_index = List::<T, N>::get_leaf_index(index);
self.cache.invalidate(leaf_index);
Some(next)
} else {
None
}
self.inner.next()
}
}

Expand Down
41 changes: 0 additions & 41 deletions ssz-rs/src/merkleization/cache.rs

This file was deleted.

2 changes: 0 additions & 2 deletions ssz-rs/src/merkleization/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod cache;
mod node;
mod proofs;

Expand All @@ -8,7 +7,6 @@ use crate::{
};
use sha2::{Digest, Sha256};

pub use cache::Cache as MerkleCache;
pub use node::Node;
pub use proofs::is_valid_merkle_branch;

Expand Down
55 changes: 7 additions & 48 deletions ssz-rs/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use crate::{
de::{deserialize_homogeneous_composite, Deserialize, DeserializeError},
error::{Error, InstanceError, TypeError},
lib::*,
merkleization::{
merkleize, pack, MerkleCache, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK,
},
merkleization::{merkleize, pack, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK},
ser::{serialize_composite, Serialize, SerializeError},
SimpleSerialize, Sized,
};
Expand All @@ -18,7 +16,6 @@ use std::marker::PhantomData;
#[derive(Clone)]
pub struct Vector<T: SimpleSerialize, const N: usize> {
data: Vec<T>,
cache: MerkleCache,
}

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -94,8 +91,7 @@ impl<T: SimpleSerialize, const N: usize> TryFrom<Vec<T>> for Vector<T, N> {
let len = data.len();
Err((data, Error::Instance(InstanceError::Exact { required: N, provided: len })))
} else {
let leaf_count = Self::get_leaf_count();
Ok(Self { data, cache: MerkleCache::with_leaves(leaf_count) })
Ok(Self { data })
}
}
}
Expand Down Expand Up @@ -159,8 +155,6 @@ where
T: SimpleSerialize,
{
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let leaf_index = self.get_leaf_index(index);
self.cache.invalidate(leaf_index);
&mut self.data[index]
}
}
Expand Down Expand Up @@ -227,25 +221,6 @@ impl<T, const N: usize> Vector<T, N>
where
T: SimpleSerialize,
{
// the number of leafs in the Merkle tree of this `Vector`
fn get_leaf_count() -> usize {
if T::is_composite_type() {
N
} else {
let encoded_length = Self::size_hint();
(encoded_length + 31) / 32
}
}

fn get_leaf_index(&self, index: usize) -> usize {
if T::is_composite_type() {
index
} else {
// TODO: compute correct leaf index
index + 1
}
}

fn compute_hash_tree_root(&mut self) -> Result<Node, MerkleizationError> {
if T::is_composite_type() {
let mut chunks = vec![0u8; self.len() * BYTES_PER_CHUNK];
Expand All @@ -262,29 +237,20 @@ where
}

pub fn iter_mut(&mut self) -> IterMut<'_, T> {
let inner = self.data.iter_mut().enumerate();
let cache = &mut self.cache;
IterMut { inner, cache }
let inner = self.data.iter_mut();
IterMut { inner }
}
}

pub struct IterMut<'a, T: 'a> {
inner: Enumerate<slice::IterMut<'a, T>>,
cache: &'a mut MerkleCache,
inner: slice::IterMut<'a, T>,
}

impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;

fn next(&mut self) -> Option<Self::Item> {
if let Some((index, next)) = self.inner.next() {
// TODO: compute correct `leaf_index`
let leaf_index = index;
self.cache.invalidate(leaf_index);
Some(next)
} else {
None
}
self.inner.next()
}
}

Expand All @@ -293,14 +259,7 @@ where
T: SimpleSerialize,
{
fn hash_tree_root(&mut self) -> Result<Node, MerkleizationError> {
if !self.cache.valid() {
// which leaves are dirty
// figure out which elements are needed and recompute leaves
// update cache w/ new leaves
let root = self.compute_hash_tree_root()?;
self.cache.update(root);
}
Ok(self.cache.root())
self.compute_hash_tree_root()
}
}

Expand Down