enhancement/integrate-store-chat (#208)

* added store protocol

* setting peer

* fix

* line

* Update dingpu.md

* Update chat2.nim

* Update dingpu.md

* Update dingpu.md

* playing around

* fix

* fix
This commit is contained in:
Dean Eigenmann 2020-10-14 14:33:21 +02:00 committed by GitHub
parent d6fdecafd8
commit 5c338ba731
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 12 deletions

View File

@ -2,11 +2,13 @@
## Basic chat usage
> The chat app requires that the remote peer connected to is supports the WakuStore protocol. For Dingpu this is already the case.
Start two chat apps:
```
./build/chat2 --ports-shift=0
./build/chat2 --ports-shift=1
./build/chat2 --ports-shift:0 --storenode:/ip4/134.209.139.210/tcp/60000/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS
./build/chat2 --ports-shift:1 --storenode:/ip4/134.209.139.210/tcp/60000/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS
```
Type `/connect` then paste address of other node.

View File

@ -19,7 +19,7 @@ import libp2p/[switch, # manage transports, a single entry poi
muxers/muxer, # define an interface for stream multiplexing, allowing peers to offer many protocols over a single connection
muxers/mplex/mplex] # define some contants and message types for stream multiplexing
import ../../waku/node/v2/[config, wakunode2, waku_types],
../../waku/protocol/v2/waku_relay,
../../waku/protocol/v2/[waku_relay, waku_store],
../../waku/node/common
const Help = """
@ -55,21 +55,23 @@ proc initAddress(T: type MultiAddress, str: string): T =
raise newException(ValueError,
"Invalid bootstrap node multi-address")
# NOTE Dialing on WakuRelay specifically
proc dialPeer(c: Chat, address: string) {.async.} =
proc parsePeer(address: string): PeerInfo =
let multiAddr = MultiAddress.initAddress(address)
let parts = address.split("/")
let remotePeer = PeerInfo.init(parts[^1], [multiAddr])
result = PeerInfo.init(parts[^1], [multiAddr])
echo &"dialing peer: {multiAddr}"
# NOTE Dialing on WakuRelay specifically
proc dialPeer(c: Chat, peer: PeerInfo) {.async.} =
echo &"dialing peer: {peer.peerId}"
# XXX Discarding conn, do we want to keep this here?
discard await c.node.switch.dial(remotePeer, WakuRelayCodec)
discard await c.node.switch.dial(peer, WakuRelayCodec)
c.connected = true
proc connectToNodes(c: Chat, nodes: openArray[string]) =
echo "Connecting to nodes"
for nodeId in nodes:
discard dialPeer(c, nodeId)
let peer = parsePeer(nodeId)
discard dialPeer(c, peer)
proc publish(c: Chat, line: string) =
let payload = cast[seq[byte]](line)
@ -113,7 +115,8 @@ proc writeAndPrint(c: Chat) {.async.} =
echo "enter address of remote peer"
let address = await c.transp.readLine()
if address.len > 0:
await c.dialPeer(address)
let peer = parsePeer(address)
await c.dialPeer(peer)
# elif line.startsWith("/exit"):
# if p.connected and p.conn.closed.not:
@ -131,7 +134,8 @@ proc writeAndPrint(c: Chat) {.async.} =
else:
try:
if line.startsWith("/") and "p2p" in line:
await c.dialPeer(line)
let peer = parsePeer(line)
await c.dialPeer(peer)
except:
echo &"unable to dial remote peer {line}"
echo getCurrentExceptionMsg()
@ -172,10 +176,23 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
let listenStr = $peerInfo.addrs[0] & "/p2p/" & $peerInfo.peerId
echo &"Listening on\n {listenStr}"
let topic = cast[Topic](DefaultContentTopic)
let multiAddr = MultiAddress.initAddress(conf.storenode)
let parts = conf.storenode.split("/")
node.wakuStore.setPeer(PeerInfo.init(parts[^1], [multiAddr]))
proc storeHandler(response: HistoryResponse) {.gcsafe.} =
for msg in response.messages:
let payload = cast[string](msg.payload)
echo &"{payload}"
info "Hit store handler"
await node.query(HistoryQuery(topics: @[topic]), storeHandler)
# Subscribe to a topic
# TODO To get end to end sender would require more information in payload
# We could possibly indicate the relayer point with connection somehow probably (?)
let topic = cast[Topic](DefaultTopic)
proc handler(topic: Topic, data: seq[byte]) {.async, gcsafe.} =
let message = WakuMessage.init(data).value
let payload = cast[string](message.payload)

View File

@ -133,6 +133,7 @@ proc start*(node: WakuNode) {.async.} =
let msg = WakuMessage.init(data)
if msg.isOk():
node.filters.notify(msg.value(), "")
await node.subscriptions.notify(topic, msg.value())
await node.wakuRelay.subscribe("waku", relayHandler)