mirror of
https://github.com/codex-storage/codex-contracts-eth.git
synced 2025-02-20 15:18:44 +00:00
Add client address to requests
This commit is contained in:
parent
b349b76ab7
commit
51e2d65596
@ -22,6 +22,7 @@ contract Marketplace is Collateral {
|
|||||||
marketplaceInvariant
|
marketplaceInvariant
|
||||||
{
|
{
|
||||||
bytes32 id = keccak256(abi.encode(request));
|
bytes32 id = keccak256(abi.encode(request));
|
||||||
|
require(request.client == msg.sender, "Invalid client address");
|
||||||
require(request.size > 0, "Invalid size");
|
require(request.size > 0, "Invalid size");
|
||||||
require(requests[id].size == 0, "Request already exists");
|
require(requests[id].size == 0, "Request already exists");
|
||||||
requests[id] = request;
|
requests[id] = request;
|
||||||
@ -45,6 +46,7 @@ contract Marketplace is Collateral {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Request {
|
struct Request {
|
||||||
|
address client;
|
||||||
uint256 duration;
|
uint256 duration;
|
||||||
uint256 size;
|
uint256 size;
|
||||||
bytes32 contentHash;
|
bytes32 contentHash;
|
||||||
|
@ -5,24 +5,42 @@ const { now, hours } = require("./time")
|
|||||||
const { keccak256, defaultAbiCoder } = ethers.utils
|
const { keccak256, defaultAbiCoder } = ethers.utils
|
||||||
|
|
||||||
describe("Marketplace", function () {
|
describe("Marketplace", function () {
|
||||||
const request = exampleRequest()
|
|
||||||
const offer = { ...exampleOffer(), requestId: requestId(request) }
|
|
||||||
const collateral = 100
|
const collateral = 100
|
||||||
|
|
||||||
let marketplace
|
let marketplace
|
||||||
let token
|
let token
|
||||||
let accounts
|
let client, host
|
||||||
|
let request, offer
|
||||||
|
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
|
;[client, host] = await ethers.getSigners()
|
||||||
|
|
||||||
const TestToken = await ethers.getContractFactory("TestToken")
|
const TestToken = await ethers.getContractFactory("TestToken")
|
||||||
token = await TestToken.deploy()
|
token = await TestToken.deploy()
|
||||||
|
await token.mint(client.address, 1000)
|
||||||
|
await token.mint(host.address, 1000)
|
||||||
|
|
||||||
const Marketplace = await ethers.getContractFactory("Marketplace")
|
const Marketplace = await ethers.getContractFactory("Marketplace")
|
||||||
marketplace = await Marketplace.deploy(token.address, collateral)
|
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 () {
|
describe("requesting storage", function () {
|
||||||
|
beforeEach(function () {
|
||||||
|
switchAccount(client)
|
||||||
|
})
|
||||||
|
|
||||||
it("emits event when storage is requested", async function () {
|
it("emits event when storage is requested", async function () {
|
||||||
await token.approve(marketplace.address, request.maxPrice)
|
await token.approve(marketplace.address, request.maxPrice)
|
||||||
await expect(marketplace.requestStorage(request))
|
await expect(marketplace.requestStorage(request))
|
||||||
@ -30,6 +48,14 @@ describe("Marketplace", function () {
|
|||||||
.withArgs(requestId(request), requestToArray(request))
|
.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 () {
|
it("rejects request with insufficient payment", async function () {
|
||||||
let insufficient = request.maxPrice - 1
|
let insufficient = request.maxPrice - 1
|
||||||
await token.approve(marketplace.address, insufficient)
|
await token.approve(marketplace.address, insufficient)
|
||||||
@ -57,8 +83,10 @@ describe("Marketplace", function () {
|
|||||||
|
|
||||||
describe("offering storage", function () {
|
describe("offering storage", function () {
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
|
switchAccount(client)
|
||||||
await token.approve(marketplace.address, request.maxPrice)
|
await token.approve(marketplace.address, request.maxPrice)
|
||||||
await marketplace.requestStorage(request)
|
await marketplace.requestStorage(request)
|
||||||
|
switchAccount(host)
|
||||||
await token.approve(marketplace.address, collateral)
|
await token.approve(marketplace.address, collateral)
|
||||||
await marketplace.deposit(collateral)
|
await marketplace.deposit(collateral)
|
||||||
})
|
})
|
||||||
@ -114,6 +142,7 @@ function requestId(request) {
|
|||||||
return keccak256(
|
return keccak256(
|
||||||
defaultAbiCoder.encode(
|
defaultAbiCoder.encode(
|
||||||
[
|
[
|
||||||
|
"address",
|
||||||
"uint256",
|
"uint256",
|
||||||
"uint256",
|
"uint256",
|
||||||
"bytes32",
|
"bytes32",
|
||||||
@ -138,6 +167,7 @@ function offerId(offer) {
|
|||||||
|
|
||||||
function requestToArray(request) {
|
function requestToArray(request) {
|
||||||
return [
|
return [
|
||||||
|
request.client,
|
||||||
request.duration,
|
request.duration,
|
||||||
request.size,
|
request.size,
|
||||||
request.contentHash,
|
request.contentHash,
|
||||||
|
@ -3,6 +3,7 @@ const { now, hours } = require("./time")
|
|||||||
const { sha256, hexlify, randomBytes } = ethers.utils
|
const { sha256, hexlify, randomBytes } = ethers.utils
|
||||||
|
|
||||||
const exampleRequest = () => ({
|
const exampleRequest = () => ({
|
||||||
|
client: hexlify(randomBytes(20)),
|
||||||
duration: 150, // 150 blocks ≈ half an hour
|
duration: 150, // 150 blocks ≈ half an hour
|
||||||
size: 1 * 1024 * 1024 * 1024, // 1 Gigabyte
|
size: 1 * 1024 * 1024 * 1024, // 1 Gigabyte
|
||||||
contentHash: sha256("0xdeadbeef"),
|
contentHash: sha256("0xdeadbeef"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user