diff --git a/test/IdentityTreeStore/IdentityTreeStore.test.ts b/test/IdentityTreeStore/IdentityTreeStore.test.ts index e3acfbb0..230db1a0 100644 --- a/test/IdentityTreeStore/IdentityTreeStore.test.ts +++ b/test/IdentityTreeStore/IdentityTreeStore.test.ts @@ -3,18 +3,26 @@ import { poseidon } from "@iden3/js-crypto"; import { DeployHelper } from "../../helpers/DeployHelper"; import { Contract } from "ethers"; import { publishStateWithStubProof } from "../utils/state-utils"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; const verifierStubName = "Groth16VerifierStub"; describe("IdentityTreeStore", function () { let identityTreeStore, stateContract: Contract; - beforeEach(async function () { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); - ({ state: stateContract } = await deployHelper.deployStateWithLibraries(["0x0100"], verifierStubName)); + ({ state: stateContract } = await deployHelper.deployStateWithLibraries( + ["0x0100"], + verifierStubName, + )); ({ identityTreeStore } = await deployHelper.deployIdentityTreeStore( await stateContract.getAddress(), )); + } + + beforeEach(async function () { + await loadFixture(deployContractsFixture); }); it("Should return the revocation status single leaf", async function () { @@ -31,7 +39,7 @@ describe("IdentityTreeStore", function () { const revStatusByState = await identityTreeStore.getRevocationStatusByIdAndState( id, state, - nonce + nonce, ); const stateTransitionArgs = { @@ -196,7 +204,7 @@ describe("IdentityTreeStore", function () { const revStatusByState = await identityTreeStore.getRevocationStatusByIdAndState( id, state, - nonce + nonce, ); const stateTransitionArgs = { @@ -286,7 +294,7 @@ describe("IdentityTreeStore", function () { await publishStateWithStubProof(stateContract, stateTransitionArgs); await expect( - identityTreeStore.getRevocationStatusByIdAndState(id, state, nonce) + identityTreeStore.getRevocationStatusByIdAndState(id, state, nonce), ).to.be.rejectedWith("Invalid state node"); await expect(identityTreeStore.getRevocationStatus(id, nonce)).to.be.rejectedWith( diff --git a/test/cross-chain/cross-chain-proof-validator.test.ts b/test/cross-chain/cross-chain-proof-validator.test.ts index fd982d0f..7f66f0ba 100644 --- a/test/cross-chain/cross-chain-proof-validator.test.ts +++ b/test/cross-chain/cross-chain-proof-validator.test.ts @@ -8,15 +8,20 @@ import { expect } from "chai"; import { DeployHelper } from "../../helpers/DeployHelper"; import { Contract } from "ethers"; import { ethers } from "hardhat"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; describe("State Cross Chain", function () { let crossChainProofValidator: Contract; let signer; - beforeEach(async function () { + async function deployContractsFixture() { [signer] = await ethers.getSigners(); const deployHelper = await DeployHelper.initialize(null, true); crossChainProofValidator = await deployHelper.deployCrossChainProofValidator(); + } + + beforeEach(async function () { + await loadFixture(deployContractsFixture); }); it("Should process the messages without replacedAtTimestamp", async function () { diff --git a/test/payment/mc-payment.test.ts b/test/payment/mc-payment.test.ts index 48d86af3..a3577abe 100644 --- a/test/payment/mc-payment.test.ts +++ b/test/payment/mc-payment.test.ts @@ -2,6 +2,7 @@ import { ethers, upgrades } from "hardhat"; import { MCPayment, MCPayment__factory } from "../../typechain-types"; import { expect } from "chai"; import { Signer } from "ethers"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; describe("MC Payment Contract", () => { let payment: MCPayment; @@ -28,7 +29,7 @@ describe("MC Payment Contract", () => { ], }; - beforeEach(async () => { + async function deployContractsFixture() { const signers = await ethers.getSigners(); issuer1Signer = signers[1]; userSigner = signers[5]; @@ -39,6 +40,10 @@ describe("MC Payment Contract", () => { ownerPercentage, ])) as unknown as MCPayment; await payment.waitForDeployment(); + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); domainData = { name: "MCPayment", diff --git a/test/payment/vc-payment.test.ts b/test/payment/vc-payment.test.ts index f82447eb..b4614a36 100644 --- a/test/payment/vc-payment.test.ts +++ b/test/payment/vc-payment.test.ts @@ -4,6 +4,7 @@ import { ethers, upgrades } from "hardhat"; import { VCPayment, VCPayment__factory } from "../../typechain-types"; import { expect } from "chai"; import { Signer } from "ethers"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; describe("VC Payment Contract", () => { let payment: VCPayment; @@ -19,13 +20,8 @@ describe("VC Payment Contract", () => { let issuer1Signer, issuer2Signer, owner, userSigner: Signer; - beforeEach(async () => { + async function deployContractsFixture() { const ownerPercentage = 5; - const signers = await ethers.getSigners(); - issuer1Signer = signers[1]; - issuer2Signer = signers[2]; - userSigner = signers[5]; - owner = signers[0]; payment = (await upgrades.deployProxy(new VCPayment__factory(owner), [ await owner.getAddress(), @@ -53,6 +49,16 @@ describe("VC Payment Contract", () => { ownerPercentage, issuer2Signer.address, ); + } + + beforeEach(async () => { + const signers = await ethers.getSigners(); + issuer1Signer = signers[1]; + issuer2Signer = signers[2]; + userSigner = signers[5]; + owner = signers[0]; + + await loadFixture(deployContractsFixture); }); it("Payment and issuer withdraw:", async () => { diff --git a/test/smtLib/smtLib.test.ts b/test/smtLib/smtLib.test.ts index e9dce0a8..be6181ec 100644 --- a/test/smtLib/smtLib.test.ts +++ b/test/smtLib/smtLib.test.ts @@ -4,6 +4,7 @@ import hre from "hardhat"; import { addLeaf, FixedArray, genMaxBinaryNumber, MtpProof } from "../utils/state-utils"; import { DeployHelper } from "../../helpers/DeployHelper"; import { Contract } from "ethers"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; type ParamsProofByHistoricalRoot = { index: number | bigint | string; @@ -45,9 +46,13 @@ type TestCaseRootHistory = { describe("Merkle tree proofs of SMT", () => { let smt; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); smt = await deployHelper.deploySmtLibTestWrapper(); + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); describe("SMT existence proof", () => { @@ -1065,7 +1070,7 @@ describe("Root history requests", function () { it("should revert if out of bounds", async () => { await expect(smt.getRootHistory(historyLength, 100)).to.be.rejectedWith( - "Start index out of bounds" + "Start index out of bounds", ); }); @@ -1080,9 +1085,13 @@ describe("Root history requests", function () { describe("Root history duplicates", function () { let smt; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); smt = await deployHelper.deploySmtLibTestWrapper(); + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("comprehensive check", async () => { @@ -1117,7 +1126,7 @@ describe("Root history duplicates", function () { const riDoubleRoot = await smt.getRootInfoListByRoot(doubleRoot, 0, 100); const riTripleRoot = await smt.getRootInfoListByRoot(tripleRoot, 0, 100); await expect(smt.getRootInfoListByRoot(nonExistingRoot, 0, 100)).to.be.rejectedWith( - "Root does not exist" + "Root does not exist", ); expect(riSingleRoot.length).to.be.equal(1); @@ -1149,7 +1158,7 @@ describe("Root history duplicates", function () { await smt.add(1, 1); const root = await smt.getRoot(); await expect(smt.getRootInfoListByRoot(root, 0, 0)).to.be.rejectedWith( - "Length should be greater than 0" + "Length should be greater than 0", ); }); @@ -1157,7 +1166,7 @@ describe("Root history duplicates", function () { await smt.add(1, 1); const root = await smt.getRoot(); await expect(smt.getRootInfoListByRoot(root, 0, 10 ** 6)).to.be.rejectedWith( - "Length limit exceeded" + "Length limit exceeded", ); }); @@ -1167,7 +1176,7 @@ describe("Root history duplicates", function () { await smt.add(1, 1); const root = await smt.getRoot(); await expect(smt.getRootInfoListByRoot(root, 3, 100)).to.be.rejectedWith( - "Start index out of bounds" + "Start index out of bounds", ); }); @@ -1216,10 +1225,13 @@ describe("Binary search in SMT root history", () => { expect(riByBlock.root).to.equal(tc.expectedRoot); } - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); binarySearch = await deployHelper.deployBinarySearchTestWrapper(); + } + beforeEach(async () => { + await loadFixture(deployContractsFixture); const { number: latestBlockNumber } = await hre.ethers.provider.getBlock("latest"); let blocksToMine = 15 - latestBlockNumber; @@ -1629,9 +1641,13 @@ describe("Binary search in SMT root history", () => { describe("Binary search in SMT proofs", () => { let smt; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); smt = await deployHelper.deploySmtLibTestWrapper(); + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); describe("Zero root proofs", () => { @@ -1740,9 +1756,13 @@ describe("Binary search in SMT proofs", () => { describe("Edge cases with exceptions", () => { let smt; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); smt = await deployHelper.deploySmtLibTestWrapper(); + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("getRootInfo() should throw when root does not exist", async () => { @@ -1821,21 +1841,21 @@ async function checkTestCaseMTPProof(smt: Contract, testCase: TestCaseMTPProof) if (isProofByHistoricalRoot(testCase.paramsToGetProof)) { proof = await smt.getProofByRoot( testCase.paramsToGetProof.index, - testCase.paramsToGetProof.historicalRoot + testCase.paramsToGetProof.historicalRoot, ); } if (isProofByTime(testCase.paramsToGetProof)) { proof = await smt.getProofByTime( testCase.paramsToGetProof.index, - testCase.paramsToGetProof.timestamp + testCase.paramsToGetProof.timestamp, ); } if (isProofByBlock(testCase.paramsToGetProof)) { proof = await smt.getProofByBlock( testCase.paramsToGetProof.index, - testCase.paramsToGetProof.blockNumber + testCase.paramsToGetProof.blockNumber, ); } diff --git a/test/state/state.test.ts b/test/state/state.test.ts index 86da84c5..51e30f98 100644 --- a/test/state/state.test.ts +++ b/test/state/state.test.ts @@ -2,6 +2,7 @@ import { expect } from "chai"; import { ethers } from "hardhat"; import { publishState, publishStateWithStubProof } from "../utils/state-utils"; import { DeployHelper } from "../../helpers/DeployHelper"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; const g16VerifierStubName = "Groth16VerifierStub"; @@ -28,11 +29,15 @@ const stateTransitionsWithNoProofs = [ describe("State transition with real groth16 verifier", () => { let state; - before(async function () { - this.timeout(5000); + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); const contracts = await deployHelper.deployStateWithLibraries(["0x0100"]); state = contracts.state; + } + + before(async function () { + this.timeout(5000); + await loadFixture(deployContractsFixture); }); it("Zero-knowledge proof of state transition is not valid", async () => { @@ -113,13 +118,16 @@ describe("State transition with real groth16 verifier", () => { describe("State transition negative cases", () => { let state; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); const contracts = await deployHelper.deployStateWithLibraries( ["0x0281", "0x0000"], g16VerifierStubName, ); state = contracts.state; + } + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("Old state does not match the latest state", async () => { @@ -192,7 +200,7 @@ describe("StateInfo history", function () { let state; let publishedStates: { [key: string]: string | number }[] = []; - before(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); const contracts = await deployHelper.deployStateWithLibraries(["0x0281"], g16VerifierStubName); state = contracts.state; @@ -201,6 +209,10 @@ describe("StateInfo history", function () { for (const stateTransition of stateTransitionsWithNoProofs) { publishedStates.push(await publishStateWithStubProof(state, stateTransition)); } + } + + before(async () => { + await loadFixture(deployContractsFixture); }); it("should return state history", async () => { @@ -228,10 +240,14 @@ describe("StateInfo history", function () { describe("GIST proofs", () => { let state: any; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); const contracts = await deployHelper.deployStateWithLibraries(["0x0281"], g16VerifierStubName); state = contracts.state; + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("Should be correct historical proof by root and the latest root", async function () { @@ -301,10 +317,14 @@ describe("GIST proofs", () => { describe("GIST root history", () => { let state: any; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); const contracts = await deployHelper.deployStateWithLibraries(["0x0281"], g16VerifierStubName); state = contracts.state; + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("Should search by block and by time return same root", async function () { @@ -367,10 +387,18 @@ describe("GIST root history", () => { }); describe("Set Verifier", () => { - it("Should set groth16 verifier", async () => { + let state: any, groth16verifier: any; + + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); - const { state, groth16verifier } = await deployHelper.deployStateWithLibraries(); + ({ state, groth16verifier } = await deployHelper.deployStateWithLibraries()); + } + beforeEach(async () => { + await loadFixture(deployContractsFixture); + }); + + it("Should set groth16 verifier", async () => { const verifierAddress = await state.getVerifier(); expect(verifierAddress).to.equal(await groth16verifier.getAddress()); @@ -381,9 +409,6 @@ describe("Set Verifier", () => { }); it("Should not set groth16 verifier if not owner", async () => { - const deployHelper = await DeployHelper.initialize(); - const { state, groth16verifier } = await deployHelper.deployStateWithLibraries(); - const verifierAddress = await state.getVerifier(); expect(verifierAddress).to.equal(await groth16verifier.getAddress()); @@ -395,9 +420,6 @@ describe("Set Verifier", () => { }); it("Should allow groth16 verifier zero address to block any state transition", async () => { - const deployHelper = await DeployHelper.initialize(); - const { state } = await deployHelper.deployStateWithLibraries(); - await state.setVerifier(ethers.ZeroAddress); await expect(publishState(state, stateTransitionsWithProofs[0])).to.be.reverted; }); diff --git a/test/stateLib/stateLib.test.ts b/test/stateLib/stateLib.test.ts index 1e42f627..d23cc8f7 100644 --- a/test/stateLib/stateLib.test.ts +++ b/test/stateLib/stateLib.test.ts @@ -1,6 +1,7 @@ import { DeployHelper } from "../../helpers/DeployHelper"; import { expect } from "chai"; import { addStateToStateLib } from "../utils/state-utils"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; const id1Inputs = [ { id: 1, state: 10 }, @@ -10,20 +11,24 @@ const id1Inputs = [ describe("Negative tests", function () { let stateLibWrpr; - before(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); stateLibWrpr = await deployHelper.deployStateLibTestWrapper(); for (const { id, state } of id1Inputs) { await addStateToStateLib(stateLibWrpr, id, state); } + } + + before(async () => { + await loadFixture(deployContractsFixture); }); it("getStateInfoByID: should be reverted if identity does not exist", async () => { const missingID = 777; await expect(stateLibWrpr.getStateInfoById(missingID)).to.be.revertedWith( - "Identity does not exist" + "Identity does not exist", ); }); @@ -31,7 +36,7 @@ describe("Negative tests", function () { const missingID = 777; await expect(stateLibWrpr.getStateInfoHistoryById(missingID, 0, 1)).to.be.revertedWith( - "Identity does not exist" + "Identity does not exist", ); }); @@ -39,7 +44,7 @@ describe("Negative tests", function () { const missingID = 777; await expect(stateLibWrpr.getStateInfoHistoryLengthById(missingID)).to.be.revertedWith( - "Identity does not exist" + "Identity does not exist", ); }); @@ -48,19 +53,19 @@ describe("Negative tests", function () { const missingState = 888; await expect(stateLibWrpr.getStateInfoByIdAndState(id, missingState)).to.be.revertedWith( - "State does not exist" + "State does not exist", ); }); it("Zero timestamp and block should be only in the first identity state", async () => { await expect(stateLibWrpr.addGenesisState(2, 20)).to.be.not.reverted; await expect(stateLibWrpr.addGenesisState(2, 20)).to.be.revertedWith( - "Zero timestamp and block should be only in the first identity state" + "Zero timestamp and block should be only in the first identity state", ); await expect(stateLibWrpr.addState(3, 30)).to.be.not.reverted; await expect(stateLibWrpr.addGenesisState(3, 30)).to.be.revertedWith( - "Zero timestamp and block should be only in the first identity state" + "Zero timestamp and block should be only in the first identity state", ); }); }); @@ -69,7 +74,7 @@ describe("StateInfo history", function () { let stateLibWrpr, id1, id1HistoryLength; let addStateResults: { [key: string]: any }[] = []; - before(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); stateLibWrpr = await deployHelper.deployStateLibTestWrapper(); @@ -80,6 +85,10 @@ describe("StateInfo history", function () { id1 = id1Inputs[0].id; id1HistoryLength = await stateLibWrpr.getStateInfoHistoryLengthById(id1); + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("should return state history", async () => { @@ -109,19 +118,19 @@ describe("StateInfo history", function () { it("should be reverted if length is zero", async () => { await expect(stateLibWrpr.getStateInfoHistoryById(id1, 0, 0)).to.be.revertedWith( - "Length should be greater than 0" + "Length should be greater than 0", ); }); it("should be reverted if length limit exceeded", async () => { await expect(stateLibWrpr.getStateInfoHistoryById(id1, 0, 10 ** 6)).to.be.revertedWith( - "Length limit exceeded" + "Length limit exceeded", ); }); it("should be reverted if startIndex is out of bounds", async () => { await expect( - stateLibWrpr.getStateInfoHistoryById(id1, id1HistoryLength, 100) + stateLibWrpr.getStateInfoHistoryById(id1, id1HistoryLength, 100), ).to.be.revertedWith("Start index out of bounds"); }); @@ -136,9 +145,13 @@ describe("StateInfo history", function () { describe("State history duplicates", function () { let stateLibWrpr; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(); stateLibWrpr = await deployHelper.deployStateLibTestWrapper(); + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("comprehensive check", async () => { @@ -172,10 +185,10 @@ describe("State history duplicates", function () { 3n, ); expect( - await stateLibWrpr.getStateInfoListLengthByIdAndState(...nonExistingIdAndState1) + await stateLibWrpr.getStateInfoListLengthByIdAndState(...nonExistingIdAndState1), ).to.be.equal(0n); expect( - await stateLibWrpr.getStateInfoListLengthByIdAndState(...nonExistingIdAndState2) + await stateLibWrpr.getStateInfoListLengthByIdAndState(...nonExistingIdAndState2), ).to.be.equal(0); const siSingleIdAndState = await stateLibWrpr.getStateInfoListByIdAndState( @@ -212,7 +225,7 @@ describe("State history duplicates", function () { expect(si.replacedByState).to.be.equal(siExpNext.stateInfoByIdAndState.state ?? 0); expect(si.createdAtTimestamp).to.be.equal(siExp.stateInfoByIdAndState.createdAtTimestamp); expect(si.replacedAtTimestamp).to.be.equal( - siExpNext.stateInfoByIdAndState.createdAtTimestamp ?? 0 + siExpNext.stateInfoByIdAndState.createdAtTimestamp ?? 0, ); expect(si.createdAtBlock).to.be.equal(siExp.stateInfoByIdAndState.createdAtBlock); expect(si.replacedAtBlock).to.be.equal(siExpNext.stateInfoByIdAndState.createdAtBlock ?? 0); @@ -248,7 +261,7 @@ describe("State history duplicates", function () { const state = 1; await stateLibWrpr.addState(id, state); await expect( - stateLibWrpr.getStateInfoListByIdAndState(id, state, 0, 10 ** 6) + stateLibWrpr.getStateInfoListByIdAndState(id, state, 0, 10 ** 6), ).to.be.revertedWith("Length limit exceeded"); }); diff --git a/test/validators/mtp/index.ts b/test/validators/mtp/index.ts index c452135d..e224ea1f 100644 --- a/test/validators/mtp/index.ts +++ b/test/validators/mtp/index.ts @@ -3,6 +3,8 @@ import { prepareInputs, publishState } from "../../utils/state-utils"; import { DeployHelper } from "../../../helpers/DeployHelper"; import { packValidatorParams } from "../../utils/validator-pack-utils"; import { CircuitId } from "@0xpolygonid/js-sdk"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; +import { time } from "@nomicfoundation/hardhat-network-helpers"; const tenYears = 315360000; const testCases: any[] = [ @@ -94,19 +96,30 @@ describe("Atomic MTP Validator", function () { let state: any, mtpValidator: any; let senderAddress: string; - beforeEach(async () => { + async function deployContractsFixture() { senderAddress = "0x3930000000000000000000000000000000000000"; // because challenge is 12345 in proofs. const deployHelper = await DeployHelper.initialize(null, true); const { state: stateContract } = await deployHelper.deployStateWithLibraries(["0x0100"]); - state = stateContract; - const contracts = await deployHelper.deployValidatorContractsWithVerifiers( "mtpV2", - await state.getAddress(), + await stateContract.getAddress(), ); - state = contracts.state; - mtpValidator = contracts.validator; + const validator = contracts.validator; + + return { + stateContract, + validator, + senderAddress, + }; + } + + beforeEach(async () => { + ({ + stateContract: state, + validator: mtpValidator, + senderAddress, + } = await loadFixture(deployContractsFixture)); }); for (const test of testCases) { @@ -114,13 +127,9 @@ describe("Atomic MTP Validator", function () { this.timeout(50000); for (let i = 0; i < test.stateTransitions.length; i++) { if (test.stateTransitionDelayMs) { - await Promise.all([ - publishState(state, test.stateTransitions[i]), - delay(test.stateTransitionDelayMs), - ]); - } else { - await publishState(state, test.stateTransitions[i]); + await time.increase(test.stateTransitionDelayMs); } + await publishState(state, test.stateTransitions[i]); } const query = { diff --git a/test/validators/sig/index.ts b/test/validators/sig/index.ts index 9e86bd13..d489cad1 100644 --- a/test/validators/sig/index.ts +++ b/test/validators/sig/index.ts @@ -3,6 +3,8 @@ import { prepareInputs, publishState } from "../../utils/state-utils"; import { DeployHelper } from "../../../helpers/DeployHelper"; import { packValidatorParams } from "../../utils/validator-pack-utils"; import { CircuitId } from "@0xpolygonid/js-sdk"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; +import { time } from "@nomicfoundation/hardhat-network-helpers"; const tenYears = 315360000; const testCases: any[] = [ @@ -91,20 +93,33 @@ function delay(ms: number) { } describe("Atomic Sig Validator", function () { - let state: any, sig: any; + let state: any, sigValidator: any; let senderAddress: string; - beforeEach(async () => { + async function deployContractsFixture() { senderAddress = "0x3930000000000000000000000000000000000000"; // because challenge is 12345 in proofs. const deployHelper = await DeployHelper.initialize(null, true); const { state: stateContract } = await deployHelper.deployStateWithLibraries(["0x0100"]); - state = stateContract; const contracts = await deployHelper.deployValidatorContractsWithVerifiers( "sigV2", - await state.getAddress(), + await stateContract.getAddress(), ); - sig = contracts.validator; + const validator = contracts.validator; + + return { + stateContract, + validator, + senderAddress, + }; + } + + beforeEach(async () => { + ({ + stateContract: state, + validator: sigValidator, + senderAddress, + } = await loadFixture(deployContractsFixture)); }); for (const test of testCases) { @@ -112,13 +127,9 @@ describe("Atomic Sig Validator", function () { this.timeout(50000); for (let i = 0; i < test.stateTransitions.length; i++) { if (test.stateTransitionDelayMs) { - await Promise.all([ - publishState(state, test.stateTransitions[i]), - delay(test.stateTransitionDelayMs), - ]); - } else { - await publishState(state, test.stateTransitions[i]); + await time.increase(test.stateTransitionDelayMs); } + await publishState(state, test.stateTransitions[i]); } const query = { @@ -139,17 +150,17 @@ describe("Atomic Sig Validator", function () { const { inputs, pi_a, pi_b, pi_c } = prepareInputs(test.proofJson); if (test.setProofExpiration) { - await sig.setProofExpirationTimeout(test.setProofExpiration); + await sigValidator.setProofExpirationTimeout(test.setProofExpiration); } if (test.setRevStateExpiration) { - await sig.setRevocationStateExpirationTimeout(test.setRevStateExpiration); + await sigValidator.setRevocationStateExpirationTimeout(test.setRevStateExpiration); } if (test.setGISTRootExpiration) { - await sig.setGISTRootExpirationTimeout(test.setGISTRootExpiration); + await sigValidator.setGISTRootExpirationTimeout(test.setGISTRootExpiration); } if (test.errorMessage) { await expect( - sig.verify( + sigValidator.verify( inputs, pi_a, pi_b, @@ -160,7 +171,7 @@ describe("Atomic Sig Validator", function () { ).to.be.rejectedWith(test.errorMessage); } else if (test.errorMessage === "") { await expect( - sig.verify( + sigValidator.verify( inputs, pi_a, pi_b, @@ -170,7 +181,7 @@ describe("Atomic Sig Validator", function () { ), ).to.be.reverted; } else { - await sig.verify( + await sigValidator.verify( inputs, pi_a, pi_b, @@ -183,7 +194,7 @@ describe("Atomic Sig Validator", function () { } it("check inputIndexOf", async () => { - const challengeIndx = await sig.inputIndexOf("challenge"); + const challengeIndx = await sigValidator.inputIndexOf("challenge"); expect(challengeIndx).to.be.equal(5); }); }); diff --git a/test/validators/v3/index.ts b/test/validators/v3/index.ts index 558e7c8b..ccd39b96 100644 --- a/test/validators/v3/index.ts +++ b/test/validators/v3/index.ts @@ -4,6 +4,8 @@ import { DeployHelper } from "../../../helpers/DeployHelper"; import { packV3ValidatorParams } from "../../utils/validator-pack-utils"; import { calculateQueryHashV3 } from "../../utils/query-hash-utils"; import { CircuitId } from "@0xpolygonid/js-sdk"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; +import { time } from "@nomicfoundation/hardhat-network-helpers"; const tenYears = 315360000; const testCases: any[] = [ @@ -329,17 +331,25 @@ function delay(ms: number) { describe("Atomic V3 Validator", function () { let state: any, v3validator; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(null, true); const { state: stateContract } = await deployHelper.deployStateWithLibraries(["0x0212"]); - state = stateContract; const contracts = await deployHelper.deployValidatorContractsWithVerifiers( "v3", - await state.getAddress(), + await stateContract.getAddress(), ); - v3validator = contracts.validator; + const validator = contracts.validator; + + return { + stateContract, + validator, + }; + } + + beforeEach(async () => { + ({ stateContract: state, validator: v3validator } = await loadFixture(deployContractsFixture)); }); for (const test of testCases) { @@ -347,13 +357,9 @@ describe("Atomic V3 Validator", function () { this.timeout(50000); for (let i = 0; i < test.stateTransitions.length; i++) { if (test.stateTransitionDelayMs) { - await Promise.all([ - publishState(state, test.stateTransitions[i]), - delay(test.stateTransitionDelayMs), - ]); - } else { - await publishState(state, test.stateTransitions[i]); + await time.increase(test.stateTransitionDelayMs); } + await publishState(state, test.stateTransitions[i]); } const value = ["20010101", ...new Array(63).fill("0")]; diff --git a/test/verifier/embedded-zkp-verifier.test.ts b/test/verifier/embedded-zkp-verifier.test.ts index 42927c1b..ac52d91b 100644 --- a/test/verifier/embedded-zkp-verifier.test.ts +++ b/test/verifier/embedded-zkp-verifier.test.ts @@ -7,6 +7,7 @@ import { Block, Signer } from "ethers"; import { buildCrossChainProofs, packCrossChainProofs, packZKProof } from "../utils/packData"; import proofJson from "../validators/sig/data/valid_sig_user_genesis.json"; import { CircuitId } from "@0xpolygonid/js-sdk"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; describe("Embedded ZKP Verifier", function () { let verifier: any, sig: any; @@ -27,7 +28,7 @@ describe("Embedded ZKP Verifier", function () { claimPathNotExists: 0, }; - beforeEach(async () => { + async function deployContractsFixture() { const deployHelper = await DeployHelper.initialize(null, true); [owner] = await ethers.getSigners(); @@ -43,6 +44,10 @@ describe("Embedded ZKP Verifier", function () { const stub = await deployHelper.deployValidatorStub(); sig = stub; + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("test submit response", async () => { diff --git a/test/verifier/universal-verifier-linked-proofs.test.ts b/test/verifier/universal-verifier-linked-proofs.test.ts index 2bf189e6..33d0d31f 100644 --- a/test/verifier/universal-verifier-linked-proofs.test.ts +++ b/test/verifier/universal-verifier-linked-proofs.test.ts @@ -4,6 +4,7 @@ import { packV3ValidatorParams } from "../utils/validator-pack-utils"; import { prepareInputs, publishState } from "../utils/state-utils"; import { expect } from "chai"; import testData from "./linked-proofs-data.json"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; describe("Universal Verifier Linked proofs", function () { let verifier: any, v3: any, state: any; @@ -11,7 +12,7 @@ describe("Universal Verifier Linked proofs", function () { let signerAddress: string; let deployHelper: DeployHelper; - beforeEach(async () => { + async function deployContractsFixture() { [signer, signer2] = await ethers.getSigners(); signerAddress = await signer.getAddress(); @@ -48,6 +49,10 @@ describe("Universal Verifier Linked proofs", function () { const { inputs, pi_a, pi_b, pi_c } = prepareInputs(testData.queryData.zkpResponses[i]); await verifier.submitZKPResponse(100 + i, inputs, pi_a, pi_b, pi_c); } + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("should linked proof validation pass", async () => { diff --git a/test/verifier/universal-verifier-submit-V2.test.ts b/test/verifier/universal-verifier-submit-V2.test.ts index 8ce7f6cf..0300806a 100644 --- a/test/verifier/universal-verifier-submit-V2.test.ts +++ b/test/verifier/universal-verifier-submit-V2.test.ts @@ -7,6 +7,7 @@ import { Block, Contract } from "ethers"; import proofJson from "../validators/sig/data/valid_sig_user_genesis.json"; import { buildCrossChainProofs, packCrossChainProofs, packZKProof } from "../utils/packData"; import { CircuitId } from "@0xpolygonid/js-sdk"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; describe("Universal Verifier V2 MTP & SIG validators", function () { let verifier: any, sig: any; @@ -51,7 +52,7 @@ describe("Universal Verifier V2 MTP & SIG validators", function () { claimPathNotExists: 0, }; - beforeEach(async () => { + async function deployContractsFixture() { [signer] = await ethers.getSigners(); signerAddress = await signer.getAddress(); @@ -75,6 +76,10 @@ describe("Universal Verifier V2 MTP & SIG validators", function () { sig = validatorStub; await verifier.addValidatorToWhitelist(await sig.getAddress()); await verifier.connect(); + } + + beforeEach(async () => { + await loadFixture(deployContractsFixture); }); it("Test submit response V2", async () => { diff --git a/test/verifier/universal-verifier.events.test.ts b/test/verifier/universal-verifier.events.test.ts index 2eb276a8..597f8548 100644 --- a/test/verifier/universal-verifier.events.test.ts +++ b/test/verifier/universal-verifier.events.test.ts @@ -117,8 +117,7 @@ describe("Universal Verifier events", function () { const coder = AbiCoder.defaultAbiCoder(); logs.map((log, index) => { - // @ts-ignore - const [decodedData] = coder.decode(abi, log.args.data); + const [decodedData] = coder.decode(abi as any, log.args.data); expect(decodedData.schema).to.equal(queries[index].schema); expect(decodedData.claimPathKey).to.equal(queries[index].claimPathKey); expect(decodedData.operator).to.equal(queries[index].operator); diff --git a/test/verifier/universal-verifier.test.ts b/test/verifier/universal-verifier.test.ts index 67a02417..bbe89e77 100644 --- a/test/verifier/universal-verifier.test.ts +++ b/test/verifier/universal-verifier.test.ts @@ -6,9 +6,10 @@ import { prepareInputs } from "../utils/state-utils"; import { Block } from "ethers"; import proofJson from "../validators/mtp/data/valid_mtp_user_genesis.json"; import { CircuitId } from "@0xpolygonid/js-sdk"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; describe("Universal Verifier MTP & SIG validators", function () { - let verifier: any, sig: any, state: any; + let verifier: any, sigValidator: any, state: any; let signer, signer2, signer3; let signerAddress: string; let deployHelper: DeployHelper; @@ -28,25 +29,38 @@ describe("Universal Verifier MTP & SIG validators", function () { claimPathNotExists: 0, }; - beforeEach(async () => { - [signer, signer2, signer3] = await ethers.getSigners(); - signerAddress = await signer.getAddress(); + async function deployContractsFixture() { + const [ethSigner, ethSigner2, ethSigner3] = await ethers.getSigners(); deployHelper = await DeployHelper.initialize(null, true); - ({ state } = await deployHelper.deployStateWithLibraries(["0x0112"])); + const { state: stateContract } = await deployHelper.deployStateWithLibraries(["0x0112"]); const verifierLib = await deployHelper.deployVerifierLib(); - verifier = await deployHelper.deployUniversalVerifier( - signer, - await state.getAddress(), + const universalVerifier: any = await deployHelper.deployUniversalVerifier( + ethSigner, + await stateContract.getAddress(), await verifierLib.getAddress(), ); const stub = await deployHelper.deployValidatorStub(); - sig = stub; - await verifier.addValidatorToWhitelist(await sig.getAddress()); - await verifier.connect(); + const validator = stub; + await universalVerifier.addValidatorToWhitelist(await validator.getAddress()); + await universalVerifier.connect(); + + return { ethSigner, ethSigner2, ethSigner3, stateContract, universalVerifier, validator }; + } + + beforeEach(async () => { + ({ + ethSigner: signer, + ethSigner2: signer2, + ethSigner3: signer3, + stateContract: state, + universalVerifier: verifier, + validator: sigValidator, + } = await loadFixture(deployContractsFixture)); + signerAddress = await signer.getAddress(); }); it("Test get state address", async () => { @@ -56,7 +70,7 @@ describe("Universal Verifier MTP & SIG validators", function () { it("Test add, get ZKPRequest, requestIdExists, getZKPRequestsCount", async () => { const requestsCount = 3; - const validatorAddr = await sig.getAddress(); + const validatorAddr = await sigValidator.getAddress(); for (let i = 0; i < requestsCount; i++) { await expect( @@ -92,7 +106,7 @@ describe("Universal Verifier MTP & SIG validators", function () { await verifier.setZKPRequest(0, { metadata: "metadata", - validator: await sig.getAddress(), + validator: await sigValidator.getAddress(), data: data, }); @@ -136,7 +150,7 @@ describe("Universal Verifier MTP & SIG validators", function () { for (let i = 0; i < 30; i++) { await verifier.setZKPRequest(i, { metadata: "metadataN" + i, - validator: await sig.getAddress(), + validator: await sigValidator.getAddress(), data: "0x00", }); } @@ -166,7 +180,7 @@ describe("Universal Verifier MTP & SIG validators", function () { ); await verifier.connect(requestOwner).setZKPRequest(requestId, { metadata: "metadata", - validator: await sig.getAddress(), + validator: await sigValidator.getAddress(), data: packValidatorParams(query), }); @@ -205,7 +219,7 @@ describe("Universal Verifier MTP & SIG validators", function () { await verifier.connect(requestOwner).setZKPRequest(requestId, { metadata: "metadata", - validator: await sig.getAddress(), + validator: await sigValidator.getAddress(), data: packValidatorParams(query), }); expect(await verifier.isZKPRequestEnabled(requestId)).to.be.true; @@ -258,7 +272,6 @@ describe("Universal Verifier MTP & SIG validators", function () { const someAddress = signer2; const requestId = 1; const otherRequestId = 2; - const { state } = await deployHelper.deployStateWithLibraries(); const { validator: mtp } = await deployHelper.deployValidatorContractsWithVerifiers( "mtpV2", await state.getAddress(), diff --git a/test/verifier/universal-verifier.v3.test.ts b/test/verifier/universal-verifier.v3.test.ts index e211c756..d1067607 100644 --- a/test/verifier/universal-verifier.v3.test.ts +++ b/test/verifier/universal-verifier.v3.test.ts @@ -5,9 +5,15 @@ import { prepareInputs, publishState } from "../utils/state-utils"; import { calculateQueryHashV3 } from "../utils/query-hash-utils"; import { expect } from "chai"; import { CircuitId } from "@0xpolygonid/js-sdk"; +import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; +import proofJson from "../validators/v3/data/valid_bjj_user_genesis_auth_disabled_v3.json"; +import stateTransition1 from "../validators/common-data/issuer_from_genesis_state_to_first_auth_disabled_transition_v3.json"; +import stateTransition11 from "../validators/common-data/issuer_from_genesis_state_to_first_transition_v3.json"; +import stateTransition12 from "../validators/common-data/user_from_genesis_state_to_first_transition_v3.json"; +import stateTransition13 from "../validators/common-data/issuer_from_first_state_to_second_transition_v3.json"; describe("Universal Verifier V3 validator", function () { - let verifier: any, v3: any, state: any; + let verifier: any, v3Validator: any, state: any; let signer, signer2; let deployHelper: DeployHelper; @@ -49,48 +55,52 @@ describe("Universal Verifier V3 validator", function () { verifierID: verifierId, }; - const proofJson = require("../validators/v3/data/valid_bjj_user_genesis_auth_disabled_v3.json"); - const stateTransition1 = require("../validators/common-data/issuer_from_genesis_state_to_first_auth_disabled_transition_v3.json"); - - const stateTransition11 = require("../validators/common-data/issuer_from_genesis_state_to_first_transition_v3.json"); - const stateTransition12 = require("../validators/common-data/user_from_genesis_state_to_first_transition_v3.json"); - const stateTransition13 = require("../validators/common-data/issuer_from_first_state_to_second_transition_v3.json"); - const initializeState = async () => { deployHelper = await DeployHelper.initialize(null, true); const { state: stateContract } = await deployHelper.deployStateWithLibraries(["0x0212"]); - state = stateContract; const verifierLib = await deployHelper.deployVerifierLib(); const contracts = await deployHelper.deployValidatorContractsWithVerifiers( "v3", - await state.getAddress(), + await stateContract.getAddress(), ); - v3 = contracts.validator; - verifier = await deployHelper.deployUniversalVerifier( + const validator = contracts.validator; + const universalVerifier: any = await deployHelper.deployUniversalVerifier( signer, - await state.getAddress(), + await stateContract.getAddress(), await verifierLib.getAddress(), ); - await verifier.addValidatorToWhitelist(await v3.getAddress()); - await verifier.connect(); + await universalVerifier.addValidatorToWhitelist(await validator.getAddress()); + await universalVerifier.connect(); + + return { stateContract, validator, universalVerifier }; }; - before(async () => { - [signer, signer2] = await ethers.getSigners(); + async function deployContractsFixture() { + const [ethSigner, ethSigner2] = await ethers.getSigners(); + const { stateContract, validator, universalVerifier } = await initializeState(); + return { ethSigner, ethSigner2, stateContract, universalVerifier, validator }; + } - await initializeState(); + before(async () => { + ({ + ethSigner: signer, + ethSigner2: signer2, + stateContract: state, + validator: v3Validator, + universalVerifier: verifier, + } = await loadFixture(deployContractsFixture)); }); it("Test submit response", async () => { - await publishState(state, stateTransition1); + await publishState(state, stateTransition1 as any); const data = packV3ValidatorParams(query); await verifier.setZKPRequest(32, { metadata: "metadata", - validator: await v3.getAddress(), + validator: await v3Validator.getAddress(), data: data, }); - await v3.setProofExpirationTimeout(315360000); + await v3Validator.setProofExpirationTimeout(315360000); const { inputs, pi_a, pi_b, pi_c } = prepareInputs(proofJson); @@ -118,7 +128,7 @@ describe("Universal Verifier V3 validator", function () { const data = packV3ValidatorParams(query, ["1"]); await verifier.setZKPRequest(33, { metadata: "metadata", - validator: await v3.getAddress(), + validator: await v3Validator.getAddress(), data: data, }); @@ -137,7 +147,7 @@ describe("Universal Verifier V3 validator", function () { const data = packV3ValidatorParams(query2); await verifier.setZKPRequest(34, { metadata: "metadata", - validator: await v3.getAddress(), + validator: await v3Validator.getAddress(), data: data, }); @@ -156,7 +166,7 @@ describe("Universal Verifier V3 validator", function () { const data = packV3ValidatorParams(query2); await verifier.setZKPRequest(35, { metadata: "metadata", - validator: await v3.getAddress(), + validator: await v3Validator.getAddress(), data: data, }); @@ -175,7 +185,7 @@ describe("Universal Verifier V3 validator", function () { const data = packV3ValidatorParams(query2); await verifier.setZKPRequest(36, { metadata: "metadata", - validator: await v3.getAddress(), + validator: await v3Validator.getAddress(), data: data, }); @@ -194,7 +204,7 @@ describe("Universal Verifier V3 validator", function () { const data = packV3ValidatorParams(query2); await verifier.setZKPRequest(37, { metadata: "metadata", - validator: await v3.getAddress(), + validator: await v3Validator.getAddress(), data: data, }); @@ -206,16 +216,22 @@ describe("Universal Verifier V3 validator", function () { }); it("Test submit response fails with Generated proof is outdated", async () => { - await initializeState(); - - await publishState(state, stateTransition11); - await publishState(state, stateTransition12); - await publishState(state, stateTransition13); + ({ + ethSigner: signer, + ethSigner2: signer2, + stateContract: state, + validator: v3Validator, + universalVerifier: verifier, + } = await loadFixture(deployContractsFixture)); + + await publishState(state, stateTransition11 as any); + await publishState(state, stateTransition12 as any); + await publishState(state, stateTransition13 as any); const data = packV3ValidatorParams(query); await verifier.setZKPRequest(37, { metadata: "metadata", - validator: await v3.getAddress(), + validator: await v3Validator.getAddress(), data: data, });