event handlers should't be permitted to raise anything other than a Defect

This commit is contained in:
Viktor Kirilov 2020-05-08 17:20:59 +03:00
parent 0ca6089962
commit f1d3e94dda
4 changed files with 53 additions and 24 deletions

View File

@ -91,14 +91,20 @@ proc test() {.async.} =
let notifFut = newFuture[void]()
var notificationsReceived = 0
let s = await ns.subscribe(Transfer) do(fromAddr, toAddr: Address, value: Uint256):
echo "onTransfer: ", fromAddr, " transferred ", value, " to ", toAddr
inc notificationsReceived
assert(fromAddr == web3.defaultAccount)
assert((notificationsReceived == 1 and value == 50.u256) or
(notificationsReceived == 2 and value == 100.u256))
if notificationsReceived == 2:
notifFut.complete()
let s = await ns.subscribe(Transfer) do (
fromAddr, toAddr: Address, value: Uint256)
{.raises: [Defect], gcsafe.}:
try:
echo "onTransfer: ", fromAddr, " transferred ", value, " to ", toAddr
inc notificationsReceived
assert(fromAddr == web3.defaultAccount)
assert((notificationsReceived == 1 and value == 50.u256) or
(notificationsReceived == 2 and value == 100.u256))
if notificationsReceived == 2:
notifFut.complete()
except Exception as err:
# chronos still raises exceptions which inherit directly from Exception
doAssert false, err.msg
echo "getbalance (now): ", await ns.getBalance(web3.defaultAccount).call()
echo "getbalance (after creation): ", await ns.getBalance(web3.defaultAccount).call(blockNumber = deployedAtBlock)

View File

@ -32,15 +32,21 @@ proc test() {.async.} =
var fut = newFuture[void]()
let s = await ns.subscribe(DepositEvent, %*{"fromBlock": "0x0"}) do(pubkey: Bytes48, withdrawalCredentials: Bytes32, amount: Bytes8, signature: Bytes96, merkleTreeIndex: Bytes8):
echo "onDeposit"
echo "pubkey: ", pubkey
echo "withdrawalCredentials: ", withdrawalCredentials
echo "amount: ", amount
echo "signature: ", signature
echo "merkleTreeIndex: ", merkleTreeIndex
assert(pubkey == pk)
fut.complete()
let s = await ns.subscribe(DepositEvent, %*{"fromBlock": "0x0"}) do (
pubkey: Bytes48, withdrawalCredentials: Bytes32, amount: Bytes8, signature: Bytes96, merkleTreeIndex: Bytes8)
{.raises: [Defect], gcsafe.}:
try:
echo "onDeposit"
echo "pubkey: ", pubkey
echo "withdrawalCredentials: ", withdrawalCredentials
echo "amount: ", amount
echo "signature: ", signature
echo "merkleTreeIndex: ", merkleTreeIndex
assert(pubkey == pk)
fut.complete()
except Exception as err:
# chronos still raises exceptions which inherit directly from Exception
doAssert false, err.msg
discard await ns.deposit(pk, cr, sig, dataRoot).send(value = 32.u256.ethToWei)

View File

@ -62,12 +62,18 @@ proc test() {.async.} =
let notifFut = newFuture[void]()
var notificationsReceived = 0
let s = await ns.subscribe(MyEvent, %*{"fromBlock": "0x0"}) do(sender: Address, value: Uint256):
echo "onEvent: ", sender, " value ", value
inc notificationsReceived
let s = await ns.subscribe(MyEvent, %*{"fromBlock": "0x0"}) do (
sender: Address, value: Uint256)
{.raises: [Defect], gcsafe.}:
try:
echo "onEvent: ", sender, " value ", value
inc notificationsReceived
if notificationsReceived == invocationsBefore + invocationsAfter:
notifFut.complete()
if notificationsReceived == invocationsBefore + invocationsAfter:
notifFut.complete()
except Exception as err:
# chronos still raises exceptions which inherit directly from Exception
doAssert false, err.msg
for i in 1 .. invocationsAfter:
await testInvoke()

View File

@ -648,7 +648,18 @@ macro contract*(cname: untyped, body: untyped): untyped =
procTy = nnkProcTy.newTree(params, newEmptyNode())
signature = getSignature(obj.eventObject)
procTy[1] = nnkPragma.newTree(ident"gcsafe") # TODO: use addPragma in nim 0.20.4 and later
# generated with dumpAstGen - produces "{.raises: [Defect], gcsafe.}"
let pragmas = nnkPragma.newTree(
nnkExprColonExpr.newTree(
newIdentNode("raises"),
nnkBracket.newTree(
newIdentNode("Defect")
)
),
newIdentNode("gcsafe")
)
procTy[1] = pragmas
callWithRawData.add jsonIdent
paramsWithRawData.add nnkIdentDefs.newTree(
@ -658,7 +669,7 @@ macro contract*(cname: untyped, body: untyped): untyped =
)
let procTyWithRawData = nnkProcTy.newTree(paramsWithRawData, newEmptyNode())
procTyWithRawData[1] = nnkPragma.newTree(ident"gcsafe") # TODO: use addPragma in nim 0.20.4 and later
procTyWithRawData[1] = pragmas
result.add quote do:
type `cbident` = object