logos-storage-contracts-eth/test/SlotReservations.test.js
Arnaud dee3d7b654
feat: move to hardhat ignition for deployments (#231)
* Move to ethers 6 and use hardhat ignition for deployments

* Update prettier to the last version

* Remove comment

* Remove npx call in package.json

* Remove useless comment

* Add localhost configuration for hardhat ignition

* Use contract initial balance instead of const value

* Update dependencies and use extract mining to a script in order to use hardhat ignition deploy command

* Fix deployment modules for local env

* Remove unused function

* Export contract deployment custom error assert into a function

* Refactoring

* Remove old deployments folder

* Add process names when running concurrently

* Remove conditional allowBlocksWithSameTimestamp, set true everytime

* Update dependencies

* Update Vault tests for ignition

* Update token description

* Add vault ignition module

* Remove old .tool-versions

* Fix formatting

* Remove deployments folder and add README for previous files references

* Put back the comment related to hardhat automine

* Set hardhat gas limit to auto and restore manual mining for Vault tests

* Apply prettier formatting and bug test with ignition syntax

* Add deployments artifacts

* Fix build-info ignore

* Use HARDHAT_NETWORK env variable to deploy marketplace contract

* Add guard to check that configs has tags

* Add testnet deployment addresses

* Add TOKEN_ADDRESS to reuse the token contract deployed

* Fix token deployment with contractAt

* Remove localhost deployment artifacts

* Add section in README for deployments

* Ignore localhost deployments in git

* Set mine script for localhost deployment only and add deploy reset command

* Remove previous deployment scripts

* Fix typo in documentation

* Add log when reusing token address

* Update testnet artifact reference

* Remove HARDHAT_NETWORK and update documentation

* fix md format

* Npm audit fix

* Update dependencies

* Remove default deployer

* Update commit for last testnet artifacts

* Remove deployments files from linea and testnet and update the last commit hashes to those artifacts
2025-06-20 16:05:57 +02:00

164 lines
5.6 KiB
JavaScript

const { expect } = require("chai")
const { ethers } = require("hardhat")
const { exampleRequest, exampleConfiguration } = require("./examples")
const { requestId, slotId } = require("./ids")
const { SlotState } = require("./requests")
const SlotReservationsModule = require("../ignition/modules/slot-reservations")
describe("SlotReservations", function () {
let reservations
let provider, address1, address2, address3
let request
let reqId
let slot
let slotIndex
let id // can't use slotId because it'll shadow the function slotId
const config = exampleConfiguration()
beforeEach(async function () {
const { testSlotReservations } = await ignition.deploy(
SlotReservationsModule,
{
parameters: {
SlotReservations: {
configuration: config.reservations,
},
},
},
)
reservations = testSlotReservations
;[provider, address1, address2, address3] = await ethers.getSigners()
request = await exampleRequest()
reqId = requestId(request)
slotIndex = request.ask.slots / 2
slot = {
request: reqId,
index: slotIndex,
}
id = slotId(slot)
await reservations.setSlotState(id, SlotState.Free)
})
function switchAccount(account) {
reservations = reservations.connect(account)
}
it("allows a slot to be reserved if free", async function () {
expect(reservations.reserveSlot(reqId, slotIndex)).to.not.be.reverted
})
it("allows a slot to be reserved if in repair", async function () {
await reservations.setSlotState(id, SlotState.Repair)
expect(reservations.reserveSlot(reqId, slotIndex)).to.not.be.reverted
})
it("contains the correct addresses after reservation", async function () {
await reservations.reserveSlot(reqId, slotIndex)
expect(await reservations.contains(id, provider.address)).to.be.true
switchAccount(address1)
await reservations.reserveSlot(reqId, slotIndex)
expect(await reservations.contains(id, address1.address)).to.be.true
})
it("has the correct number of addresses after reservation", async function () {
await reservations.reserveSlot(reqId, slotIndex)
expect(await reservations.length(id)).to.equal(1)
switchAccount(address1)
await reservations.reserveSlot(reqId, slotIndex)
expect(await reservations.length(id)).to.equal(2)
})
it("reports a slot can be reserved if free", async function () {
await reservations.setSlotState(id, SlotState.Free)
expect(await reservations.canReserveSlot(reqId, slotIndex)).to.be.true
})
it("reports a slot can be reserved if in repair", async function () {
await reservations.setSlotState(id, SlotState.Repair)
expect(await reservations.canReserveSlot(reqId, slotIndex)).to.be.true
})
it("cannot reserve a slot more than once", async function () {
await reservations.reserveSlot(reqId, slotIndex)
await expect(
reservations.reserveSlot(reqId, slotIndex),
).to.be.revertedWithCustomError(
reservations,
"SlotReservations_ReservationNotAllowed",
)
expect(await reservations.length(id)).to.equal(1)
})
it("reports a slot cannot be reserved if already reserved", async function () {
await reservations.reserveSlot(reqId, slotIndex)
expect(await reservations.canReserveSlot(reqId, slotIndex)).to.be.false
})
it("cannot reserve a slot if reservations are at capacity", async function () {
switchAccount(address1)
await reservations.reserveSlot(reqId, slotIndex)
switchAccount(address2)
await reservations.reserveSlot(reqId, slotIndex)
switchAccount(address3)
await reservations.reserveSlot(reqId, slotIndex)
switchAccount(provider)
await expect(
reservations.reserveSlot(reqId, slotIndex),
).to.be.revertedWithCustomError(
reservations,
"SlotReservations_ReservationNotAllowed",
)
expect(await reservations.length(id)).to.equal(3)
expect(await reservations.contains(id, provider.address)).to.be.false
})
it("reports a slot cannot be reserved if reservations are at capacity", async function () {
switchAccount(address1)
await reservations.reserveSlot(reqId, slotIndex)
switchAccount(address2)
await reservations.reserveSlot(reqId, slotIndex)
switchAccount(address3)
await reservations.reserveSlot(reqId, slotIndex)
switchAccount(provider)
expect(await reservations.canReserveSlot(reqId, slotIndex)).to.be.false
})
it("cannot reserve a slot if not free or not in repair", async function () {
await reservations.setSlotState(id, SlotState.Filled)
await expect(
reservations.reserveSlot(reqId, slotIndex),
).to.be.revertedWithCustomError(
reservations,
"SlotReservations_ReservationNotAllowed",
)
expect(await reservations.length(id)).to.equal(0)
})
it("reports a slot cannot be reserved if not free or not in repair", async function () {
await reservations.setSlotState(id, SlotState.Filled)
expect(await reservations.canReserveSlot(reqId, slotIndex)).to.be.false
})
it("should emit an event when slot reservations are full", async function () {
await reservations.reserveSlot(reqId, slotIndex)
switchAccount(address1)
await reservations.reserveSlot(reqId, slotIndex)
switchAccount(address2)
await expect(reservations.reserveSlot(reqId, slotIndex))
.to.emit(reservations, "SlotReservationsFull")
.withArgs(reqId, slotIndex)
})
it("should not emit an event when reservations are not full", async function () {
await expect(reservations.reserveSlot(reqId, slotIndex)).to.not.emit(
reservations,
"SlotReservationsFull",
)
})
})