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.
This commit is contained in:
Eric Mastro 2022-09-21 12:50:11 +10:00 committed by Eric Mastro
parent a9cea802ff
commit 25df599c08
7 changed files with 25 additions and 40 deletions

View File

@ -40,10 +40,10 @@ method getRequest(market: OnChainMarket,
id: RequestId): Future[?StorageRequest] {.async.} = id: RequestId): Future[?StorageRequest] {.async.} =
try: try:
return some await market.contract.getRequest(id) return some await market.contract.getRequest(id)
except JsonRpcProviderError as e: except ProviderError as e:
if e.revertReason.contains("Unknown request"): if e.revertReason.contains("Unknown request"):
# Unknown request
return none StorageRequest return none StorageRequest
raise e
method getHost(market: OnChainMarket, method getHost(market: OnChainMarket,
requestId: RequestId, requestId: RequestId,

View File

@ -28,7 +28,7 @@ method isProofRequired*(proofs: OnChainProofs,
id: SlotId): Future[bool] {.async.} = id: SlotId): Future[bool] {.async.} =
try: try:
return await proofs.storage.isProofRequired(id) return await proofs.storage.isProofRequired(id)
except JsonRpcProviderError as e: except ProviderError as e:
if e.revertReason.contains("Slot empty"): if e.revertReason.contains("Slot empty"):
return false return false
raise e raise e
@ -37,7 +37,7 @@ method willProofBeRequired*(proofs: OnChainProofs,
id: SlotId): Future[bool] {.async.} = id: SlotId): Future[bool] {.async.} =
try: try:
return await proofs.storage.willProofBeRequired(id) return await proofs.storage.willProofBeRequired(id)
except JsonRpcProviderError as e: except ProviderError as e:
if e.revertReason.contains("Slot empty"): if e.revertReason.contains("Slot empty"):
return false return false
raise e raise e
@ -46,7 +46,7 @@ method getProofEnd*(proofs: OnChainProofs,
id: SlotId): Future[UInt256] {.async.} = id: SlotId): Future[UInt256] {.async.} =
try: try:
return await proofs.storage.proofEnd(id) return await proofs.storage.proofEnd(id)
except JsonRpcProviderError as e: except ProviderError as e:
if e.revertReason.contains("Slot empty"): if e.revertReason.contains("Slot empty"):
return 0.u256 return 0.u256
raise e raise e

View File

@ -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

View File

@ -6,7 +6,6 @@ import codex/contracts/testtoken
import codex/storageproofs import codex/storageproofs
import ../ethertest import ../ethertest
import ./examples import ./examples
import ./matchers
import ./time import ./time
ethersuite "Storage contracts": ethersuite "Storage contracts":
@ -82,6 +81,6 @@ ethersuite "Storage contracts":
switchAccount(client) switchAccount(client)
let missingPeriod = periodicity.periodOf(await provider.currentTime()) let missingPeriod = periodicity.periodOf(await provider.currentTime())
await provider.advanceTime(periodicity.seconds) await provider.advanceTime(periodicity.seconds)
check: check await storage
revertsWith "Slot not accepting proofs": .markProofAsMissing(slotId, missingPeriod)
await storage.markProofAsMissing(slotId, missingPeriod) .reverts("Slot not accepting proofs")

View File

@ -1,8 +1,8 @@
import std/options
import pkg/chronos import pkg/chronos
import pkg/ethers/testing import pkg/ethers/testing
import codex/contracts import codex/contracts
import codex/contracts/testtoken import codex/contracts/testtoken
import stew/byteutils # delete me
import ../ethertest import ../ethertest
import ./examples import ./examples
import ./time import ./time
@ -181,10 +181,13 @@ ethersuite "On-Chain Market":
let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled) let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled)
await provider.advanceTimeTo(request.expiry) # shares expiry with otherRequest await provider.advanceTimeTo(request.expiry) # shares expiry with otherRequest
check: check await market
revertsWith "Invalid client address": .withdrawFunds(otherRequest.id)
await market.withdrawFunds(otherRequest.id) .reverts("Invalid client address")
check receivedIds.len == 0 check receivedIds.len == 0
await market.withdrawFunds(request.id) await market.withdrawFunds(request.id)
check receivedIds == @[request.id] check receivedIds == @[request.id]
await subscription.unsubscribe() await subscription.unsubscribe()
test "request is none when unknown":
check isNone await market.getRequest(request.id)

View File

@ -49,3 +49,12 @@ ethersuite "On-Chain Proofs":
check receivedProofs == @[proof] check receivedProofs == @[proof]
await subscription.unsubscribe() 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

2
vendor/nim-ethers vendored

@ -1 +1 @@
Subproject commit 90d432db56f74a15c4b5c734da6999cbfa18737f Subproject commit 36f2e19f9906b1f61c809e6eda5c0fc779ab46ab