Optimize `extend_with_marked_digests`
SupremoUGH opened this issue · comments
Using the following optimization for the sync protocol leads to the InvalidUTXOAccumulatorOutput
bug. Investigate why and implement the optimization.
/// Appends an iterator of marked leaf digests at the end of the tree, returning the iterator back
/// if it could not be inserted because the tree has exhausted its capacity.
///
/// # Implementation Note
///
/// This operation is meant to be atomic, so if appending the iterator should fail, the
/// implementation must ensure that the tree returns to the same state before the insertion
/// occured.
#[inline]
pub fn extend_with_marked_digests<I>(
&mut self,
parameters: &Parameters<C>,
marked_leaf_digests: I,
) -> Result<(), I::IntoIter>
where
I: IntoIterator<Item = (bool, LeafDigest<C>)>,
L: Default,
M: Default,
InnerDigest<C>: Clone + Default + PartialEq,
LeafDigest<C>: Clone + Default,
{
let marked_leaf_digests = marked_leaf_digests.into_iter();
if matches!(marked_leaf_digests.size_hint().1, Some(max) if max <= capacity::<C, _>() - self.len())
{
let mut marked_inserts = Vec::new();
for (marking, leaf_digest) in marked_leaf_digests {
println!("{:?}", marking);
if marking {
marked_inserts.push(leaf_digest);
} else {
if !marked_inserts.is_empty() {
println!("Marked inserts length: {:?}", marked_inserts.len());
assert!(self.batch_push_digest(parameters, || marked_inserts.drain(..).collect::<Vec<_>>()),
"Pushing a leaf digest into the tree should always succeed because of the check above.");
println!("Marked inserts length: {:?}", marked_inserts.len());
}
assert!(self.push_provable_digest(parameters, move || leaf_digest),
"Pushing a leaf digest into the tree should always succeed because of the check above.");
}
}
if !marked_inserts.is_empty() {
assert!(self.batch_push_digest(parameters, || marked_inserts.drain(..).collect::<Vec<_>>()),
"Pushing a leaf digest into the tree should always succeed because of the check above.");
}
return Ok(());
}
Err(marked_leaf_digests)
}
```