[Storage] Move getRequest() and getHost() to Marketplace

This commit is contained in:
Mark Spanbroek 2023-01-09 13:38:12 +01:00 committed by markspanbroek
parent 124457608d
commit 9b8bcc0bc7
4 changed files with 29 additions and 27 deletions

View File

@ -264,6 +264,10 @@ contract Marketplace is Collateral, Proofs {
return slots[slotId].host; return slots[slotId].host;
} }
function getHost(SlotId slotId) public view returns (address) {
return _host(slotId);
}
function _request(RequestId requestId) function _request(RequestId requestId)
internal internal
view view
@ -274,6 +278,14 @@ contract Marketplace is Collateral, Proofs {
return request; return request;
} }
function getRequest(RequestId requestId)
public
view
returns (Request memory)
{
return _request(requestId);
}
function _slot(SlotId slotId) internal view returns (Slot storage) { function _slot(SlotId slotId) internal view returns (Slot storage) {
Slot storage slot = slots[slotId]; Slot storage slot = slots[slotId];
require(slot.host != address(0), "Slot empty"); require(slot.host != address(0), "Slot empty");

View File

@ -35,22 +35,10 @@ contract Storage is Marketplace {
minCollateralThreshold = _minCollateralThreshold; minCollateralThreshold = _minCollateralThreshold;
} }
function getRequest(RequestId requestId)
public
view
returns (Request memory)
{
return _request(requestId);
}
function getSlot(SlotId slotId) public view returns (Slot memory) { function getSlot(SlotId slotId) public view returns (Slot memory) {
return _slot(slotId); return _slot(slotId);
} }
function getHost(SlotId slotId) public view returns (address) {
return _host(slotId);
}
function missingProofs(SlotId slotId) public view returns (uint256) { function missingProofs(SlotId slotId) public view returns (uint256) {
return _missed(_toProofId(slotId)); return _missed(_toProofId(slotId));
} }

View File

@ -1,5 +1,6 @@
const { ethers } = require("hardhat") const { ethers } = require("hardhat")
const { hexlify, randomBytes } = ethers.utils const { hexlify, randomBytes } = ethers.utils
const { AddressZero } = ethers.constants
const { expect } = require("chai") const { expect } = require("chai")
const { exampleRequest } = require("./examples") const { exampleRequest } = require("./examples")
const { hours, minutes } = require("./time") const { hours, minutes } = require("./time")
@ -85,6 +86,16 @@ describe("Marketplace", function () {
.withArgs(requestId(request), askToArray(request.ask)) .withArgs(requestId(request), askToArray(request.ask))
}) })
it("allows retrieval of request details", async 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)
})
it("rejects request with invalid client address", async function () { it("rejects request with invalid client address", async function () {
let invalid = { ...request, client: host.address } let invalid = { ...request, client: host.address }
await token.approve(marketplace.address, price(invalid)) await token.approve(marketplace.address, price(invalid))
@ -126,6 +137,12 @@ describe("Marketplace", function () {
.withArgs(slot.request, slot.index, slotId(slot)) .withArgs(slot.request, slot.index, slotId(slot))
}) })
it("allows retrieval of host that filled slot", async function () {
expect(await marketplace.getHost(slotId(slot))).to.equal(AddressZero)
await marketplace.fillSlot(slot.request, slot.index, proof)
expect(await marketplace.getHost(slotId(slot))).to.equal(host.address)
})
it("starts requiring storage proofs", async function () { it("starts requiring storage proofs", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof) await marketplace.fillSlot(slot.request, slot.index, proof)
expect(await marketplace.proofEnd(slotId(slot))).to.be.gt(0) expect(await marketplace.proofEnd(slotId(slot))).to.be.gt(0)

View File

@ -2,7 +2,6 @@ const { expect } = require("chai")
const { ethers, deployments } = require("hardhat") const { ethers, deployments } = require("hardhat")
const { BigNumber } = ethers const { BigNumber } = ethers
const { hexlify, randomBytes } = ethers.utils const { hexlify, randomBytes } = ethers.utils
const { AddressZero } = ethers.constants
const { exampleRequest } = require("./examples") const { exampleRequest } = require("./examples")
const { advanceTime, advanceTimeTo, currentTime, mine } = require("./evm") const { advanceTime, advanceTimeTo, currentTime, mine } = require("./evm")
const { requestId, slotId } = require("./ids") const { requestId, slotId } = require("./ids")
@ -59,20 +58,6 @@ describe("Storage", function () {
await storage.deposit(collateralAmount) await storage.deposit(collateralAmount)
}) })
it("can retrieve storage requests", async function () {
const id = requestId(request)
const retrieved = await storage.getRequest(id)
expect(retrieved.client).to.equal(request.client)
expect(retrieved.expiry).to.equal(request.expiry)
expect(retrieved.nonce).to.equal(request.nonce)
})
it("can retrieve host that filled slot", async function () {
expect(await storage.getHost(slotId(slot))).to.equal(AddressZero)
await storage.fillSlot(slot.request, slot.index, proof)
expect(await storage.getHost(slotId(slot))).to.equal(host.address)
})
describe("ending the contract", function () { describe("ending the contract", function () {
it("unlocks the host collateral", async function () { it("unlocks the host collateral", async function () {
await storage.fillSlot(slot.request, slot.index, proof) await storage.fillSlot(slot.request, slot.index, proof)