From 25df599c086e7ce600812e8d58806aea8e5a3409 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Wed, 21 Sep 2022 12:50:11 +1000 Subject: [PATCH] Update to new reverts API, add tests 1. use `ProviderError` to catch contract errors instaed of `JsonRpcProviderError`. 2. Add tests for `isProofRequired`, `willProofBeRequired`, and `getProofEnd` when slot is empty (and is call is reverted) 3. bump nim-ethers to branch that has new `reverts` API and `ProviderError` support. 4. Update to new `reverts` API. --- codex/contracts/market.nim | 4 ++-- codex/contracts/proofs.nim | 6 +++--- tests/contracts/matchers.nim | 26 -------------------------- tests/contracts/testContracts.nim | 7 +++---- tests/contracts/testMarket.nim | 11 +++++++---- tests/contracts/testProofs.nim | 9 +++++++++ vendor/nim-ethers | 2 +- 7 files changed, 25 insertions(+), 40 deletions(-) delete mode 100644 tests/contracts/matchers.nim diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 5fd7e2aa..b84f1c50 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -40,10 +40,10 @@ method getRequest(market: OnChainMarket, id: RequestId): Future[?StorageRequest] {.async.} = try: return some await market.contract.getRequest(id) - except JsonRpcProviderError as e: + except ProviderError as e: if e.revertReason.contains("Unknown request"): - # Unknown request return none StorageRequest + raise e method getHost(market: OnChainMarket, requestId: RequestId, diff --git a/codex/contracts/proofs.nim b/codex/contracts/proofs.nim index 794c2a7d..f67073a1 100644 --- a/codex/contracts/proofs.nim +++ b/codex/contracts/proofs.nim @@ -28,7 +28,7 @@ method isProofRequired*(proofs: OnChainProofs, id: SlotId): Future[bool] {.async.} = try: return await proofs.storage.isProofRequired(id) - except JsonRpcProviderError as e: + except ProviderError as e: if e.revertReason.contains("Slot empty"): return false raise e @@ -37,7 +37,7 @@ method willProofBeRequired*(proofs: OnChainProofs, id: SlotId): Future[bool] {.async.} = try: return await proofs.storage.willProofBeRequired(id) - except JsonRpcProviderError as e: + except ProviderError as e: if e.revertReason.contains("Slot empty"): return false raise e @@ -46,7 +46,7 @@ method getProofEnd*(proofs: OnChainProofs, id: SlotId): Future[UInt256] {.async.} = try: return await proofs.storage.proofEnd(id) - except JsonRpcProviderError as e: + except ProviderError as e: if e.revertReason.contains("Slot empty"): return 0.u256 raise e diff --git a/tests/contracts/matchers.nim b/tests/contracts/matchers.nim deleted file mode 100644 index 647712bd..00000000 --- a/tests/contracts/matchers.nim +++ /dev/null @@ -1,26 +0,0 @@ -import std/json -import std/strutils -import pkg/asynctest -import pkg/ethers - -proc revertReason*(e: ref ValueError): string = - try: - let json = parseJson(e.msg) - var msg = json{"message"}.getStr - const revertPrefix = - "Error: VM Exception while processing transaction: reverted with " & - "reason string " - msg = msg.replace(revertPrefix) - msg = msg.replace("\'", "") - return msg - except JsonParsingError: - return "" - - -template revertsWith*(reason, body) = - var revertReason = "" - try: - body - except ValueError as e: - revertReason = e.revertReason - check revertReason == reason \ No newline at end of file diff --git a/tests/contracts/testContracts.nim b/tests/contracts/testContracts.nim index fb0455ec..3303b003 100644 --- a/tests/contracts/testContracts.nim +++ b/tests/contracts/testContracts.nim @@ -6,7 +6,6 @@ import codex/contracts/testtoken import codex/storageproofs import ../ethertest import ./examples -import ./matchers import ./time ethersuite "Storage contracts": @@ -82,6 +81,6 @@ ethersuite "Storage contracts": switchAccount(client) let missingPeriod = periodicity.periodOf(await provider.currentTime()) await provider.advanceTime(periodicity.seconds) - check: - revertsWith "Slot not accepting proofs": - await storage.markProofAsMissing(slotId, missingPeriod) + check await storage + .markProofAsMissing(slotId, missingPeriod) + .reverts("Slot not accepting proofs") diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index 85286dad..daeb0944 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -1,8 +1,8 @@ +import std/options import pkg/chronos import pkg/ethers/testing import codex/contracts import codex/contracts/testtoken -import stew/byteutils # delete me import ../ethertest import ./examples import ./time @@ -181,10 +181,13 @@ ethersuite "On-Chain Market": let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled) await provider.advanceTimeTo(request.expiry) # shares expiry with otherRequest - check: - revertsWith "Invalid client address": - await market.withdrawFunds(otherRequest.id) + check await market + .withdrawFunds(otherRequest.id) + .reverts("Invalid client address") check receivedIds.len == 0 await market.withdrawFunds(request.id) check receivedIds == @[request.id] await subscription.unsubscribe() + + test "request is none when unknown": + check isNone await market.getRequest(request.id) diff --git a/tests/contracts/testProofs.nim b/tests/contracts/testProofs.nim index 975ce80d..b1b8c1ca 100644 --- a/tests/contracts/testProofs.nim +++ b/tests/contracts/testProofs.nim @@ -49,3 +49,12 @@ ethersuite "On-Chain Proofs": check receivedProofs == @[proof] await subscription.unsubscribe() + + test "proof not required when slot is empty": + check not await proofs.isProofRequired(contractId) + + test "proof will not be required when slot is empty": + check not await proofs.willProofBeRequired(contractId) + + test "proof end is zero when slot is empty": + check (await proofs.getProofEnd(contractId)) == 0.u256 diff --git a/vendor/nim-ethers b/vendor/nim-ethers index 90d432db..36f2e19f 160000 --- a/vendor/nim-ethers +++ b/vendor/nim-ethers @@ -1 +1 @@ -Subproject commit 90d432db56f74a15c4b5c734da6999cbfa18737f +Subproject commit 36f2e19f9906b1f61c809e6eda5c0fc779ab46ab