Minimal accounting (#264)

* Minimal accounting

Start of accounting PoC and keeping track of balance with peer

- Untested
- Flag should be off by default
- should be put in a separaet module

* Move accountFor to right place

* Accounting Use length of messages

* Disable flag by default

* Move account update func sig
This commit is contained in:
Oskar Thorén 2020-11-16 17:55:49 +08:00 committed by GitHub
parent 1568fa6e6f
commit d46f357495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 1 deletions

View File

@ -169,6 +169,9 @@ type
date*: uint32
amount*: uint32
AccountUpdateFunc* = proc(peerId: PeerId, amount: int) {.gcsafe.}
# Encoding and decoding -------------------------------------------------------
# TODO Move out to to waku_message module
# Possibly same with util functions

View File

@ -10,7 +10,7 @@ import
libp2p/protocols/pubsub/pubsub,
libp2p/peerinfo,
libp2p/standard_setup,
../../protocol/v2/[waku_relay, waku_store, waku_filter, message_notifier],
../../protocol/v2/[waku_relay, waku_store, waku_filter, waku_swap, message_notifier],
./waku_types, ./message_store
export waku_types
@ -21,6 +21,8 @@ logScope:
# Default clientId
const clientId* = "Nimbus Waku v2 node"
const SWAPAccountingEnabled* = false
# key and crypto modules different
type
KeyPair* = crypto.KeyPair
@ -196,6 +198,7 @@ proc unsubscribe*(node: WakuNode, request: FilterRequest) {.async, gcsafe.} =
await node.wakuFilter.unsubscribe(request)
node.filters.removeContentFilters(request.contentFilters)
proc publish*(node: WakuNode, topic: Topic, message: WakuMessage) =
## Publish a `WakuMessage` to a PubSub topic. `WakuMessage` should contain a
## `contentTopic` field for light node functionality. This field may be also
@ -219,6 +222,10 @@ proc query*(node: WakuNode, query: HistoryQuery, handler: QueryHandlerFunc) {.as
## Status: Implemented.
await node.wakuStore.query(query, handler)
if SWAPAccountingEnabled:
debug "Using SWAPAccounting query"
await node.wakuStore.queryWithAccounting(query, handler, accountFor)
# TODO Extend with more relevant info: topics, peers, memory usage, online time, etc
proc info*(node: WakuNode): WakuInfo =
## Returns information about the Node, such as what multiaddress it can be reached at.
@ -378,6 +385,12 @@ when isMainModule:
waitFor node.start()
# TODO Move to conf
if SWAPAccountingEnabled:
info "SWAP Accounting enabled"
# TODO Mount SWAP protocol
# TODO Enable account module
if conf.store:
var store: MessageStore

View File

@ -352,3 +352,34 @@ proc query*(w: WakuStore, query: HistoryQuery, handler: QueryHandlerFunc) {.asyn
return
handler(response.value.response)
# NOTE: Experimental, maybe incorporate as part of query call
proc queryWithAccounting*(w: WakuStore, query: HistoryQuery, handler: QueryHandlerFunc,
accountFor: AccountUpdateFunc) {.async, gcsafe.} =
# @TODO We need to be more stratigic about which peers we dial. Right now we just set one on the service.
# Ideally depending on the query and our set of peers we take a subset of ideal peers.
# This will require us to check for various factors such as:
# - which topics they track
# - latency?
# - default store peer?
let peer = w.peers[0]
let conn = await w.switch.dial(peer.peerInfo.peerId, peer.peerInfo.addrs, WakuStoreCodec)
await conn.writeLP(HistoryRPC(requestId: generateRequestId(w.rng),
query: query).encode().buffer)
var message = await conn.readLp(64*1024)
let response = HistoryRPC.init(message)
if response.isErr:
error "failed to decode response"
return
# NOTE Perform accounting operation
# if SWAPAccountingEnabled:
let peerId = peer.peerInfo.peerId
let messages = response.value.response.messages
accountFor(peerId, messages.len)
handler(response.value.response)

View File

@ -71,3 +71,13 @@ proc init*(T: type Cheque, buffer: seq[byte]): ProtoResult[T] =
discard ? pb.getField(3, cheque.amount)
ok(cheque)
# Accounting
#
proc accountFor*(peerId: PeerId, n: int) {.gcsafe.} =
info "Accounting for", peerId, n
# TODO End to end communication
# TODO Better state management (STDOUT for now)