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

Initial sync pruning fix #363

Merged
merged 12 commits into from
Aug 31, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Removed

### Fixed
- [\#363](https://github.com/Manta-Network/manta-rs/pull/363) Initial sync pruning fix

### Security

Expand Down
20 changes: 11 additions & 9 deletions manta-crypto/src/merkle_tree/leaf_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ where
/// Tries to remove the [`LeafDigest`] stored at `index`. Fails when trying to remove the current leaf.
fn remove(&mut self, index: usize) -> bool;

/// Builds a [`LeafMap`] from `leaf_digests`.
fn from_vec(leaf_digests: Vec<LeafDigest<C>>) -> Self;
/// Builds a [`LeafMap`] from `leaf_digests`. The value `marked` determines whether the leaves will be
/// marked or unmarked.
fn from_vec(leaf_digests: Vec<LeafDigest<C>>, marked: bool) -> Self;

/// Returns a vector with all [`LeafDigest`]s in `self`.
#[inline]
Expand Down Expand Up @@ -190,7 +191,8 @@ where
}

#[inline]
fn from_vec(leaf_digests: Vec<LeafDigest<C>>) -> Self {
fn from_vec(leaf_digests: Vec<LeafDigest<C>>, marked: bool) -> Self {
let _ = marked;
Self(leaf_digests)
}

Expand Down Expand Up @@ -292,9 +294,9 @@ where
}

#[inline]
fn from_vec(leaf_digests: Vec<LeafDigest<C>>) -> Self {
fn from_vec(leaf_digests: Vec<LeafDigest<C>>, marked: bool) -> Self {
Self {
vec: leaf_digests.into_iter().map(|x| (false, x)).collect(),
vec: leaf_digests.into_iter().map(|x| (marked, x)).collect(),
}
}

Expand Down Expand Up @@ -399,7 +401,7 @@ where
}

#[inline]
fn from_vec(leaf_digests: Vec<LeafDigest<C>>) -> Self {
fn from_vec(leaf_digests: Vec<LeafDigest<C>>, marked: bool) -> Self {
let digest_count = leaf_digests.len();
if digest_count == 0 {
Self {
Expand All @@ -410,7 +412,7 @@ where
Self {
map: leaf_digests
.into_iter()
.map(|x| (false, x))
.map(|x| (marked, x))
.enumerate()
.collect::<BTreeMap<usize, (bool, LeafDigest<C>)>>(),
last_index: Some(digest_count - 1),
Expand Down Expand Up @@ -530,7 +532,7 @@ where
}

#[inline]
fn from_vec(leaf_digests: Vec<LeafDigest<C>>) -> Self {
fn from_vec(leaf_digests: Vec<LeafDigest<C>>, marked: bool) -> Self {
let digest_count = leaf_digests.len();
if digest_count == 0 {
Self {
Expand All @@ -541,7 +543,7 @@ where
Self {
map: leaf_digests
.into_iter()
.map(|x| (false, x))
.map(|x| (marked, x))
.enumerate()
.collect::<HashMap<usize, (bool, LeafDigest<C>)>>(),
last_index: Some(digest_count - 1),
Expand Down
11 changes: 8 additions & 3 deletions manta-crypto/src/merkle_tree/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ where
leaf_digests: Vec<LeafDigest<C>>,
inner_digests: PartialInnerTree<C, M>,
) -> Self {
Self::new_unchecked(LeafMap::from_vec(leaf_digests), inner_digests)
Self::new_unchecked(LeafMap::from_vec(leaf_digests, false), inner_digests)
}

/// Builds a new [`Partial`] from `leaf_digests` and `path` without checking that
/// `path` is consistent with the leaves and that it is a [`CurrentPath`].
///
/// # Note
///
/// This function is intended to be used for the initialization of partial Merkle trees
/// in the signer, so every leaf inserted will be marked for deletion by default.
#[inline]
pub fn from_leaves_and_path_unchecked(
parameters: &Parameters<C>,
Expand All @@ -110,14 +115,14 @@ where
{
let n = leaf_digests.len();
if n == 0 {
Self::new_unchecked(LeafMap::from_vec(leaf_digests), Default::default())
Self::new_unchecked(LeafMap::from_vec(leaf_digests, true), Default::default())
} else {
let base = match Parity::from_index(n - 1) {
Parity::Left => parameters.join_leaves(&leaf_digests[n - 1], &path.sibling_digest),
Parity::Right => parameters.join_leaves(&path.sibling_digest, &leaf_digests[n - 1]),
};
let mut partial_tree = Self::new_unchecked(
LeafMap::from_vec(leaf_digests),
LeafMap::from_vec(leaf_digests, true),
PartialInnerTree::from_current(
parameters,
base,
Expand Down
Loading