Disallow starting an offer that was not selected

This commit is contained in:
Mark Spanbroek 2022-03-22 11:39:45 +01:00 committed by markspanbroek
parent 0587c2d585
commit c606b455d6
3 changed files with 17 additions and 3 deletions

View File

@ -67,9 +67,9 @@ contract Marketplace is Collateral {
require(request.client == msg.sender, "Only client can select offer"); require(request.client == msg.sender, "Only client can select offer");
RequestState storage state = requestState[offer.requestId]; RequestState storage state = requestState[offer.requestId];
require(!state.offerSelected, "Offer already selected"); require(state.selectedOffer == bytes32(0), "Offer already selected");
state.offerSelected = true; state.selectedOffer = id;
_createLock(id, offer.expiry); _createLock(id, offer.expiry);
_lock(offer.host, id); _lock(offer.host, id);
@ -91,6 +91,10 @@ contract Marketplace is Collateral {
return offers[id]; return offers[id];
} }
function _selectedOffer(bytes32 requestId) internal view returns (bytes32) {
return requestState[requestId].selectedOffer;
}
struct Request { struct Request {
address client; address client;
uint256 duration; uint256 duration;
@ -103,7 +107,7 @@ contract Marketplace is Collateral {
} }
struct RequestState { struct RequestState {
bool offerSelected; bytes32 selectedOffer;
} }
struct Offer { struct Offer {

View File

@ -32,6 +32,7 @@ contract Storage is Collateral, Marketplace, Proofs {
function startContract(bytes32 id) public { function startContract(bytes32 id) public {
Offer storage offer = _offer(id); Offer storage offer = _offer(id);
require(msg.sender == offer.host, "Only host can call this function"); require(msg.sender == offer.host, "Only host can call this function");
require(_selectedOffer(offer.requestId) == id, "Offer was not selected");
Request storage request = _request(offer.requestId); Request storage request = _request(offer.requestId);
_expectProofs(id, request.proofProbability, request.duration); _expectProofs(id, request.proofProbability, request.duration);
} }

View File

@ -70,6 +70,15 @@ describe("Storage", function () {
await storage.startContract(id) await storage.startContract(id)
await expect(storage.startContract(id)).to.be.reverted await expect(storage.startContract(id)).to.be.reverted
}) })
it("can only be done for a selected offer", async function () {
switchAccount(host)
const differentOffer = { ...offer, price: offer.price * 2 }
await storage.offerStorage(differentOffer)
await expect(
storage.startContract(offerId(differentOffer))
).to.be.revertedWith("Offer was not selected")
})
}) })
describe("finishing the contract", function () { describe("finishing the contract", function () {