mirror of
https://github.com/privacy-scaling-explorations/zk-kit.git
synced 2026-04-22 03:00:15 -04:00
feat: add update method
This commit is contained in:
@@ -90,6 +90,12 @@ const tree = new IncrementalMerkleTree(poseidon, 16, BigInt(0), 2) // Binary tre
|
||||
tree.insert(BigInt(1))
|
||||
```
|
||||
|
||||
\# **update**(index: _number_, newLeaf: _Node_)
|
||||
|
||||
```typescript
|
||||
tree.update(0, BigInt(2))
|
||||
```
|
||||
|
||||
\# **delete**(index: _number_)
|
||||
|
||||
```typescript
|
||||
|
||||
@@ -130,6 +130,16 @@ export default class IncrementalMerkleTree {
|
||||
this._root = _update(index, this.zeroes[0], this.depth, this.arity, this._nodes, this.zeroes, this._hash)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a leaf from the tree. It does not remove the leaf from
|
||||
* the data structure. It set the leaf to be deleted to a zero value.
|
||||
* @param index Index of the leaf to be deleted.
|
||||
* @param newLeaf New leaf value.
|
||||
*/
|
||||
public update(index: number, newLeaf: Node) {
|
||||
this._root = _update(index, newLeaf, this.depth, this.arity, this._nodes, this.zeroes, this._hash)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a proof of membership.
|
||||
* @param index Index of the proof's leaf.
|
||||
|
||||
@@ -89,6 +89,22 @@ describe("Incremental Merkle Tree", () => {
|
||||
}
|
||||
})
|
||||
|
||||
it(`Should update ${numberOfLeaves} leaves`, () => {
|
||||
for (let i = 0; i < numberOfLeaves; i += 1) {
|
||||
tree.insert(BigInt(1))
|
||||
oldTree.insert(BigInt(1))
|
||||
}
|
||||
|
||||
for (let i = 0; i < numberOfLeaves; i += 1) {
|
||||
tree.update(i, BigInt(0))
|
||||
oldTree.update(i, BigInt(0))
|
||||
|
||||
const { root } = oldTree.genMerklePath(0)
|
||||
|
||||
expect(tree.root).toEqual(root)
|
||||
}
|
||||
})
|
||||
|
||||
it("Should return the index of a leaf", () => {
|
||||
tree.insert(BigInt(1))
|
||||
tree.insert(BigInt(2))
|
||||
|
||||
Reference in New Issue
Block a user