mirror of
https://github.com/status-im/nim-libp2p.git
synced 2025-01-10 13:06:09 +00:00
e623e70e7b
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
81 lines
2.4 KiB
Nim
81 lines
2.4 KiB
Nim
## Nim-LibP2P
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
|
## 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.
|
|
|
|
import chronos, options
|
|
import nimcrypto/utils, chronicles
|
|
import types,
|
|
../../connection,
|
|
../../varint,
|
|
../../vbuffer,
|
|
../../stream/lpstream
|
|
|
|
logScope:
|
|
topic = "MplexCoder"
|
|
|
|
type
|
|
Msg* = tuple
|
|
id: uint
|
|
msgType: MessageType
|
|
data: seq[byte]
|
|
|
|
proc readMplexVarint(conn: Connection): Future[Option[uint]] {.async, gcsafe.} =
|
|
var
|
|
varint: uint
|
|
length: int
|
|
res: VarintStatus
|
|
buffer = newSeq[byte](10)
|
|
|
|
result = none(uint)
|
|
try:
|
|
for i in 0..<len(buffer):
|
|
await conn.readExactly(addr buffer[i], 1)
|
|
res = PB.getUVarint(buffer.toOpenArray(0, i), length, varint)
|
|
if res == VarintStatus.Success:
|
|
return some(varint)
|
|
if res != VarintStatus.Success:
|
|
raise newInvalidVarintException()
|
|
except LPStreamIncompleteError as exc:
|
|
trace "unable to read varint", exc = exc.msg
|
|
|
|
proc readMsg*(conn: Connection): Future[Option[Msg]] {.async, gcsafe.} =
|
|
let headerVarint = await conn.readMplexVarint()
|
|
if headerVarint.isNone:
|
|
return
|
|
|
|
trace "read header varint", varint = headerVarint
|
|
|
|
let dataLenVarint = await conn.readMplexVarint()
|
|
var data: seq[byte]
|
|
if dataLenVarint.isSome and dataLenVarint.get() > 0.uint:
|
|
data = await conn.read(dataLenVarint.get().int)
|
|
trace "read size varint", varint = dataLenVarint
|
|
|
|
let header = headerVarint.get()
|
|
result = some((header shr 3, MessageType(header and 0x7), data))
|
|
|
|
proc writeMsg*(conn: Connection,
|
|
id: uint,
|
|
msgType: MessageType,
|
|
data: seq[byte] = @[]) {.async, gcsafe.} =
|
|
## write lenght prefixed
|
|
var buf = initVBuffer()
|
|
buf.writePBVarint(id shl 3 or ord(msgType).uint)
|
|
buf.writePBVarint(data.len().uint) # size should be always sent
|
|
buf.finish()
|
|
try:
|
|
await conn.write(buf.buffer & data)
|
|
except LPStreamIncompleteError as exc:
|
|
trace "unable to send message", exc = exc.msg
|
|
|
|
proc writeMsg*(conn: Connection,
|
|
id: uint,
|
|
msgType: MessageType,
|
|
data: string) {.async, gcsafe.} =
|
|
result = conn.writeMsg(id, msgType, cast[seq[byte]](data))
|