codex-contracts-eth/test/Contracts.test.js

151 lines
3.7 KiB
JavaScript
Raw Normal View History

const { expect } = require("chai")
const { ethers } = require("hardhat")
const { hashRequest, hashBid, sign } = require("./marketplace")
const { examples } = require("./examples")
2021-11-01 15:34:01 +00:00
describe("Contracts", function () {
const {
duration,
size,
contentHash,
proofPeriod,
proofTimeout,
price,
nonce,
bidExpiry
} = examples()
let client, host
let contracts
let requestHash, bidHash
let id
beforeEach(async function () {
[client, host] = await ethers.getSigners()
let Contracts = await ethers.getContractFactory("TestContracts")
contracts = await Contracts.deploy()
requestHash = hashRequest(
duration,
size,
contentHash,
proofPeriod,
proofTimeout,
nonce
)
bidHash = hashBid(requestHash, bidExpiry, price)
id = bidHash
})
it("creates a new storage contract", async function () {
await contracts.newContract(
duration,
size,
contentHash,
proofPeriod,
proofTimeout,
nonce,
price,
await host.getAddress(),
bidExpiry,
await sign(client, requestHash),
await sign(host, bidHash)
)
expect(await contracts.duration(id)).to.equal(duration)
expect(await contracts.size(id)).to.equal(size)
expect(await contracts.contentHash(id)).to.equal(contentHash)
expect(await contracts.price(id)).to.equal(price)
expect(await contracts.host(id)).to.equal(await host.getAddress())
})
it("does not allow reuse of contract ids", async function () {
await contracts.newContract(
duration,
size,
contentHash,
proofPeriod,
proofTimeout,
nonce,
price,
await host.getAddress(),
bidExpiry,
await sign(client, requestHash),
await sign(host, bidHash)
)
await expect(contracts.newContract(
duration,
size,
contentHash,
proofPeriod,
proofTimeout,
nonce,
price,
await host.getAddress(),
bidExpiry,
await sign(client, requestHash),
await sign(host, bidHash)
)).to.be.revertedWith("A contract with this id already exists")
})
it("cannot be created when client signature is invalid", async function () {
let invalidHash = hashRequest(
duration + 1,
size,
contentHash,
proofPeriod,
proofTimeout,
nonce
)
let invalidSignature = await sign(client, invalidHash)
await expect(contracts.newContract(
duration,
size,
contentHash,
proofPeriod,
proofTimeout,
nonce,
price,
await host.getAddress(),
bidExpiry,
invalidSignature,
await sign(host, bidHash)
)).to.be.revertedWith("Invalid signature")
})
it("cannot be created when host signature is invalid", async function () {
let invalidBid = hashBid(requestHash, bidExpiry, price - 1)
let invalidSignature = await sign(host, invalidBid)
await expect(contracts.newContract(
duration,
size,
contentHash,
proofPeriod,
proofTimeout,
nonce,
price,
await host.getAddress(),
bidExpiry,
await sign(client, requestHash),
invalidSignature
)).to.be.revertedWith("Invalid signature")
})
it("cannot be created when bid has expired", async function () {
let expired = Math.round(Date.now() / 1000) - 60 // 1 minute ago
let bidHash = hashBid(requestHash, expired, price)
await expect(contracts.newContract(
duration,
size,
contentHash,
proofPeriod,
proofTimeout,
nonce,
price,
await host.getAddress(),
expired,
await sign(client, requestHash),
await sign(host, bidHash),
)).to.be.revertedWith("Bid expired")
})
})