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;
}
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");
_;

View File

@ -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(

View File

@ -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 }