fix: fn works as expected now

This commit is contained in:
rymnc 2023-05-09 22:38:04 +05:30
parent ed4fad8f9e
commit c1de1c3bca
No known key found for this signature in database
GPG Key ID: AAA088D5C68ECD34
3 changed files with 50 additions and 6 deletions

View File

@ -227,7 +227,7 @@ where
let end = start + leaves.len(); let end = start + leaves.len();
// check if the leaves are in the correct range // check if the leaves are in the correct range
if leaves.len() + start > self.capacity() { if leaves.len() + start - to_remove_indices.len() > self.capacity() {
return Err(PmtreeErrorKind::TreeError(TreeErrorKind::IndexOutOfBounds)); return Err(PmtreeErrorKind::TreeError(TreeErrorKind::IndexOutOfBounds));
} }
@ -238,7 +238,9 @@ where
// check if the indices are unique // check if the indices are unique
if to_remove_indices.len() != to_remove_indices.iter().collect::<HashSet<_>>().len() { if to_remove_indices.len() != to_remove_indices.iter().collect::<HashSet<_>>().len() {
return Err(PmtreeErrorKind::TreeError(TreeErrorKind::CustomError("Indices are not unique".to_string()))); return Err(PmtreeErrorKind::TreeError(TreeErrorKind::CustomError(
"Indices are not unique".to_string(),
)));
} }
let mut subtree = HashMap::<Key, H::Fr>::new(); let mut subtree = HashMap::<Key, H::Fr>::new();
@ -249,10 +251,9 @@ where
self.fill_nodes(root_key, start, end, &mut subtree, &leaves, start)?; self.fill_nodes(root_key, start, end, &mut subtree, &leaves, start)?;
for i in to_remove_indices { for i in to_remove_indices {
subtree.insert(Key(self.depth, i - leaves.len()), H::default_leaf()); subtree.insert(Key(self.depth, i - leaves.len() + 1), H::default_leaf());
} }
let subtree = Arc::new(RwLock::new(subtree)); let subtree = Arc::new(RwLock::new(subtree));
let root_val = rayon::ThreadPoolBuilder::new() let root_val = rayon::ThreadPoolBuilder::new()

View File

@ -150,3 +150,45 @@ fn set_range() -> PmtreeResult<()> {
Ok(()) Ok(())
} }
#[test]
fn batch_operations() -> PmtreeResult<()> {
let mut mt = MerkleTree::<MemoryDB, MyKeccak>::new(2, MemoryDBConfig)?;
let leaves = [
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("0000000000000000000000000000000000000000000000000000000000000002"),
];
mt.batch_insert(None, leaves)?;
assert_eq!(
mt.root(),
hex!("893760ec5b5bee236f29e85aef64f17139c3c1b7ff24ce64eb6315fca0f2485b")
);
let leaves = [
hex!("0000000000000000000000000000000000000000000000000000000000000003"),
hex!("0000000000000000000000000000000000000000000000000000000000000004"),
];
mt.batch_operations(None, leaves, [])?;
assert_eq!(
mt.root(),
hex!("a9bb8c3f1f12e9aa903a50c47f314b57610a3ab32f2d463293f58836def38d36")
);
// this should be a no-op
mt.batch_operations(None, [leaves[1]], [3])?;
assert_eq!(
mt.root(),
hex!("222ff5e0b5877792c2bc1670e2ccd0c2c97cd7bb1672a57d598db05092d3d72c")
);
Ok(())
}

View File

@ -230,7 +230,8 @@ fn batch_operations() -> PmtreeResult<()> {
hex!("a9bb8c3f1f12e9aa903a50c47f314b57610a3ab32f2d463293f58836def38d36") hex!("a9bb8c3f1f12e9aa903a50c47f314b57610a3ab32f2d463293f58836def38d36")
); );
mt.batch_operations(None, [], [3])?; // this should be a no-op
mt.batch_operations(None, [leaves[1]], [3])?;
assert_eq!( assert_eq!(
mt.root(), mt.root(),