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>
This commit is contained in:
Ebube Sered Ud 2021-06-15 03:06:36 +01:00 committed by GitHub
parent 4ca59bcce0
commit 76d4b89dbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 52 deletions

View File

@ -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()

View File

@ -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())

View File

@ -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) =

View File

@ -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
)