diff --git a/CHANGELOG.md b/CHANGELOG.md index 08ea6929b..9263a932c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,7 @@ This release contains the following: - A CLI chat application ([`chat2`](https://github.com/status-im/nim-waku/blob/master/docs/tutorial/chat2.md)) over Waku v2 with [bridging to matterbridge](https://github.com/status-im/nim-waku/blob/master/docs/tutorial/chat2.md#bridge-messages-between-chat2-and-matterbridge) ### Changes - +- Enable `swap` protocol by default and improve logging #### General refactoring - Split out `waku_types` types into the right place; create `utils` folder. diff --git a/tests/v2/test_waku_swap.nim b/tests/v2/test_waku_swap.nim index 62405e73f..3061f7f0e 100644 --- a/tests/v2/test_waku_swap.nim +++ b/tests/v2/test_waku_swap.nim @@ -92,49 +92,50 @@ procSuite "Waku SWAP Accounting": await node2.stop() # TODO Add cheque here - 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) + # 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) - 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) + # # Start nodes and mount protocols + # await node1.start() + # node1.mountSwap() + # node1.mountStore(persistMessages = true) + # await node2.start() + # node2.mountSwap() + # node2.mountStore(persistMessages = true) - await node2.subscriptions.notify("/waku/2/default-waku/proto", message) + # await node2.subscriptions.notify("/waku/2/default-waku/proto", message) - await sleepAsync(2000.millis) + # await sleepAsync(2000.millis) - node1.wakuStore.setPeer(node2.peerInfo) - node1.wakuSwap.setPeer(node2.peerInfo) - node2.wakuSwap.setPeer(node1.peerInfo) + # node1.wakuStore.setPeer(node2.peerInfo) + # node1.wakuSwap.setPeer(node2.peerInfo) + # node2.wakuSwap.setPeer(node1.peerInfo) - proc handler1(response: HistoryResponse) {.gcsafe, closure.} = - futures[0].complete(true) - proc handler2(response: HistoryResponse) {.gcsafe, closure.} = - futures[1].complete(true) + # proc handler1(response: HistoryResponse) {.gcsafe, closure.} = + # futures[0].complete(true) + # proc handler2(response: HistoryResponse) {.gcsafe, closure.} = + # futures[1].complete(true) - # 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) + # # 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() + # 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/config.nim b/waku/v2/node/config.nim index d0a1b062c..db58bd70f 100644 --- a/waku/v2/node/config.nim +++ b/waku/v2/node/config.nim @@ -117,7 +117,7 @@ type swap* {. desc: "Enable swap protocol: true|false", - defaultValue: false + defaultValue: true name: "swap" }: bool ## Lightpush config diff --git a/waku/v2/protocol/waku_swap/waku_swap.nim b/waku/v2/protocol/waku_swap/waku_swap.nim index 5ae18b66f..cc24803aa 100644 --- a/waku/v2/protocol/waku_swap/waku_swap.nim +++ b/waku/v2/protocol/waku_swap/waku_swap.nim @@ -207,37 +207,38 @@ proc init*(wakuSwap: WakuSwap) = wakuSwap.handleCheque(res.value) proc credit(peerId: PeerId, n: int) {.gcsafe, closure.} = - info "Crediting peer for", peerId, n + info "Crediting peer: ", peer=peerId, amount=n if wakuSwap.accounting.hasKey(peerId): wakuSwap.accounting[peerId] -= n else: wakuSwap.accounting[peerId] = -n info "Accounting state", accounting = wakuSwap.accounting[peerId] - - # TODO Isolate to policy function - # TODO Tunable disconnect threshhold, hard code for PoC - let disconnectThreshhold = 2 - if wakuSwap.accounting[peerId] >= disconnectThreshhold: - info "Disconnect threshhold hit, disconnect peer" - else: - info "Disconnect threshhold not hit" + wakuSwap.applyPolicy(peerId) # TODO Debit and credit here for Karma asset proc debit(peerId: PeerId, n: int) {.gcsafe, closure.} = - info "Debiting peer for", peerId, n + info "Debiting peer: ", peer=peerId, amount=n if wakuSwap.accounting.hasKey(peerId): wakuSwap.accounting[peerId] += n else: wakuSwap.accounting[peerId] = n info "Accounting state", accounting = wakuSwap.accounting[peerId] + wakuSwap.applyPolicy(peerId) + + proc applyPolicy(peerId: PeerId) {.gcsafe, closure.} = + # 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) - # TODO Isolate to policy function - # TODO Tunable payment threshhold, hard code for PoC - # XXX: Where should this happen? Apply policy... - let paymentThreshhold = 1 - if wakuSwap.accounting[peerId] >= paymentThreshhold: - info "Payment threshhold hit, send cheque" - discard wakuSwap.sendCheque() + #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] + 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] + #In soft phase we don't send cheques yet + #discard wakuSwap.sendCheque() else: info "Payment threshhold not hit" @@ -245,6 +246,7 @@ proc init*(wakuSwap: WakuSwap) = wakuSwap.codec = WakuSwapCodec wakuSwap.credit = credit wakuSwap.debit = debit + wakuswap.applyPolicy = applyPolicy # TODO Expression return? proc init*(T: type WakuSwap, peerManager: PeerManager, rng: ref BrHmacDrbgContext): T = @@ -254,6 +256,8 @@ proc init*(T: type WakuSwap, peerManager: PeerManager, rng: ref BrHmacDrbgContex result.peerManager = peerManager result.accounting = initTable[PeerId, int]() result.text = "test" + result.paymentThreshold = 100 + result.disconnectThreshold = -100 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 23cf96bef..f7c1a6616 100644 --- a/waku/v2/protocol/waku_swap/waku_swap_types.nim +++ b/waku/v2/protocol/waku_swap/waku_swap_types.nim @@ -22,11 +22,15 @@ type CreditHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.} DebitHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.} + ApplyPolicyHandler* = proc(peerId: PeerId) {.gcsafe, closure.} WakuSwap* = ref object of LPProtocol peerManager*: PeerManager rng*: ref BrHmacDrbgContext text*: string + paymentThreshold*: int + disconnectThreshold*: int accounting*: Table[PeerId, int] credit*: CreditHandler debit*: DebitHandler + applyPolicy*: ApplyPolicyHandler