Skip to content

Commit

Permalink
Smart contracts tests speed up (#315)
Browse files Browse the repository at this point in the history
* add hardhat fixtures to tests

* add hardhat time instead of delay in scripts

* fix time increase before publishing state
  • Loading branch information
daveroga authored Nov 11, 2024
1 parent bb385ac commit fd2cd71
Show file tree
Hide file tree
Showing 16 changed files with 298 additions and 150 deletions.
18 changes: 13 additions & 5 deletions test/IdentityTreeStore/IdentityTreeStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -31,7 +39,7 @@ describe("IdentityTreeStore", function () {
const revStatusByState = await identityTreeStore.getRevocationStatusByIdAndState(
id,
state,
nonce
nonce,
);

const stateTransitionArgs = {
Expand Down Expand Up @@ -196,7 +204,7 @@ describe("IdentityTreeStore", function () {
const revStatusByState = await identityTreeStore.getRevocationStatusByIdAndState(
id,
state,
nonce
nonce,
);

const stateTransitionArgs = {
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 6 additions & 1 deletion test/cross-chain/cross-chain-proof-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
7 changes: 6 additions & 1 deletion test/payment/mc-payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +29,7 @@ describe("MC Payment Contract", () => {
],
};

beforeEach(async () => {
async function deployContractsFixture() {
const signers = await ethers.getSigners();
issuer1Signer = signers[1];
userSigner = signers[5];
Expand All @@ -39,6 +40,10 @@ describe("MC Payment Contract", () => {
ownerPercentage,
])) as unknown as MCPayment;
await payment.waitForDeployment();
}

beforeEach(async () => {
await loadFixture(deployContractsFixture);

domainData = {
name: "MCPayment",
Expand Down
18 changes: 12 additions & 6 deletions test/payment/vc-payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down Expand Up @@ -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 () => {
Expand Down
46 changes: 33 additions & 13 deletions test/smtLib/smtLib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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",
);
});

Expand All @@ -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 () => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1149,15 +1158,15 @@ 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",
);
});

it("should revert if length limit exceeded", async () => {
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",
);
});

Expand All @@ -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",
);
});

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

Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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,
);
}

Expand Down
Loading

0 comments on commit fd2cd71

Please sign in to comment.