Skip to content

Commit

Permalink
move leaves generation to before block
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanProgrammer committed Mar 26, 2024
1 parent 9a8990b commit 8f7122a
Showing 1 changed file with 17 additions and 55 deletions.
72 changes: 17 additions & 55 deletions test/data-structures/SparseMerkleTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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]);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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;
Expand Down

0 comments on commit 8f7122a

Please sign in to comment.