mirror of
https://github.com/logos-storage/logos-storage-contracts-eth.git
synced 2026-01-04 06:13:09 +00:00
refactor(Marketplace): Use custom errors instead of string messages (#141)
Co-authored-by: Adam Uhlíř <adam@uhlir.dev>
This commit is contained in:
parent
dfab6102e7
commit
02e3b8d22b
@ -10,7 +10,7 @@ insert_final_newline = true
|
|||||||
|
|
||||||
# Matches multiple files with brace expansion notation
|
# Matches multiple files with brace expansion notation
|
||||||
# Set default charset
|
# Set default charset
|
||||||
[{*.js, *.sol}]
|
[{*.js,*.sol}]
|
||||||
charset = utf-8
|
charset = utf-8
|
||||||
indent_style = space
|
indent_style = space
|
||||||
indent_size = 2
|
indent_size = 2
|
||||||
|
|||||||
@ -13,6 +13,27 @@ import "./Endian.sol";
|
|||||||
import "./Groth16.sol";
|
import "./Groth16.sol";
|
||||||
|
|
||||||
contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
||||||
|
error Marketplace_RepairRewardPercentageTooHigh();
|
||||||
|
error Marketplace_SlashPercentageTooHigh();
|
||||||
|
error Marketplace_MaximumSlashingTooHigh();
|
||||||
|
error Marketplace_InvalidExpiry();
|
||||||
|
error Marketplace_InvalidMaxSlotLoss();
|
||||||
|
error Marketplace_InsufficientSlots();
|
||||||
|
error Marketplace_InvalidClientAddress();
|
||||||
|
error Marketplace_RequestAlreadyExists();
|
||||||
|
error Marketplace_InvalidSlot();
|
||||||
|
error Marketplace_SlotNotFree();
|
||||||
|
error Marketplace_InvalidSlotHost();
|
||||||
|
error Marketplace_AlreadyPaid();
|
||||||
|
error Marketplace_TransferFailed();
|
||||||
|
error Marketplace_UnknownRequest();
|
||||||
|
error Marketplace_InvalidState();
|
||||||
|
error Marketplace_StartNotBeforeExpiry();
|
||||||
|
error Marketplace_SlotNotAcceptingProofs();
|
||||||
|
error Marketplace_SlotIsFree();
|
||||||
|
error Marketplace_ReservationRequired();
|
||||||
|
error Marketplace_NothingToWithdraw();
|
||||||
|
|
||||||
using EnumerableSet for EnumerableSet.Bytes32Set;
|
using EnumerableSet for EnumerableSet.Bytes32Set;
|
||||||
using EnumerableSet for EnumerableSet.AddressSet;
|
using EnumerableSet for EnumerableSet.AddressSet;
|
||||||
using Requests for Request;
|
using Requests for Request;
|
||||||
@ -71,20 +92,18 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
{
|
{
|
||||||
_token = token_;
|
_token = token_;
|
||||||
|
|
||||||
require(
|
if (configuration.collateral.repairRewardPercentage > 100)
|
||||||
configuration.collateral.repairRewardPercentage <= 100,
|
revert Marketplace_RepairRewardPercentageTooHigh();
|
||||||
"Must be less than 100"
|
if (configuration.collateral.slashPercentage > 100)
|
||||||
);
|
revert Marketplace_SlashPercentageTooHigh();
|
||||||
require(
|
|
||||||
configuration.collateral.slashPercentage <= 100,
|
if (
|
||||||
"Must be less than 100"
|
|
||||||
);
|
|
||||||
require(
|
|
||||||
configuration.collateral.maxNumberOfSlashes *
|
configuration.collateral.maxNumberOfSlashes *
|
||||||
configuration.collateral.slashPercentage <=
|
configuration.collateral.slashPercentage >
|
||||||
100,
|
100
|
||||||
"Maximum slashing exceeds 100%"
|
) {
|
||||||
);
|
revert Marketplace_MaximumSlashingTooHigh();
|
||||||
|
}
|
||||||
_config = configuration;
|
_config = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,19 +116,16 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function requestStorage(Request calldata request) public {
|
function requestStorage(Request calldata request) public {
|
||||||
require(request.client == msg.sender, "Invalid client address");
|
|
||||||
|
|
||||||
RequestId id = request.id();
|
RequestId id = request.id();
|
||||||
require(_requests[id].client == address(0), "Request already exists");
|
|
||||||
require(
|
if (request.client != msg.sender) revert Marketplace_InvalidClientAddress();
|
||||||
request.expiry > 0 && request.expiry < request.ask.duration,
|
if (_requests[id].client != address(0))
|
||||||
"Expiry not in range"
|
revert Marketplace_RequestAlreadyExists();
|
||||||
);
|
if (request.expiry == 0 || request.expiry >= request.ask.duration)
|
||||||
require(request.ask.slots > 0, "Insufficient slots");
|
revert Marketplace_InvalidExpiry();
|
||||||
require(
|
if (request.ask.slots == 0) revert Marketplace_InsufficientSlots();
|
||||||
request.ask.maxSlotLoss <= request.ask.slots,
|
if (request.ask.maxSlotLoss > request.ask.slots)
|
||||||
"maxSlotLoss exceeds slots"
|
revert Marketplace_InvalidMaxSlotLoss();
|
||||||
);
|
|
||||||
|
|
||||||
_requests[id] = request;
|
_requests[id] = request;
|
||||||
_requestContexts[id].endsAt = block.timestamp + request.ask.duration;
|
_requestContexts[id].endsAt = block.timestamp + request.ask.duration;
|
||||||
@ -139,21 +155,24 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
Groth16Proof calldata proof
|
Groth16Proof calldata proof
|
||||||
) public requestIsKnown(requestId) {
|
) public requestIsKnown(requestId) {
|
||||||
Request storage request = _requests[requestId];
|
Request storage request = _requests[requestId];
|
||||||
require(slotIndex < request.ask.slots, "Invalid slot");
|
if (slotIndex >= request.ask.slots) revert Marketplace_InvalidSlot();
|
||||||
|
|
||||||
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
||||||
require(_reservations[slotId].contains(msg.sender), "Reservation required");
|
|
||||||
|
if (!_reservations[slotId].contains(msg.sender))
|
||||||
|
revert Marketplace_ReservationRequired();
|
||||||
|
|
||||||
Slot storage slot = _slots[slotId];
|
Slot storage slot = _slots[slotId];
|
||||||
slot.requestId = requestId;
|
slot.requestId = requestId;
|
||||||
slot.slotIndex = slotIndex;
|
slot.slotIndex = slotIndex;
|
||||||
RequestContext storage context = _requestContexts[requestId];
|
RequestContext storage context = _requestContexts[requestId];
|
||||||
|
|
||||||
require(
|
if (
|
||||||
slotState(slotId) == SlotState.Free ||
|
slotState(slotId) != SlotState.Free &&
|
||||||
slotState(slotId) == SlotState.Repair,
|
slotState(slotId) != SlotState.Repair
|
||||||
"Slot is not free"
|
) {
|
||||||
);
|
revert Marketplace_SlotNotFree();
|
||||||
|
}
|
||||||
|
|
||||||
_startRequiringProofs(slotId, request.ask.proofProbability);
|
_startRequiringProofs(slotId, request.ask.proofProbability);
|
||||||
submitProof(slotId, proof);
|
submitProof(slotId, proof);
|
||||||
@ -221,9 +240,10 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
address collateralRecipient
|
address collateralRecipient
|
||||||
) public slotIsNotFree(slotId) {
|
) public slotIsNotFree(slotId) {
|
||||||
Slot storage slot = _slots[slotId];
|
Slot storage slot = _slots[slotId];
|
||||||
require(slot.host == msg.sender, "Slot filled by other host");
|
if (slot.host != msg.sender) revert Marketplace_InvalidSlotHost();
|
||||||
|
|
||||||
SlotState state = slotState(slotId);
|
SlotState state = slotState(slotId);
|
||||||
require(state != SlotState.Paid, "Already paid");
|
if (state == SlotState.Paid) revert Marketplace_AlreadyPaid();
|
||||||
|
|
||||||
if (state == SlotState.Finished) {
|
if (state == SlotState.Finished) {
|
||||||
_payoutSlot(slot.requestId, slotId, rewardRecipient, collateralRecipient);
|
_payoutSlot(slot.requestId, slotId, rewardRecipient, collateralRecipient);
|
||||||
@ -276,7 +296,9 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function markProofAsMissing(SlotId slotId, Period period) public {
|
function markProofAsMissing(SlotId slotId, Period period) public {
|
||||||
require(slotState(slotId) == SlotState.Filled, "Slot not accepting proofs");
|
if (slotState(slotId) != SlotState.Filled)
|
||||||
|
revert Marketplace_SlotNotAcceptingProofs();
|
||||||
|
|
||||||
_markProofAsMissing(slotId, period);
|
_markProofAsMissing(slotId, period);
|
||||||
Slot storage slot = _slots[slotId];
|
Slot storage slot = _slots[slotId];
|
||||||
Request storage request = _requests[slot.requestId];
|
Request storage request = _requests[slot.requestId];
|
||||||
@ -409,21 +431,25 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
function withdrawFunds(
|
function withdrawFunds(
|
||||||
RequestId requestId,
|
RequestId requestId,
|
||||||
address withdrawRecipient
|
address withdrawRecipient
|
||||||
) public {
|
) public requestIsKnown(requestId) {
|
||||||
Request storage request = _requests[requestId];
|
Request storage request = _requests[requestId];
|
||||||
require(request.client == msg.sender, "Invalid client address");
|
|
||||||
RequestContext storage context = _requestContexts[requestId];
|
RequestContext storage context = _requestContexts[requestId];
|
||||||
|
|
||||||
|
if (request.client != msg.sender) revert Marketplace_InvalidClientAddress();
|
||||||
|
|
||||||
RequestState state = requestState(requestId);
|
RequestState state = requestState(requestId);
|
||||||
require(
|
if (
|
||||||
state == RequestState.Cancelled ||
|
state != RequestState.Cancelled &&
|
||||||
state == RequestState.Failed ||
|
state != RequestState.Failed &&
|
||||||
state == RequestState.Finished,
|
state != RequestState.Finished
|
||||||
"Invalid state"
|
) {
|
||||||
);
|
revert Marketplace_InvalidState();
|
||||||
|
}
|
||||||
|
|
||||||
// fundsToReturnToClient == 0 is used for "double-spend" protection, once the funds are withdrawn
|
// fundsToReturnToClient == 0 is used for "double-spend" protection, once the funds are withdrawn
|
||||||
// then this variable is set to 0.
|
// then this variable is set to 0.
|
||||||
require(context.fundsToReturnToClient != 0, "Nothing to withdraw");
|
if (context.fundsToReturnToClient == 0)
|
||||||
|
revert Marketplace_NothingToWithdraw();
|
||||||
|
|
||||||
if (state == RequestState.Cancelled) {
|
if (state == RequestState.Cancelled) {
|
||||||
context.state = RequestState.Cancelled;
|
context.state = RequestState.Cancelled;
|
||||||
@ -464,7 +490,9 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
}
|
}
|
||||||
|
|
||||||
modifier requestIsKnown(RequestId requestId) {
|
modifier requestIsKnown(RequestId requestId) {
|
||||||
require(_requests[requestId].client != address(0), "Unknown request");
|
if (_requests[requestId].client == address(0))
|
||||||
|
revert Marketplace_UnknownRequest();
|
||||||
|
|
||||||
_;
|
_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -475,7 +503,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
}
|
}
|
||||||
|
|
||||||
modifier slotIsNotFree(SlotId slotId) {
|
modifier slotIsNotFree(SlotId slotId) {
|
||||||
require(_slots[slotId].state != SlotState.Free, "Slot is free");
|
if (_slots[slotId].state == SlotState.Free) revert Marketplace_SlotIsFree();
|
||||||
_;
|
_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -523,8 +551,8 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
uint256 endingTimestamp
|
uint256 endingTimestamp
|
||||||
) private view returns (uint256) {
|
) private view returns (uint256) {
|
||||||
Request storage request = _requests[requestId];
|
Request storage request = _requests[requestId];
|
||||||
require(startingTimestamp < endingTimestamp, "Start not before expiry");
|
if (startingTimestamp >= endingTimestamp)
|
||||||
|
revert Marketplace_StartNotBeforeExpiry();
|
||||||
return (endingTimestamp - startingTimestamp) * request.ask.reward;
|
return (endingTimestamp - startingTimestamp) * request.ask.reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -574,7 +602,8 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
|
|||||||
|
|
||||||
function _transferFrom(address sender, uint256 amount) internal {
|
function _transferFrom(address sender, uint256 amount) internal {
|
||||||
address receiver = address(this);
|
address receiver = address(this);
|
||||||
require(_token.transferFrom(sender, receiver, amount), "Transfer failed");
|
if (!_token.transferFrom(sender, receiver, amount))
|
||||||
|
revert Marketplace_TransferFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
event StorageRequested(RequestId requestId, Ask ask, uint256 expiry);
|
event StorageRequested(RequestId requestId, Ask ask, uint256 expiry);
|
||||||
|
|||||||
@ -11,6 +11,15 @@ import "./Groth16.sol";
|
|||||||
* @notice Abstract contract that handles proofs tracking, validation and reporting functionality
|
* @notice Abstract contract that handles proofs tracking, validation and reporting functionality
|
||||||
*/
|
*/
|
||||||
abstract contract Proofs is Periods {
|
abstract contract Proofs is Periods {
|
||||||
|
error Proofs_InsufficientBlockHeight();
|
||||||
|
error Proofs_InvalidProof();
|
||||||
|
error Proofs_ProofAlreadySubmitted();
|
||||||
|
error Proofs_PeriodNotEnded();
|
||||||
|
error Proofs_ValidationTimedOut();
|
||||||
|
error Proofs_ProofNotMissing();
|
||||||
|
error Proofs_ProofNotRequired();
|
||||||
|
error Proofs_ProofAlreadyMarkedMissing();
|
||||||
|
|
||||||
ProofConfig private _config;
|
ProofConfig private _config;
|
||||||
IGroth16Verifier private _verifier;
|
IGroth16Verifier private _verifier;
|
||||||
|
|
||||||
@ -22,7 +31,10 @@ abstract contract Proofs is Periods {
|
|||||||
ProofConfig memory config,
|
ProofConfig memory config,
|
||||||
IGroth16Verifier verifier
|
IGroth16Verifier verifier
|
||||||
) Periods(config.period) {
|
) Periods(config.period) {
|
||||||
require(block.number > 256, "Insufficient block height");
|
if (block.number <= 256) {
|
||||||
|
revert Proofs_InsufficientBlockHeight();
|
||||||
|
}
|
||||||
|
|
||||||
_config = config;
|
_config = config;
|
||||||
_verifier = verifier;
|
_verifier = verifier;
|
||||||
}
|
}
|
||||||
@ -189,8 +201,9 @@ abstract contract Proofs is Periods {
|
|||||||
Groth16Proof calldata proof,
|
Groth16Proof calldata proof,
|
||||||
uint[] memory pubSignals
|
uint[] memory pubSignals
|
||||||
) internal {
|
) internal {
|
||||||
require(!_received[id][_blockPeriod()], "Proof already submitted");
|
if (_received[id][_blockPeriod()]) revert Proofs_ProofAlreadySubmitted();
|
||||||
require(_verifier.verify(proof, pubSignals), "Invalid proof");
|
if (!_verifier.verify(proof, pubSignals)) revert Proofs_InvalidProof();
|
||||||
|
|
||||||
_received[id][_blockPeriod()] = true;
|
_received[id][_blockPeriod()] = true;
|
||||||
emit ProofSubmitted(id);
|
emit ProofSubmitted(id);
|
||||||
}
|
}
|
||||||
@ -209,11 +222,13 @@ abstract contract Proofs is Periods {
|
|||||||
*/
|
*/
|
||||||
function _markProofAsMissing(SlotId id, Period missedPeriod) internal {
|
function _markProofAsMissing(SlotId id, Period missedPeriod) internal {
|
||||||
uint256 end = _periodEnd(missedPeriod);
|
uint256 end = _periodEnd(missedPeriod);
|
||||||
require(end < block.timestamp, "Period has not ended yet");
|
if (end >= block.timestamp) revert Proofs_PeriodNotEnded();
|
||||||
require(block.timestamp < end + _config.timeout, "Validation timed out");
|
if (block.timestamp >= end + _config.timeout)
|
||||||
require(!_received[id][missedPeriod], "Proof was submitted, not missing");
|
revert Proofs_ValidationTimedOut();
|
||||||
require(_isProofRequired(id, missedPeriod), "Proof was not required");
|
if (_received[id][missedPeriod]) revert Proofs_ProofNotMissing();
|
||||||
require(!_missing[id][missedPeriod], "Proof already marked as missing");
|
if (!_isProofRequired(id, missedPeriod)) revert Proofs_ProofNotRequired();
|
||||||
|
if (_missing[id][missedPeriod]) revert Proofs_ProofAlreadyMarkedMissing();
|
||||||
|
|
||||||
_missing[id][missedPeriod] = true;
|
_missing[id][missedPeriod] = true;
|
||||||
_missed[id] += 1;
|
_missed[id] += 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import "./Configuration.sol";
|
|||||||
|
|
||||||
abstract contract SlotReservations {
|
abstract contract SlotReservations {
|
||||||
using EnumerableSet for EnumerableSet.AddressSet;
|
using EnumerableSet for EnumerableSet.AddressSet;
|
||||||
|
error SlotReservations_ReservationNotAllowed();
|
||||||
|
|
||||||
mapping(SlotId => EnumerableSet.AddressSet) internal _reservations;
|
mapping(SlotId => EnumerableSet.AddressSet) internal _reservations;
|
||||||
SlotReservationsConfig private _config;
|
SlotReservationsConfig private _config;
|
||||||
@ -18,7 +19,8 @@ abstract contract SlotReservations {
|
|||||||
function _slotIsFree(SlotId slotId) internal view virtual returns (bool);
|
function _slotIsFree(SlotId slotId) internal view virtual returns (bool);
|
||||||
|
|
||||||
function reserveSlot(RequestId requestId, uint256 slotIndex) public {
|
function reserveSlot(RequestId requestId, uint256 slotIndex) public {
|
||||||
require(canReserveSlot(requestId, slotIndex), "Reservation not allowed");
|
if (!canReserveSlot(requestId, slotIndex))
|
||||||
|
revert SlotReservations_ReservationNotAllowed();
|
||||||
|
|
||||||
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
||||||
_reservations[slotId].add(msg.sender);
|
_reservations[slotId].add(msg.sender);
|
||||||
|
|||||||
@ -58,18 +58,24 @@ describe("Marketplace constructor", function () {
|
|||||||
await revert()
|
await revert()
|
||||||
})
|
})
|
||||||
|
|
||||||
function testPercentageOverflow(property) {
|
function testPercentageOverflow(property, expectedError) {
|
||||||
it(`should reject for ${property} overflowing percentage values`, async () => {
|
it(`should reject for ${property} overflowing percentage values`, async () => {
|
||||||
config.collateral[property] = 101
|
config.collateral[property] = 101
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
Marketplace.deploy(config, token.address, verifier.address)
|
Marketplace.deploy(config, token.address, verifier.address)
|
||||||
).to.be.revertedWith("Must be less than 100")
|
).to.be.revertedWith(expectedError)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
testPercentageOverflow("repairRewardPercentage")
|
testPercentageOverflow(
|
||||||
testPercentageOverflow("slashPercentage")
|
"repairRewardPercentage",
|
||||||
|
"Marketplace_RepairRewardPercentageTooHigh"
|
||||||
|
)
|
||||||
|
testPercentageOverflow(
|
||||||
|
"slashPercentage",
|
||||||
|
"Marketplace_SlashPercentageTooHigh"
|
||||||
|
)
|
||||||
|
|
||||||
it("should reject when total slash percentage exceeds 100%", async () => {
|
it("should reject when total slash percentage exceeds 100%", async () => {
|
||||||
config.collateral.slashPercentage = 1
|
config.collateral.slashPercentage = 1
|
||||||
@ -77,7 +83,7 @@ describe("Marketplace constructor", function () {
|
|||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
Marketplace.deploy(config, token.address, verifier.address)
|
Marketplace.deploy(config, token.address, verifier.address)
|
||||||
).to.be.revertedWith("Maximum slashing exceeds 100%")
|
).to.be.revertedWith("Marketplace_MaximumSlashingTooHigh")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -185,7 +191,7 @@ describe("Marketplace", function () {
|
|||||||
let invalid = { ...request, client: host.address }
|
let invalid = { ...request, client: host.address }
|
||||||
await token.approve(marketplace.address, maxPrice(invalid))
|
await token.approve(marketplace.address, maxPrice(invalid))
|
||||||
await expect(marketplace.requestStorage(invalid)).to.be.revertedWith(
|
await expect(marketplace.requestStorage(invalid)).to.be.revertedWith(
|
||||||
"Invalid client address"
|
"Marketplace_InvalidClientAddress"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -202,26 +208,26 @@ describe("Marketplace", function () {
|
|||||||
|
|
||||||
request.expiry = request.ask.duration + 1
|
request.expiry = request.ask.duration + 1
|
||||||
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
||||||
"Expiry not in range"
|
"Marketplace_InvalidExpiry"
|
||||||
)
|
)
|
||||||
|
|
||||||
request.expiry = 0
|
request.expiry = 0
|
||||||
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
||||||
"Expiry not in range"
|
"Marketplace_InvalidExpiry"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("is rejected with insufficient slots ", async function () {
|
it("is rejected with insufficient slots ", async function () {
|
||||||
request.ask.slots = 0
|
request.ask.slots = 0
|
||||||
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
||||||
"Insufficient slots"
|
"Marketplace_InsufficientSlots"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("is rejected when maxSlotLoss exceeds slots", async function () {
|
it("is rejected when maxSlotLoss exceeds slots", async function () {
|
||||||
request.ask.maxSlotLoss = request.ask.slots + 1
|
request.ask.maxSlotLoss = request.ask.slots + 1
|
||||||
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
||||||
"maxSlotLoss exceeds slots"
|
"Marketplace_InvalidMaxSlotLoss"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -229,7 +235,7 @@ describe("Marketplace", function () {
|
|||||||
await token.approve(marketplace.address, maxPrice(request) * 2)
|
await token.approve(marketplace.address, maxPrice(request) * 2)
|
||||||
await marketplace.requestStorage(request)
|
await marketplace.requestStorage(request)
|
||||||
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
||||||
"Request already exists"
|
"Marketplace_RequestAlreadyExists"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -286,7 +292,7 @@ describe("Marketplace", function () {
|
|||||||
|
|
||||||
it("fails to retrieve a request of an empty slot", async function () {
|
it("fails to retrieve a request of an empty slot", async function () {
|
||||||
expect(marketplace.getActiveSlot(slotId(slot))).to.be.revertedWith(
|
expect(marketplace.getActiveSlot(slotId(slot))).to.be.revertedWith(
|
||||||
"Slot is free"
|
"Marketplace_SlotIsFree"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -302,7 +308,7 @@ describe("Marketplace", function () {
|
|||||||
await marketplace.reserveSlot(slot.request, slot.index)
|
await marketplace.reserveSlot(slot.request, slot.index)
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(slot.request, slot.index, invalidProof())
|
marketplace.fillSlot(slot.request, slot.index, invalidProof())
|
||||||
).to.be.revertedWith("Invalid proof")
|
).to.be.revertedWith("Proofs_InvalidProof")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("is rejected when slot already filled", async function () {
|
it("is rejected when slot already filled", async function () {
|
||||||
@ -310,14 +316,14 @@ describe("Marketplace", function () {
|
|||||||
await marketplace.fillSlot(slot.request, slot.index, proof)
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(slot.request, slot.index, proof)
|
marketplace.fillSlot(slot.request, slot.index, proof)
|
||||||
).to.be.revertedWith("Slot is not free")
|
).to.be.revertedWith("Marketplace_SlotNotFree")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("is rejected when request is unknown", async function () {
|
it("is rejected when request is unknown", async function () {
|
||||||
let unknown = await exampleRequest()
|
let unknown = await exampleRequest()
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(requestId(unknown), 0, proof)
|
marketplace.fillSlot(requestId(unknown), 0, proof)
|
||||||
).to.be.revertedWith("Unknown request")
|
).to.be.revertedWith("Marketplace_UnknownRequest")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("is rejected when request is cancelled", async function () {
|
it("is rejected when request is cancelled", async function () {
|
||||||
@ -330,7 +336,7 @@ describe("Marketplace", function () {
|
|||||||
await marketplace.reserveSlot(requestId(expired), slot.index)
|
await marketplace.reserveSlot(requestId(expired), slot.index)
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(requestId(expired), slot.index, proof)
|
marketplace.fillSlot(requestId(expired), slot.index, proof)
|
||||||
).to.be.revertedWith("Slot is not free")
|
).to.be.revertedWith("Marketplace_SlotNotFree")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("is rejected when request is finished", async function () {
|
it("is rejected when request is finished", async function () {
|
||||||
@ -338,7 +344,7 @@ describe("Marketplace", function () {
|
|||||||
await waitUntilFinished(marketplace, slot.request)
|
await waitUntilFinished(marketplace, slot.request)
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(slot.request, slot.index, proof)
|
marketplace.fillSlot(slot.request, slot.index, proof)
|
||||||
).to.be.revertedWith("Slot is not free")
|
).to.be.revertedWith("Marketplace_SlotNotFree")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("is rejected when request is failed", async function () {
|
it("is rejected when request is failed", async function () {
|
||||||
@ -346,14 +352,14 @@ describe("Marketplace", function () {
|
|||||||
await waitUntilFailed(marketplace, request)
|
await waitUntilFailed(marketplace, request)
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(slot.request, slot.index, proof)
|
marketplace.fillSlot(slot.request, slot.index, proof)
|
||||||
).to.be.revertedWith("Slot is not free")
|
).to.be.revertedWith("Marketplace_SlotNotFree")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("is rejected when slot index not in range", async function () {
|
it("is rejected when slot index not in range", async function () {
|
||||||
const invalid = request.ask.slots
|
const invalid = request.ask.slots
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(slot.request, invalid, proof)
|
marketplace.fillSlot(slot.request, invalid, proof)
|
||||||
).to.be.revertedWith("Invalid slot")
|
).to.be.revertedWith("Marketplace_InvalidSlot")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("fails when all slots are already filled", async function () {
|
it("fails when all slots are already filled", async function () {
|
||||||
@ -369,13 +375,13 @@ describe("Marketplace", function () {
|
|||||||
}
|
}
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(slot.request, lastSlot, proof)
|
marketplace.fillSlot(slot.request, lastSlot, proof)
|
||||||
).to.be.revertedWith("Slot is not free")
|
).to.be.revertedWith("Marketplace_SlotNotFree")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("fails if slot is not reserved first", async function () {
|
it("fails if slot is not reserved first", async function () {
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(slot.request, slot.index, proof)
|
marketplace.fillSlot(slot.request, slot.index, proof)
|
||||||
).to.be.revertedWith("Reservation required")
|
).to.be.revertedWith("Marketplace_ReservationRequired")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -514,7 +520,7 @@ describe("Marketplace", function () {
|
|||||||
slot.index = 5
|
slot.index = 5
|
||||||
let nonExistentId = slotId(slot)
|
let nonExistentId = slotId(slot)
|
||||||
await expect(marketplace.freeSlot(nonExistentId)).to.be.revertedWith(
|
await expect(marketplace.freeSlot(nonExistentId)).to.be.revertedWith(
|
||||||
"Slot is free"
|
"Marketplace_SlotIsFree"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -522,7 +528,7 @@ describe("Marketplace", function () {
|
|||||||
await waitUntilStarted(marketplace, request, proof, token)
|
await waitUntilStarted(marketplace, request, proof, token)
|
||||||
switchAccount(client)
|
switchAccount(client)
|
||||||
await expect(marketplace.freeSlot(id)).to.be.revertedWith(
|
await expect(marketplace.freeSlot(id)).to.be.revertedWith(
|
||||||
"Slot filled by other host"
|
"Marketplace_InvalidSlotHost"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -714,7 +720,7 @@ describe("Marketplace", function () {
|
|||||||
await waitUntilFinished(marketplace, requestId(request))
|
await waitUntilFinished(marketplace, requestId(request))
|
||||||
await marketplace.freeSlot(slotId(slot))
|
await marketplace.freeSlot(slotId(slot))
|
||||||
await expect(marketplace.freeSlot(slotId(slot))).to.be.revertedWith(
|
await expect(marketplace.freeSlot(slotId(slot))).to.be.revertedWith(
|
||||||
"Already paid"
|
"Marketplace_AlreadyPaid"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -776,7 +782,7 @@ describe("Marketplace", function () {
|
|||||||
}
|
}
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.fillSlot(slot.request, lastSlot, proof)
|
marketplace.fillSlot(slot.request, lastSlot, proof)
|
||||||
).to.be.revertedWith("Slot is not free")
|
).to.be.revertedWith("Marketplace_SlotNotFree")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -793,14 +799,14 @@ describe("Marketplace", function () {
|
|||||||
switchAccount(client)
|
switchAccount(client)
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address)
|
marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address)
|
||||||
).to.be.revertedWith("Invalid state")
|
).to.be.revertedWith("Marketplace_InvalidState")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("rejects withdraw when wrong account used", async function () {
|
it("rejects withdraw when wrong account used", async function () {
|
||||||
await waitUntilCancelled(request)
|
await waitUntilCancelled(request)
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address)
|
marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address)
|
||||||
).to.be.revertedWith("Invalid client address")
|
).to.be.revertedWith("Marketplace_InvalidClientAddress")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("rejects withdraw when in wrong state", async function () {
|
it("rejects withdraw when in wrong state", async function () {
|
||||||
@ -818,7 +824,7 @@ describe("Marketplace", function () {
|
|||||||
switchAccount(client)
|
switchAccount(client)
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address)
|
marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address)
|
||||||
).to.be.revertedWith("Invalid state")
|
).to.be.revertedWith("Marketplace_InvalidState")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("rejects withdraw when already withdrawn", async function () {
|
it("rejects withdraw when already withdrawn", async function () {
|
||||||
@ -832,7 +838,7 @@ describe("Marketplace", function () {
|
|||||||
)
|
)
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address)
|
marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address)
|
||||||
).to.be.revertedWith("Nothing to withdraw")
|
).to.be.revertedWith("Marketplace_NothingToWithdraw")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("emits event once request is cancelled", async function () {
|
it("emits event once request is cancelled", async function () {
|
||||||
@ -1256,7 +1262,7 @@ describe("Marketplace", function () {
|
|||||||
let missedPeriod = periodOf(await currentTime())
|
let missedPeriod = periodOf(await currentTime())
|
||||||
await expect(
|
await expect(
|
||||||
marketplace.markProofAsMissing(slotId(slot), missedPeriod)
|
marketplace.markProofAsMissing(slotId(slot), missedPeriod)
|
||||||
).to.be.revertedWith("Slot not accepting proofs")
|
).to.be.revertedWith("Marketplace_SlotNotAcceptingProofs")
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("slashing when missing proofs", function () {
|
describe("slashing when missing proofs", function () {
|
||||||
|
|||||||
@ -208,14 +208,14 @@ describe("Proofs", function () {
|
|||||||
let invalid = exampleProof()
|
let invalid = exampleProof()
|
||||||
await expect(
|
await expect(
|
||||||
proofs.proofReceived(slotId, invalid, pubSignals)
|
proofs.proofReceived(slotId, invalid, pubSignals)
|
||||||
).to.be.revertedWith("Invalid proof")
|
).to.be.revertedWith("Proofs_InvalidProof")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("fails proof submission when public input is incorrect", async function () {
|
it("fails proof submission when public input is incorrect", async function () {
|
||||||
let invalid = [1, 2, 3]
|
let invalid = [1, 2, 3]
|
||||||
await expect(
|
await expect(
|
||||||
proofs.proofReceived(slotId, proof, invalid)
|
proofs.proofReceived(slotId, proof, invalid)
|
||||||
).to.be.revertedWith("Invalid proof")
|
).to.be.revertedWith("Proofs_InvalidProof")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("emits an event when proof was submitted", async function () {
|
it("emits an event when proof was submitted", async function () {
|
||||||
@ -229,7 +229,7 @@ describe("Proofs", function () {
|
|||||||
await proofs.proofReceived(slotId, proof, pubSignals)
|
await proofs.proofReceived(slotId, proof, pubSignals)
|
||||||
await expect(
|
await expect(
|
||||||
proofs.proofReceived(slotId, proof, pubSignals)
|
proofs.proofReceived(slotId, proof, pubSignals)
|
||||||
).to.be.revertedWith("Proof already submitted")
|
).to.be.revertedWith("Proofs_ProofAlreadySubmitted")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("marks a proof as missing", async function () {
|
it("marks a proof as missing", async function () {
|
||||||
@ -247,7 +247,7 @@ describe("Proofs", function () {
|
|||||||
let currentPeriod = periodOf(await currentTime())
|
let currentPeriod = periodOf(await currentTime())
|
||||||
await expect(
|
await expect(
|
||||||
proofs.markProofAsMissing(slotId, currentPeriod)
|
proofs.markProofAsMissing(slotId, currentPeriod)
|
||||||
).to.be.revertedWith("Period has not ended yet")
|
).to.be.revertedWith("Proofs_PeriodNotEnded")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("does not mark a proof as missing after timeout", async function () {
|
it("does not mark a proof as missing after timeout", async function () {
|
||||||
@ -256,7 +256,7 @@ describe("Proofs", function () {
|
|||||||
await advanceTimeToForNextBlock(periodEnd(currentPeriod) + timeout)
|
await advanceTimeToForNextBlock(periodEnd(currentPeriod) + timeout)
|
||||||
await expect(
|
await expect(
|
||||||
proofs.markProofAsMissing(slotId, currentPeriod)
|
proofs.markProofAsMissing(slotId, currentPeriod)
|
||||||
).to.be.revertedWith("Validation timed out")
|
).to.be.revertedWith("Proofs_ValidationTimedOut")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("does not mark a received proof as missing", async function () {
|
it("does not mark a received proof as missing", async function () {
|
||||||
@ -267,7 +267,7 @@ describe("Proofs", function () {
|
|||||||
await mine()
|
await mine()
|
||||||
await expect(
|
await expect(
|
||||||
proofs.markProofAsMissing(slotId, receivedPeriod)
|
proofs.markProofAsMissing(slotId, receivedPeriod)
|
||||||
).to.be.revertedWith("Proof was submitted, not missing")
|
).to.be.revertedWith("Proofs_ProofNotMissing")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("does not mark proof as missing when not required", async function () {
|
it("does not mark proof as missing when not required", async function () {
|
||||||
@ -280,7 +280,7 @@ describe("Proofs", function () {
|
|||||||
await mine()
|
await mine()
|
||||||
await expect(
|
await expect(
|
||||||
proofs.markProofAsMissing(slotId, currentPeriod)
|
proofs.markProofAsMissing(slotId, currentPeriod)
|
||||||
).to.be.revertedWith("Proof was not required")
|
).to.be.revertedWith("Proofs_ProofNotRequired")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("does not mark proof as missing twice", async function () {
|
it("does not mark proof as missing twice", async function () {
|
||||||
@ -291,7 +291,7 @@ describe("Proofs", function () {
|
|||||||
await proofs.markProofAsMissing(slotId, missedPeriod)
|
await proofs.markProofAsMissing(slotId, missedPeriod)
|
||||||
await expect(
|
await expect(
|
||||||
proofs.markProofAsMissing(slotId, missedPeriod)
|
proofs.markProofAsMissing(slotId, missedPeriod)
|
||||||
).to.be.revertedWith("Proof already marked as missing")
|
).to.be.revertedWith("Proofs_ProofAlreadyMarkedMissing")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("requires no proofs when slot is finished", async function () {
|
it("requires no proofs when slot is finished", async function () {
|
||||||
|
|||||||
@ -66,7 +66,7 @@ describe("SlotReservations", function () {
|
|||||||
it("cannot reserve a slot more than once", async function () {
|
it("cannot reserve a slot more than once", async function () {
|
||||||
await reservations.reserveSlot(reqId, slotIndex)
|
await reservations.reserveSlot(reqId, slotIndex)
|
||||||
await expect(reservations.reserveSlot(reqId, slotIndex)).to.be.revertedWith(
|
await expect(reservations.reserveSlot(reqId, slotIndex)).to.be.revertedWith(
|
||||||
"Reservation not allowed"
|
"SlotReservations_ReservationNotAllowed"
|
||||||
)
|
)
|
||||||
expect(await reservations.length(id)).to.equal(1)
|
expect(await reservations.length(id)).to.equal(1)
|
||||||
})
|
})
|
||||||
@ -85,7 +85,7 @@ describe("SlotReservations", function () {
|
|||||||
await reservations.reserveSlot(reqId, slotIndex)
|
await reservations.reserveSlot(reqId, slotIndex)
|
||||||
switchAccount(provider)
|
switchAccount(provider)
|
||||||
await expect(reservations.reserveSlot(reqId, slotIndex)).to.be.revertedWith(
|
await expect(reservations.reserveSlot(reqId, slotIndex)).to.be.revertedWith(
|
||||||
"Reservation not allowed"
|
"SlotReservations_ReservationNotAllowed"
|
||||||
)
|
)
|
||||||
expect(await reservations.length(id)).to.equal(3)
|
expect(await reservations.length(id)).to.equal(3)
|
||||||
expect(await reservations.contains(id, provider.address)).to.be.false
|
expect(await reservations.contains(id, provider.address)).to.be.false
|
||||||
@ -105,7 +105,7 @@ describe("SlotReservations", function () {
|
|||||||
it("cannot reserve a slot if not free", async function () {
|
it("cannot reserve a slot if not free", async function () {
|
||||||
await reservations.setSlotState(id, SlotState.Filled)
|
await reservations.setSlotState(id, SlotState.Filled)
|
||||||
await expect(reservations.reserveSlot(reqId, slotIndex)).to.be.revertedWith(
|
await expect(reservations.reserveSlot(reqId, slotIndex)).to.be.revertedWith(
|
||||||
"Reservation not allowed"
|
"SlotReservations_ReservationNotAllowed"
|
||||||
)
|
)
|
||||||
expect(await reservations.length(id)).to.equal(0)
|
expect(await reservations.length(id)).to.equal(0)
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user