Formatting

This commit is contained in:
Eric 2024-07-25 15:39:28 +10:00
parent 2b840dcc80
commit 280adc7c71
No known key found for this signature in database
7 changed files with 64 additions and 80 deletions

View File

@ -59,8 +59,10 @@ contract Marketplace is Proofs, Validation, StateRetrieval, Endian {
MarketplaceConfig memory configuration, MarketplaceConfig memory configuration,
IERC20 token_, IERC20 token_,
IGroth16Verifier verifier IGroth16Verifier verifier
) Proofs(configuration.proofs, verifier) )
Validation(configuration.validation) { Proofs(configuration.proofs, verifier)
Validation(configuration.validation)
{
_token = token_; _token = token_;
require( require(

View File

@ -20,7 +20,9 @@ contract StateRetrieval {
return _slotsPerHost[msg.sender].values().toSlotIds(); 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(); return _slotsPerValidator[groupIdx].values().toSlotIds();
} }

View File

@ -5,10 +5,7 @@ import "./Validation.sol";
import "./Requests.sol"; import "./Requests.sol";
contract TestValidation is Validation { 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) { function getValidatorIndex(SlotId slotId) public view returns (uint16) {
return _getValidatorIndex(slotId); return _getValidatorIndex(slotId);

View File

@ -19,9 +19,7 @@ abstract contract Validation {
* @param config network-level validator configuration used to determine * @param config network-level validator configuration used to determine
number of SlotIds per validator. number of SlotIds per validator.
*/ */
constructor( constructor(ValidationConfig memory config) {
ValidationConfig memory config
) {
require(config.validators > 0, "validators must be > 0"); require(config.validators > 0, "validators must be > 0");
uint256 high = type(uint256).max; uint256 high = type(uint256).max;
@ -47,7 +45,7 @@ abstract contract Validation {
on the number of total validators in the config. on the number of total validators in the config.
* @param slotId SlotID for which to determine the validator group index. * @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)); uint256 slotIdInt = uint256(SlotId.unwrap(slotId));
return uint16(slotIdInt / _idsPerValidator); return uint16(slotIdInt / _idsPerValidator);
} }

View File

@ -1129,15 +1129,10 @@ describe("Marketplace", function () {
let slot1 = { ...slot, index: slot.index + 1 } let slot1 = { ...slot, index: slot.index + 1 }
await token.approve(marketplace.address, request.ask.collateral) await token.approve(marketplace.address, request.ask.collateral)
await marketplace.fillSlot(slot.request, slot1.index, proof) await marketplace.fillSlot(slot.request, slot1.index, proof)
const allSlots = ( const allSlots = (await marketplace.validationSlots(0))
await marketplace.validationSlots(0)).concat( .concat(await marketplace.validationSlots(1))
await marketplace.validationSlots(1)).concat( .concat(await marketplace.validationSlots(2))
await marketplace.validationSlots(2) expect(allSlots).to.have.members([slotId(slot), slotId(slot1)])
)
expect(allSlots).to.have.members([
slotId(slot),
slotId(slot1),
])
}) })
it("removes slot from list when slot is freed", async function () { 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 marketplace.fillSlot(slot.request, slot1.index, proof)
await token.approve(marketplace.address, request.ask.collateral) await token.approve(marketplace.address, request.ask.collateral)
await marketplace.freeSlot(slotId(slot)) await marketplace.freeSlot(slotId(slot))
const allSlots = ( const allSlots = (await marketplace.validationSlots(0))
await marketplace.validationSlots(0)).concat( .concat(await marketplace.validationSlots(1))
await marketplace.validationSlots(1)).concat( .concat(await marketplace.validationSlots(2))
await marketplace.validationSlots(2)
)
expect(allSlots).to.not.have.members([slotId(slot)]) expect(allSlots).to.not.have.members([slotId(slot)])
expect(allSlots).to.have.members([slotId(slot1)]) expect(allSlots).to.have.members([slotId(slot1)])
}) })
@ -1164,26 +1157,19 @@ describe("Marketplace", function () {
await marketplace.fillSlot(slot.request, slot1.index, proof) await marketplace.fillSlot(slot.request, slot1.index, proof)
await waitUntilCancelled(request) await waitUntilCancelled(request)
await mine() await mine()
const allSlots = ( const allSlots = (await marketplace.validationSlots(0))
await marketplace.validationSlots(0)).concat( .concat(await marketplace.validationSlots(1))
await marketplace.validationSlots(1)).concat( .concat(await marketplace.validationSlots(2))
await marketplace.validationSlots(2) expect(allSlots).to.have.members([slotId(slot), slotId(slot1)])
)
expect(allSlots).to.have.members([
slotId(slot),
slotId(slot1),
])
}) })
it("removes slot when finished slot is freed", async function () { it("removes slot when finished slot is freed", async function () {
await waitUntilStarted(marketplace, request, proof, token) await waitUntilStarted(marketplace, request, proof, token)
await waitUntilFinished(marketplace, requestId(request)) await waitUntilFinished(marketplace, requestId(request))
await marketplace.freeSlot(slotId(slot)) await marketplace.freeSlot(slotId(slot))
const allSlots = ( const allSlots = (await marketplace.validationSlots(0))
await marketplace.validationSlots(0)).concat( .concat(await marketplace.validationSlots(1))
await marketplace.validationSlots(1)).concat( .concat(await marketplace.validationSlots(2))
await marketplace.validationSlots(2)
)
expect(allSlots).to.not.contain(slotId(slot)) expect(allSlots).to.not.contain(slotId(slot))
}) })
@ -1191,11 +1177,9 @@ describe("Marketplace", function () {
await marketplace.fillSlot(slot.request, slot.index, proof) await marketplace.fillSlot(slot.request, slot.index, proof)
await waitUntilCancelled(request) await waitUntilCancelled(request)
await marketplace.freeSlot(slotId(slot)) await marketplace.freeSlot(slotId(slot))
const allSlots = ( const allSlots = (await marketplace.validationSlots(0))
await marketplace.validationSlots(0)).concat( .concat(await marketplace.validationSlots(1))
await marketplace.validationSlots(1)).concat( .concat(await marketplace.validationSlots(2))
await marketplace.validationSlots(2)
)
expect(allSlots).to.not.contain(slotId(slot)) expect(allSlots).to.not.contain(slotId(slot))
}) })
@ -1203,11 +1187,9 @@ describe("Marketplace", function () {
await waitUntilStarted(marketplace, request, proof, token) await waitUntilStarted(marketplace, request, proof, token)
await waitUntilSlotFailed(marketplace, request, slot) await waitUntilSlotFailed(marketplace, request, slot)
await marketplace.freeSlot(slotId(slot)) await marketplace.freeSlot(slotId(slot))
const allSlots = ( const allSlots = (await marketplace.validationSlots(0))
await marketplace.validationSlots(0)).concat( .concat(await marketplace.validationSlots(1))
await marketplace.validationSlots(1)).concat( .concat(await marketplace.validationSlots(2))
await marketplace.validationSlots(2)
)
expect(allSlots).to.not.contain(slotId(slot)) expect(allSlots).to.not.contain(slotId(slot))
}) })
}) })

View File

@ -1,6 +1,6 @@
const { expect } = require("chai") const { expect } = require("chai")
const { ethers } = require("hardhat") const { ethers } = require("hardhat")
const {BigNumber, utils} = require("ethers") const { BigNumber, utils } = require("ethers")
describe("Validation", function () { describe("Validation", function () {
const zero = const zero =
@ -10,7 +10,7 @@ describe("Validation", function () {
const mid = const mid =
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
describe("constructor", function() { describe("constructor", function () {
// let validation // let validation
let Validation let Validation
@ -18,27 +18,24 @@ describe("Validation", function () {
Validation = await ethers.getContractFactory("TestValidation") 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( 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 ).to.be.reverted
}) })
it("fails to deploy with 0 number of validators", async function() { it("fails to deploy with 0 number of validators", async function () {
await expect( await expect(Validation.deploy({ validators: 0 })).to.be.revertedWith(
Validation.deploy({validators: 0}) "validators must be > 0"
).to.be.revertedWith("validators must be > 0") )
}) })
it("successfully deploys with a valid number of validators", async function() { it("successfully deploys with a valid number of validators", async function () {
await expect( await expect(Validation.deploy({ validators: 1 })).to.be.ok
Validation.deploy({validators: 1})
).to.be.ok
}) })
}) })
describe("groups of SlotIds per validator", function() { describe("groups of SlotIds per validator", function () {
let Validation let Validation
const high = ethers.constants.MaxUint256 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 () { 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 validators = 2 ** 16 - 1 // max value of uint16
let idsPerGroup = high.div( validators ).add(1) // as in the contract let idsPerGroup = high.div(validators).add(1) // as in the contract
let validation = await Validation.deploy({validators}) let validation = await Validation.deploy({ validators })
// Returns the minimum SlotId of all allowed SlotIds of the validator // Returns the minimum SlotId of all allowed SlotIds of the validator
// (given its index) // (given its index)
@ -68,7 +65,9 @@ describe("Validation", function () {
// Returns the maximum SlotId of all allowed SlotIds of the validator // Returns the maximum SlotId of all allowed SlotIds of the validator
// (given its index) // (given its index)
function maxIdFor(validatorIdx) { 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 // Never return more than max value of uint256 because it would
// overflow. BigNumber.js lets us do MaxUint256+1 without overflows. // overflow. BigNumber.js lets us do MaxUint256+1 without overflows.
if (max.gt(high)) { if (max.gt(high)) {
@ -80,25 +79,29 @@ describe("Validation", function () {
// Generate randomised number of validators. If we fuzzed all possible // Generate randomised number of validators. If we fuzzed all possible
// number of validators, the test would take far too long to execute. This // number of validators, the test would take far too long to execute. This
// should absolutely never fail. // 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<validatorsRandomised.length; i++) { for (let i = 0; i < validatorsRandomised.length; i++) {
let validatorIdx = validatorsRandomised[i] let validatorIdx = validatorsRandomised[i]
// test the boundary of the SlotIds that are allowed in this particular // test the boundary of the SlotIds that are allowed in this particular
// validator validatorIdx // validator validatorIdx
let min = toUInt256Hex( minIdFor(validatorIdx) ) let min = toUInt256Hex(minIdFor(validatorIdx))
let max = toUInt256Hex( maxIdFor(validatorIdx) ) let max = toUInt256Hex(maxIdFor(validatorIdx))
try{ try {
expect(await validation.getValidatorIndex(min)).to.equal(validatorIdx) expect(await validation.getValidatorIndex(min)).to.equal(validatorIdx)
expect(await validation.getValidatorIndex(max)).to.equal(validatorIdx) expect(await validation.getValidatorIndex(max)).to.equal(validatorIdx)
} catch(e) { } catch (e) {
console.log('FAILING TEST PARAMETERS') console.log("FAILING TEST PARAMETERS")
console.log('-----------------------------------------------------------------------------------') console.log(
console.log('validator index:', validatorIdx) "-----------------------------------------------------------------------------------"
console.log('slotId min: ', min) )
console.log('slotId max: ', max) console.log("validator index:", validatorIdx)
console.log("slotId min: ", min)
console.log("slotId max: ", max)
throw e throw e
} }
} }

View File

@ -16,8 +16,8 @@ const exampleConfiguration = () => ({
zkeyHash: "", zkeyHash: "",
}, },
validation: { validation: {
validators: 3 validators: 3,
} },
}) })
const exampleRequest = async () => { const exampleRequest = async () => {