Use 'request' and 'bid' objects in tests
This commit is contained in:
parent
a5e05018ad
commit
5eb571fe1c
|
@ -1,20 +1,12 @@
|
||||||
const { expect } = require("chai")
|
const { expect } = require("chai")
|
||||||
const { ethers } = require("hardhat")
|
const { ethers } = require("hardhat")
|
||||||
const { hashRequest, hashBid, sign } = require("./marketplace")
|
const { hashRequest, hashBid, sign } = require("./marketplace")
|
||||||
const { examples } = require("./examples")
|
const { exampleRequest, exampleBid } = require("./examples")
|
||||||
|
|
||||||
describe("Contracts", function () {
|
describe("Contracts", function () {
|
||||||
|
|
||||||
const {
|
const request = exampleRequest()
|
||||||
duration,
|
const bid = exampleBid()
|
||||||
size,
|
|
||||||
contentHash,
|
|
||||||
proofPeriod,
|
|
||||||
proofTimeout,
|
|
||||||
price,
|
|
||||||
nonce,
|
|
||||||
bidExpiry
|
|
||||||
} = examples()
|
|
||||||
|
|
||||||
let client, host
|
let client, host
|
||||||
let contracts
|
let contracts
|
||||||
|
@ -25,106 +17,92 @@ describe("Contracts", function () {
|
||||||
[client, host] = await ethers.getSigners()
|
[client, host] = await ethers.getSigners()
|
||||||
let Contracts = await ethers.getContractFactory("TestContracts")
|
let Contracts = await ethers.getContractFactory("TestContracts")
|
||||||
contracts = await Contracts.deploy()
|
contracts = await Contracts.deploy()
|
||||||
requestHash = hashRequest(
|
requestHash = hashRequest(request)
|
||||||
duration,
|
bidHash = hashBid({...bid, requestHash})
|
||||||
size,
|
|
||||||
contentHash,
|
|
||||||
proofPeriod,
|
|
||||||
proofTimeout,
|
|
||||||
nonce
|
|
||||||
)
|
|
||||||
bidHash = hashBid(requestHash, bidExpiry, price)
|
|
||||||
id = bidHash
|
id = bidHash
|
||||||
})
|
})
|
||||||
|
|
||||||
it("creates a new storage contract", async function () {
|
it("creates a new storage contract", async function () {
|
||||||
await contracts.newContract(
|
await contracts.newContract(
|
||||||
duration,
|
request.duration,
|
||||||
size,
|
request.size,
|
||||||
contentHash,
|
request.contentHash,
|
||||||
proofPeriod,
|
request.proofPeriod,
|
||||||
proofTimeout,
|
request.proofTimeout,
|
||||||
nonce,
|
request.nonce,
|
||||||
price,
|
bid.price,
|
||||||
await host.getAddress(),
|
await host.getAddress(),
|
||||||
bidExpiry,
|
bid.bidExpiry,
|
||||||
await sign(client, requestHash),
|
await sign(client, requestHash),
|
||||||
await sign(host, bidHash)
|
await sign(host, bidHash)
|
||||||
)
|
)
|
||||||
expect(await contracts.duration(id)).to.equal(duration)
|
expect(await contracts.duration(id)).to.equal(request.duration)
|
||||||
expect(await contracts.size(id)).to.equal(size)
|
expect(await contracts.size(id)).to.equal(request.size)
|
||||||
expect(await contracts.contentHash(id)).to.equal(contentHash)
|
expect(await contracts.contentHash(id)).to.equal(request.contentHash)
|
||||||
expect(await contracts.price(id)).to.equal(price)
|
expect(await contracts.price(id)).to.equal(bid.price)
|
||||||
expect(await contracts.host(id)).to.equal(await host.getAddress())
|
expect(await contracts.host(id)).to.equal(await host.getAddress())
|
||||||
})
|
})
|
||||||
|
|
||||||
it("does not allow reuse of contract ids", async function () {
|
it("does not allow reuse of contract ids", async function () {
|
||||||
await contracts.newContract(
|
await contracts.newContract(
|
||||||
duration,
|
request.duration,
|
||||||
size,
|
request.size,
|
||||||
contentHash,
|
request.contentHash,
|
||||||
proofPeriod,
|
request.proofPeriod,
|
||||||
proofTimeout,
|
request.proofTimeout,
|
||||||
nonce,
|
request.nonce,
|
||||||
price,
|
bid.price,
|
||||||
await host.getAddress(),
|
await host.getAddress(),
|
||||||
bidExpiry,
|
bid.bidExpiry,
|
||||||
await sign(client, requestHash),
|
await sign(client, requestHash),
|
||||||
await sign(host, bidHash)
|
await sign(host, bidHash)
|
||||||
)
|
)
|
||||||
await expect(contracts.newContract(
|
await expect(contracts.newContract(
|
||||||
duration,
|
request.duration,
|
||||||
size,
|
request.size,
|
||||||
contentHash,
|
request.contentHash,
|
||||||
proofPeriod,
|
request.proofPeriod,
|
||||||
proofTimeout,
|
request.proofTimeout,
|
||||||
nonce,
|
request.nonce,
|
||||||
price,
|
bid.price,
|
||||||
await host.getAddress(),
|
await host.getAddress(),
|
||||||
bidExpiry,
|
bid.bidExpiry,
|
||||||
await sign(client, requestHash),
|
await sign(client, requestHash),
|
||||||
await sign(host, bidHash)
|
await sign(host, bidHash)
|
||||||
)).to.be.revertedWith("A contract with this id already exists")
|
)).to.be.revertedWith("A contract with this id already exists")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("cannot be created when client signature is invalid", async function () {
|
it("cannot be created when client signature is invalid", async function () {
|
||||||
let invalidHash = hashRequest(
|
let invalidHash = hashRequest({...request, duration: request.duration + 1})
|
||||||
duration + 1,
|
|
||||||
size,
|
|
||||||
contentHash,
|
|
||||||
proofPeriod,
|
|
||||||
proofTimeout,
|
|
||||||
nonce
|
|
||||||
)
|
|
||||||
let invalidSignature = await sign(client, invalidHash)
|
let invalidSignature = await sign(client, invalidHash)
|
||||||
await expect(contracts.newContract(
|
await expect(contracts.newContract(
|
||||||
duration,
|
request.duration,
|
||||||
size,
|
request.size,
|
||||||
contentHash,
|
request.contentHash,
|
||||||
proofPeriod,
|
request.proofPeriod,
|
||||||
proofTimeout,
|
request.proofTimeout,
|
||||||
nonce,
|
request.nonce,
|
||||||
price,
|
bid.price,
|
||||||
await host.getAddress(),
|
await host.getAddress(),
|
||||||
bidExpiry,
|
bid.bidExpiry,
|
||||||
invalidSignature,
|
invalidSignature,
|
||||||
await sign(host, bidHash)
|
await sign(host, bidHash)
|
||||||
)).to.be.revertedWith("Invalid signature")
|
)).to.be.revertedWith("Invalid signature")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("cannot be created when host signature is invalid", async function () {
|
it("cannot be created when host signature is invalid", async function () {
|
||||||
let invalidBid = hashBid(requestHash, bidExpiry, price - 1)
|
let invalidBid = hashBid({...bid, requestHash, price: bid.price - 1})
|
||||||
let invalidSignature = await sign(host, invalidBid)
|
let invalidSignature = await sign(host, invalidBid)
|
||||||
await expect(contracts.newContract(
|
await expect(contracts.newContract(
|
||||||
duration,
|
request.duration,
|
||||||
size,
|
request.size,
|
||||||
contentHash,
|
request.contentHash,
|
||||||
proofPeriod,
|
request.proofPeriod,
|
||||||
proofTimeout,
|
request.proofTimeout,
|
||||||
nonce,
|
request.nonce,
|
||||||
price,
|
bid.price,
|
||||||
await host.getAddress(),
|
await host.getAddress(),
|
||||||
bidExpiry,
|
bid.bidExpiry,
|
||||||
await sign(client, requestHash),
|
await sign(client, requestHash),
|
||||||
invalidSignature
|
invalidSignature
|
||||||
)).to.be.revertedWith("Invalid signature")
|
)).to.be.revertedWith("Invalid signature")
|
||||||
|
@ -132,15 +110,15 @@ describe("Contracts", function () {
|
||||||
|
|
||||||
it("cannot be created when bid has expired", async function () {
|
it("cannot be created when bid has expired", async function () {
|
||||||
let expired = Math.round(Date.now() / 1000) - 60 // 1 minute ago
|
let expired = Math.round(Date.now() / 1000) - 60 // 1 minute ago
|
||||||
let bidHash = hashBid(requestHash, expired, price)
|
let bidHash = hashBid({...bid, requestHash, bidExpiry: expired})
|
||||||
await expect(contracts.newContract(
|
await expect(contracts.newContract(
|
||||||
duration,
|
request.duration,
|
||||||
size,
|
request.size,
|
||||||
contentHash,
|
request.contentHash,
|
||||||
proofPeriod,
|
request.proofPeriod,
|
||||||
proofTimeout,
|
request.proofTimeout,
|
||||||
nonce,
|
request.nonce,
|
||||||
price,
|
bid.price,
|
||||||
await host.getAddress(),
|
await host.getAddress(),
|
||||||
expired,
|
expired,
|
||||||
await sign(client, requestHash),
|
await sign(client, requestHash),
|
||||||
|
|
|
@ -1,22 +1,14 @@
|
||||||
const { expect } = require("chai")
|
const { expect } = require("chai")
|
||||||
const { ethers } = require("hardhat")
|
const { ethers } = require("hardhat")
|
||||||
const { hashRequest, hashBid, sign } = require("./marketplace")
|
const { hashRequest, hashBid, sign } = require("./marketplace")
|
||||||
const { examples } = require("./examples")
|
const { exampleRequest, exampleBid } = require("./examples")
|
||||||
|
|
||||||
describe("Storage", function () {
|
describe("Storage", function () {
|
||||||
|
|
||||||
describe("creating a new storage contract", function () {
|
describe("creating a new storage contract", function () {
|
||||||
|
|
||||||
const {
|
const request = exampleRequest()
|
||||||
duration,
|
const bid = exampleBid()
|
||||||
size,
|
|
||||||
contentHash,
|
|
||||||
proofPeriod,
|
|
||||||
proofTimeout,
|
|
||||||
price,
|
|
||||||
nonce,
|
|
||||||
bidExpiry
|
|
||||||
} = examples()
|
|
||||||
|
|
||||||
let contracts
|
let contracts
|
||||||
let client, host
|
let client, host
|
||||||
|
@ -26,42 +18,35 @@ describe("Storage", function () {
|
||||||
[client, host] = await ethers.getSigners()
|
[client, host] = await ethers.getSigners()
|
||||||
let StorageContracts = await ethers.getContractFactory("Storage")
|
let StorageContracts = await ethers.getContractFactory("Storage")
|
||||||
contracts = await StorageContracts.deploy()
|
contracts = await StorageContracts.deploy()
|
||||||
let requestHash = hashRequest(
|
let requestHash = hashRequest(request)
|
||||||
duration,
|
let bidHash = hashBid({...bid, requestHash})
|
||||||
size,
|
|
||||||
contentHash,
|
|
||||||
proofPeriod,
|
|
||||||
proofTimeout,
|
|
||||||
nonce
|
|
||||||
)
|
|
||||||
let bidHash = hashBid(requestHash, bidExpiry, price)
|
|
||||||
id = bidHash
|
id = bidHash
|
||||||
await contracts.newContract(
|
await contracts.newContract(
|
||||||
duration,
|
request.duration,
|
||||||
size,
|
request.size,
|
||||||
contentHash,
|
request.contentHash,
|
||||||
proofPeriod,
|
request.proofPeriod,
|
||||||
proofTimeout,
|
request.proofTimeout,
|
||||||
nonce,
|
request.nonce,
|
||||||
price,
|
bid.price,
|
||||||
await host.getAddress(),
|
await host.getAddress(),
|
||||||
bidExpiry,
|
bid.bidExpiry,
|
||||||
await sign(client, requestHash),
|
await sign(client, requestHash),
|
||||||
await sign(host, bidHash)
|
await sign(host, bidHash)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("created the contract", async function () {
|
it("created the contract", async function () {
|
||||||
expect(await contracts.duration(id)).to.equal(duration)
|
expect(await contracts.duration(id)).to.equal(request.duration)
|
||||||
expect(await contracts.size(id)).to.equal(size)
|
expect(await contracts.size(id)).to.equal(request.size)
|
||||||
expect(await contracts.contentHash(id)).to.equal(contentHash)
|
expect(await contracts.contentHash(id)).to.equal(request.contentHash)
|
||||||
expect(await contracts.price(id)).to.equal(price)
|
expect(await contracts.price(id)).to.equal(bid.price)
|
||||||
expect(await contracts.host(id)).to.equal(await host.getAddress())
|
expect(await contracts.host(id)).to.equal(await host.getAddress())
|
||||||
})
|
})
|
||||||
|
|
||||||
it("requires storage proofs", async function (){
|
it("requires storage proofs", async function (){
|
||||||
expect(await contracts.proofPeriod(id)).to.equal(proofPeriod)
|
expect(await contracts.proofPeriod(id)).to.equal(request.proofPeriod)
|
||||||
expect(await contracts.proofTimeout(id)).to.equal(proofTimeout)
|
expect(await contracts.proofTimeout(id)).to.equal(request.proofTimeout)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
const { ethers } = require("hardhat")
|
const { ethers } = require("hardhat")
|
||||||
|
|
||||||
const examples = () => ({
|
const exampleRequest = () => ({
|
||||||
duration: 31 * 24 * 60 * 60, // 31 days
|
duration: 31 * 24 * 60 * 60, // 31 days
|
||||||
size: 1 * 1024 * 1024 * 1024, // 1 Gigabyte
|
size: 1 * 1024 * 1024 * 1024, // 1 Gigabyte
|
||||||
contentHash: ethers.utils.sha256("0xdeadbeef"),
|
contentHash: ethers.utils.sha256("0xdeadbeef"),
|
||||||
proofPeriod: 8, // 8 blocks ≈ 2 minutes
|
proofPeriod: 8, // 8 blocks ≈ 2 minutes
|
||||||
proofTimeout: 4, // 4 blocks ≈ 1 minute
|
proofTimeout: 4, // 4 blocks ≈ 1 minute
|
||||||
|
nonce: ethers.utils.randomBytes(32)
|
||||||
|
})
|
||||||
|
|
||||||
|
const exampleBid = () => ({
|
||||||
price: 42,
|
price: 42,
|
||||||
nonce: ethers.utils.randomBytes(32),
|
|
||||||
bidExpiry: Math.round(Date.now() / 1000) + 60 * 60 // 1 hour from now
|
bidExpiry: Math.round(Date.now() / 1000) + 60 * 60 // 1 hour from now
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = { examples }
|
module.exports = { exampleRequest, exampleBid }
|
||||||
|
|
|
@ -1,18 +1,20 @@
|
||||||
const { ethers } = require("hardhat")
|
const { ethers } = require("hardhat")
|
||||||
|
|
||||||
function hashRequest(duration, size, hash, proofPeriod, proofTimeout, nonce) {
|
function hashRequest({
|
||||||
|
duration, size, contentHash, proofPeriod, proofTimeout, nonce
|
||||||
|
}) {
|
||||||
const type = "[dagger.request.v1]"
|
const type = "[dagger.request.v1]"
|
||||||
return ethers.utils.solidityKeccak256(
|
return ethers.utils.solidityKeccak256(
|
||||||
["string", "uint", "uint", "bytes32", "uint", "uint", "bytes32"],
|
["string", "uint", "uint", "bytes32", "uint", "uint", "bytes32"],
|
||||||
[type, duration, size, hash, proofPeriod, proofTimeout, nonce]
|
[type, duration, size, contentHash, proofPeriod, proofTimeout, nonce]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function hashBid(requestHash, expiry, price) {
|
function hashBid({requestHash, bidExpiry, price}) {
|
||||||
const type = "[dagger.bid.v1]"
|
const type = "[dagger.bid.v1]"
|
||||||
return ethers.utils.solidityKeccak256(
|
return ethers.utils.solidityKeccak256(
|
||||||
["string", "bytes32", "uint", "uint"],
|
["string", "bytes32", "uint", "uint"],
|
||||||
[type, requestHash, expiry, price]
|
[type, requestHash, bidExpiry, price]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue