From 8f7122afeb6ab99d260543139be3801251da153d Mon Sep 17 00:00:00 2001 From: RuslanProgrammer Date: Tue, 26 Mar 2024 07:38:56 +0100 Subject: [PATCH] move leaves generation to before block --- test/data-structures/SparseMerkleTree.test.ts | 72 +++++-------------- 1 file changed, 17 insertions(+), 55 deletions(-) diff --git a/test/data-structures/SparseMerkleTree.test.ts b/test/data-structures/SparseMerkleTree.test.ts index 7e9cfa3..ebe8ff0 100644 --- a/test/data-structures/SparseMerkleTree.test.ts +++ b/test/data-structures/SparseMerkleTree.test.ts @@ -18,6 +18,9 @@ describe("SparseMerkleTree", () => { let smtMock: SparseMerkleTreeMock; let smtVerifier: SparseMerkleTreeMockVerifier; + const leaves: string[] = []; + let nonExistentLeaf: string; + before("setup", async () => { const poseidonFacade = await deployPoseidonFacade(); @@ -31,23 +34,24 @@ describe("SparseMerkleTree", () => { smtVerifier = await SparseMerkleTreeMockVerifier.deploy(); smtMock = await SparseMerkleTreeMock.deploy(); + for (let i = 0; i < 10; i++) { + let rand: string; + do { + rand = ethers.hexlify(ethers.randomBytes(1)); + } while (leaves.includes(rand)); + leaves.push(rand); + } + + nonExistentLeaf = leaves[9]; + await reverter.snapshot(); }); afterEach(reverter.revert); it("should prove the tree inclusion", async () => { - let leaves: string[] = []; - for (let i = 0; i < 9; i++) { - let rand: string; - do { - rand = ethers.hexlify(ethers.randomBytes(1)); - } while (leaves.includes(rand)); - - await smtMock.addElement(rand, rand); - - leaves.push(rand); + await smtMock.addElement(leaves[i], leaves[i]); } const merkleProof = await smtMock.getProof(leaves[5]); @@ -116,24 +120,10 @@ describe("SparseMerkleTree", () => { }); it("should prove the tree exclusion", async () => { - const leaves: string[] = []; - for (let i = 0; i < 9; i++) { - let rand: string; - do { - rand = ethers.hexlify(ethers.randomBytes(1)); - } while (leaves.includes(rand)); - - await smtMock.addElement(rand, rand); - - leaves.push(rand); + await smtMock.addElement(leaves[i], leaves[i]); } - let nonExistentLeaf; - do { - nonExistentLeaf = ethers.hexlify(ethers.randomBytes(1)); - } while (leaves.includes(nonExistentLeaf)); - const merkleProof = await smtMock.getProof(nonExistentLeaf); const auxIsEmpty = BigInt(merkleProof.auxKey) == 0n ? 1 : 0; @@ -160,11 +150,6 @@ describe("SparseMerkleTree", () => { await smtMock.addElement(rand, rand); - let nonExistentLeaf; - do { - nonExistentLeaf = ethers.hexlify(ethers.randomBytes(1)); - } while (nonExistentLeaf == rand); - const merkleProof = await smtMock.getProof(nonExistentLeaf); const auxIsEmpty = BigInt(merkleProof.auxKey) == 0n ? 1 : 0; @@ -219,17 +204,8 @@ describe("SparseMerkleTree", () => { }); it("should revert an incorrect tree inclusion", async () => { - let leaves: string[] = []; - for (let i = 0; i < 9; i++) { - let rand: string; - do { - rand = ethers.hexlify(ethers.randomBytes(1)); - } while (leaves.includes(rand)); - - await smtMock.addElement(rand, rand); - - leaves.push(rand); + await smtMock.addElement(leaves[i], leaves[i]); } const merkleProof = await smtMock.getProof(leaves[5]); @@ -251,24 +227,10 @@ describe("SparseMerkleTree", () => { }); it("should revert an incorrect tree exclusion", async () => { - const leaves: string[] = []; - for (let i = 0; i < 9; i++) { - let rand: string; - do { - rand = ethers.hexlify(ethers.randomBytes(1)); - } while (leaves.includes(rand)); - - await smtMock.addElement(rand, rand); - - leaves.push(rand); + await smtMock.addElement(leaves[i], leaves[i]); } - let nonExistentLeaf; - do { - nonExistentLeaf = ethers.hexlify(ethers.randomBytes(1)); - } while (leaves.includes(nonExistentLeaf)); - const merkleProof = await smtMock.getProof(nonExistentLeaf); let auxIsEmpty = BigInt(merkleProof.auxKey) == 0n ? 1 : 0;