add slot index to Slot, support retrieval

Adds slotIndex to the Slot struct and allows retrieval via getRequestFromSlotId.
This commit is contained in:
Eric Mastro 2023-02-09 14:19:08 +11:00
parent fcc28b3931
commit 9751b0de5e
No known key found for this signature in database
GPG Key ID: 141E3048D95A4E63
2 changed files with 6 additions and 3 deletions

View File

@ -31,6 +31,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
struct Slot { struct Slot {
SlotState state; SlotState state;
RequestId requestId; RequestId requestId;
uint256 slotIndex;
address host; address host;
} }
@ -77,6 +78,7 @@ contract Marketplace is Collateral, 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");
@ -148,6 +150,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
slot.state = SlotState.Free; slot.state = SlotState.Free;
slot.host = address(0); slot.host = address(0);
slot.requestId = RequestId.wrap(0); slot.requestId = RequestId.wrap(0);
slot.slotIndex = 0;
context.slotsFilled -= 1; context.slotsFilled -= 1;
emit SlotFreed(requestId, slotId); emit SlotFreed(requestId, slotId);
@ -220,10 +223,10 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
public public
view view
slotIsNotFree(slotId) slotIsNotFree(slotId)
returns (Request memory) returns (Request memory, uint256 slotIndex)
{ {
Slot storage slot = _slots[slotId]; Slot storage slot = _slots[slotId];
return _requests[slot.requestId]; return (_requests[slot.requestId], slot.slotIndex);
} }
modifier requestIsKnown(RequestId requestId) { modifier requestIsKnown(RequestId requestId) {

View File

@ -150,7 +150,7 @@ describe("Marketplace", function () {
await marketplace.fillSlot(slot.request, slot.index, proof) await marketplace.fillSlot(slot.request, slot.index, proof)
expect( expect(
await marketplace.getRequestFromSlotId(slotId(slot)) await marketplace.getRequestFromSlotId(slotId(slot))
).to.be.request(request) ).to.be.request((request, slot.index))
}) })
it("is rejected when proof is incorrect", async function () { it("is rejected when proof is incorrect", async function () {