From f1d3e94dda3435b3dadfbc966fa584f5ab64ff9d Mon Sep 17 00:00:00 2001 From: Viktor Kirilov Date: Fri, 8 May 2020 17:20:59 +0300 Subject: [PATCH] event handlers should't be permitted to raise anything other than a Defect --- tests/test.nim | 22 ++++++++++++++-------- tests/test_deposit_contract.nim | 24 +++++++++++++++--------- tests/test_logs.nim | 16 +++++++++++----- web3.nim | 15 +++++++++++++-- 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/tests/test.nim b/tests/test.nim index 9dcd9c0..cc17f34 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -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) diff --git a/tests/test_deposit_contract.nim b/tests/test_deposit_contract.nim index 1b5ddf4..f5245be 100644 --- a/tests/test_deposit_contract.nim +++ b/tests/test_deposit_contract.nim @@ -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) diff --git a/tests/test_logs.nim b/tests/test_logs.nim index 0e419ec..799a7b1 100644 --- a/tests/test_logs.nim +++ b/tests/test_logs.nim @@ -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() diff --git a/web3.nim b/web3.nim index 34e235c..c79e526 100644 --- a/web3.nim +++ b/web3.nim @@ -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