deploy: 3e98a78106b114cff9c7da1f0462fc39ce79095c

This commit is contained in:
EbubeUd 2021-05-26 10:33:25 +00:00
parent 44790ff3fc
commit 3342a0f002
5 changed files with 66 additions and 57 deletions

View File

@ -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) - 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 ### Changes
- Enable `swap` protocol by default and improve logging
#### General refactoring #### General refactoring
- Split out `waku_types` types into the right place; create `utils` folder. - Split out `waku_types` types into the right place; create `utils` folder.

View File

@ -92,49 +92,50 @@ procSuite "Waku SWAP Accounting":
await node2.stop() await node2.stop()
# TODO Add cheque here # TODO Add cheque here
asyncTest "Update accounting state after sending cheque": # Commenting out this test because cheques are currently not sent after the payment threshold has been reached
let # asyncTest "Update accounting state after sending cheque":
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[] # let
node1 = WakuNode.init(nodeKey1, ValidIpAddress.init("0.0.0.0"), # nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
Port(60000)) # node1 = WakuNode.init(nodeKey1, ValidIpAddress.init("0.0.0.0"),
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[] # Port(60000))
node2 = WakuNode.init(nodeKey2, ValidIpAddress.init("0.0.0.0"), # nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
Port(60001)) # node2 = WakuNode.init(nodeKey2, ValidIpAddress.init("0.0.0.0"),
contentTopic = ContentTopic("/waku/2/default-content/proto") # Port(60001))
message = WakuMessage(payload: "hello world".toBytes(), contentTopic: contentTopic) # 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 # # Start nodes and mount protocols
await node1.start() # await node1.start()
node1.mountSwap() # node1.mountSwap()
node1.mountStore(persistMessages = true) # node1.mountStore(persistMessages = true)
await node2.start() # await node2.start()
node2.mountSwap() # node2.mountSwap()
node2.mountStore(persistMessages = true) # 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.wakuStore.setPeer(node2.peerInfo)
node1.wakuSwap.setPeer(node2.peerInfo) # node1.wakuSwap.setPeer(node2.peerInfo)
node2.wakuSwap.setPeer(node1.peerInfo) # node2.wakuSwap.setPeer(node1.peerInfo)
proc handler1(response: HistoryResponse) {.gcsafe, closure.} = # proc handler1(response: HistoryResponse) {.gcsafe, closure.} =
futures[0].complete(true) # futures[0].complete(true)
proc handler2(response: HistoryResponse) {.gcsafe, closure.} = # proc handler2(response: HistoryResponse) {.gcsafe, closure.} =
futures[1].complete(true) # futures[1].complete(true)
# TODO Handshakes - for now we assume implicit, e2e still works for PoC # # 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)]), handler1)
await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler2) # await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler2)
check: # check:
(await allFutures(futures).withTimeout(5.seconds)) == true # (await allFutures(futures).withTimeout(5.seconds)) == true
# Accounting table updated with credit and debit, respectively # # Accounting table updated with credit and debit, respectively
# After sending a cheque the balance is partially adjusted # # After sending a cheque the balance is partially adjusted
node1.wakuSwap.accounting[node2.peerInfo.peerId] == 1 # node1.wakuSwap.accounting[node2.peerInfo.peerId] == 1
node2.wakuSwap.accounting[node1.peerInfo.peerId] == -1 # node2.wakuSwap.accounting[node1.peerInfo.peerId] == -1
await node1.stop() # await node1.stop()
await node2.stop() # await node2.stop()

View File

@ -117,7 +117,7 @@ type
swap* {. swap* {.
desc: "Enable swap protocol: true|false", desc: "Enable swap protocol: true|false",
defaultValue: false defaultValue: true
name: "swap" }: bool name: "swap" }: bool
## Lightpush config ## Lightpush config

View File

@ -207,37 +207,38 @@ proc init*(wakuSwap: WakuSwap) =
wakuSwap.handleCheque(res.value) wakuSwap.handleCheque(res.value)
proc credit(peerId: PeerId, n: int) {.gcsafe, closure.} = 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): if wakuSwap.accounting.hasKey(peerId):
wakuSwap.accounting[peerId] -= n wakuSwap.accounting[peerId] -= n
else: else:
wakuSwap.accounting[peerId] = -n wakuSwap.accounting[peerId] = -n
info "Accounting state", accounting = wakuSwap.accounting[peerId] info "Accounting state", accounting = wakuSwap.accounting[peerId]
wakuSwap.applyPolicy(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"
# TODO Debit and credit here for Karma asset # TODO Debit and credit here for Karma asset
proc debit(peerId: PeerId, n: int) {.gcsafe, closure.} = 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): if wakuSwap.accounting.hasKey(peerId):
wakuSwap.accounting[peerId] += n wakuSwap.accounting[peerId] += n
else: else:
wakuSwap.accounting[peerId] = n wakuSwap.accounting[peerId] = n
info "Accounting state", accounting = wakuSwap.accounting[peerId] 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 #Check if the Disconnect Threshold has been hit. Account Balance nears the disconnectThreshold after a Credit has been done
# TODO Tunable payment threshhold, hard code for PoC if wakuSwap.accounting[peerId] <= wakuSwap.disconnectThreshold:
# XXX: Where should this happen? Apply policy... warn "Disconnect threshhold has been reached: ", threshold=wakuSwap.disconnectThreshold, balance=wakuSwap.accounting[peerId]
let paymentThreshhold = 1 else:
if wakuSwap.accounting[peerId] >= paymentThreshhold: info "Disconnect threshhold not hit"
info "Payment threshhold hit, send cheque"
discard wakuSwap.sendCheque() #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: else:
info "Payment threshhold not hit" info "Payment threshhold not hit"
@ -245,6 +246,7 @@ proc init*(wakuSwap: WakuSwap) =
wakuSwap.codec = WakuSwapCodec wakuSwap.codec = WakuSwapCodec
wakuSwap.credit = credit wakuSwap.credit = credit
wakuSwap.debit = debit wakuSwap.debit = debit
wakuswap.applyPolicy = applyPolicy
# TODO Expression return? # TODO Expression return?
proc init*(T: type WakuSwap, peerManager: PeerManager, rng: ref BrHmacDrbgContext): T = 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.peerManager = peerManager
result.accounting = initTable[PeerId, int]() result.accounting = initTable[PeerId, int]()
result.text = "test" result.text = "test"
result.paymentThreshold = 100
result.disconnectThreshold = -100
result.init() result.init()
proc setPeer*(ws: WakuSwap, peer: PeerInfo) = proc setPeer*(ws: WakuSwap, peer: PeerInfo) =

View File

@ -22,11 +22,15 @@ type
CreditHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.} CreditHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.}
DebitHandler* = 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 WakuSwap* = ref object of LPProtocol
peerManager*: PeerManager peerManager*: PeerManager
rng*: ref BrHmacDrbgContext rng*: ref BrHmacDrbgContext
text*: string text*: string
paymentThreshold*: int
disconnectThreshold*: int
accounting*: Table[PeerId, int] accounting*: Table[PeerId, int]
credit*: CreditHandler credit*: CreditHandler
debit*: DebitHandler debit*: DebitHandler
applyPolicy*: ApplyPolicyHandler