Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriianChestnykh committed Nov 25, 2024
1 parent 69503fc commit ef7c419
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 53 deletions.
63 changes: 42 additions & 21 deletions test/verifier/embedded-zkp-verifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { CircuitId } from "@0xpolygonid/js-sdk";
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers";

describe("Embedded ZKP Verifier", function () {
let verifier: any, sig: any;
let verifier: any, validator: any;
let owner: Signer;

const query = {
Expand All @@ -28,6 +28,9 @@ describe("Embedded ZKP Verifier", function () {
claimPathNotExists: 0,
};

const { inputs, pi_a, pi_b, pi_c } = prepareInputs(proofJson);
const metadatas = "0x";

async function deployContractsFixture() {
const deployHelper = await DeployHelper.initialize(null, true);
[owner] = await ethers.getSigners();
Expand All @@ -42,15 +45,17 @@ describe("Embedded ZKP Verifier", function () {
await verifierLib.getAddress(),
);

const stub = await deployHelper.deployValidatorStub();
sig = stub;
validator = await deployHelper.deployValidatorStub();
}

async function checkStorageFields(verifier: any, requestId: number) {
const fieldsToCheck = ["userID", "issuerID"];
for (const field of fieldsToCheck) {
const value = await verifier.getProofStorageField(await owner.getAddress(), requestId, field);
expect(value).to.be.greaterThan(0n);
async function checkStorageFields(verifier: any, requestId: number, storageFields: any[]) {
for (const field of storageFields) {
const value = await verifier.getProofStorageField(
await owner.getAddress(),
requestId,
field.name,
);
expect(value).to.be.equal(field.value);
}
}

Expand All @@ -61,15 +66,24 @@ describe("Embedded ZKP Verifier", function () {
it("test submit response", async () => {
await verifier.setZKPRequest(0, {
metadata: "metadata",
validator: await sig.getAddress(),
validator: await validator.getAddress(),
data: packValidatorParams(query),
});

const { inputs, pi_a, pi_b, pi_c } = prepareInputs(proofJson);

const tx = await verifier.submitZKPResponse(0, inputs, pi_a, pi_b, pi_c);
const txRes = await tx.wait();
await checkStorageFields(verifier, 0);
const storageFields = [
{
name: "userID",
value: inputs[1],
},
{
name: "issuerID",
value: inputs[2],
},
];

await checkStorageFields(verifier, 0, storageFields);
const receipt = await ethers.provider.getTransactionReceipt(txRes.hash);

// 2 events are emitted
Expand All @@ -85,7 +99,7 @@ describe("Embedded ZKP Verifier", function () {
);
expect(eventBeforeProofSubmit[0]).to.equal(0);
expect(eventBeforeProofSubmit[1]).to.deep.equal(inputs.map((x) => BigInt(x)));
expect(eventBeforeProofSubmit[2]).to.equal(await sig.getAddress());
expect(eventBeforeProofSubmit[2]).to.equal(await validator.getAddress());

const interfaceEventAfterProofSubmit = new ethers.Interface([
"event AfterProofSubmit(uint64 requestId, uint256[] inputs, address validator)",
Expand All @@ -97,7 +111,7 @@ describe("Embedded ZKP Verifier", function () {
);
expect(eventAfterProofSubmit[0]).to.equal(0);
expect(eventAfterProofSubmit[1]).to.deep.equal(inputs.map((x) => BigInt(x)));
expect(eventAfterProofSubmit[2]).to.equal(await sig.getAddress());
expect(eventAfterProofSubmit[2]).to.equal(await validator.getAddress());

const ownerAddress = await owner.getAddress();
const requestID = 0;
Expand Down Expand Up @@ -138,12 +152,10 @@ describe("Embedded ZKP Verifier", function () {

await verifier.setZKPRequest(0, {
metadata: "metadata",
validator: await sig.getAddress(),
validator: await validator.getAddress(),
data: packValidatorParams(query),
});

const { inputs, pi_a, pi_b, pi_c } = prepareInputs(proofJson);

const zkProof = packZKProof(inputs, pi_a, pi_b, pi_c);
const [signer] = await ethers.getSigners();

Expand All @@ -154,8 +166,6 @@ describe("Embedded ZKP Verifier", function () {
),
);

const metadatas = "0x";

const tx = await verifier.submitZKPResponseV2(
[
{
Expand All @@ -168,7 +178,18 @@ describe("Embedded ZKP Verifier", function () {
);

const txRes = await tx.wait();
await checkStorageFields(verifier, 0);

const storageFields = [
{
name: "userID",
value: 1n,
},
{
name: "issuerID",
value: 2n,
},
];
await checkStorageFields(verifier, 0, storageFields);

const receipt = await ethers.provider.getTransactionReceipt(txRes.hash);

Expand Down Expand Up @@ -219,7 +240,7 @@ describe("Embedded ZKP Verifier", function () {
for (let i = 0; i < requestsCount; i++) {
await verifier.setZKPRequest(i, {
metadata: "metadataN" + i,
validator: await sig.getAddress(),
validator: await validator.getAddress(),
data: "0x00",
});
const reqeustIdExists = await verifier.requestIdExists(i);
Expand Down
26 changes: 18 additions & 8 deletions test/verifier/universal-verifier-submit-V2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { buildCrossChainProofs, packCrossChainProofs, packZKProof } from "../uti
import { CircuitId } from "@0xpolygonid/js-sdk";
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers";

describe("Universal Verifier V2 MTP & SIG validators", function () {
describe("Universal Verifier submitZKPResponseV2 SigV2 validators", function () {
let verifier: any, sig: any;
let signer;
let signerAddress: string;
Expand Down Expand Up @@ -117,15 +117,25 @@ describe("Universal Verifier V2 MTP & SIG validators", function () {
}
}

async function checkStorageFields(verifier: any, requestId: number) {
const fieldsToCheck = ["userID", "issuerID"];
for (const field of fieldsToCheck) {
const storageFields = [
{
name: "userID",
value: 1n,
},
{
name: "issuerID",
value: 2n,
},
];

async function checkStorageFields(verifier: any, requestId: number, storageFields: any[]) {
for (const field of storageFields) {
const value = await verifier.getProofStorageField(
await signer.getAddress(),
requestId,
field,
field.name,
);
expect(value).to.be.greaterThan(0n);
expect(value).to.be.equal(field.value);
}
}

Expand All @@ -144,7 +154,7 @@ describe("Universal Verifier V2 MTP & SIG validators", function () {
const tx = await verifier.submitZKPResponseV2(singleProof, crossChainProofs);

const txRes = await tx.wait();
await checkStorageFields(verifier, requestId);
await checkStorageFields(verifier, requestId, storageFields);
const filter = verifier.filters.ZKPResponseSubmitted;

const events = await verifier.queryFilter(filter, -1);
Expand Down Expand Up @@ -186,7 +196,7 @@ describe("Universal Verifier V2 MTP & SIG validators", function () {
expect(status.validatorVersion).to.be.equal("2.0.2-mock");
expect(status.blockNumber).to.be.equal(txResMulti.blockNumber);
expect(status.blockTimestamp).to.be.equal(txResTimestampMuti);
await checkStorageFields(verifier, requestId);
await checkStorageFields(verifier, requestId, storageFields);
}
});

Expand Down
55 changes: 31 additions & 24 deletions test/verifier/universal-verifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CircuitId } from "@0xpolygonid/js-sdk";
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers";

describe("Universal Verifier MTP & SIG validators", function () {
let verifier: any, sigValidator: any, state: any;
let verifier: any, validator: any, state: any;
let signer, signer2, signer3;
let signerAddress: string;
let deployHelper: DeployHelper;
Expand All @@ -36,30 +36,27 @@ describe("Universal Verifier MTP & SIG validators", function () {
const { state: stateContract } = await deployHelper.deployStateWithLibraries(["0x0112"]);
const verifierLib = await deployHelper.deployVerifierLib();

const universalVerifier: any = await deployHelper.deployUniversalVerifier(
const verifier: any = await deployHelper.deployUniversalVerifier(
ethSigner,
await stateContract.getAddress(),
await verifierLib.getAddress(),
);

const stub = await deployHelper.deployValidatorStub();
const validator = await deployHelper.deployValidatorStub();
await verifier.addValidatorToWhitelist(await validator.getAddress());
await verifier.connect();

const validator = stub;
await universalVerifier.addValidatorToWhitelist(await validator.getAddress());
await universalVerifier.connect();

return { ethSigner, ethSigner2, ethSigner3, stateContract, universalVerifier, validator };
return { ethSigner, ethSigner2, ethSigner3, stateContract, verifier, validator };
}

async function checkStorageFields(verifier: any, requestId: number) {
const fieldsToCheck = ["userID", "issuerID"];
for (const field of fieldsToCheck) {
async function checkStorageFields(verifier: any, requestId: number, storageFields: any[]) {
for (const field of storageFields) {
const value = await verifier.getProofStorageField(
await signer.getAddress(),
requestId,
field,
field.name,
);
expect(value).to.be.greaterThan(0n);
expect(value).to.be.equal(field.value);
}
}

Expand All @@ -69,8 +66,8 @@ describe("Universal Verifier MTP & SIG validators", function () {
ethSigner2: signer2,
ethSigner3: signer3,
stateContract: state,
universalVerifier: verifier,
validator: sigValidator,
verifier,
validator,
} = await loadFixture(deployContractsFixture));
signerAddress = await signer.getAddress();
});
Expand All @@ -82,7 +79,7 @@ describe("Universal Verifier MTP & SIG validators", function () {

it("Test add, get ZKPRequest, requestIdExists, getZKPRequestsCount", async () => {
const requestsCount = 3;
const validatorAddr = await sigValidator.getAddress();
const validatorAddr = await validator.getAddress();

for (let i = 0; i < requestsCount; i++) {
await expect(
Expand Down Expand Up @@ -118,14 +115,24 @@ describe("Universal Verifier MTP & SIG validators", function () {

await verifier.setZKPRequest(0, {
metadata: "metadata",
validator: await sigValidator.getAddress(),
validator: await validator.getAddress(),
data: data,
});

const { inputs, pi_a, pi_b, pi_c } = prepareInputs(proofJson);
const tx = await verifier.submitZKPResponse(0, inputs, pi_a, pi_b, pi_c);
const txRes = await tx.wait();
await checkStorageFields(verifier, requestId);
const storageFields = [
{
name: "userID",
value: inputs[1],
},
{
name: "issuerID",
value: inputs[2],
},
];
await checkStorageFields(verifier, requestId, storageFields);
const filter = verifier.filters.ZKPResponseSubmitted;

const events = await verifier.queryFilter(filter, -1);
Expand Down Expand Up @@ -173,7 +180,7 @@ describe("Universal Verifier MTP & SIG validators", function () {
);
await verifier.connect(requestOwner).setZKPRequest(requestId, {
metadata: "metadata",
validator: await sigValidator.getAddress(),
validator: await validator.getAddress(),
data: packValidatorParams(query),
});

Expand Down Expand Up @@ -212,7 +219,7 @@ describe("Universal Verifier MTP & SIG validators", function () {

await verifier.connect(requestOwner).setZKPRequest(requestId, {
metadata: "metadata",
validator: await sigValidator.getAddress(),
validator: await validator.getAddress(),
data: packValidatorParams(query),
});
expect(await verifier.isZKPRequestEnabled(requestId)).to.be.true;
Expand Down Expand Up @@ -331,7 +338,7 @@ describe("Universal Verifier MTP & SIG validators", function () {

await verifier.connect(requestOwner).setZKPRequest(requestId, {
metadata: "metadata",
validator: await sigValidator.getAddress(),
validator: await validator.getAddress(),
data: data,
});

Expand All @@ -341,14 +348,14 @@ describe("Universal Verifier MTP & SIG validators", function () {
await expect(
verifier.connect(requestOwner).updateZKPRequest(requestId, {
metadata: "metadata",
validator: await sigValidator.getAddress(),
validator: await validator.getAddress(),
data: data,
}),
).to.be.revertedWithCustomError(verifier, "OwnableUnauthorizedAccount");

await verifier.connect(owner).updateZKPRequest(requestId, {
metadata: "metadata2",
validator: await sigValidator.getAddress(),
validator: await validator.getAddress(),
data: data,
});

Expand All @@ -364,7 +371,7 @@ describe("Universal Verifier MTP & SIG validators", function () {
await expect(
verifier.connect(owner).updateZKPRequest(requestId, {
metadata: "metadata",
validator: await sigValidator.getAddress(),
validator: await validator.getAddress(),
data: data,
}),
).to.be.rejectedWith("equest id doesn't exis");
Expand Down

0 comments on commit ef7c419

Please sign in to comment.