From 410f323673f56faa16081546de6119cf858295a6 Mon Sep 17 00:00:00 2001 From: EbubeUd Date: Tue, 22 Jun 2021 21:16:44 +0000 Subject: [PATCH] deploy: 1c3978b7066e05986e60c8d957a2eec8c0db4cf5 --- docs/tutorial/swap.md | 50 ------------------- waku/v2/protocol/waku_store/waku_store.nim | 8 +-- waku/v2/protocol/waku_swap/waku_swap.nim | 39 ++++++--------- .../v2/protocol/waku_swap/waku_swap_types.nim | 6 +-- 4 files changed, 23 insertions(+), 80 deletions(-) delete mode 100644 docs/tutorial/swap.md diff --git a/docs/tutorial/swap.md b/docs/tutorial/swap.md deleted file mode 100644 index 78fc148b5..000000000 --- a/docs/tutorial/swap.md +++ /dev/null @@ -1,50 +0,0 @@ -# Running Swap Protocol - -## How to - -Build: - -``` -# make wakunode2 is run as part of scripts2 target -make scripts2 -``` - -Run two nodes and connect them: - -``` -# Starts listening on 60000 with RPC server on 8545. -# Note the "listening on address" in logs. -./build/wakunode2 --ports-shift:0 - -# Run another node with staticnode argument -./build/wakunode2 --ports-shift:1 --staticnode:/ip4/0.0.0.0/tcp/60000/p2p/16Uiu2HAmF4tuht6fmna6uDqoSMgFqhUrdaVR6VQRyGr6sCpfS2jp --storenode:/ip4/0.0.0.0/tcp/60000/p2p/16Uiu2HAmF4tuht6fmna6uDqoSMgFqhUrdaVR6VQRyGr6sCpfS2jp --persist-messages:true - - -# Run the chat application -./build/chat2 --staticnode:/ip4/0.0.0.0/tcp/60000/p2p/16Uiu2HAmF4tuht6fmna6uDqoSMgFqhUrdaVR6VQRyGr6sCpfS2jp --storenode:/ip4/0.0.0.0/tcp/60000/p2p/16Uiu2HAmF4tuht6fmna6uDqoSMgFqhUrdaVR6VQRyGr6sCpfS2jp - -``` - You should be prompted to provide a nickname for the chat session. - -``` -Choose a nickname >> -``` - -After entering a nickname, the app will be connected to peer `16Uiu2HAmF4tuht6fmna6uDqoSMgFqhUrdaVR6VQRyGr6sCpfS2jp` - -``` - -# Run another chat application -./build/chat2 --staticnode:/ip4/0.0.0.0/tcp/60000/p2p/16Uiu2HAmF4tuht6fmna6uDqoSMgFqhUrdaVR6VQRyGr6sCpfS2jp --storenode:/ip4/0.0.0.0/tcp/60000/p2p/16Uiu2HAmF4tuht6fmna6uDqoSMgFqhUrdaVR6VQRyGr6sCpfS2jp -``` -Connect with a different nickname. -You should see a log saying: -``` -Crediting Peer: peer=16U*CpfS2jp amount=1 -``` - where amount is the number of Messages received. - - -## To View the Account Metrics: -To view the account metrics on Grafana Dashboard locally, follow the steps [here](https://github.com/status-im/nim-waku/tree/master/waku/v2#using-metrics) to set up grafana and prometheus. -The metrics on the live cluster can be accessed [here](https://grafana.infra.status.im/d/qrp_ZCTGz/nim-waku-v2?orgId=1&refresh=5m) \ No newline at end of file diff --git a/waku/v2/protocol/waku_store/waku_store.nim b/waku/v2/protocol/waku_store/waku_store.nim index c2bc2933a..bf95bbbaa 100644 --- a/waku/v2/protocol/waku_store/waku_store.nim +++ b/waku/v2/protocol/waku_store/waku_store.nim @@ -360,9 +360,9 @@ proc init*(ws: WakuStore) {.raises: [Defect, Exception]} = if not ws.wakuSwap.isNil: info "handle store swap test", text=ws.wakuSwap.text # NOTE Perform accounting operation - let peerId = conn.peerInfo.peerId + let peerInfo = conn.peerInfo let messages = response.messages - ws.wakuSwap.credit(peerId, messages.len) + ws.wakuSwap.credit(peerInfo, messages.len) else: info "handle store swap is nil" @@ -635,9 +635,9 @@ proc queryWithAccounting*(ws: WakuStore, query: HistoryQuery, handler: QueryHand # NOTE Perform accounting operation # Assumes wakuSwap protocol is mounted - let peerId = peerOpt.get().peerId + let peerInfo = peerOpt.get() let messages = response.value.response.messages - ws.wakuSwap.debit(peerId, messages.len) + ws.wakuSwap.debit(peerInfo, messages.len) waku_store_messages.set(response.value.response.messages.len.int64, labelValues = ["retrieved"]) diff --git a/waku/v2/protocol/waku_swap/waku_swap.nim b/waku/v2/protocol/waku_swap/waku_swap.nim index 5e9ae5d65..e1fec7db9 100644 --- a/waku/v2/protocol/waku_swap/waku_swap.nim +++ b/waku/v2/protocol/waku_swap/waku_swap.nim @@ -98,17 +98,8 @@ proc init*(T: type Cheque, buffer: seq[byte]): ProtoResult[T] = # TODO Assume we calculated cheque -proc sendCheque*(ws: WakuSwap) {.async.} = - let peerOpt = ws.peerManager.selectPeer(WakuSwapCodec) - - if peerOpt.isNone(): - error "no suitable remote peers" - waku_swap_errors.inc(labelValues = [dialFailure]) - return - - let peer = peerOpt.get() - - let connOpt = await ws.peerManager.dialPeer(peer, WakuSwapCodec) +proc sendCheque*(ws: WakuSwap, peerInfo : PeerInfo) {.async.} = + let connOpt = await ws.peerManager.dialPeer(peerInfo, WakuSwapCodec) if connOpt.isNone(): # @TODO more sophisticated error handling here @@ -138,16 +129,15 @@ proc sendCheque*(ws: WakuSwap) {.async.} = await connOpt.get().writeLP(Cheque(amount: 1, signature: sigBytes, issuerAddress: aliceWalletAddress).encode().buffer) # Set new balance - let peerId = peer.peerId + let peerId = peerInfo.peerId ws.accounting[peerId] -= 1 info "New accounting state", accounting = ws.accounting[peerId] # TODO Authenticate cheque, check beneficiary etc -proc handleCheque*(ws: WakuSwap, cheque: Cheque) = +proc handleCheque*(ws: WakuSwap, cheque: Cheque, peerInfo : PeerInfo) = info "handle incoming cheque" - # XXX Assume peerId is first peer - let peerOpt = ws.peerManager.selectPeer(WakuSwapCodec) - let peerId = peerOpt.get().peerId + + let peerId = peerInfo.peerId # Get the original signer using web3. For now, a static value (0x6C3d502f1a97d4470b881015b83D9Dd1062172e1) will be used. # Check if web3.eth.personal.ecRecover(messageHash, signature); or an equivalent function has been implemented in nim-web3 @@ -221,28 +211,31 @@ proc init*(wakuSwap: WakuSwap) = return info "received cheque", value=res.value - wakuSwap.handleCheque(res.value) + wakuSwap.handleCheque(res.value, conn.peerInfo) - proc credit(peerId: PeerId, n: int) {.gcsafe, closure.} = + proc credit(peerInfo: PeerInfo, n: int) {.gcsafe, closure.} = + let peerId = peerInfo.peerId 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] - wakuSwap.applyPolicy(peerId) + wakuSwap.applyPolicy(peerInfo) # TODO Debit and credit here for Karma asset - proc debit(peerId: PeerId, n: int) {.gcsafe, closure.} = + proc debit(peerInfo: PeerInfo, n: int) {.gcsafe, closure.} = + let peerId = peerInfo.peerId 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) + wakuSwap.applyPolicy(peerInfo) - proc applyPolicy(peerId: PeerId) {.gcsafe, closure.} = + proc applyPolicy(peerInfo: PeerInfo) {.gcsafe, closure.} = + let peerId = peerInfo.peerId # 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 @@ -256,7 +249,7 @@ proc init*(wakuSwap: WakuSwap) = warn "Payment threshhold has been reached: ", threshold=wakuSwap.config.paymentThreshold, balance=wakuSwap.accounting[peerId] #In soft phase we don't send cheques yet if wakuSwap.config.mode == Mock: - discard wakuSwap.sendCheque() + discard wakuSwap.sendCheque(peerInfo) else: info "Payment threshhold not hit" diff --git a/waku/v2/protocol/waku_swap/waku_swap_types.nim b/waku/v2/protocol/waku_swap/waku_swap_types.nim index 7d34ca2f9..dd65b61a5 100644 --- a/waku/v2/protocol/waku_swap/waku_swap_types.nim +++ b/waku/v2/protocol/waku_swap/waku_swap_types.nim @@ -34,9 +34,9 @@ type amount*: uint32 signature*: seq[byte] - CreditHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.} - DebitHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.} - ApplyPolicyHandler* = proc(peerId: PeerId) {.gcsafe, closure.} + CreditHandler* = proc (peerInfo: PeerInfo, amount: int) {.gcsafe, closure.} + DebitHandler* = proc (peerInfo: PeerInfo, amount: int) {.gcsafe, closure.} + ApplyPolicyHandler* = proc(peerInfo: PeerInfo) {.gcsafe, closure.} WakuSwap* = ref object of LPProtocol peerManager*: PeerManager