From 11d8ba55ffaa035afe6113b75c064d5e07cf1506 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 15 Apr 2025 15:41:29 +0200 Subject: [PATCH] contracts: remove testContracts No longer test against the Marketplace contract directly, but only through the Market abstraction Reason: gas estimates are often incorrect, leading to test failures, and the same test scenarios are already handled by testMarkets --- tests/contracts/testContracts.nim | 108 ------------------------------ tests/contracts/testMarket.nim | 10 +++ tests/testContracts.nim | 1 - 3 files changed, 10 insertions(+), 109 deletions(-) delete mode 100644 tests/contracts/testContracts.nim diff --git a/tests/contracts/testContracts.nim b/tests/contracts/testContracts.nim deleted file mode 100644 index 4d17bd2f..00000000 --- a/tests/contracts/testContracts.nim +++ /dev/null @@ -1,108 +0,0 @@ -import pkg/chronos -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 marketplace: Marketplace - var token: Erc20Token - var periodicity: Periodicity - var request: StorageRequest - var slotId: SlotId - var filledAt: StorageTimestamp - - proc expectedPayout(endTimestamp: StorageTimestamp): Tokens = - return request.ask.pricePerSlotPerSecond * filledAt.until(endTimestamp) - - proc switchAccount(account: Signer) = - marketplace = marketplace.connect(account) - token = token.connect(account) - - setup: - client = ethProvider.getSigner(accounts[0]) - host = ethProvider.getSigner(accounts[1]) - - 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.totalPrice.u256).confirm(1) - discard await marketplace.requestStorage(request).confirm(1) - switchAccount(host) - discard - await token.approve(marketplace.address, request.ask.collateralPerSlot.u256).confirm(1) - discard await marketplace.reserveSlot(request.id, 0.uint64).confirm(1) - let receipt = await marketplace.fillSlot(request.id, 0.uint64, proof).confirm(1) - let receiptTime = await ethProvider.blockTime(BlockTag.init(!receipt.blockNumber)) - filledAt = StorageTimestamp.init(receiptTime.stuint(40)) - slotId = request.slotId(0.uint64) - - proc waitUntilProofRequired(slotId: SlotId) {.async.} = - let currentPeriod = - periodicity.periodOf((await ethProvider.currentTime()).truncate(int64)) - await ethProvider.advanceTimeTo(periodicity.periodEnd(currentPeriod).u256) - while not ( - (await marketplace.isProofRequired(slotId)) and - (await marketplace.getPointer(slotId)) < 250 - ) - : - await ethProvider.advanceTime(periodicity.seconds.u256) - - proc startContract() {.async.} = - for slotIndex in 1 ..< request.ask.slots: - discard await token - .approve(marketplace.address, request.ask.collateralPerSlot.u256) - .confirm(1) - discard await marketplace.reserveSlot(request.id, slotIndex.uint64).confirm(1) - discard await marketplace.fillSlot(request.id, slotIndex.uint64, 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()).truncate(int64)) - let endOfPeriod = periodicity.periodEnd(missingPeriod) - await ethProvider.advanceTimeTo(endOfPeriod.u256 + 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) + request.ask.collateralPerSlot).u256 - - 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()).truncate(int64)) - await ethProvider.advanceTime(periodicity.seconds.u256) - expect Marketplace_SlotNotAcceptingProofs: - discard await marketplace.markProofAsMissing(slotId, missingPeriod).confirm(1) diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index c433a50b..5643db4d 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -171,6 +171,16 @@ ethersuite "On-Chain Market": await market.markProofAsMissing(slotId, missingPeriod) check (await marketplace.missingProofs(slotId)) == 1 + test "cannot mark proofs missing for cancelled request": + let slotId = slotId(request, slotIndex) + await market.requestStorage(request) + await advanceToCancelledRequest(request) + let missingPeriod = + periodicity.periodOf((await ethProvider.currentTime()).truncate(int64)) + await advanceToNextPeriod() + expect MarketError: + await market.markProofAsMissing(slotId, missingPeriod) + test "can check whether a proof can be marked as missing": let slotId = slotId(request, slotIndex) await market.requestStorage(request) diff --git a/tests/testContracts.nim b/tests/testContracts.nim index d5ed7d6a..07b25156 100644 --- a/tests/testContracts.nim +++ b/tests/testContracts.nim @@ -1,4 +1,3 @@ -import ./contracts/testContracts import ./contracts/testMarket import ./contracts/testDeployment import ./contracts/testClock