diff --git a/packages/lean-imt/src/lean-imt.ts b/packages/lean-imt/src/lean-imt.ts index d4cbcb6b9..4fdc4f067 100644 --- a/packages/lean-imt/src/lean-imt.ts +++ b/packages/lean-imt/src/lean-imt.ts @@ -238,25 +238,21 @@ export default class LeanIMT { * It is more efficient than using the {@link LeanIMT#update} method N times because it * prevents updating middle nodes several times. This would happen when updating leaves * with common ancestors. - * @param leaves The list of leaves to be updated. * @param indices The list of indices of the respective leaves. + * @param leaves The list of leaves to be updated. */ - public updateMany(leaves: N[], indices: number[]) { + public updateMany(indices: number[], leaves: N[]) { requireDefined(leaves, "leaves") requireDefined(indices, "indices") requireArray(leaves, "leaves") requireArray(indices, "indices") - if (leaves.length === 0) { - throw new Error("There are no leaves to modify") - } if (leaves.length !== indices.length) { throw new Error("There is no correspondence between indices and leaves") } for (let leaf = 0; leaf < indices.length; leaf += 1) { if (indices[leaf] < 0 || indices[leaf] >= this.size) { throw new Error(`Index ${leaf} is out of range`) - // throw new Error("Index " + leaf.toString() + " is out of range") } } diff --git a/packages/lean-imt/tests/lean-imt.test.ts b/packages/lean-imt/tests/lean-imt.test.ts index 0fb3c57e6..1c7633cd0 100644 --- a/packages/lean-imt/tests/lean-imt.test.ts +++ b/packages/lean-imt/tests/lean-imt.test.ts @@ -220,6 +220,59 @@ describe("Lean IMT", () => { }) }) + describe("# updateMany", () => { + it(`Should not update any leaf if one of the parameters is not defined`, () => { + const tree = new LeanIMT(poseidon, leaves) + + const fun1 = () => tree.updateMany([1], undefined as any) + const fun2 = () => tree.updateMany(undefined as any, [BigInt(1)]) + + expect(fun1).toThrow("Parameter 'leaves' is not defined") + expect(fun2).toThrow("Parameter 'indices' is not defined") + }) + + it(`Should not update any leaf if the parameters are not arrays`, () => { + const tree = new LeanIMT(poseidon, leaves) + + const fun1 = () => tree.updateMany([3], BigInt(1) as any) + const fun2 = () => tree.updateMany(3 as any, [BigInt(1)]) + + expect(fun1).toThrow("Parameter 'leaves' is not an Array instance") + expect(fun2).toThrow("Parameter 'indices' is not an Array instance") + }) + + it(`Should not update any leaf if the parameters are of different size`, () => { + const tree = new LeanIMT(poseidon, leaves) + + const fun = () => tree.updateMany([1, 2, 3], [BigInt(1), BigInt(2)]) + + expect(fun).toThrow("There is no correspondence between indices and leaves") + }) + + it(`Should not update any leaf if some index is out of range`, () => { + const tree = new LeanIMT(poseidon, leaves) + + const fun1 = () => tree.updateMany([-1, 2, 3], [BigInt(1), BigInt(2), BigInt(3)]) + const fun2 = () => tree.updateMany([1, 200000, 3], [BigInt(1), BigInt(2), BigInt(3)]) + const fun3 = () => tree.updateMany([1, 2, 345], [BigInt(1), BigInt(2), BigInt(3)]) + + expect(fun1).toThrow("Index 0 is out of range") + expect(fun2).toThrow("Index 1 is out of range") + expect(fun3).toThrow("Index 2 is out of range") + }) + + it(`Should not update any leaf when passing an empty list`, () => { + const tree = new LeanIMT(poseidon, leaves) + const previousRoot = tree.root + + tree.updateMany([], []) + + expect(tree.root).toBe(previousRoot) + }) + + it(`Should update leafs correctly`, () => {}) + }) + describe("# generateProof", () => { it(`Should not generate any proof if the index is not defined`, () => { const tree = new LeanIMT(poseidon, leaves)