mirror of
https://github.com/logos-storage/logos-storage-contracts-eth.git
synced 2026-01-07 07:43:08 +00:00
Related to nim-codex/457, nim-codex/458. To cover the entire SlotId (uint256) address space, each validator must validate a portion of the SlotId space. When a slot is filled, the SlotId will be put in to a bucket, based on the value of the SlotId and the number of buckets (validators) configured. Similar to `myRequests` and `mySlots`, a function called `validationSlots` can be used to retrieve the `SlotIds` being validated for a particular bucket (validator index). This facilitates loading actively filled slots in need of validation when a validator starts.
108 lines
3.6 KiB
JavaScript
108 lines
3.6 KiB
JavaScript
const { expect } = require("chai")
|
|
const { ethers } = require("hardhat")
|
|
const {BigNumber, utils} = require("ethers")
|
|
|
|
describe("Validation", function () {
|
|
const zero =
|
|
"0x0000000000000000000000000000000000000000000000000000000000000000"
|
|
const low =
|
|
"0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
const mid =
|
|
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
describe("constructor", function() {
|
|
// let validation
|
|
let Validation
|
|
|
|
beforeEach(async function () {
|
|
Validation = await ethers.getContractFactory("TestValidation")
|
|
})
|
|
|
|
it("fails to deploy with > uint16.max validators", async function() {
|
|
await expect(
|
|
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("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() {
|
|
|
|
let Validation
|
|
|
|
const high = ethers.constants.MaxUint256
|
|
|
|
function toUInt256Hex(bn) {
|
|
return utils.hexZeroPad(bn.toHexString(), 32)
|
|
}
|
|
|
|
function random(max) {
|
|
return Math.floor(Math.random() * max)
|
|
}
|
|
|
|
beforeEach(async function () {
|
|
Validation = await ethers.getContractFactory("TestValidation")
|
|
})
|
|
|
|
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})
|
|
|
|
// Returns the minimum SlotId of all allowed SlotIds of the validator
|
|
// (given its index)
|
|
function minIdFor(validatorIdx) {
|
|
return BigNumber.from(validatorIdx).mul(idsPerGroup)
|
|
}
|
|
// 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)
|
|
// 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)) {
|
|
return high
|
|
}
|
|
return max
|
|
}
|
|
|
|
// 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))
|
|
|
|
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) )
|
|
|
|
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)
|
|
throw e
|
|
}
|
|
}
|
|
})
|
|
})
|
|
})
|