Fix issues with Peer reference in Cheque Functions (#635)

* Add function for getting peer Id

* Modify Handle Cheque and Send Cheque function

* Include PeerInfo in debit and credit functions

* Minor changes to queryWithAccounting function
This commit is contained in:
Ebube Sered Ud 2021-06-22 21:55:01 +01:00 committed by GitHub
parent 82bad26e02
commit 6079ae8525
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 80 deletions

View File

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

View File

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

View File

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

View File

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