mirror of
https://github.com/status-im/nim-codex.git
synced 2025-01-11 03:16:16 +00:00
29433bad9a
* Use http subscriptions instead of websocket for tests To work around this issue when subscriptions are inactive for more than 5 minutes: https://github.com/NomicFoundation/hardhat/issues/2053 Use 100 millisecond polling; default polling interval of 4 seconds is too close to the 5 second timeout for `check eventually`. * use .confirm(1) instead of confirm(0) confirm(0) doesn't wait at all, confirm(1) waits for the transaction to be mined * speed up partial payout integration test * update nim-ethers to version 0.10.0 includes fixes for http polling and .confirm() * fix timing of marketplace tests allow for a bit more time to withdraw funds * use .confirm(1) in marketplace tests to ensure that the transaction has been processed before continuing with the test * fix timing issue in validation unit test * fix proof integration test there were two logic errors in this test: - a slot is freed anyway at the end of the contract - when starting the request takes a long time, the first slot can already be freed because there were too many missing proofs * fix intermittent error in contract tests currentTime() doesn't always correctly reflect the time of the next transaction * reduce number of slots in integration test otherwise the windows runner in the CI won't be able to start the request before it expires * fix timing in purchasing test allow for a bit more time for a request to be submitted * fix timing of request submission in test windows ci is so slow, it can take up to 40 seconds just to submit a storage request to hardhat * increase proof period to 90 seconds * adjust timing of integration tests reason: with the increased period length of 90 seconds, it can take longer to wait for a stable challenge at the beginning of a period. * increase CI timeout to 2 hours * Fix slow builds on windows apparently it takes windows 2-3 seconds to resolve "localhost" to 127.0.0.1 for every json-rpc connection that we make 🤦
125 lines
5.1 KiB
Nim
125 lines
5.1 KiB
Nim
import pkg/chronos
|
|
import pkg/ethers/testing
|
|
import pkg/ethers/erc20
|
|
import codex/contracts
|
|
import ../ethertest
|
|
import ./examples
|
|
import ./time
|
|
import ./deployment
|
|
|
|
ethersuite "Marketplace contracts":
|
|
let proof = Groth16Proof.example
|
|
|
|
var client, host: Signer
|
|
var rewardRecipient, collateralRecipient: Address
|
|
var marketplace: Marketplace
|
|
var token: Erc20Token
|
|
var periodicity: Periodicity
|
|
var request: StorageRequest
|
|
var slotId: SlotId
|
|
var filledAt: UInt256
|
|
|
|
proc expectedPayout(endTimestamp: UInt256): UInt256 =
|
|
return (endTimestamp - filledAt) * request.ask.reward
|
|
|
|
proc switchAccount(account: Signer) =
|
|
marketplace = marketplace.connect(account)
|
|
token = token.connect(account)
|
|
|
|
setup:
|
|
client = ethProvider.getSigner(accounts[0])
|
|
host = ethProvider.getSigner(accounts[1])
|
|
rewardRecipient = accounts[2]
|
|
collateralRecipient = accounts[3]
|
|
|
|
let address = Marketplace.address(dummyVerifier = true)
|
|
marketplace = Marketplace.new(address, ethProvider.getSigner())
|
|
|
|
let tokenAddress = await marketplace.token()
|
|
token = Erc20Token.new(tokenAddress, ethProvider.getSigner())
|
|
|
|
let config = await marketplace.configuration()
|
|
periodicity = Periodicity(seconds: config.proofs.period)
|
|
|
|
request = StorageRequest.example
|
|
request.client = await client.getAddress()
|
|
|
|
switchAccount(client)
|
|
discard await token.approve(marketplace.address, request.price).confirm(1)
|
|
discard await marketplace.requestStorage(request).confirm(1)
|
|
switchAccount(host)
|
|
discard await token.approve(marketplace.address, request.ask.collateral).confirm(1)
|
|
discard await marketplace.reserveSlot(request.id, 0.u256).confirm(1)
|
|
let receipt = await marketplace.fillSlot(request.id, 0.u256, proof).confirm(1)
|
|
filledAt = await ethProvider.blockTime(BlockTag.init(!receipt.blockNumber))
|
|
slotId = request.slotId(0.u256)
|
|
|
|
proc waitUntilProofRequired(slotId: SlotId) {.async.} =
|
|
let currentPeriod = periodicity.periodOf(await ethProvider.currentTime())
|
|
await ethProvider.advanceTimeTo(periodicity.periodEnd(currentPeriod))
|
|
while not (
|
|
(await marketplace.isProofRequired(slotId)) and
|
|
(await marketplace.getPointer(slotId)) < 250
|
|
):
|
|
await ethProvider.advanceTime(periodicity.seconds)
|
|
|
|
proc startContract() {.async.} =
|
|
for slotIndex in 1..<request.ask.slots:
|
|
discard await token.approve(marketplace.address, request.ask.collateral).confirm(1)
|
|
discard await marketplace.reserveSlot(request.id, slotIndex.u256).confirm(1)
|
|
discard await marketplace.fillSlot(request.id, slotIndex.u256, proof).confirm(1)
|
|
|
|
test "accept marketplace proofs":
|
|
switchAccount(host)
|
|
await waitUntilProofRequired(slotId)
|
|
discard await marketplace.submitProof(slotId, proof).confirm(1)
|
|
|
|
test "can mark missing proofs":
|
|
switchAccount(host)
|
|
await waitUntilProofRequired(slotId)
|
|
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
|
|
let endOfPeriod = periodicity.periodEnd(missingPeriod)
|
|
await ethProvider.advanceTimeTo(endOfPeriod + 1)
|
|
switchAccount(client)
|
|
discard await marketplace.markProofAsMissing(slotId, missingPeriod).confirm(1)
|
|
|
|
test "can be paid out at the end":
|
|
switchAccount(host)
|
|
let address = await host.getAddress()
|
|
await startContract()
|
|
let requestEnd = await marketplace.requestEnd(request.id)
|
|
await ethProvider.advanceTimeTo(requestEnd.u256 + 1)
|
|
let startBalance = await token.balanceOf(address)
|
|
discard await marketplace.freeSlot(slotId).confirm(1)
|
|
let endBalance = await token.balanceOf(address)
|
|
check endBalance == (startBalance + expectedPayout(requestEnd.u256) + request.ask.collateral)
|
|
|
|
test "can be paid out at the end, specifying reward and collateral recipient":
|
|
switchAccount(host)
|
|
let hostAddress = await host.getAddress()
|
|
await startContract()
|
|
let requestEnd = await marketplace.requestEnd(request.id)
|
|
await ethProvider.advanceTimeTo(requestEnd.u256 + 1)
|
|
let startBalanceHost = await token.balanceOf(hostAddress)
|
|
let startBalanceReward = await token.balanceOf(rewardRecipient)
|
|
let startBalanceCollateral = await token.balanceOf(collateralRecipient)
|
|
discard await marketplace.freeSlot(slotId, rewardRecipient, collateralRecipient).confirm(1)
|
|
let endBalanceHost = await token.balanceOf(hostAddress)
|
|
let endBalanceReward = await token.balanceOf(rewardRecipient)
|
|
let endBalanceCollateral = await token.balanceOf(collateralRecipient)
|
|
|
|
check endBalanceHost == startBalanceHost
|
|
check endBalanceReward == (startBalanceReward + expectedPayout(requestEnd.u256))
|
|
check endBalanceCollateral == (startBalanceCollateral + request.ask.collateral)
|
|
|
|
test "cannot mark proofs missing for cancelled request":
|
|
let expiry = await marketplace.requestExpiry(request.id)
|
|
await ethProvider.advanceTimeTo((expiry + 1).u256)
|
|
switchAccount(client)
|
|
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
|
|
await ethProvider.advanceTime(periodicity.seconds)
|
|
check await marketplace
|
|
.markProofAsMissing(slotId, missingPeriod)
|
|
.confirm(1)
|
|
.reverts("Slot not accepting proofs")
|