Skip to content

Commit

Permalink
feat(lean-imt): implemented some tests on lean-imt
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinoCribioli committed Aug 28, 2024
1 parent 130b977 commit 6049ea4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
8 changes: 2 additions & 6 deletions packages/lean-imt/src/lean-imt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,21 @@ export default class LeanIMT<N = bigint> {
* 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")
}
}

Expand Down
53 changes: 53 additions & 0 deletions packages/lean-imt/tests/lean-imt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6049ea4

Please sign in to comment.