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,
IERC20 token_,
IGroth16Verifier verifier
) Proofs(configuration.proofs, verifier)
Validation(configuration.validation) {
)
Proofs(configuration.proofs, verifier)
Validation(configuration.validation)
{
_token = token_;
require(

View File

@ -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();
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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))
})
})

View File

@ -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<validatorsRandomised.length; i++) {
for (let i = 0; i < validatorsRandomised.length; i++) {
let validatorIdx = validatorsRandomised[i]
// test the boundary of the SlotIds that are allowed in this particular
// validator validatorIdx
let min = toUInt256Hex( minIdFor(validatorIdx) )
let max = toUInt256Hex( maxIdFor(validatorIdx) )
let min = toUInt256Hex(minIdFor(validatorIdx))
let max = toUInt256Hex(maxIdFor(validatorIdx))
try{
try {
expect(await validation.getValidatorIndex(min)).to.equal(validatorIdx)
expect(await validation.getValidatorIndex(max)).to.equal(validatorIdx)
} catch(e) {
console.log('FAILING TEST PARAMETERS')
console.log('-----------------------------------------------------------------------------------')
console.log('validator index:', validatorIdx)
console.log('slotId min: ', min)
console.log('slotId max: ', max)
} catch (e) {
console.log("FAILING TEST PARAMETERS")
console.log(
"-----------------------------------------------------------------------------------"
)
console.log("validator index:", validatorIdx)
console.log("slotId min: ", min)
console.log("slotId max: ", max)
throw e
}
}

View File

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