diff --git a/packages/evm/test/Enclave.spec.ts b/packages/evm/test/Enclave.spec.ts index 4380a0ee..d4cae1a4 100644 --- a/packages/evm/test/Enclave.spec.ts +++ b/packages/evm/test/Enclave.spec.ts @@ -1,166 +1,164 @@ import { loadFixture, time } from "@nomicfoundation/hardhat-network-helpers"; import { expect } from "chai"; +import { ZeroHash } from "ethers"; import { ethers } from "hardhat"; import { deployEnclaveFixture } from "./fixtures/Enclave.fixture"; import { deployComputationModuleFixture } from "./fixtures/MockComputationModule.fixture"; -import { deployMockCypherNodeRegistryFixture } from "./fixtures/MockCypherNodeRegistry.fixture"; -import { deployMockExecutionModuleFixture } from "./fixtures/MockExecutionModule.fixture"; -import { deployMockInputValidatorFixture } from "./fixtures/MockInputValidator.fixture"; -import { deployMockOutputVerifierFixture } from "./fixtures/MockOutputVerifier.fixture"; +import { deployCypherNodeRegistryFixture } from "./fixtures/MockCypherNodeRegistry.fixture"; +import { deployExecutionModuleFixture } from "./fixtures/MockExecutionModule.fixture"; +import { deployInputValidatorFixture } from "./fixtures/MockInputValidator.fixture"; +import { deployOutputVerifierFixture } from "./fixtures/MockOutputVerifier.fixture"; const abiCoder = ethers.AbiCoder.defaultAbiCoder(); const AddressTwo = "0x0000000000000000000000000000000000000002"; describe("Enclave", function () { - beforeEach(async function () { - const enclave = await loadFixture(deployEnclaveFixture); - this.enclave = enclave; - - const computationModule = await loadFixture(deployComputationModuleFixture); - - const { mockOutputVerifier, mockOutputVerifier_address } = await loadFixture(deployMockOutputVerifierFixture); - this.mockOutputVerifier = mockOutputVerifier; - this.mockOutputVerifier_address = mockOutputVerifier_address; - - const { mockCypherNodeRegistry, mockCypherNodeRegistry_address } = await loadFixture( - deployMockCypherNodeRegistryFixture, - ); - this.mockCypherNodeRegistry = mockCypherNodeRegistry; - this.mockCypherNodeRegistry_address = mockCypherNodeRegistry_address; - - const { mockExecutionModule, mockExecutionModule_address } = await loadFixture(deployMockExecutionModuleFixture); - this.mockExecutionModule = mockExecutionModule; - this.mockExecutionModule_address = mockExecutionModule_address; - - const { mockInputValidator, mockInputValidator_address } = await loadFixture(deployMockInputValidatorFixture); - this.mockInputValidator = mockInputValidator; - this.mockInputValidator_address = mockInputValidator_address; - - await this.enclave.setCypherNodeRegistry(this.mockCypherNodeRegistry_address); - await this.enclave.enableComputationModule(await computationModule.getAddress()); - await this.enclave.enableExecutionModule(this.mockExecutionModule_address); - - this.requestParams = { - poolId: 1n, - threshold: [2n, 2n], - duration: time.duration.days(30), - computationModule: await computationModule.getAddress(), - cMParams: abiCoder.encode(["address"], [this.mockInputValidator_address]), - executionModule: this.mockExecutionModule_address, - eMParams: abiCoder.encode(["address"], [this.mockOutputVerifier_address]), + async function setup() { + const [owner, notTheOwner] = await ethers.getSigners(); + + const registry = await deployCypherNodeRegistryFixture(); + const computationModule = await deployComputationModuleFixture(); + const outputVerifier = await deployOutputVerifierFixture(); + const executionModule = await deployExecutionModuleFixture(); + const inputValidator = await deployInputValidatorFixture(); + + const enclave = await deployEnclaveFixture({ owner, registry: await registry.getAddress() }); + + await enclave.enableComputationModule(await computationModule.getAddress()); + await enclave.enableExecutionModule(await executionModule.getAddress()); + + return { + owner, + notTheOwner, + enclave, + mocks: { computationModule, outputVerifier, executionModule, inputValidator, registry }, + request: { + poolId: 1n, + threshold: [2, 2] as [number, number], + duration: time.duration.days(30), + computationModule: await computationModule.getAddress(), + cMParams: abiCoder.encode(["address"], [await inputValidator.getAddress()]), + executionModule: await executionModule.getAddress(), + eMParams: abiCoder.encode(["address"], [await outputVerifier.getAddress()]), + }, }; - }); + } describe("constructor / initialize()", function () { it("correctly sets owner", async function () { - const owner1 = await this.enclave.owner(); - const [, , , otherOwner, otherRegistry] = await ethers.getSigners(); - - const enclave = await deployEnclaveFixture({ owner: otherOwner, registry: otherRegistry }); - - // expect the owner to be the same as the one set in the fixture - expect(owner1).to.not.equal(otherOwner); - // expect the owner to be the same as the one set in the new deployment - expect(await enclave.owner()).to.equal(otherOwner); - expect(await enclave.cypherNodeRegistry()).to.equal(otherRegistry); + const [, , , someSigner] = await ethers.getSigners(); + const enclave = await deployEnclaveFixture({ owner: someSigner, registry: AddressTwo }); + expect(await enclave.cypherNodeRegistry()).to.equal(AddressTwo); }); it("correctly sets cypherNodeRegistry address", async function () { - const cypherNodeRegistry = await this.enclave.cypherNodeRegistry(); - - expect(cypherNodeRegistry).to.equal(this.mockCypherNodeRegistry_address); + const [aSigner] = await ethers.getSigners(); + const enclave = await deployEnclaveFixture({ owner: aSigner, registry: AddressTwo }); + expect(await enclave.cypherNodeRegistry()).to.equal(AddressTwo); }); it("correctly sets max duration", async function () { - const enclave = await deployEnclaveFixture({ maxDuration: 9876 }); + const [aSigner] = await ethers.getSigners(); + const enclave = await deployEnclaveFixture({ owner: aSigner, registry: AddressTwo, maxDuration: 9876 }); expect(await enclave.maxDuration()).to.equal(9876); }); }); describe("setMaxDuration()", function () { it("reverts if not called by owner", async function () { - const [, , , notTheOwner] = await ethers.getSigners(); - await expect(this.enclave.connect(notTheOwner).setMaxDuration(1)) - .to.be.revertedWithCustomError(this.enclave, "OwnableUnauthorizedAccount") + const { enclave, notTheOwner } = await loadFixture(setup); + await expect(enclave.connect(notTheOwner).setMaxDuration(1)) + .to.be.revertedWithCustomError(enclave, "OwnableUnauthorizedAccount") .withArgs(notTheOwner); }); it("set max duration correctly", async function () { - await this.enclave.setMaxDuration(1); - - const maxDuration = await this.enclave.maxDuration(); - expect(maxDuration).to.equal(1); + const { enclave } = await loadFixture(setup); + await enclave.setMaxDuration(1); + expect(await enclave.maxDuration()).to.equal(1); }); it("returns true if max duration is set successfully", async function () { - const result = await this.enclave.setMaxDuration.staticCall(1); - + const { enclave } = await loadFixture(setup); + const result = await enclave.setMaxDuration.staticCall(1); expect(result).to.be.true; }); it("emits MaxDurationSet event", async function () { - await expect(this.enclave.setMaxDuration(1)).to.emit(this.enclave, "MaxDurationSet").withArgs(1); + const { enclave } = await loadFixture(setup); + await expect(enclave.setMaxDuration(1)).to.emit(enclave, "MaxDurationSet").withArgs(1); }); }); describe("setCypherNodeRegistry()", function () { it("reverts if not called by owner", async function () { - const [, , , notTheOwner] = await ethers.getSigners(); + const { enclave, notTheOwner } = await loadFixture(setup); - await expect(this.enclave.connect(notTheOwner).setCypherNodeRegistry(AddressTwo)) - .to.be.revertedWithCustomError(this.enclave, "OwnableUnauthorizedAccount") + await expect(enclave.connect(notTheOwner).setCypherNodeRegistry(AddressTwo)) + .to.be.revertedWithCustomError(enclave, "OwnableUnauthorizedAccount") .withArgs(notTheOwner); }); it("reverts if given address(0)", async function () { - await expect(this.enclave.setCypherNodeRegistry(ethers.ZeroAddress)) - .to.be.revertedWithCustomError(this.enclave, "InvalidCypherNodeRegistry") + const { enclave } = await loadFixture(setup); + await expect(enclave.setCypherNodeRegistry(ethers.ZeroAddress)) + .to.be.revertedWithCustomError(enclave, "InvalidCypherNodeRegistry") .withArgs(ethers.ZeroAddress); }); it("reverts if given address is the same as the current cypherNodeRegistry", async function () { - await expect(this.enclave.setCypherNodeRegistry(this.mockCypherNodeRegistry_address)) - .to.be.revertedWithCustomError(this.enclave, "InvalidCypherNodeRegistry") - .withArgs(this.mockCypherNodeRegistry_address); + const { + enclave, + mocks: { registry }, + } = await loadFixture(setup); + await expect(enclave.setCypherNodeRegistry(registry)) + .to.be.revertedWithCustomError(enclave, "InvalidCypherNodeRegistry") + .withArgs(registry); }); it("sets cypherNodeRegistry correctly", async function () { - expect(await this.enclave.cypherNodeRegistry()).to.not.equal(AddressTwo); - - await this.enclave.setCypherNodeRegistry(AddressTwo); + const { enclave } = await loadFixture(setup); - expect(await this.enclave.cypherNodeRegistry()).to.equal(AddressTwo); + expect(await enclave.cypherNodeRegistry()).to.not.equal(AddressTwo); + await enclave.setCypherNodeRegistry(AddressTwo); + expect(await enclave.cypherNodeRegistry()).to.equal(AddressTwo); }); it("returns true if cypherNodeRegistry is set successfully", async function () { - const result = await this.enclave.setCypherNodeRegistry.staticCall(AddressTwo); + const { enclave } = await loadFixture(setup); + const result = await enclave.setCypherNodeRegistry.staticCall(AddressTwo); expect(result).to.be.true; }); it("emits CypherNodeRegistrySet event", async function () { - await expect(this.enclave.setCypherNodeRegistry(AddressTwo)) - .to.emit(this.enclave, "CypherNodeRegistrySet") + const { enclave } = await loadFixture(setup); + + await expect(enclave.setCypherNodeRegistry(AddressTwo)) + .to.emit(enclave, "CypherNodeRegistrySet") .withArgs(AddressTwo); }); }); describe("getE3()", function () { it("reverts if E3 does not exist", async function () { - await expect(this.enclave.getE3(1)).to.be.revertedWithCustomError(this.enclave, "E3DoesNotExist").withArgs(1); + const { enclave } = await loadFixture(setup); + + await expect(enclave.getE3(1)).to.be.revertedWithCustomError(enclave, "E3DoesNotExist").withArgs(1); }); it("returns correct E3 details", async function () { - await this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, - this.requestParams.duration, - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + const { enclave, request } = await loadFixture(setup); + await enclave.request( + request.poolId, + request.threshold, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, { value: 10 }, ); - const e3 = await this.enclave.getE3(0); + const e3 = await enclave.getE3(0); - expect(e3.threshold).to.deep.equal(this.requestParams.threshold); + expect(e3.threshold).to.deep.equal(request.threshold); expect(e3.expiration).to.equal(0n); - expect(e3.computationModule).to.equal(this.requestParams.computationModule); - expect(e3.inputValidator).to.equal(abiCoder.decode(["address"], this.requestParams.cMParams)[0]); - expect(e3.executionModule).to.equal(this.requestParams.executionModule); - expect(e3.outputVerifier).to.equal(abiCoder.decode(["address"], this.requestParams.eMParams)[0]); + expect(e3.computationModule).to.equal(request.computationModule); + expect(e3.inputValidator).to.equal(abiCoder.decode(["address"], request.cMParams)[0]); + expect(e3.executionModule).to.equal(request.executionModule); + expect(e3.outputVerifier).to.equal(abiCoder.decode(["address"], request.eMParams)[0]); expect(e3.committeePublicKey).to.equal("0x"); expect(e3.ciphertextOutput).to.equal("0x"); expect(e3.plaintextOutput).to.equal("0x"); @@ -169,314 +167,377 @@ describe("Enclave", function () { describe("enableComputationModule()", function () { it("reverts if not called by owner", async function () { - const [, , , notTheOwner] = await ethers.getSigners(); - const { computationModule } = this.requestParams; - await expect(this.enclave.connect(notTheOwner).enableComputationModule(computationModule)) - .to.be.revertedWithCustomError(this.enclave, "OwnableUnauthorizedAccount") + const { + notTheOwner, + enclave, + mocks: { computationModule }, + } = await loadFixture(setup); + + await expect(enclave.connect(notTheOwner).enableComputationModule(computationModule)) + .to.be.revertedWithCustomError(enclave, "OwnableUnauthorizedAccount") .withArgs(notTheOwner); }); it("reverts if computation module is already enabled", async function () { - const { computationModule } = this.requestParams; - await expect(this.enclave.enableComputationModule(computationModule)) - .to.be.revertedWithCustomError(this.enclave, "ModuleAlreadyEnabled") + const { + enclave, + mocks: { computationModule }, + } = await loadFixture(setup); + + await expect(enclave.enableComputationModule(computationModule)) + .to.be.revertedWithCustomError(enclave, "ModuleAlreadyEnabled") .withArgs(computationModule); }); it("enables computation module correctly", async function () { - const { computationModule } = this.requestParams; - const enabled = await this.enclave.computationModules(computationModule); + const { + enclave, + mocks: { computationModule }, + } = await loadFixture(setup); + const enabled = await enclave.computationModules(computationModule); expect(enabled).to.be.true; }); it("returns true if computation module is enabled successfully", async function () { - const result = await this.enclave.enableComputationModule.staticCall(AddressTwo); + const { enclave } = await loadFixture(setup); + const result = await enclave.enableComputationModule.staticCall(AddressTwo); expect(result).to.be.true; }); it("emits ComputationModuleEnabled event", async function () { - await expect(this.enclave.enableComputationModule(AddressTwo)) - .to.emit(this.enclave, "ComputationModuleEnabled") + const { enclave } = await loadFixture(setup); + await expect(enclave.enableComputationModule(AddressTwo)) + .to.emit(enclave, "ComputationModuleEnabled") .withArgs(AddressTwo); }); }); describe("disableComputationModule()", function () { it("reverts if not called by owner", async function () { - const [, , , notTheOwner] = await ethers.getSigners(); - const { computationModule } = this.requestParams; - await expect(this.enclave.connect(notTheOwner).disableComputationModule(computationModule)) - .to.be.revertedWithCustomError(this.enclave, "OwnableUnauthorizedAccount") + const { + notTheOwner, + enclave, + mocks: { computationModule }, + } = await loadFixture(setup); + await expect(enclave.connect(notTheOwner).disableComputationModule(computationModule)) + .to.be.revertedWithCustomError(enclave, "OwnableUnauthorizedAccount") .withArgs(notTheOwner); }); it("reverts if computation module is not enabled", async function () { - await expect(this.enclave.disableComputationModule(AddressTwo)) - .to.be.revertedWithCustomError(this.enclave, "ModuleNotEnabled") + const { enclave } = await loadFixture(setup); + await expect(enclave.disableComputationModule(AddressTwo)) + .to.be.revertedWithCustomError(enclave, "ModuleNotEnabled") .withArgs(AddressTwo); }); it("disables computation module correctly", async function () { - const { computationModule } = this.requestParams; - await this.enclave.disableComputationModule(computationModule); + const { + enclave, + mocks: { computationModule }, + } = await loadFixture(setup); + await enclave.disableComputationModule(computationModule); - const enabled = await this.enclave.computationModules(computationModule); + const enabled = await enclave.computationModules(computationModule); expect(enabled).to.be.false; }); it("returns true if computation module is disabled successfully", async function () { - const { computationModule } = this.requestParams; - const result = await this.enclave.disableComputationModule.staticCall(computationModule); + const { + enclave, + mocks: { computationModule }, + } = await loadFixture(setup); + const result = await enclave.disableComputationModule.staticCall(computationModule); expect(result).to.be.true; }); it("emits ComputationModuleDisabled event", async function () { - const { computationModule } = this.requestParams; - await expect(this.enclave.disableComputationModule(computationModule)) - .to.emit(this.enclave, "ComputationModuleDisabled") + const { + enclave, + mocks: { computationModule }, + } = await loadFixture(setup); + await expect(enclave.disableComputationModule(computationModule)) + .to.emit(enclave, "ComputationModuleDisabled") .withArgs(computationModule); }); }); describe("enableExecutionModule()", function () { it("reverts if not called by owner", async function () { - const [, , , notTheOwner] = await ethers.getSigners(); - await expect(this.enclave.connect(notTheOwner).enableExecutionModule(this.mockExecutionModule_address)) - .to.be.revertedWithCustomError(this.enclave, "OwnableUnauthorizedAccount") + const { notTheOwner, enclave } = await loadFixture(setup); + await expect(enclave.connect(notTheOwner).enableExecutionModule(AddressTwo)) + .to.be.revertedWithCustomError(enclave, "OwnableUnauthorizedAccount") .withArgs(notTheOwner.address); }); it("reverts if execution module is already enabled", async function () { - await expect(this.enclave.enableExecutionModule(this.mockExecutionModule_address)) - .to.be.revertedWithCustomError(this.enclave, "ModuleAlreadyEnabled") - .withArgs(this.mockExecutionModule_address); + const { + enclave, + mocks: { executionModule }, + } = await loadFixture(setup); + await expect(enclave.enableExecutionModule(executionModule)) + .to.be.revertedWithCustomError(enclave, "ModuleAlreadyEnabled") + .withArgs(executionModule); }); it("enables execution module correctly", async function () { - const enabled = await this.enclave.executionModules(this.mockExecutionModule_address); + const { + enclave, + mocks: { executionModule }, + } = await loadFixture(setup); + const enabled = await enclave.executionModules(executionModule); expect(enabled).to.be.true; }); it("returns true if execution module is enabled successfully", async function () { - const result = await this.enclave.enableExecutionModule.staticCall(AddressTwo); + const { enclave } = await loadFixture(setup); + const result = await enclave.enableExecutionModule.staticCall(AddressTwo); expect(result).to.be.true; }); it("emits ExecutionModuleEnabled event", async function () { - await expect(this.enclave.enableExecutionModule(AddressTwo)) - .to.emit(this.enclave, "ExecutionModuleEnabled") + const { enclave } = await loadFixture(setup); + await expect(enclave.enableExecutionModule(AddressTwo)) + .to.emit(enclave, "ExecutionModuleEnabled") .withArgs(AddressTwo); }); }); describe("disableExecutionModule()", function () { it("reverts if not called by owner", async function () { - const [, , , notTheOwner] = await ethers.getSigners(); - - await expect(this.enclave.connect(notTheOwner).disableExecutionModule(this.mockExecutionModule_address)) - .to.be.revertedWithCustomError(this.enclave, "OwnableUnauthorizedAccount") + const { + notTheOwner, + enclave, + mocks: { executionModule }, + } = await loadFixture(setup); + + await expect(enclave.connect(notTheOwner).disableExecutionModule(executionModule)) + .to.be.revertedWithCustomError(enclave, "OwnableUnauthorizedAccount") .withArgs(notTheOwner); }); it("reverts if execution module is not enabled", async function () { - await expect(this.enclave.disableExecutionModule(AddressTwo)) - .to.be.revertedWithCustomError(this.enclave, "ModuleNotEnabled") + const { enclave } = await loadFixture(setup); + await expect(enclave.disableExecutionModule(AddressTwo)) + .to.be.revertedWithCustomError(enclave, "ModuleNotEnabled") .withArgs(AddressTwo); }); it("disables execution module correctly", async function () { - await this.enclave.disableExecutionModule(this.mockExecutionModule_address); + const { + enclave, + mocks: { executionModule }, + } = await loadFixture(setup); - const enabled = await this.enclave.executionModules(this.mockExecutionModule_address); - expect(enabled).to.be.false; + expect(await enclave.executionModules(executionModule)).to.be.true; + await enclave.disableExecutionModule(executionModule); + expect(await enclave.executionModules(executionModule)).to.be.false; }); it("returns true if execution module is disabled successfully", async function () { - const result = await this.enclave.disableExecutionModule.staticCall(this.mockExecutionModule_address); + const { + enclave, + mocks: { executionModule }, + } = await loadFixture(setup); + const result = await enclave.disableExecutionModule.staticCall(executionModule); expect(result).to.be.true; }); it("emits ExecutionModuleDisabled event", async function () { - await expect(this.enclave.disableExecutionModule(this.mockExecutionModule_address)) - .to.emit(this.enclave, "ExecutionModuleDisabled") - .withArgs(this.mockExecutionModule_address); + const { + enclave, + mocks: { executionModule }, + } = await loadFixture(setup); + + await expect(enclave.disableExecutionModule(executionModule)) + .to.emit(enclave, "ExecutionModuleDisabled") + .withArgs(executionModule); }); }); describe("request()", function () { it("reverts if msg.value is 0", async function () { + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, - this.requestParams.duration, - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + enclave.request( + request.poolId, + request.threshold, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, ), - ).to.be.revertedWithCustomError(this.enclave, "PaymentRequired"); + ).to.be.revertedWithCustomError(enclave, "PaymentRequired"); }); it("reverts if threshold is 0", async function () { + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( - this.requestParams.poolId, + enclave.request( + request.poolId, [0, 2], - this.requestParams.duration, - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, { value: 10 }, ), - ).to.be.revertedWithCustomError(this.enclave, "InvalidThreshold"); + ).to.be.revertedWithCustomError(enclave, "InvalidThreshold"); }); it("reverts if threshold is greater than number", async function () { + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( - this.requestParams.poolId, + enclave.request( + request.poolId, [3, 2], - this.requestParams.duration, - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, { value: 10 }, ), - ).to.be.revertedWithCustomError(this.enclave, "InvalidThreshold"); + ).to.be.revertedWithCustomError(enclave, "InvalidThreshold"); }); it("reverts if duration is 0", async function () { + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, + enclave.request( + request.poolId, + request.threshold, 0, - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, { value: 10 }, ), - ).to.be.revertedWithCustomError(this.enclave, "InvalidDuration"); + ).to.be.revertedWithCustomError(enclave, "InvalidDuration"); }); it("reverts if duration is greater than maxDuration", async function () { + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, + enclave.request( + request.poolId, + request.threshold, time.duration.days(31), - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, { value: 10 }, ), - ).to.be.revertedWithCustomError(this.enclave, "InvalidDuration"); + ).to.be.revertedWithCustomError(enclave, "InvalidDuration"); }); it("reverts if computation module is not enabled", async function () { + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, - this.requestParams.duration, + enclave.request( + request.poolId, + request.threshold, + request.duration, ethers.ZeroAddress, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + request.cMParams, + request.executionModule, + request.eMParams, { value: 10 }, ), ) - .to.be.revertedWithCustomError(this.enclave, "ComputationModuleNotAllowed") + .to.be.revertedWithCustomError(enclave, "ComputationModuleNotAllowed") .withArgs(ethers.ZeroAddress); }); it("reverts if execution module is not enabled", async function () { + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, - this.requestParams.duration, - this.requestParams.computationModule, - this.requestParams.cMParams, + enclave.request( + request.poolId, + request.threshold, + request.duration, + request.computationModule, + request.cMParams, ethers.ZeroAddress, - this.requestParams.eMParams, + request.eMParams, { value: 10 }, ), ) - .to.be.revertedWithCustomError(this.enclave, "ModuleNotEnabled") + .to.be.revertedWithCustomError(enclave, "ModuleNotEnabled") .withArgs(ethers.ZeroAddress); }); it("reverts if input computation module does not return input validator address", async function () { - const zeroInput = abiCoder.encode(["address"], [ethers.ZeroAddress]); + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, - this.requestParams.duration, - this.requestParams.computationModule, - zeroInput, - this.requestParams.executionModule, - this.requestParams.eMParams, + enclave.request( + request.poolId, + request.threshold, + request.duration, + request.computationModule, + ZeroHash, + request.executionModule, + request.eMParams, { value: 10 }, ), - ).to.be.revertedWithCustomError(this.enclave, "InvalidComputation"); + ).to.be.revertedWithCustomError(enclave, "InvalidComputation"); }); it("reverts if input execution module does not return output verifier address", async function () { - const zeroInput = abiCoder.encode(["address"], [ethers.ZeroAddress]); - + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, - this.requestParams.duration, - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - zeroInput, + enclave.request( + request.poolId, + request.threshold, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + ZeroHash, { value: 10 }, ), - ).to.be.revertedWithCustomError(this.enclave, "InvalidExecutionModuleSetup"); + ).to.be.revertedWithCustomError(enclave, "InvalidExecutionModuleSetup"); }); it("reverts if committee selection fails", async function () { + const { enclave, request } = await loadFixture(setup); await expect( - this.enclave.request( + enclave.request( 0, - this.requestParams.threshold, - this.requestParams.duration, - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + request.threshold, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, { value: 10 }, ), - ).to.be.revertedWithCustomError(this.enclave, "CommitteeSelectionFailed"); + ).to.be.revertedWithCustomError(enclave, "CommitteeSelectionFailed"); }); it("instantiates a new E3", async function () { - await this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, - this.requestParams.duration, - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + const { enclave, request } = await loadFixture(setup); + await enclave.request( + request.poolId, + request.threshold, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, { value: 10 }, ); - const e3 = await this.enclave.getE3(0); + const e3 = await enclave.getE3(0); - expect(e3.threshold).to.deep.equal(this.requestParams.threshold); + expect(e3.threshold).to.deep.equal(request.threshold); expect(e3.expiration).to.equal(0n); - expect(e3.computationModule).to.equal(this.requestParams.computationModule); - expect(e3.inputValidator).to.equal(abiCoder.decode(["address"], this.requestParams.cMParams)[0]); - expect(e3.executionModule).to.equal(this.requestParams.executionModule); - expect(e3.outputVerifier).to.equal(abiCoder.decode(["address"], this.requestParams.eMParams)[0]); + expect(e3.computationModule).to.equal(request.computationModule); + expect(e3.inputValidator).to.equal(abiCoder.decode(["address"], request.cMParams)[0]); + expect(e3.executionModule).to.equal(request.executionModule); + expect(e3.outputVerifier).to.equal(abiCoder.decode(["address"], request.eMParams)[0]); expect(e3.committeePublicKey).to.equal("0x"); expect(e3.ciphertextOutput).to.equal("0x"); expect(e3.plaintextOutput).to.equal("0x"); }); it("emits E3Requested event", async function () { - const tx = await this.enclave.request( - this.requestParams.poolId, - this.requestParams.threshold, - this.requestParams.duration, - this.requestParams.computationModule, - this.requestParams.cMParams, - this.requestParams.executionModule, - this.requestParams.eMParams, + const { enclave, request } = await loadFixture(setup); + const tx = await enclave.request( + request.poolId, + request.threshold, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, { value: 10 }, ); - const e3 = await this.enclave.getE3(0); + const e3 = await enclave.getE3(0); await expect(tx) - .to.emit(this.enclave, "E3Requested") - .withArgs(0, e3, 1, this.requestParams.computationModule, this.requestParams.executionModule); + .to.emit(enclave, "E3Requested") + .withArgs(0, e3, 1, request.computationModule, request.executionModule); }); });