From 76d4b89dbbb7f9bc12c04e0ccd3840f49a886f0b Mon Sep 17 00:00:00 2001 From: Ebube Sered Ud Date: Tue, 15 Jun 2021 03:06:36 +0100 Subject: [PATCH] Add mode to waku swap (#601) * Add mode to waku swap * Modify swap test and policy function * Make rln optional (dependencies and compilation) (#602) * makes rlnlib target optional * makes the rln-related targets conditioned to the RLN command line flag * adds the rln compiler flag condition * adds the rln compiler flag condition to the rln module * wakunode2 rln conditional compilation * updates wakunode2 test * updates waku rln relay tests * removing the rln test from the default imports of the v2 tests * imports rln module conditionally * removes the rln flag condition from the rln module * separates rln data types from its procs * adds the import statement * brings back the contract def to the rln utils * adds rln module import to the rln unit tests * clean up and reorganization * adds a todo * minor edits on a comment * Fix indentation and typo * Add Swap Config Object and set default config value on mount * Minor changes * Fix issues with grammar in code comment * Create init procedure for setting default values in SwapConfig * Minor changes to mountSwap call in wakunode2 * Improve Docs on Swap Mode Co-authored-by: Sanaz Taheri Boshrooyeh <35961250+staheri14@users.noreply.github.com> Co-authored-by: Hanno Cornelius <68783915+jm-clius@users.noreply.github.com> --- tests/v2/test_waku_swap.nim | 81 ++++++++++--------- waku/v2/node/wakunode2.nim | 6 +- waku/v2/protocol/waku_swap/waku_swap.nim | 16 ++-- .../v2/protocol/waku_swap/waku_swap_types.nim | 24 +++++- 4 files changed, 75 insertions(+), 52 deletions(-) diff --git a/tests/v2/test_waku_swap.nim b/tests/v2/test_waku_swap.nim index 3061f7f0e..ad322394d 100644 --- a/tests/v2/test_waku_swap.nim +++ b/tests/v2/test_waku_swap.nim @@ -92,50 +92,53 @@ procSuite "Waku SWAP Accounting": await node2.stop() # TODO Add cheque here - # Commenting out this test because cheques are currently not sent after the payment threshold has been reached - # asyncTest "Update accounting state after sending cheque": - # let - # nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[] - # node1 = WakuNode.init(nodeKey1, ValidIpAddress.init("0.0.0.0"), - # Port(60000)) - # nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[] - # node2 = WakuNode.init(nodeKey2, ValidIpAddress.init("0.0.0.0"), - # Port(60001)) - # contentTopic = ContentTopic("/waku/2/default-content/proto") - # message = WakuMessage(payload: "hello world".toBytes(), contentTopic: contentTopic) + # This test will only Be checked if in Mock mode + asyncTest "Update accounting state after sending cheque": + let + nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[] + node1 = WakuNode.init(nodeKey1, ValidIpAddress.init("0.0.0.0"), + Port(60000)) + nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[] + node2 = WakuNode.init(nodeKey2, ValidIpAddress.init("0.0.0.0"), + Port(60001)) + contentTopic = ContentTopic("/waku/2/default-content/proto") + message = WakuMessage(payload: "hello world".toBytes(), contentTopic: contentTopic) - # var futures = [newFuture[bool](), newFuture[bool]()] + var futures = [newFuture[bool](), newFuture[bool]()] - # # Start nodes and mount protocols - # await node1.start() - # node1.mountSwap() - # node1.mountStore(persistMessages = true) - # await node2.start() - # node2.mountSwap() - # node2.mountStore(persistMessages = true) + # Define the waku swap Config for this test + let swapConfig = SwapConfig(mode: SwapMode.Mock, paymentThreshold: 1, disconnectThreshold: -1) - # await node2.subscriptions.notify("/waku/2/default-waku/proto", message) + # Start nodes and mount protocols + await node1.start() + node1.mountSwap(swapConfig) + node1.mountStore(persistMessages = true) + await node2.start() + node2.mountSwap(swapConfig) + node2.mountStore(persistMessages = true) - # await sleepAsync(2000.millis) + await node2.subscriptions.notify("/waku/2/default-waku/proto", message) - # node1.wakuStore.setPeer(node2.peerInfo) - # node1.wakuSwap.setPeer(node2.peerInfo) - # node2.wakuSwap.setPeer(node1.peerInfo) + await sleepAsync(2000.millis) - # proc handler1(response: HistoryResponse) {.gcsafe, closure.} = - # futures[0].complete(true) - # proc handler2(response: HistoryResponse) {.gcsafe, closure.} = - # futures[1].complete(true) + node1.wakuStore.setPeer(node2.peerInfo) + node1.wakuSwap.setPeer(node2.peerInfo) + node2.wakuSwap.setPeer(node1.peerInfo) - # # TODO Handshakes - for now we assume implicit, e2e still works for PoC - # await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler1) - # await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler2) + proc handler1(response: HistoryResponse) {.gcsafe, closure.} = + futures[0].complete(true) + proc handler2(response: HistoryResponse) {.gcsafe, closure.} = + futures[1].complete(true) - # check: - # (await allFutures(futures).withTimeout(5.seconds)) == true - # # Accounting table updated with credit and debit, respectively - # # After sending a cheque the balance is partially adjusted - # node1.wakuSwap.accounting[node2.peerInfo.peerId] == 1 - # node2.wakuSwap.accounting[node1.peerInfo.peerId] == -1 - # await node1.stop() - # await node2.stop() + # TODO Handshakes - for now we assume implicit, e2e still works for PoC + await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler1) + await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler2) + + check: + (await allFutures(futures).withTimeout(5.seconds)) == true + # Accounting table updated with credit and debit, respectively + # After sending a cheque the balance is partially adjusted + node1.wakuSwap.accounting[node2.peerInfo.peerId] == 1 + node2.wakuSwap.accounting[node1.peerInfo.peerId] == -1 + await node1.stop() + await node2.stop() diff --git a/waku/v2/node/wakunode2.nim b/waku/v2/node/wakunode2.nim index 012eb6277..77fd4b8a4 100644 --- a/waku/v2/node/wakunode2.nim +++ b/waku/v2/node/wakunode2.nim @@ -393,9 +393,9 @@ proc mountFilter*(node: WakuNode) = # NOTE: If using the swap protocol, it must be mounted before store. This is # because store is using a reference to the swap protocol. -proc mountSwap*(node: WakuNode) = - info "mounting swap" - node.wakuSwap = WakuSwap.init(node.peerManager, node.rng) +proc mountSwap*(node: WakuNode, swapConfig: SwapConfig = SwapConfig.init()) = + info "mounting swap", mode = $swapConfig.mode + node.wakuSwap = WakuSwap.init(node.peerManager, node.rng, swapConfig) node.switch.mount(node.wakuSwap) # NYI - Do we need this? #node.subscriptions.subscribe(WakuSwapCodec, node.wakuSwap.subscription()) diff --git a/waku/v2/protocol/waku_swap/waku_swap.nim b/waku/v2/protocol/waku_swap/waku_swap.nim index bc69d345f..cf307ad85 100644 --- a/waku/v2/protocol/waku_swap/waku_swap.nim +++ b/waku/v2/protocol/waku_swap/waku_swap.nim @@ -235,16 +235,17 @@ proc init*(wakuSwap: WakuSwap) = # 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) #Check if the Disconnect Threshold has been hit. Account Balance nears the disconnectThreshold after a Credit has been done - if wakuSwap.accounting[peerId] <= wakuSwap.disconnectThreshold: - warn "Disconnect threshhold has been reached: ", threshold=wakuSwap.disconnectThreshold, balance=wakuSwap.accounting[peerId] + if wakuSwap.accounting[peerId] <= wakuSwap.config.disconnectThreshold: + warn "Disconnect threshhold has been reached: ", threshold=wakuSwap.config.disconnectThreshold, balance=wakuSwap.accounting[peerId] else: info "Disconnect threshhold not hit" #Check if the Payment threshold has been hit. Account Balance nears the paymentThreshold after a Debit has been done - if wakuSwap.accounting[peerId] >= wakuSwap.paymentThreshold: - warn "Payment threshhold has been reached: ", threshold=wakuSwap.paymentThreshold, balance=wakuSwap.accounting[peerId] + if wakuSwap.accounting[peerId] >= wakuSwap.config.paymentThreshold: + warn "Payment threshhold has been reached: ", threshold=wakuSwap.config.paymentThreshold, balance=wakuSwap.accounting[peerId] #In soft phase we don't send cheques yet - #discard wakuSwap.sendCheque() + if wakuSwap.config.mode == Mock: + discard wakuSwap.sendCheque() else: info "Payment threshhold not hit" @@ -257,15 +258,14 @@ proc init*(wakuSwap: WakuSwap) = wakuswap.applyPolicy = applyPolicy # TODO Expression return? -proc init*(T: type WakuSwap, peerManager: PeerManager, rng: ref BrHmacDrbgContext): T = +proc init*(T: type WakuSwap, peerManager: PeerManager, rng: ref BrHmacDrbgContext, swapConfig: SwapConfig): T = info "wakuSwap init 2" new result result.rng = rng result.peerManager = peerManager result.accounting = initTable[PeerId, int]() result.text = "test" - result.paymentThreshold = 100 - result.disconnectThreshold = -100 + result.config = swapConfig result.init() proc setPeer*(ws: WakuSwap, peer: PeerInfo) = diff --git a/waku/v2/protocol/waku_swap/waku_swap_types.nim b/waku/v2/protocol/waku_swap/waku_swap_types.nim index f7c1a6616..603142398 100644 --- a/waku/v2/protocol/waku_swap/waku_swap_types.nim +++ b/waku/v2/protocol/waku_swap/waku_swap_types.nim @@ -6,6 +6,20 @@ import ../../node/peer_manager/peer_manager type + # The Swap Mode determines the functionality available in the swap protocol. + # Soft: Deals with the account balance (Credit and debit) of each peer. + # Mock: Includes the Send Cheque Functionality and peer disconnection upon failed signature verification or low balance. + # Hard: Includes interactions with Smart Contracts. + SwapMode* = enum + Soft, + Mock, + Hard + + SwapConfig* = object + mode* : SwapMode + paymentThreshold* : int + disconnectThreshold* : int + Beneficiary* = seq[byte] # TODO Consider adding payment threshhold and terms field @@ -28,9 +42,15 @@ type peerManager*: PeerManager rng*: ref BrHmacDrbgContext text*: string - paymentThreshold*: int - disconnectThreshold*: int accounting*: Table[PeerId, int] credit*: CreditHandler debit*: DebitHandler applyPolicy*: ApplyPolicyHandler + config*: SwapConfig + +proc init*(_: type[SwapConfig]): SwapConfig = + SwapConfig( + mode: SwapMode.Soft, + paymentThreshold: 100, + disconnectThreshold: -100 + ) \ No newline at end of file