mirror of
https://github.com/status-im/codex-contracts-eth.git
synced 2025-02-25 14:45:35 +00:00
[marketplace] set request state to started once fulfilled
Once all slots are filled, the request state is set to Started.
This commit is contained in:
parent
08a3fbb12b
commit
1933ed489a
@ -9,7 +9,7 @@ contract Marketplace is Collateral, Proofs {
|
|||||||
uint256 public immutable collateral;
|
uint256 public immutable collateral;
|
||||||
MarketplaceFunds private funds;
|
MarketplaceFunds private funds;
|
||||||
mapping(bytes32 => Request) private requests;
|
mapping(bytes32 => Request) private requests;
|
||||||
mapping(bytes32 => RequestState) private requestState;
|
mapping(bytes32 => RequestContext) private requestContexts;
|
||||||
mapping(bytes32 => Slot) private slots;
|
mapping(bytes32 => Slot) private slots;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@ -56,6 +56,10 @@ contract Marketplace is Collateral, Proofs {
|
|||||||
require(request.client != address(0), "Unknown request");
|
require(request.client != address(0), "Unknown request");
|
||||||
require(request.expiry > block.timestamp, "Request expired");
|
require(request.expiry > block.timestamp, "Request expired");
|
||||||
require(slotIndex < request.ask.slots, "Invalid slot");
|
require(slotIndex < request.ask.slots, "Invalid slot");
|
||||||
|
RequestContext storage context = requestContexts[requestId];
|
||||||
|
// TODO: in the case of repair, update below require condition by adding
|
||||||
|
// || context.state == RequestState.Started
|
||||||
|
require(context.state == RequestState.New, "Invalid state");
|
||||||
|
|
||||||
bytes32 slotId = keccak256(abi.encode(requestId, slotIndex));
|
bytes32 slotId = keccak256(abi.encode(requestId, slotIndex));
|
||||||
Slot storage slot = slots[slotId];
|
Slot storage slot = slots[slotId];
|
||||||
@ -67,11 +71,11 @@ contract Marketplace is Collateral, Proofs {
|
|||||||
_expectProofs(slotId, request.ask.proofProbability, request.ask.duration);
|
_expectProofs(slotId, request.ask.proofProbability, request.ask.duration);
|
||||||
_submitProof(slotId, proof);
|
_submitProof(slotId, proof);
|
||||||
|
|
||||||
RequestState storage state = requestState[requestId];
|
|
||||||
slot.host = msg.sender;
|
slot.host = msg.sender;
|
||||||
state.slotsFilled += 1;
|
context.slotsFilled += 1;
|
||||||
emit SlotFilled(requestId, slotIndex, slotId);
|
emit SlotFilled(requestId, slotIndex, slotId);
|
||||||
if (state.slotsFilled == request.ask.slots) {
|
if (context.slotsFilled == request.ask.slots) {
|
||||||
|
context.state = RequestState.Started;
|
||||||
emit RequestFulfilled(requestId);
|
emit RequestFulfilled(requestId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,6 +124,10 @@ contract Marketplace is Collateral, Proofs {
|
|||||||
return request.ask.duration * request.ask.reward;
|
return request.ask.duration * request.ask.reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function state(bytes32 requestId) public view returns (RequestState) {
|
||||||
|
return requestContexts[requestId].state;
|
||||||
|
}
|
||||||
|
|
||||||
struct Request {
|
struct Request {
|
||||||
address client;
|
address client;
|
||||||
Ask ask;
|
Ask ask;
|
||||||
@ -152,8 +160,16 @@ contract Marketplace is Collateral, Proofs {
|
|||||||
bytes name; // random name
|
bytes name; // random name
|
||||||
}
|
}
|
||||||
|
|
||||||
struct RequestState {
|
enum RequestState {
|
||||||
|
New, // [default] waiting to fill slots
|
||||||
|
Started, // all slots filled, accepting regular proofs
|
||||||
|
Cancelled, // not enough slots filled before expiry
|
||||||
|
Finished // successfully completed
|
||||||
|
}
|
||||||
|
|
||||||
|
struct RequestContext {
|
||||||
uint256 slotsFilled;
|
uint256 slotsFilled;
|
||||||
|
RequestState state;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Slot {
|
struct Slot {
|
||||||
|
@ -251,5 +251,12 @@ describe("Marketplace", function () {
|
|||||||
.to.emit(marketplace, "RequestFulfilled")
|
.to.emit(marketplace, "RequestFulfilled")
|
||||||
.withArgs(requestId(request))
|
.withArgs(requestId(request))
|
||||||
})
|
})
|
||||||
|
it("sets state when all slots are filled", async function () {
|
||||||
|
const lastSlot = request.ask.slots - 1
|
||||||
|
for (let i = 0; i <= lastSlot; i++) {
|
||||||
|
await marketplace.fillSlot(slot.request, i, proof)
|
||||||
|
}
|
||||||
|
await expect(await marketplace.state(slot.request)).to.equal(1)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user