mirror of https://github.com/waku-org/nwaku.git
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:
parent
4ca59bcce0
commit
76d4b89dbb
|
@ -92,50 +92,53 @@ procSuite "Waku SWAP Accounting":
|
||||||
await node2.stop()
|
await node2.stop()
|
||||||
|
|
||||||
# TODO Add cheque here
|
# TODO Add cheque here
|
||||||
# Commenting out this test because cheques are currently not sent after the payment threshold has been reached
|
# This test will only Be checked if in Mock mode
|
||||||
# asyncTest "Update accounting state after sending cheque":
|
asyncTest "Update accounting state after sending cheque":
|
||||||
# let
|
let
|
||||||
# nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
||||||
# node1 = WakuNode.init(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
node1 = WakuNode.init(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
||||||
# Port(60000))
|
Port(60000))
|
||||||
# nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
||||||
# node2 = WakuNode.init(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
node2 = WakuNode.init(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
||||||
# Port(60001))
|
Port(60001))
|
||||||
# contentTopic = ContentTopic("/waku/2/default-content/proto")
|
contentTopic = ContentTopic("/waku/2/default-content/proto")
|
||||||
# message = WakuMessage(payload: "hello world".toBytes(), contentTopic: contentTopic)
|
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
|
# Define the waku swap Config for this test
|
||||||
# await node1.start()
|
let swapConfig = SwapConfig(mode: SwapMode.Mock, paymentThreshold: 1, disconnectThreshold: -1)
|
||||||
# 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)
|
# 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)
|
await sleepAsync(2000.millis)
|
||||||
# node1.wakuSwap.setPeer(node2.peerInfo)
|
|
||||||
# node2.wakuSwap.setPeer(node1.peerInfo)
|
|
||||||
|
|
||||||
# proc handler1(response: HistoryResponse) {.gcsafe, closure.} =
|
node1.wakuStore.setPeer(node2.peerInfo)
|
||||||
# futures[0].complete(true)
|
node1.wakuSwap.setPeer(node2.peerInfo)
|
||||||
# proc handler2(response: HistoryResponse) {.gcsafe, closure.} =
|
node2.wakuSwap.setPeer(node1.peerInfo)
|
||||||
# futures[1].complete(true)
|
|
||||||
|
|
||||||
# # TODO Handshakes - for now we assume implicit, e2e still works for PoC
|
proc handler1(response: HistoryResponse) {.gcsafe, closure.} =
|
||||||
# await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler1)
|
futures[0].complete(true)
|
||||||
# await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler2)
|
proc handler2(response: HistoryResponse) {.gcsafe, closure.} =
|
||||||
|
futures[1].complete(true)
|
||||||
|
|
||||||
# check:
|
# TODO Handshakes - for now we assume implicit, e2e still works for PoC
|
||||||
# (await allFutures(futures).withTimeout(5.seconds)) == true
|
await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler1)
|
||||||
# # Accounting table updated with credit and debit, respectively
|
await node1.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: contentTopic)]), handler2)
|
||||||
# # After sending a cheque the balance is partially adjusted
|
|
||||||
# node1.wakuSwap.accounting[node2.peerInfo.peerId] == 1
|
check:
|
||||||
# node2.wakuSwap.accounting[node1.peerInfo.peerId] == -1
|
(await allFutures(futures).withTimeout(5.seconds)) == true
|
||||||
# await node1.stop()
|
# Accounting table updated with credit and debit, respectively
|
||||||
# await node2.stop()
|
# 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()
|
||||||
|
|
|
@ -393,9 +393,9 @@ proc mountFilter*(node: WakuNode) =
|
||||||
|
|
||||||
# NOTE: If using the swap protocol, it must be mounted before store. This is
|
# NOTE: If using the swap protocol, it must be mounted before store. This is
|
||||||
# because store is using a reference to the swap protocol.
|
# because store is using a reference to the swap protocol.
|
||||||
proc mountSwap*(node: WakuNode) =
|
proc mountSwap*(node: WakuNode, swapConfig: SwapConfig = SwapConfig.init()) =
|
||||||
info "mounting swap"
|
info "mounting swap", mode = $swapConfig.mode
|
||||||
node.wakuSwap = WakuSwap.init(node.peerManager, node.rng)
|
node.wakuSwap = WakuSwap.init(node.peerManager, node.rng, swapConfig)
|
||||||
node.switch.mount(node.wakuSwap)
|
node.switch.mount(node.wakuSwap)
|
||||||
# NYI - Do we need this?
|
# NYI - Do we need this?
|
||||||
#node.subscriptions.subscribe(WakuSwapCodec, node.wakuSwap.subscription())
|
#node.subscriptions.subscribe(WakuSwapCodec, node.wakuSwap.subscription())
|
||||||
|
|
|
@ -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)
|
# 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
|
#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:
|
if wakuSwap.accounting[peerId] <= wakuSwap.config.disconnectThreshold:
|
||||||
warn "Disconnect threshhold has been reached: ", threshold=wakuSwap.disconnectThreshold, balance=wakuSwap.accounting[peerId]
|
warn "Disconnect threshhold has been reached: ", threshold=wakuSwap.config.disconnectThreshold, balance=wakuSwap.accounting[peerId]
|
||||||
else:
|
else:
|
||||||
info "Disconnect threshhold not hit"
|
info "Disconnect threshhold not hit"
|
||||||
|
|
||||||
#Check if the Payment threshold has been hit. Account Balance nears the paymentThreshold after a Debit has been done
|
#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:
|
if wakuSwap.accounting[peerId] >= wakuSwap.config.paymentThreshold:
|
||||||
warn "Payment threshhold has been reached: ", threshold=wakuSwap.paymentThreshold, balance=wakuSwap.accounting[peerId]
|
warn "Payment threshhold has been reached: ", threshold=wakuSwap.config.paymentThreshold, balance=wakuSwap.accounting[peerId]
|
||||||
#In soft phase we don't send cheques yet
|
#In soft phase we don't send cheques yet
|
||||||
#discard wakuSwap.sendCheque()
|
if wakuSwap.config.mode == Mock:
|
||||||
|
discard wakuSwap.sendCheque()
|
||||||
else:
|
else:
|
||||||
info "Payment threshhold not hit"
|
info "Payment threshhold not hit"
|
||||||
|
|
||||||
|
@ -257,15 +258,14 @@ proc init*(wakuSwap: WakuSwap) =
|
||||||
wakuswap.applyPolicy = applyPolicy
|
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, swapConfig: SwapConfig): T =
|
||||||
info "wakuSwap init 2"
|
info "wakuSwap init 2"
|
||||||
new result
|
new result
|
||||||
result.rng = rng
|
result.rng = rng
|
||||||
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.config = swapConfig
|
||||||
result.disconnectThreshold = -100
|
|
||||||
result.init()
|
result.init()
|
||||||
|
|
||||||
proc setPeer*(ws: WakuSwap, peer: PeerInfo) =
|
proc setPeer*(ws: WakuSwap, peer: PeerInfo) =
|
||||||
|
|
|
@ -6,6 +6,20 @@ import
|
||||||
../../node/peer_manager/peer_manager
|
../../node/peer_manager/peer_manager
|
||||||
|
|
||||||
type
|
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]
|
Beneficiary* = seq[byte]
|
||||||
|
|
||||||
# TODO Consider adding payment threshhold and terms field
|
# TODO Consider adding payment threshhold and terms field
|
||||||
|
@ -28,9 +42,15 @@ type
|
||||||
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
|
applyPolicy*: ApplyPolicyHandler
|
||||||
|
config*: SwapConfig
|
||||||
|
|
||||||
|
proc init*(_: type[SwapConfig]): SwapConfig =
|
||||||
|
SwapConfig(
|
||||||
|
mode: SwapMode.Soft,
|
||||||
|
paymentThreshold: 100,
|
||||||
|
disconnectThreshold: -100
|
||||||
|
)
|
Loading…
Reference in New Issue