Skip to content

Commit

Permalink
add test wrappers and tests for RequestDisableable, RequestOwnership …
Browse files Browse the repository at this point in the history
…and validatorWhitelist
  • Loading branch information
daveroga committed Jan 17, 2025
1 parent 822c81c commit 39d67f5
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 16 deletions.
21 changes: 21 additions & 0 deletions contracts/test-helpers/RequestDisableableTestWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

import {RequestDisableable} from "../verifiers/RequestDisableable.sol";
import {IState} from "../interfaces/IState.sol";

contract RequestDisableableTestWrapper is RequestDisableable {
function initialize(IState state) public initializer {
__Verifier_init(state);
}

function disableRequest(uint256 requestId) public {
_disableRequest(requestId);
}

function enableRequest(uint256 requestId) public {
_enableRequest(requestId);
}

function testModifier(uint256 requestId) public view onlyEnabledRequest(requestId) {}
}
15 changes: 15 additions & 0 deletions contracts/test-helpers/RequestOwnershipTestWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

import {RequestOwnership} from "../verifiers/RequestOwnership.sol";
import {IState} from "../interfaces/IState.sol";

contract RequestOwnershipTestWrapper is RequestOwnership {
function initialize(IState state) public initializer {
__Verifier_init(state);
}

function setRequestOwner(uint256 requestId, address requestOwner) public {
_setRequestOwner(requestId, requestOwner);
}
}
24 changes: 24 additions & 0 deletions contracts/test-helpers/ValidatorWhitelistTestWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

import {ValidatorWhitelist} from "../verifiers/ValidatorWhitelist.sol";
import {IState} from "../interfaces/IState.sol";
import {IRequestValidator} from "../interfaces/IRequestValidator.sol";

contract ValidatorWhitelistTestWrapper is ValidatorWhitelist {
function initialize(IState state) public initializer {
__Verifier_init(state);
}

function addValidatorToWhitelist(IRequestValidator validator) public {
_addValidatorToWhitelist(validator);
}

function removeValidatorFromWhitelist(IRequestValidator validator) public {
_removeValidatorFromWhitelist(validator);
}

function testModifier(
IRequestValidator validator
) public view onlyWhitelistedValidator(validator) {}
}
9 changes: 5 additions & 4 deletions contracts/verifiers/UniversalVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {ValidatorWhitelist} from "./ValidatorWhitelist.sol";
import {Verifier} from "./Verifier.sol";
import {IState} from "../interfaces/IState.sol";

error NotAnOwnerOrRequestOwner(address);

/// @title Universal Verifier Contract
/// @notice A contract to manage ZKP (Zero-Knowledge Proof) requests and proofs.
contract UniversalVerifier is
Expand Down Expand Up @@ -73,10 +75,9 @@ contract UniversalVerifier is
/// @dev Modifier to check if the caller is the contract Owner or ZKP Request Owner
modifier onlyOwnerOrRequestOwner(uint256 requestId) {
address sender = _msgSender();
require(
sender == getRequestOwner(requestId) || sender == owner(),
"Not an owner or request owner"
);
if (sender != getRequestOwner(requestId) && sender != owner()) {
revert NotAnOwnerOrRequestOwner(sender);
}
_;
}

Expand Down
27 changes: 27 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ const config: HardhatUserConfig = {
},
},
},
"contracts/test-helpers/RequestDisableableTestWrapper.sol": {
version: "0.8.27",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
"contracts/test-helpers/RequestOwnershipTestWrapper.sol": {
version: "0.8.27",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
"contracts/test-helpers/ValidatorWhitelistTestWrapper.sol": {
version: "0.8.27",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
},
},
networks: {
Expand Down
67 changes: 67 additions & 0 deletions test/verifier/requestDisableable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ethers } from "hardhat";
import { beforeEach } from "mocha";
import { DeployHelper } from "../../helpers/DeployHelper";
import { expect } from "chai";

describe("RequestDisableable tests", function () {
let verifier, validator: any;
let request, paramsFromValidator: any;

async function deployContractsFixture() {
const deployHelper = await DeployHelper.initialize(null, true);
const verifierLib = await ethers.deployContract("VerifierLib");
const verifier = await ethers.deployContract("RequestDisableableTestWrapper", [], {
libraries: { VerifierLib: await verifierLib.getAddress() },
});

const { state } = await deployHelper.deployStateWithLibraries([], "Groth16VerifierStub");
await verifier.initialize(await state.getAddress());

const validator = await ethers.deployContract("RequestValidatorStub");
return { verifier, validator };
}

beforeEach(async function () {
({ verifier, validator } = await deployContractsFixture());

request = {
requestId: 1,
metadata: "0x",
validator: await validator.getAddress(),
params: "0x",
};

paramsFromValidator = {
groupID: 0,
verifierID: 0,
nullifierSessionID: 0,
};
});

it("disable/enable request and onlyEnabledRequest modifier", async function () {
await validator.stub_setRequestParams([request.params], [paramsFromValidator]);

await verifier.setRequests([request]);

let isRequestEnabled = await verifier.isRequestEnabled(request.requestId);
expect(isRequestEnabled).to.be.true;

await expect(verifier.testModifier(request.requestId)).not.to.be.reverted;

await verifier.disableRequest(request.requestId);

isRequestEnabled = await verifier.isRequestEnabled(request.requestId);
expect(isRequestEnabled).to.be.false;

await expect(verifier.testModifier(request.requestId))
.to.be.revertedWithCustomError(verifier, "RequestIsDisabled")
.withArgs(request.requestId);

await verifier.enableRequest(request.requestId);

isRequestEnabled = await verifier.isRequestEnabled(request.requestId);
expect(isRequestEnabled).to.be.true;

await expect(verifier.testModifier(request.requestId)).not.to.be.reverted;
});
});
56 changes: 56 additions & 0 deletions test/verifier/requestOwnership.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ethers } from "hardhat";
import { beforeEach } from "mocha";
import { DeployHelper } from "../../helpers/DeployHelper";
import { expect } from "chai";

describe("RequestOwnership tests", function () {
let verifier, validator: any;
let request, paramsFromValidator: any;
let signer1, signer2: any;

async function deployContractsFixture() {
[signer1, signer2] = await ethers.getSigners();

const deployHelper = await DeployHelper.initialize(null, true);
const verifierLib = await ethers.deployContract("VerifierLib");
const verifier = await ethers.deployContract("RequestOwnershipTestWrapper", [], {
libraries: { VerifierLib: await verifierLib.getAddress() },
});

const { state } = await deployHelper.deployStateWithLibraries([], "Groth16VerifierStub");
await verifier.initialize(await state.getAddress());

const validator = await ethers.deployContract("RequestValidatorStub");
return { verifier, validator, signer1, signer2 };
}

beforeEach(async function () {
({ verifier, validator, signer1, signer2 } = await deployContractsFixture());

request = {
requestId: 1,
metadata: "0x",
validator: await validator.getAddress(),
params: "0x",
};

paramsFromValidator = {
groupID: 0,
verifierID: 0,
nullifierSessionID: 0,
};
});

it("setRequestOwner: change request ownership", async function () {
await validator.stub_setRequestParams([request.params], [paramsFromValidator]);
await verifier.setRequests([request]);

let owner = await verifier.getRequestOwner(request.requestId);
expect(owner).to.be.equal(await signer1.getAddress());

await verifier.setRequestOwner(request.requestId, await signer2.getAddress());

owner = await verifier.getRequestOwner(request.requestId);
expect(owner).to.be.equal(await signer2.getAddress());
});
});
27 changes: 15 additions & 12 deletions test/verifier/universal-verifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,17 @@ describe("Universal Verifier MTP & SIG validators", function () {
]);

expect(await verifier.getRequestOwner(requestId)).to.be.equal(requestOwnerAddr);
await expect(
verifier.connect(someSigner).setRequestOwner(requestId, someSigner),
).to.be.rejectedWith("Not an owner or request owner");
await expect(verifier.connect(someSigner).setRequestOwner(requestId, someSigner))
.to.be.revertedWithCustomError(verifier, "NotAnOwnerOrRequestOwner")
.withArgs(someSigner);

await verifier.connect(requestOwner).setRequestOwner(requestId, someSigner);
expect(await verifier.getRequestOwner(requestId)).to.be.equal(someSignerAddress);

await expect(
verifier.connect(requestOwner).setRequestOwner(requestId, requestOwnerAddr),
).to.be.rejectedWith("Not an owner or request owner");
await expect(verifier.connect(requestOwner).setRequestOwner(requestId, requestOwnerAddr))
.to.be.revertedWithCustomError(verifier, "NotAnOwnerOrRequestOwner")
.withArgs(requestOwner);

await verifier.connect(owner).setRequestOwner(requestId, requestOwnerAddr);
expect(await verifier.getRequestOwner(requestId)).to.be.equal(requestOwnerAddr);

Expand Down Expand Up @@ -273,17 +274,19 @@ describe("Universal Verifier MTP & SIG validators", function () {

expect(await verifier.isRequestEnabled(requestId)).to.be.true;

await expect(verifier.connect(someSigner).disableRequest(requestId)).to.be.rejectedWith(
"Not an owner or request owner",
);
await expect(verifier.connect(someSigner).disableRequest(requestId))
.to.be.revertedWithCustomError(verifier, "NotAnOwnerOrRequestOwner")
.withArgs(someSigner);

expect(await verifier.isRequestEnabled(requestId)).to.be.true;

await verifier.connect(owner).disableRequest(requestId);
expect(await verifier.isRequestEnabled(requestId)).to.be.false;

await expect(verifier.connect(someSigner).enableRequest(requestId)).to.be.rejectedWith(
"Not an owner or request owner",
);
await expect(verifier.connect(someSigner).enableRequest(requestId))
.to.be.revertedWithCustomError(verifier, "NotAnOwnerOrRequestOwner")
.withArgs(someSigner);

await verifier.connect(requestOwner).enableRequest(requestId);
expect(await verifier.isRequestEnabled(requestId)).to.be.true;

Expand Down
58 changes: 58 additions & 0 deletions test/verifier/validatorWhitelist.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ethers } from "hardhat";
import { beforeEach } from "mocha";
import { DeployHelper } from "../../helpers/DeployHelper";
import { expect } from "chai";

describe("ValidatorWhitelist tests", function () {
let verifier, validator: any;
let signer1, signer2: any;

async function deployContractsFixture() {
[signer1, signer2] = await ethers.getSigners();

const deployHelper = await DeployHelper.initialize(null, true);
const verifierLib = await ethers.deployContract("VerifierLib");
const verifier = await ethers.deployContract("ValidatorWhitelistTestWrapper", [], {
libraries: { VerifierLib: await verifierLib.getAddress() },
});

const { state } = await deployHelper.deployStateWithLibraries([], "Groth16VerifierStub");
await verifier.initialize(await state.getAddress());

const validator = await ethers.deployContract("RequestValidatorStub");
return { verifier, validator, signer1, signer2 };
}

beforeEach(async function () {
({ verifier, validator, signer1, signer2 } = await deployContractsFixture());
});

it("whitelist/remove Validators and modifier onlyWhitelistedValidator", async function () {
let isWhitelistedValidator = await verifier.isWhitelistedValidator(
await validator.getAddress(),
);
expect(isWhitelistedValidator).to.be.false;

await expect(verifier.testModifier(await validator.getAddress())).to.be.revertedWithCustomError(
verifier,
"ValidatorIsNotWhitelisted",
);

await verifier.addValidatorToWhitelist(await validator.getAddress());

await expect(verifier.testModifier(await validator.getAddress())).not.to.be.reverted;

isWhitelistedValidator = await verifier.isWhitelistedValidator(await validator.getAddress());
expect(isWhitelistedValidator).to.be.true;

await verifier.removeValidatorFromWhitelist(await validator.getAddress());

await expect(verifier.testModifier(await validator.getAddress())).to.be.revertedWithCustomError(
verifier,
"ValidatorIsNotWhitelisted",
);

isWhitelistedValidator = await verifier.isWhitelistedValidator(await validator.getAddress());
expect(isWhitelistedValidator).to.be.false;
});
});

0 comments on commit 39d67f5

Please sign in to comment.