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:
parent
63d8ec786a
commit
6e66abbfcd
|
@ -31,6 +31,7 @@ contract Marketplace is Proofs, StateRetrieval {
|
||||||
struct Slot {
|
struct Slot {
|
||||||
SlotState state;
|
SlotState state;
|
||||||
RequestId requestId;
|
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.
|
/// @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
|
/// @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;
|
address host;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ActiveSlot {
|
||||||
|
Request request;
|
||||||
|
uint256 slotIndex;
|
||||||
|
}
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
IERC20 token_,
|
IERC20 token_,
|
||||||
MarketplaceConfig memory configuration
|
MarketplaceConfig memory configuration
|
||||||
|
@ -83,6 +89,7 @@ contract Marketplace is Proofs, StateRetrieval {
|
||||||
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
||||||
Slot storage slot = _slots[slotId];
|
Slot storage slot = _slots[slotId];
|
||||||
slot.requestId = requestId;
|
slot.requestId = requestId;
|
||||||
|
slot.slotIndex = slotIndex;
|
||||||
|
|
||||||
require(slotState(slotId) == SlotState.Free, "Slot is not free");
|
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");
|
require(token.transfer(msg.sender, amount), "Withdraw failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRequestFromSlotId(SlotId slotId)
|
function getActiveSlot(SlotId slotId)
|
||||||
public
|
public
|
||||||
view
|
view
|
||||||
slotIsNotFree(slotId)
|
slotIsNotFree(slotId)
|
||||||
returns (Request memory)
|
returns (ActiveSlot memory)
|
||||||
{
|
{
|
||||||
Slot storage slot = _slots[slotId];
|
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) {
|
modifier requestIsKnown(RequestId requestId) {
|
||||||
|
|
|
@ -181,16 +181,16 @@ 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.getRequestFromSlotId(slotId(slot))).to.be.revertedWith(
|
expect(marketplace.getActiveSlot(slotId(slot))).to.be.revertedWith(
|
||||||
"Slot is free"
|
"Slot is free"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("allows retrieval of request of a filled slot", async function () {
|
it("allows retrieval of request of a filled slot", async function () {
|
||||||
await marketplace.fillSlot(slot.request, slot.index, proof)
|
await marketplace.fillSlot(slot.request, slot.index, proof)
|
||||||
expect(
|
let activeSlot = await marketplace.getActiveSlot(slotId(slot))
|
||||||
await marketplace.getRequestFromSlotId(slotId(slot))
|
expect(activeSlot.request).to.be.request(request)
|
||||||
).to.be.request(request)
|
expect(activeSlot.slotIndex).to.equal(slot.index)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("is rejected when proof is incorrect", async function () {
|
it("is rejected when proof is incorrect", async function () {
|
||||||
|
|
Loading…
Reference in New Issue