From f84bb097228caf082769d50a2ad28661d8383b63 Mon Sep 17 00:00:00 2001 From: oskarth Date: Mon, 19 Jul 2021 09:12:25 +0000 Subject: [PATCH] deploy: 8998d0f45fd13904205e851a0f8b1d0d2db1b8f8 --- waku/v2/protocol/waku_swap/waku_swap.nim | 23 ++++++++++++------- .../v2/protocol/waku_swap/waku_swap_types.nim | 4 +++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/waku/v2/protocol/waku_swap/waku_swap.nim b/waku/v2/protocol/waku_swap/waku_swap.nim index 70266a42e..99a08fb77 100644 --- a/waku/v2/protocol/waku_swap/waku_swap.nim +++ b/waku/v2/protocol/waku_swap/waku_swap.nim @@ -21,12 +21,16 @@ ## Things like settlement is for future work. ## +{.push raises: [Defect].} + +# TODO Generally clean up errors here, a lot of Exceptions, Defects and KeyErros +# +# On KeyEror specifically: # Accessing Table's items is prone to KeyError exception when the key does not belong to the table -# such exception can be avoided by calling hasKey() before accessing the key (which is the case in this module) +# such exception can be avoided by calling hasKey() before accessing the key (which is the case in this module) # but from the compiler point of view, the use of hasKey() does not make any difference in the potential exceptions -# @TODO thus any key access should be wrapped inside try-except -# @TODO or otherwise the exception should be thrown by the proc and handled by the higher level calls -# {.push raises: [Defect].} +# - thus any key access should be wrapped inside try-except +# - or otherwise the exception should be thrown by the proc and handled by the higher level calls import std/[tables, options, json], @@ -146,7 +150,7 @@ proc sendCheque*(ws: WakuSwap, peerInfo : PeerInfo) {.async.} = info "New accounting state", accounting = ws.accounting[peerId] # TODO Authenticate cheque, check beneficiary etc -proc handleCheque*(ws: WakuSwap, cheque: Cheque, peerInfo : PeerInfo) = +proc handleCheque*(ws: WakuSwap, cheque: Cheque, peerInfo : PeerInfo) {.raises: [Defect, KeyError].} = info "handle incoming cheque" let peerId = peerInfo.peerId @@ -225,7 +229,8 @@ proc init*(wakuSwap: WakuSwap) = info "received cheque", value=res.value wakuSwap.handleCheque(res.value, conn.peerInfo) - proc credit(peerInfo: PeerInfo, n: int) {.gcsafe, closure.} = + proc credit(peerInfo: PeerInfo, n: int) + {.gcsafe, closure, raises: [Defect, KeyError, Exception].} = let peerId = peerInfo.peerId info "Crediting peer: ", peer=peerId, amount=n if wakuSwap.accounting.hasKey(peerId): @@ -236,7 +241,8 @@ proc init*(wakuSwap: WakuSwap) = wakuSwap.applyPolicy(peerInfo) # TODO Debit and credit here for Karma asset - proc debit(peerInfo: PeerInfo, n: int) {.gcsafe, closure.} = + proc debit(peerInfo: PeerInfo, n: int) + {.gcsafe, closure, raises: [Defect, KeyError, Exception].} = let peerId = peerInfo.peerId info "Debiting peer: ", peer=peerId, amount=n if wakuSwap.accounting.hasKey(peerId): @@ -246,7 +252,8 @@ proc init*(wakuSwap: WakuSwap) = info "Accounting state", accounting = wakuSwap.accounting[peerId] wakuSwap.applyPolicy(peerInfo) - proc applyPolicy(peerInfo: PeerInfo) {.gcsafe, closure.} = + proc applyPolicy(peerInfo: PeerInfo) + {.gcsafe, closure, raises: [Defect, KeyError, Exception].} = let peerId = peerInfo.peerId # TODO Separate out depending on if policy is soft (accounting only) mock (send cheque but don't cash/verify) hard (actually send funds over testnet) diff --git a/waku/v2/protocol/waku_swap/waku_swap_types.nim b/waku/v2/protocol/waku_swap/waku_swap_types.nim index dd65b61a5..43fdab33b 100644 --- a/waku/v2/protocol/waku_swap/waku_swap_types.nim +++ b/waku/v2/protocol/waku_swap/waku_swap_types.nim @@ -1,3 +1,5 @@ +{.push raises: [Defect].} + import std/tables, bearssl, @@ -53,4 +55,4 @@ proc init*(_: type[SwapConfig]): SwapConfig = mode: SwapMode.Soft, paymentThreshold: 100, disconnectThreshold: -100 - ) \ No newline at end of file + )