diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index cc0e91e..ae01a31 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -59,8 +59,10 @@ contract Marketplace is Proofs, Validation, StateRetrieval, Endian { MarketplaceConfig memory configuration, IERC20 token_, IGroth16Verifier verifier - ) Proofs(configuration.proofs, verifier) - Validation(configuration.validation) { + ) + Proofs(configuration.proofs, verifier) + Validation(configuration.validation) + { _token = token_; require( diff --git a/contracts/StateRetrieval.sol b/contracts/StateRetrieval.sol index 3fcf5b9..6c72e0b 100644 --- a/contracts/StateRetrieval.sol +++ b/contracts/StateRetrieval.sol @@ -20,7 +20,9 @@ contract StateRetrieval { return _slotsPerHost[msg.sender].values().toSlotIds(); } - function validationSlots(uint16 groupIdx) public view returns (SlotId[] memory) { + function validationSlots( + uint16 groupIdx + ) public view returns (SlotId[] memory) { return _slotsPerValidator[groupIdx].values().toSlotIds(); } diff --git a/contracts/TestValidation.sol b/contracts/TestValidation.sol index c10c50a..4d5092a 100644 --- a/contracts/TestValidation.sol +++ b/contracts/TestValidation.sol @@ -5,10 +5,7 @@ import "./Validation.sol"; import "./Requests.sol"; contract TestValidation is Validation { - - constructor( - ValidationConfig memory config - ) Validation(config) {} // solhint-disable-line no-empty-blocks + constructor(ValidationConfig memory config) Validation(config) {} // solhint-disable-line no-empty-blocks function getValidatorIndex(SlotId slotId) public view returns (uint16) { return _getValidatorIndex(slotId); diff --git a/contracts/Validation.sol b/contracts/Validation.sol index 8cf0625..7052651 100644 --- a/contracts/Validation.sol +++ b/contracts/Validation.sol @@ -19,9 +19,7 @@ abstract contract Validation { * @param config network-level validator configuration used to determine number of SlotIds per validator. */ - constructor( - ValidationConfig memory config - ) { + constructor(ValidationConfig memory config) { require(config.validators > 0, "validators must be > 0"); uint256 high = type(uint256).max; @@ -47,7 +45,7 @@ abstract contract Validation { on the number of total validators in the config. * @param slotId SlotID for which to determine the validator group index. */ - function _getValidatorIndex(SlotId slotId) internal view returns(uint16) { + function _getValidatorIndex(SlotId slotId) internal view returns (uint16) { uint256 slotIdInt = uint256(SlotId.unwrap(slotId)); return uint16(slotIdInt / _idsPerValidator); } diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index 069af6e..91356b3 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -1129,15 +1129,10 @@ describe("Marketplace", function () { let slot1 = { ...slot, index: slot.index + 1 } await token.approve(marketplace.address, request.ask.collateral) await marketplace.fillSlot(slot.request, slot1.index, proof) - const allSlots = ( - await marketplace.validationSlots(0)).concat( - await marketplace.validationSlots(1)).concat( - await marketplace.validationSlots(2) - ) - expect(allSlots).to.have.members([ - slotId(slot), - slotId(slot1), - ]) + const allSlots = (await marketplace.validationSlots(0)) + .concat(await marketplace.validationSlots(1)) + .concat(await marketplace.validationSlots(2)) + expect(allSlots).to.have.members([slotId(slot), slotId(slot1)]) }) it("removes slot from list when slot is freed", async function () { @@ -1147,11 +1142,9 @@ describe("Marketplace", function () { await marketplace.fillSlot(slot.request, slot1.index, proof) await token.approve(marketplace.address, request.ask.collateral) await marketplace.freeSlot(slotId(slot)) - const allSlots = ( - await marketplace.validationSlots(0)).concat( - await marketplace.validationSlots(1)).concat( - await marketplace.validationSlots(2) - ) + const allSlots = (await marketplace.validationSlots(0)) + .concat(await marketplace.validationSlots(1)) + .concat(await marketplace.validationSlots(2)) expect(allSlots).to.not.have.members([slotId(slot)]) expect(allSlots).to.have.members([slotId(slot1)]) }) @@ -1164,26 +1157,19 @@ describe("Marketplace", function () { await marketplace.fillSlot(slot.request, slot1.index, proof) await waitUntilCancelled(request) await mine() - const allSlots = ( - await marketplace.validationSlots(0)).concat( - await marketplace.validationSlots(1)).concat( - await marketplace.validationSlots(2) - ) - expect(allSlots).to.have.members([ - slotId(slot), - slotId(slot1), - ]) + const allSlots = (await marketplace.validationSlots(0)) + .concat(await marketplace.validationSlots(1)) + .concat(await marketplace.validationSlots(2)) + expect(allSlots).to.have.members([slotId(slot), slotId(slot1)]) }) it("removes slot when finished slot is freed", async function () { await waitUntilStarted(marketplace, request, proof, token) await waitUntilFinished(marketplace, requestId(request)) await marketplace.freeSlot(slotId(slot)) - const allSlots = ( - await marketplace.validationSlots(0)).concat( - await marketplace.validationSlots(1)).concat( - await marketplace.validationSlots(2) - ) + const allSlots = (await marketplace.validationSlots(0)) + .concat(await marketplace.validationSlots(1)) + .concat(await marketplace.validationSlots(2)) expect(allSlots).to.not.contain(slotId(slot)) }) @@ -1191,11 +1177,9 @@ describe("Marketplace", function () { await marketplace.fillSlot(slot.request, slot.index, proof) await waitUntilCancelled(request) await marketplace.freeSlot(slotId(slot)) - const allSlots = ( - await marketplace.validationSlots(0)).concat( - await marketplace.validationSlots(1)).concat( - await marketplace.validationSlots(2) - ) + const allSlots = (await marketplace.validationSlots(0)) + .concat(await marketplace.validationSlots(1)) + .concat(await marketplace.validationSlots(2)) expect(allSlots).to.not.contain(slotId(slot)) }) @@ -1203,11 +1187,9 @@ describe("Marketplace", function () { await waitUntilStarted(marketplace, request, proof, token) await waitUntilSlotFailed(marketplace, request, slot) await marketplace.freeSlot(slotId(slot)) - const allSlots = ( - await marketplace.validationSlots(0)).concat( - await marketplace.validationSlots(1)).concat( - await marketplace.validationSlots(2) - ) + const allSlots = (await marketplace.validationSlots(0)) + .concat(await marketplace.validationSlots(1)) + .concat(await marketplace.validationSlots(2)) expect(allSlots).to.not.contain(slotId(slot)) }) }) diff --git a/test/Validation.test.js b/test/Validation.test.js index 6d4ed4e..8c4dee4 100644 --- a/test/Validation.test.js +++ b/test/Validation.test.js @@ -1,6 +1,6 @@ const { expect } = require("chai") const { ethers } = require("hardhat") -const {BigNumber, utils} = require("ethers") +const { BigNumber, utils } = require("ethers") describe("Validation", function () { const zero = @@ -10,7 +10,7 @@ describe("Validation", function () { const mid = "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" - describe("constructor", function() { + describe("constructor", function () { // let validation let Validation @@ -18,27 +18,24 @@ describe("Validation", function () { Validation = await ethers.getContractFactory("TestValidation") }) - it("fails to deploy with > uint16.max validators", async function() { + it("fails to deploy with > uint16.max validators", async function () { await expect( - Validation.deploy({validators: 2**16}) // uint16.max is 2^16-1 + Validation.deploy({ validators: 2 ** 16 }) // uint16.max is 2^16-1 ).to.be.reverted }) - it("fails to deploy with 0 number of validators", async function() { - await expect( - Validation.deploy({validators: 0}) - ).to.be.revertedWith("validators must be > 0") + it("fails to deploy with 0 number of validators", async function () { + await expect(Validation.deploy({ validators: 0 })).to.be.revertedWith( + "validators must be > 0" + ) }) - it("successfully deploys with a valid number of validators", async function() { - await expect( - Validation.deploy({validators: 1}) - ).to.be.ok + it("successfully deploys with a valid number of validators", async function () { + await expect(Validation.deploy({ validators: 1 })).to.be.ok }) }) - describe("groups of SlotIds per validator", function() { - + describe("groups of SlotIds per validator", function () { let Validation const high = ethers.constants.MaxUint256 @@ -56,9 +53,9 @@ describe("Validation", function () { }) it("tests that the min and max boundary SlotIds into the correct group", async function () { - let validators = 2**16-1 // max value of uint16 - let idsPerGroup = high.div( validators ).add(1) // as in the contract - let validation = await Validation.deploy({validators}) + let validators = 2 ** 16 - 1 // max value of uint16 + let idsPerGroup = high.div(validators).add(1) // as in the contract + let validation = await Validation.deploy({ validators }) // Returns the minimum SlotId of all allowed SlotIds of the validator // (given its index) @@ -68,7 +65,9 @@ describe("Validation", function () { // Returns the maximum SlotId of all allowed SlotIds of the validator // (given its index) function maxIdFor(validatorIdx) { - const max = BigNumber.from(validatorIdx + 1).mul(idsPerGroup).sub(1) + const max = BigNumber.from(validatorIdx + 1) + .mul(idsPerGroup) + .sub(1) // Never return more than max value of uint256 because it would // overflow. BigNumber.js lets us do MaxUint256+1 without overflows. if (max.gt(high)) { @@ -80,25 +79,29 @@ describe("Validation", function () { // Generate randomised number of validators. If we fuzzed all possible // number of validators, the test would take far too long to execute. This // should absolutely never fail. - let validatorsRandomised = Array.from({ length: 128 }, (_) => random(validators)) + let validatorsRandomised = Array.from({ length: 128 }, (_) => + random(validators) + ) - for(let i=0; i ({ zkeyHash: "", }, validation: { - validators: 3 - } + validators: 3, + }, }) const exampleRequest = async () => {