nim-libp2p/libp2p/multistream.nim

202 lines
6.1 KiB
Nim
Raw Normal View History

2019-08-30 05:16:55 +00:00
## Nim-LibP2P
2019-09-24 17:48:23 +00:00
## Copyright (c) 2019 Status Research & Development GmbH
2019-08-30 05:16:55 +00:00
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
2020-06-03 02:21:11 +00:00
import strutils
import chronos, chronicles, stew/byteutils
import stream/connection,
2019-10-04 16:04:21 +00:00
vbuffer,
PubSub (Gossip & Flood) Implementation (#36) This adds gossipsub and floodsub, as well as basic interop testing with the go libp2p daemon. * add close event * wip: gossipsub * splitting rpc message * making message handling more consistent * initial gossipsub implementation * feat: nim 1.0 cleanup * wip: gossipsub protobuf * adding encoding/decoding of gossipsub messages * add disconnect handler * add proper gossipsub msg handling * misc: cleanup for nim 1.0 * splitting floodsub and gossipsub tests * feat: add mesh rebalansing * test pubsub * add mesh rebalansing tests * testing mesh maintenance * finishing mcache implementatin * wip: commenting out broken tests * wip: don't run heartbeat for now * switchout debug for trace logging * testing gossip peer selection algorithm * test stream piping * more work around message amplification * get the peerid from message * use timed cache as backing store * allow setting timeout in constructor * several changes to improve performance * more through testing of msg amplification * prevent gc issues * allow piping to self and prevent deadlocks * improove floodsub * allow running hook on cache eviction * prevent race conditions * prevent race conditions and improove tests * use hashes as cache keys * removing useless file * don't create a new seq * re-enable pubsub tests * fix imports * reduce number of runs to speed up tests * break out control message processing * normalize sleeps between steps * implement proper transport filtering * initial interop testing * clean up floodsub publish logic * allow dialing without a protocol * adding multiple reads/writes * use protobuf varint in mplex * don't loose conn's peerInfo * initial interop pubsub tests * don't duplicate connections/peers * bring back interop tests * wip: interop * re-enable interop and daemon tests * add multiple read write tests from handlers * don't cleanup channel prematurely * use correct channel to send/receive msgs * adjust tests with latest changes * include interop tests * remove temp logging output * fix ci * use correct public key serialization * additional tests for pubsub interop
2019-12-06 02:16:18 +00:00
protocols/protocol
2019-08-30 05:16:55 +00:00
2019-09-11 21:06:10 +00:00
logScope:
topics = "multistream"
2019-09-11 21:06:10 +00:00
2020-01-07 08:02:37 +00:00
const
2019-10-04 16:04:21 +00:00
MsgSize* = 64*1024
Codec* = "/multistream/1.0.0"
MSCodec* = "\x13" & Codec & "\n"
2019-10-04 20:10:01 +00:00
Na* = "\x03na\n"
Ls* = "\x03ls\n"
2019-08-30 05:16:55 +00:00
type
2019-08-30 22:15:47 +00:00
Matcher* = proc (proto: string): bool {.gcsafe.}
2019-08-30 05:16:55 +00:00
HandlerHolder* = object
2019-09-06 00:16:49 +00:00
proto*: string
protocol*: LPProtocol
match*: Matcher
2019-08-30 05:16:55 +00:00
2020-02-04 16:16:21 +00:00
MultistreamSelect* = ref object of RootObj
2019-08-30 05:16:55 +00:00
handlers*: seq[HandlerHolder]
codec*: string
2020-02-04 16:16:21 +00:00
proc newMultistream*(): MultistreamSelect =
2019-08-30 05:16:55 +00:00
new result
2019-09-06 21:25:54 +00:00
result.codec = MSCodec
2019-08-30 05:16:55 +00:00
template validateSuffix(str: string): untyped =
if str.endsWith("\n"):
str.removeSuffix("\n")
else:
raise newException(CatchableError, "MultistreamSelect failed, malformed message")
2020-02-04 16:16:21 +00:00
proc select*(m: MultistreamSelect,
2019-08-30 05:16:55 +00:00
conn: Connection,
2019-09-14 13:55:52 +00:00
proto: seq[string]):
Future[string] {.async.} =
trace "initiating handshake", conn, codec = m.codec
2019-08-30 05:16:55 +00:00
## select a remote protocol
2019-09-05 15:19:15 +00:00
await conn.write(m.codec) # write handshake
2019-08-30 05:16:55 +00:00
if proto.len() > 0:
trace "selecting proto", conn, proto = proto[0]
2019-09-05 15:19:15 +00:00
await conn.writeLp((proto[0] & "\n")) # select proto
2019-08-30 05:16:55 +00:00
var s = string.fromBytes((await conn.readLp(MsgSize))) # read ms header
validateSuffix(s)
if s != Codec:
notice "handshake failed", conn, codec = s
raise newException(CatchableError, "MultistreamSelect handshake failed")
else:
trace "multistream handshake success", conn
2019-08-30 05:16:55 +00:00
if proto.len() == 0: # no protocols, must be a handshake call
return Codec
else:
s = string.fromBytes(await conn.readLp(MsgSize)) # read the first proto
validateSuffix(s)
trace "reading first requested proto", conn
if s == proto[0]:
trace "successfully selected ", conn, proto = proto[0]
return proto[0]
elif proto.len > 1:
# Try to negotiate alternatives
let protos = proto[1..<proto.len()]
trace "selecting one of several protos", conn, protos = protos
for p in protos:
trace "selecting proto", conn, proto = p
await conn.writeLp((p & "\n")) # select proto
s = string.fromBytes(await conn.readLp(MsgSize)) # read the first proto
validateSuffix(s)
if s == p:
trace "selected protocol", conn, protocol = s
return s
return ""
else:
# No alternatives, fail
return ""
2020-02-04 16:16:21 +00:00
proc select*(m: MultistreamSelect,
conn: Connection,
2020-01-07 08:02:37 +00:00
proto: string): Future[bool] {.async.} =
if proto.len > 0:
return (await m.select(conn, @[proto])) == proto
2020-01-07 08:02:37 +00:00
else:
return (await m.select(conn, @[])) == Codec
2020-02-04 16:16:21 +00:00
proc select*(m: MultistreamSelect, conn: Connection): Future[bool] =
2019-09-08 07:43:33 +00:00
m.select(conn, "")
2019-08-30 05:16:55 +00:00
2020-02-04 16:16:21 +00:00
proc list*(m: MultistreamSelect,
2019-08-30 05:16:55 +00:00
conn: Connection): Future[seq[string]] {.async.} =
## list remote protos requests on connection
if not await m.select(conn):
2019-08-30 05:16:55 +00:00
return
await conn.write(Ls) # send ls
2019-08-30 05:16:55 +00:00
var list = newSeq[string]()
let ms = string.fromBytes(await conn.readLp(MsgSize))
2019-08-30 05:16:55 +00:00
for s in ms.split("\n"):
if s.len() > 0:
list.add(s)
result = list
proc handle*(m: MultistreamSelect, conn: Connection, active: bool = false) {.async, gcsafe.} =
trace "Starting multistream handler", conn, handshaked = active
var handshaked = active
2020-05-23 17:10:22 +00:00
try:
while not conn.atEof:
var ms = string.fromBytes(await conn.readLp(MsgSize))
validateSuffix(ms)
if not handshaked and ms != Codec:
notice "expected handshake message", conn, instead=ms
2020-08-02 10:22:49 +00:00
raise newException(CatchableError,
"MultistreamSelect handling failed, invalid first message")
2020-01-07 08:02:37 +00:00
trace "handle: got request", conn, ms
if ms.len() <= 0:
trace "handle: invalid proto", conn
await conn.write(Na)
if m.handlers.len() == 0:
trace "handle: sending `na` for protocol", conn, protocol = ms
await conn.write(Na)
continue
case ms:
of "ls":
trace "handle: listing protos", conn
var protos = ""
for h in m.handlers:
protos &= (h.proto & "\n")
await conn.writeLp(protos)
of Codec:
if not handshaked:
await conn.write(m.codec)
handshaked = true
else:
trace "handle: sending `na` for duplicate handshake while handshaked",
conn
await conn.write(Na)
else:
for h in m.handlers:
if (not isNil(h.match) and h.match(ms)) or ms == h.proto:
trace "found handler", conn, protocol = ms
await conn.writeLp((h.proto & "\n"))
await h.protocol.handler(conn, ms)
return
debug "no handlers", conn, protocol = ms
await conn.write(Na)
except CancelledError as exc:
raise exc
2020-05-23 17:10:22 +00:00
except CatchableError as exc:
trace "Exception in multistream", conn, msg = exc.msg
2020-05-23 17:10:22 +00:00
finally:
await conn.close()
trace "Stopped multistream handler", conn
2019-08-30 05:16:55 +00:00
proc addHandler*(m: MultistreamSelect,
codec: string,
protocol: LPProtocol,
matcher: Matcher = nil) =
2019-09-14 13:55:52 +00:00
## register a protocol
trace "registering protocol", codec = codec
2019-09-14 13:55:52 +00:00
m.handlers.add(HandlerHolder(proto: codec,
protocol: protocol,
match: matcher))
proc addHandler*(m: MultistreamSelect,
codec: string,
handler: LPProtoHandler,
matcher: Matcher = nil) =
2019-09-14 13:55:52 +00:00
## helper to allow registering pure handlers
2019-09-14 15:55:58 +00:00
trace "registering proto handler", codec = codec
2019-09-14 13:55:52 +00:00
let protocol = new LPProtocol
protocol.codec = codec
protocol.handler = handler
2019-09-03 20:40:19 +00:00
m.handlers.add(HandlerHolder(proto: codec,
2019-08-30 05:16:55 +00:00
protocol: protocol,
match: matcher))