Add client address to requests

This commit is contained in:
Mark Spanbroek 2022-02-17 11:00:18 +01:00 committed by markspanbroek
parent b349b76ab7
commit 51e2d65596
3 changed files with 38 additions and 5 deletions

View File

@ -22,6 +22,7 @@ contract Marketplace is Collateral {
marketplaceInvariant
{
bytes32 id = keccak256(abi.encode(request));
require(request.client == msg.sender, "Invalid client address");
require(request.size > 0, "Invalid size");
require(requests[id].size == 0, "Request already exists");
requests[id] = request;
@ -45,6 +46,7 @@ contract Marketplace is Collateral {
}
struct Request {
address client;
uint256 duration;
uint256 size;
bytes32 contentHash;

View File

@ -5,24 +5,42 @@ const { now, hours } = require("./time")
const { keccak256, defaultAbiCoder } = ethers.utils
describe("Marketplace", function () {
const request = exampleRequest()
const offer = { ...exampleOffer(), requestId: requestId(request) }
const collateral = 100
let marketplace
let token
let accounts
let client, host
let request, offer
beforeEach(async function () {
;[client, host] = await ethers.getSigners()
const TestToken = await ethers.getContractFactory("TestToken")
token = await TestToken.deploy()
await token.mint(client.address, 1000)
await token.mint(host.address, 1000)
const Marketplace = await ethers.getContractFactory("Marketplace")
marketplace = await Marketplace.deploy(token.address, collateral)
accounts = await ethers.getSigners()
await token.mint(accounts[0].address, 1000)
request = exampleRequest()
request.client = client.address
offer = exampleOffer()
offer.host = host.address
offer.requestId = requestId(request)
})
function switchAccount(account) {
token = token.connect(account)
marketplace = marketplace.connect(account)
}
describe("requesting storage", function () {
beforeEach(function () {
switchAccount(client)
})
it("emits event when storage is requested", async function () {
await token.approve(marketplace.address, request.maxPrice)
await expect(marketplace.requestStorage(request))
@ -30,6 +48,14 @@ describe("Marketplace", function () {
.withArgs(requestId(request), requestToArray(request))
})
it("rejects request with invalid client address", async function () {
let invalid = { ...request, client: host.address }
await token.approve(marketplace.address, invalid.maxPrice)
await expect(marketplace.requestStorage(invalid)).to.be.revertedWith(
"Invalid client address"
)
})
it("rejects request with insufficient payment", async function () {
let insufficient = request.maxPrice - 1
await token.approve(marketplace.address, insufficient)
@ -57,8 +83,10 @@ describe("Marketplace", function () {
describe("offering storage", function () {
beforeEach(async function () {
switchAccount(client)
await token.approve(marketplace.address, request.maxPrice)
await marketplace.requestStorage(request)
switchAccount(host)
await token.approve(marketplace.address, collateral)
await marketplace.deposit(collateral)
})
@ -114,6 +142,7 @@ function requestId(request) {
return keccak256(
defaultAbiCoder.encode(
[
"address",
"uint256",
"uint256",
"bytes32",
@ -138,6 +167,7 @@ function offerId(offer) {
function requestToArray(request) {
return [
request.client,
request.duration,
request.size,
request.contentHash,

View File

@ -3,6 +3,7 @@ const { now, hours } = require("./time")
const { sha256, hexlify, randomBytes } = ethers.utils
const exampleRequest = () => ({
client: hexlify(randomBytes(20)),
duration: 150, // 150 blocks ≈ half an hour
size: 1 * 1024 * 1024 * 1024, // 1 Gigabyte
contentHash: sha256("0xdeadbeef"),