From cde543626236bd48188354d842cbe1513052c560 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Tue, 24 Jan 2023 15:59:56 +1100 Subject: [PATCH] get request from slot id (#34) * get request from slot id - Add public function to get request from slot id. - Add chai test assertion that compares requests. Usage: `expect(await marketplace.getRequestFromSlotId(slotId(slot))).to.be.request(request)` This is used when restoring active sales, and a node calls `mySlots`, then iterates the slots and needs originating request details. * merge upstream changes --- contracts/Marketplace.sol | 10 ++++++++++ test/Marketplace.test.js | 26 +++++++++++++++++++++----- test/requests.js | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index 4841b4a..36f5c49 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -216,6 +216,16 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { return _slots[slotId].host; } + function getRequestFromSlotId(SlotId slotId) + public + view + slotIsNotFree(slotId) + returns (Request memory) + { + Slot storage slot = _slots[slotId]; + return _requests[slot.requestId]; + } + modifier requestIsKnown(RequestId requestId) { require(_requests[requestId].client != address(0), "Unknown request"); _; diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index fc43acd..029da1c 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -6,7 +6,11 @@ const { expect } = require("chai") const { exampleConfiguration, exampleRequest } = require("./examples") const { periodic, hours } = require("./time") const { requestId, slotId, askToArray } = require("./ids") -const { RequestState, SlotState } = require("./requests") +const { + RequestState, + SlotState, + enableRequestAssertions, +} = require("./requests") const { waitUntilCancelled, waitUntilStarted, @@ -35,6 +39,8 @@ describe("Marketplace", function () { let request let slot + enableRequestAssertions() + beforeEach(async function () { await snapshot() await ensureMinimumBlockHeight(256) @@ -84,10 +90,7 @@ describe("Marketplace", function () { await token.approve(marketplace.address, price(request)) await marketplace.requestStorage(request) const id = requestId(request) - const retrieved = await marketplace.getRequest(id) - expect(retrieved.client).to.equal(request.client) - expect(retrieved.expiry).to.equal(request.expiry) - expect(retrieved.nonce).to.equal(request.nonce) + expect(await marketplace.getRequest(id)).to.be.request(request) }) it("rejects request with invalid client address", async function () { @@ -137,6 +140,19 @@ describe("Marketplace", function () { expect(await marketplace.getHost(slotId(slot))).to.equal(host.address) }) + it("fails to retrieve a request of an empty slot", async function () { + expect(marketplace.getRequestFromSlotId(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) + }) + it("is rejected when proof is incorrect", async function () { let invalid = hexlify([]) await expect( diff --git a/test/requests.js b/test/requests.js index af30b32..7396a2c 100644 --- a/test/requests.js +++ b/test/requests.js @@ -1,3 +1,5 @@ +const { Assertion } = require("chai") + const RequestState = { New: 0, Started: 1, @@ -14,4 +16,33 @@ const SlotState = { Paid: 4, } -module.exports = { RequestState, SlotState } +const enableRequestAssertions = function () { + // language chain method + Assertion.addMethod("request", function (request) { + var actual = this._obj + + this.assert( + actual.client === request.client, + "expected request #{this} to have client #{exp} but got #{act}", + "expected request #{this} to not have client #{act}, expected #{exp}", + request.client, // expected + actual.client // actual + ) + this.assert( + actual.expiry == request.expiry, + "expected request #{this} to have expiry #{exp} but got #{act}", + "expected request #{this} to not have expiry #{act}, expected #{exp}", + request.expiry, // expected + actual.expiry // actual + ) + this.assert( + actual.nonce === request.nonce, + "expected request #{this} to have nonce #{exp} but got #{act}", + "expected request #{this} to not have nonce #{act}, expected #{exp}", + request.nonce, // expected + actual.nonce // actual + ) + }) +} + +module.exports = { RequestState, SlotState, enableRequestAssertions }