retreive active slot for sales state restoration (#51)

Store slotIndex in slot struct and expose the slot via getActiveSlot. This is to be used when restoring state in the sales module after a node restart.
This commit is contained in:
Eric Mastro 2023-04-14 09:28:39 +10:00 committed by GitHub
parent 63d8ec786a
commit 6e66abbfcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View File

@ -31,6 +31,7 @@ contract Marketplace is Proofs, StateRetrieval {
struct Slot {
SlotState state;
RequestId requestId;
uint256 slotIndex;
/// @notice Tracks the current amount of host's collateral that is to be payed out at the end of Slot's lifespan.
/// @dev When Slot is filled, the collateral is collected in amount of request.ask.collateral
@ -39,6 +40,11 @@ contract Marketplace is Proofs, StateRetrieval {
address host;
}
struct ActiveSlot {
Request request;
uint256 slotIndex;
}
constructor(
IERC20 token_,
MarketplaceConfig memory configuration
@ -83,6 +89,7 @@ contract Marketplace is Proofs, StateRetrieval {
SlotId slotId = Requests.slotId(requestId, slotIndex);
Slot storage slot = _slots[slotId];
slot.requestId = requestId;
slot.slotIndex = slotIndex;
require(slotState(slotId) == SlotState.Free, "Slot is not free");
@ -217,14 +224,17 @@ contract Marketplace is Proofs, StateRetrieval {
require(token.transfer(msg.sender, amount), "Withdraw failed");
}
function getRequestFromSlotId(SlotId slotId)
function getActiveSlot(SlotId slotId)
public
view
slotIsNotFree(slotId)
returns (Request memory)
returns (ActiveSlot memory)
{
Slot storage slot = _slots[slotId];
return _requests[slot.requestId];
ActiveSlot memory activeSlot;
activeSlot.request = _requests[slot.requestId];
activeSlot.slotIndex = slot.slotIndex;
return activeSlot;
}
modifier requestIsKnown(RequestId requestId) {

View File

@ -181,16 +181,16 @@ describe("Marketplace", function () {
})
it("fails to retrieve a request of an empty slot", async function () {
expect(marketplace.getRequestFromSlotId(slotId(slot))).to.be.revertedWith(
expect(marketplace.getActiveSlot(slotId(slot))).to.be.revertedWith(
"Slot is free"
)
})
it("allows retrieval of request of a filled slot", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof)
expect(
await marketplace.getRequestFromSlotId(slotId(slot))
).to.be.request(request)
let activeSlot = await marketplace.getActiveSlot(slotId(slot))
expect(activeSlot.request).to.be.request(request)
expect(activeSlot.slotIndex).to.equal(slot.index)
})
it("is rejected when proof is incorrect", async function () {