-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test wrappers and tests for RequestDisableable, RequestOwnership …
…and validatorWhitelist
- Loading branch information
Showing
9 changed files
with
288 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |