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
This commit is contained in:
Eric Mastro 2023-01-24 15:59:56 +11:00 committed by GitHub
parent be38c54622
commit cde5436262
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 6 deletions

View File

@ -216,6 +216,16 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
return _slots[slotId].host; 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) { modifier requestIsKnown(RequestId requestId) {
require(_requests[requestId].client != address(0), "Unknown request"); require(_requests[requestId].client != address(0), "Unknown request");
_; _;

View File

@ -6,7 +6,11 @@ const { expect } = require("chai")
const { exampleConfiguration, exampleRequest } = require("./examples") const { exampleConfiguration, exampleRequest } = require("./examples")
const { periodic, hours } = require("./time") const { periodic, hours } = require("./time")
const { requestId, slotId, askToArray } = require("./ids") const { requestId, slotId, askToArray } = require("./ids")
const { RequestState, SlotState } = require("./requests") const {
RequestState,
SlotState,
enableRequestAssertions,
} = require("./requests")
const { const {
waitUntilCancelled, waitUntilCancelled,
waitUntilStarted, waitUntilStarted,
@ -35,6 +39,8 @@ describe("Marketplace", function () {
let request let request
let slot let slot
enableRequestAssertions()
beforeEach(async function () { beforeEach(async function () {
await snapshot() await snapshot()
await ensureMinimumBlockHeight(256) await ensureMinimumBlockHeight(256)
@ -84,10 +90,7 @@ describe("Marketplace", function () {
await token.approve(marketplace.address, price(request)) await token.approve(marketplace.address, price(request))
await marketplace.requestStorage(request) await marketplace.requestStorage(request)
const id = requestId(request) const id = requestId(request)
const retrieved = await marketplace.getRequest(id) expect(await marketplace.getRequest(id)).to.be.request(request)
expect(retrieved.client).to.equal(request.client)
expect(retrieved.expiry).to.equal(request.expiry)
expect(retrieved.nonce).to.equal(request.nonce)
}) })
it("rejects request with invalid client address", async function () { 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) 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 () { it("is rejected when proof is incorrect", async function () {
let invalid = hexlify([]) let invalid = hexlify([])
await expect( await expect(

View File

@ -1,3 +1,5 @@
const { Assertion } = require("chai")
const RequestState = { const RequestState = {
New: 0, New: 0,
Started: 1, Started: 1,
@ -14,4 +16,33 @@ const SlotState = {
Paid: 4, 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 }