feat: add update method

This commit is contained in:
cedoor
2022-01-25 14:44:10 +01:00
parent feebaf2e1d
commit 95e1cbda9a
3 changed files with 32 additions and 0 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -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))