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 { ethers } = require("hardhat")
|
||||
const { hashRequest, hashBid, sign } = require("./marketplace")
|
||||
const { examples } = require("./examples")
|
||||
const { exampleRequest, exampleBid } = require("./examples")
|
||||
|
||||
describe("Contracts", function () {
|
||||
|
||||
const {
|
||||
duration,
|
||||
size,
|
||||
contentHash,
|
||||
proofPeriod,
|
||||
proofTimeout,
|
||||
price,
|
||||
nonce,
|
||||
bidExpiry
|
||||
} = examples()
|
||||
const request = exampleRequest()
|
||||
const bid = exampleBid()
|
||||
|
||||
let client, host
|
||||
let contracts
|
||||
|
@ -25,106 +17,92 @@ describe("Contracts", 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)
|
||||
requestHash = hashRequest(request)
|
||||
bidHash = hashBid({...bid, requestHash})
|
||||
id = bidHash
|
||||
})
|
||||
|
||||
it("creates a new storage contract", async function () {
|
||||
await contracts.newContract(
|
||||
duration,
|
||||
size,
|
||||
contentHash,
|
||||
proofPeriod,
|
||||
proofTimeout,
|
||||
nonce,
|
||||
price,
|
||||
request.duration,
|
||||
request.size,
|
||||
request.contentHash,
|
||||
request.proofPeriod,
|
||||
request.proofTimeout,
|
||||
request.nonce,
|
||||
bid.price,
|
||||
await host.getAddress(),
|
||||
bidExpiry,
|
||||
bid.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.duration(id)).to.equal(request.duration)
|
||||
expect(await contracts.size(id)).to.equal(request.size)
|
||||
expect(await contracts.contentHash(id)).to.equal(request.contentHash)
|
||||
expect(await contracts.price(id)).to.equal(bid.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,
|
||||
request.duration,
|
||||
request.size,
|
||||
request.contentHash,
|
||||
request.proofPeriod,
|
||||
request.proofTimeout,
|
||||
request.nonce,
|
||||
bid.price,
|
||||
await host.getAddress(),
|
||||
bidExpiry,
|
||||
bid.bidExpiry,
|
||||
await sign(client, requestHash),
|
||||
await sign(host, bidHash)
|
||||
)
|
||||
await expect(contracts.newContract(
|
||||
duration,
|
||||
size,
|
||||
contentHash,
|
||||
proofPeriod,
|
||||
proofTimeout,
|
||||
nonce,
|
||||
price,
|
||||
request.duration,
|
||||
request.size,
|
||||
request.contentHash,
|
||||
request.proofPeriod,
|
||||
request.proofTimeout,
|
||||
request.nonce,
|
||||
bid.price,
|
||||
await host.getAddress(),
|
||||
bidExpiry,
|
||||
bid.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 invalidHash = hashRequest({...request, duration: request.duration + 1})
|
||||
let invalidSignature = await sign(client, invalidHash)
|
||||
await expect(contracts.newContract(
|
||||
duration,
|
||||
size,
|
||||
contentHash,
|
||||
proofPeriod,
|
||||
proofTimeout,
|
||||
nonce,
|
||||
price,
|
||||
request.duration,
|
||||
request.size,
|
||||
request.contentHash,
|
||||
request.proofPeriod,
|
||||
request.proofTimeout,
|
||||
request.nonce,
|
||||
bid.price,
|
||||
await host.getAddress(),
|
||||
bidExpiry,
|
||||
bid.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 invalidBid = hashBid({...bid, requestHash, price: bid.price - 1})
|
||||
let invalidSignature = await sign(host, invalidBid)
|
||||
await expect(contracts.newContract(
|
||||
duration,
|
||||
size,
|
||||
contentHash,
|
||||
proofPeriod,
|
||||
proofTimeout,
|
||||
nonce,
|
||||
price,
|
||||
request.duration,
|
||||
request.size,
|
||||
request.contentHash,
|
||||
request.proofPeriod,
|
||||
request.proofTimeout,
|
||||
request.nonce,
|
||||
bid.price,
|
||||
await host.getAddress(),
|
||||
bidExpiry,
|
||||
bid.bidExpiry,
|
||||
await sign(client, requestHash),
|
||||
invalidSignature
|
||||
)).to.be.revertedWith("Invalid signature")
|
||||
|
@ -132,15 +110,15 @@ describe("Contracts", function () {
|
|||
|
||||
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)
|
||||
let bidHash = hashBid({...bid, requestHash, bidExpiry: expired})
|
||||
await expect(contracts.newContract(
|
||||
duration,
|
||||
size,
|
||||
contentHash,
|
||||
proofPeriod,
|
||||
proofTimeout,
|
||||
nonce,
|
||||
price,
|
||||
request.duration,
|
||||
request.size,
|
||||
request.contentHash,
|
||||
request.proofPeriod,
|
||||
request.proofTimeout,
|
||||
request.nonce,
|
||||
bid.price,
|
||||
await host.getAddress(),
|
||||
expired,
|
||||
await sign(client, requestHash),
|
||||
|
|
|
@ -1,22 +1,14 @@
|
|||
const { expect } = require("chai")
|
||||
const { ethers } = require("hardhat")
|
||||
const { hashRequest, hashBid, sign } = require("./marketplace")
|
||||
const { examples } = require("./examples")
|
||||
const { exampleRequest, exampleBid } = require("./examples")
|
||||
|
||||
describe("Storage", function () {
|
||||
|
||||
describe("creating a new storage contract", function () {
|
||||
|
||||
const {
|
||||
duration,
|
||||
size,
|
||||
contentHash,
|
||||
proofPeriod,
|
||||
proofTimeout,
|
||||
price,
|
||||
nonce,
|
||||
bidExpiry
|
||||
} = examples()
|
||||
const request = exampleRequest()
|
||||
const bid = exampleBid()
|
||||
|
||||
let contracts
|
||||
let client, host
|
||||
|
@ -26,42 +18,35 @@ describe("Storage", function () {
|
|||
[client, host] = await ethers.getSigners()
|
||||
let StorageContracts = await ethers.getContractFactory("Storage")
|
||||
contracts = await StorageContracts.deploy()
|
||||
let requestHash = hashRequest(
|
||||
duration,
|
||||
size,
|
||||
contentHash,
|
||||
proofPeriod,
|
||||
proofTimeout,
|
||||
nonce
|
||||
)
|
||||
let bidHash = hashBid(requestHash, bidExpiry, price)
|
||||
let requestHash = hashRequest(request)
|
||||
let bidHash = hashBid({...bid, requestHash})
|
||||
id = bidHash
|
||||
await contracts.newContract(
|
||||
duration,
|
||||
size,
|
||||
contentHash,
|
||||
proofPeriod,
|
||||
proofTimeout,
|
||||
nonce,
|
||||
price,
|
||||
request.duration,
|
||||
request.size,
|
||||
request.contentHash,
|
||||
request.proofPeriod,
|
||||
request.proofTimeout,
|
||||
request.nonce,
|
||||
bid.price,
|
||||
await host.getAddress(),
|
||||
bidExpiry,
|
||||
bid.bidExpiry,
|
||||
await sign(client, requestHash),
|
||||
await sign(host, bidHash)
|
||||
)
|
||||
})
|
||||
|
||||
it("created the contract", async function () {
|
||||
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.duration(id)).to.equal(request.duration)
|
||||
expect(await contracts.size(id)).to.equal(request.size)
|
||||
expect(await contracts.contentHash(id)).to.equal(request.contentHash)
|
||||
expect(await contracts.price(id)).to.equal(bid.price)
|
||||
expect(await contracts.host(id)).to.equal(await host.getAddress())
|
||||
})
|
||||
|
||||
it("requires storage proofs", async function (){
|
||||
expect(await contracts.proofPeriod(id)).to.equal(proofPeriod)
|
||||
expect(await contracts.proofTimeout(id)).to.equal(proofTimeout)
|
||||
expect(await contracts.proofPeriod(id)).to.equal(request.proofPeriod)
|
||||
expect(await contracts.proofTimeout(id)).to.equal(request.proofTimeout)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
const { ethers } = require("hardhat")
|
||||
|
||||
const examples = () => ({
|
||||
const exampleRequest = () => ({
|
||||
duration: 31 * 24 * 60 * 60, // 31 days
|
||||
size: 1 * 1024 * 1024 * 1024, // 1 Gigabyte
|
||||
contentHash: ethers.utils.sha256("0xdeadbeef"),
|
||||
proofPeriod: 8, // 8 blocks ≈ 2 minutes
|
||||
proofTimeout: 4, // 4 blocks ≈ 1 minute
|
||||
nonce: ethers.utils.randomBytes(32)
|
||||
})
|
||||
|
||||
const exampleBid = () => ({
|
||||
price: 42,
|
||||
nonce: ethers.utils.randomBytes(32),
|
||||
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")
|
||||
|
||||
function hashRequest(duration, size, hash, proofPeriod, proofTimeout, nonce) {
|
||||
function hashRequest({
|
||||
duration, size, contentHash, proofPeriod, proofTimeout, nonce
|
||||
}) {
|
||||
const type = "[dagger.request.v1]"
|
||||
return ethers.utils.solidityKeccak256(
|
||||
["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]"
|
||||
return ethers.utils.solidityKeccak256(
|
||||
["string", "bytes32", "uint", "uint"],
|
||||
[type, requestHash, expiry, price]
|
||||
[type, requestHash, bidExpiry, price]
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue