remove casts from (#203)

This commit is contained in:
Dmitriy Ryajov 2020-06-02 20:21:11 -06:00 committed by GitHub
parent bb8bff2195
commit 5960d42c50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 123 additions and 124 deletions

View File

@ -7,8 +7,8 @@
## This file may not be copied, modified, or distributed except according to ## This file may not be copied, modified, or distributed except according to
## those terms. ## those terms.
import strutils, stew/byteutils import strutils
import chronos, chronicles import chronos, chronicles, stew/byteutils
import connection, import connection,
vbuffer, vbuffer,
errors, errors,
@ -58,7 +58,7 @@ proc select*(m: MultistreamSelect,
trace "selecting proto", proto = proto trace "selecting proto", proto = proto
await conn.writeLp((proto[0] & "\n")) # select proto await conn.writeLp((proto[0] & "\n")) # select proto
result = cast[string]((await conn.readLp(1024))) # read ms header result = string.fromBytes((await conn.readLp(1024))) # read ms header
result.removeSuffix("\n") result.removeSuffix("\n")
if result != Codec: if result != Codec:
notice "handshake failed", codec = result.toHex() notice "handshake failed", codec = result.toHex()

View File

@ -8,7 +8,7 @@
## those terms. ## those terms.
import chronos import chronos
import nimcrypto/utils, chronicles import nimcrypto/utils, chronicles, stew/byteutils
import types, import types,
../../connection, ../../connection,
../../utility, ../../utility,
@ -74,7 +74,7 @@ proc writeMsg*(conn: Connection,
msgType: MessageType, msgType: MessageType,
data: string) {.async, gcsafe.} = data: string) {.async, gcsafe.} =
# TODO: changing this to # TODO: changing this to
#`await conn.writeMsg(id, msgType, cast[seq[byte]](data))` #`await conn.writeMsg(id, msgType, data.toBytes())`
# causes all sorts of race conditions and hangs. # causes all sorts of race conditions and hangs.
# DON'T DO IT! # DON'T DO IT!
result = conn.writeMsg(id, msgType, cast[seq[byte]](data)) result = conn.writeMsg(id, msgType, data.toBytes())

View File

@ -8,7 +8,7 @@
## those terms. ## those terms.
import tables, sequtils, oids import tables, sequtils, oids
import chronos, chronicles import chronos, chronicles, stew/byteutils
import ../muxer, import ../muxer,
../../connection, ../../connection,
../../stream/lpstream, ../../stream/lpstream,
@ -89,7 +89,7 @@ method handle*(m: Mplex) {.async, gcsafe.} =
case msgType: case msgType:
of MessageType.New: of MessageType.New:
let name = cast[string](data) let name = string.fromBytes(data)
channel = await m.newStreamInternal(false, id, name) channel = await m.newStreamInternal(false, id, name)
trace "created channel", id = id, trace "created channel", id = id,
name = name, name = name,

View File

@ -8,7 +8,7 @@
## those terms. ## those terms.
import options import options
import chronicles import chronicles, stew/byteutils
import nimcrypto/sysrand import nimcrypto/sysrand
import messages, protobuf, import messages, protobuf,
../../../peer, ../../../peer,
@ -23,7 +23,7 @@ const PubSubPrefix = "libp2p-pubsub:"
proc msgIdProvider(m: Message): string = proc msgIdProvider(m: Message): string =
## default msg id provider ## default msg id provider
m.seqno.toHex() & PeerID.init(m.fromPeer).pretty crypto.toHex(m.seqno) & PeerID.init(m.fromPeer).pretty
template msgId*(m: Message): string = template msgId*(m: Message): string =
## calls the ``msgIdProvider`` from ## calls the ``msgIdProvider`` from
@ -41,7 +41,7 @@ proc sign*(msg: Message, p: PeerInfo): Message {.gcsafe.} =
if buff.buffer.len > 0: if buff.buffer.len > 0:
result = msg result = msg
result.signature = p.privateKey. result.signature = p.privateKey.
sign(cast[seq[byte]](PubSubPrefix) & buff.buffer).tryGet(). sign(PubSubPrefix.toBytes() & buff.buffer).tryGet().
getBytes() getBytes()
proc verify*(m: Message, p: PeerInfo): bool = proc verify*(m: Message, p: PeerInfo): bool =
@ -57,7 +57,7 @@ proc verify*(m: Message, p: PeerInfo): bool =
var key: PublicKey var key: PublicKey
if remote.init(m.signature) and key.init(m.key): if remote.init(m.signature) and key.init(m.key):
trace "verifying signature", remoteSignature = remote trace "verifying signature", remoteSignature = remote
result = remote.verify(cast[seq[byte]](PubSubPrefix) & buff.buffer, key) result = remote.verify(PubSubPrefix.toBytes() & buff.buffer, key)
proc newMessage*(p: PeerInfo, proc newMessage*(p: PeerInfo,
data: seq[byte], data: seq[byte],

View File

@ -10,7 +10,7 @@
{.used.} {.used.}
import unittest, sequtils, options, tables, sets import unittest, sequtils, options, tables, sets
import chronos import chronos, stew/byteutils
import utils, import utils,
../../libp2p/[errors, ../../libp2p/[errors,
switch, switch,
@ -60,7 +60,7 @@ suite "FloodSub":
await nodes[1].subscribe("foobar", handler) await nodes[1].subscribe("foobar", handler)
await waitSub(nodes[0], nodes[1], "foobar") await waitSub(nodes[0], nodes[1], "foobar")
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!")) await nodes[0].publish("foobar", "Hello!".toBytes())
result = await completionFut.wait(5.seconds) result = await completionFut.wait(5.seconds)
@ -91,7 +91,7 @@ suite "FloodSub":
await nodes[0].subscribe("foobar", handler) await nodes[0].subscribe("foobar", handler)
await waitSub(nodes[1], nodes[0], "foobar") await waitSub(nodes[1], nodes[0], "foobar")
await nodes[1].publish("foobar", cast[seq[byte]]("Hello!")) await nodes[1].publish("foobar", "Hello!".toBytes())
result = await completionFut.wait(5.seconds) result = await completionFut.wait(5.seconds)
@ -126,7 +126,7 @@ suite "FloodSub":
nodes[1].addValidator("foobar", validator) nodes[1].addValidator("foobar", validator)
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!")) await nodes[0].publish("foobar", "Hello!".toBytes())
check (await handlerFut) == true check (await handlerFut) == true
await allFuturesThrowing( await allFuturesThrowing(
@ -160,7 +160,7 @@ suite "FloodSub":
nodes[1].addValidator("foobar", validator) nodes[1].addValidator("foobar", validator)
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!")) await nodes[0].publish("foobar", "Hello!".toBytes())
await allFuturesThrowing( await allFuturesThrowing(
nodes[0].stop(), nodes[0].stop(),
@ -198,8 +198,8 @@ suite "FloodSub":
nodes[1].addValidator("foo", "bar", validator) nodes[1].addValidator("foo", "bar", validator)
await nodes[0].publish("foo", cast[seq[byte]]("Hello!")) await nodes[0].publish("foo", "Hello!".toBytes())
await nodes[0].publish("bar", cast[seq[byte]]("Hello!")) await nodes[0].publish("bar", "Hello!".toBytes())
await allFuturesThrowing( await allFuturesThrowing(
nodes[0].stop(), nodes[0].stop(),
@ -252,7 +252,7 @@ suite "FloodSub":
var pubs: seq[Future[void]] var pubs: seq[Future[void]]
for i in 0..<runs: for i in 0..<runs:
pubs &= nodes[i].publish("foobar", cast[seq[byte]]("Hello!")) pubs &= nodes[i].publish("foobar", "Hello!".toBytes())
await allFuturesThrowing(pubs) await allFuturesThrowing(pubs)
await allFuturesThrowing(futs.mapIt(it[0])) await allFuturesThrowing(futs.mapIt(it[0]))
@ -306,7 +306,7 @@ suite "FloodSub":
var pubs: seq[Future[void]] var pubs: seq[Future[void]]
for i in 0..<runs: for i in 0..<runs:
pubs &= nodes[i].publish("foobar", cast[seq[byte]]("Hello!")) pubs &= nodes[i].publish("foobar", "Hello!".toBytes())
await allFuturesThrowing(pubs) await allFuturesThrowing(pubs)
await allFuturesThrowing(futs.mapIt(it[0])) await allFuturesThrowing(futs.mapIt(it[0]))

View File

@ -10,7 +10,7 @@
{.used.} {.used.}
import unittest, sequtils, options, tables, sets import unittest, sequtils, options, tables, sets
import chronos import chronos, stew/byteutils
import chronicles import chronicles
import utils, ../../libp2p/[errors, import utils, ../../libp2p/[errors,
peer, peer,
@ -76,7 +76,7 @@ suite "GossipSub":
result = true result = true
nodes[1].addValidator("foobar", validator) nodes[1].addValidator("foobar", validator)
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!")) await nodes[0].publish("foobar", "Hello!".toBytes())
result = (await validatorFut) and (await handlerFut) result = (await validatorFut) and (await handlerFut)
await allFuturesThrowing( await allFuturesThrowing(
@ -110,7 +110,7 @@ suite "GossipSub":
result = false result = false
nodes[1].addValidator("foobar", validator) nodes[1].addValidator("foobar", validator)
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!")) await nodes[0].publish("foobar", "Hello!".toBytes())
result = await validatorFut result = await validatorFut
await allFuturesThrowing( await allFuturesThrowing(
@ -151,8 +151,8 @@ suite "GossipSub":
false false
nodes[1].addValidator("foo", "bar", validator) nodes[1].addValidator("foo", "bar", validator)
await nodes[0].publish("foo", cast[seq[byte]]("Hello!")) await nodes[0].publish("foo", "Hello!".toBytes())
await nodes[0].publish("bar", cast[seq[byte]]("Hello!")) await nodes[0].publish("bar", "Hello!".toBytes())
result = ((await passed) and (await failed) and (await handlerFut)) result = ((await passed) and (await failed) and (await handlerFut))
await allFuturesThrowing( await allFuturesThrowing(
@ -272,7 +272,7 @@ suite "GossipSub":
nodes[1].pubsub.get().addObserver(obs1) nodes[1].pubsub.get().addObserver(obs1)
nodes[0].pubsub.get().addObserver(obs2) nodes[0].pubsub.get().addObserver(obs2)
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!")) await nodes[0].publish("foobar", "Hello!".toBytes())
var gossipSub1: GossipSub = GossipSub(nodes[0].pubSub.get()) var gossipSub1: GossipSub = GossipSub(nodes[0].pubSub.get())
@ -309,7 +309,7 @@ suite "GossipSub":
await nodes[1].subscribe("foobar", handler) await nodes[1].subscribe("foobar", handler)
await waitSub(nodes[0], nodes[1], "foobar") await waitSub(nodes[0], nodes[1], "foobar")
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!")) await nodes[0].publish("foobar", "Hello!".toBytes())
result = await passed result = await passed

View File

@ -1,7 +1,7 @@
{.used.} {.used.}
import options, sets, sequtils import unittest, options, sets, sequtils
import unittest import stew/byteutils
import ../../libp2p/[peer, import ../../libp2p/[peer,
crypto/crypto, crypto/crypto,
protocols/pubsub/mcache, protocols/pubsub/mcache,
@ -12,7 +12,7 @@ suite "MCache":
test "put/get": test "put/get":
var mCache = newMCache(3, 5) var mCache = newMCache(3, 5)
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data, var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345")) seqno: "12345".toBytes())
mCache.put(msg) mCache.put(msg)
check mCache.get(msg.msgId).isSome and mCache.get(msg.msgId).get() == msg check mCache.get(msg.msgId).isSome and mCache.get(msg.msgId).get() == msg
@ -21,13 +21,13 @@ suite "MCache":
for i in 0..<3: for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data, var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"), seqno: "12345".toBytes(),
topicIDs: @["foo"]) topicIDs: @["foo"])
mCache.put(msg) mCache.put(msg)
for i in 0..<5: for i in 0..<5:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data, var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"), seqno: "12345".toBytes(),
topicIDs: @["bar"]) topicIDs: @["bar"])
mCache.put(msg) mCache.put(msg)
@ -42,7 +42,7 @@ suite "MCache":
for i in 0..<3: for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data, var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"), seqno: "12345".toBytes(),
topicIDs: @["foo"]) topicIDs: @["foo"])
mCache.put(msg) mCache.put(msg)
@ -51,7 +51,7 @@ suite "MCache":
for i in 0..<3: for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data, var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"), seqno: "12345".toBytes(),
topicIDs: @["bar"]) topicIDs: @["bar"])
mCache.put(msg) mCache.put(msg)
@ -60,7 +60,7 @@ suite "MCache":
for i in 0..<3: for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data, var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"), seqno: "12345".toBytes(),
topicIDs: @["baz"]) topicIDs: @["baz"])
mCache.put(msg) mCache.put(msg)
@ -72,19 +72,19 @@ suite "MCache":
for i in 0..<3: for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data, var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"), seqno: "12345".toBytes(),
topicIDs: @["foo"]) topicIDs: @["foo"])
mCache.put(msg) mCache.put(msg)
for i in 0..<3: for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data, var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"), seqno: "12345".toBytes(),
topicIDs: @["bar"]) topicIDs: @["bar"])
mCache.put(msg) mCache.put(msg)
for i in 0..<3: for i in 0..<3:
var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data, var msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: cast[seq[byte]]("12345"), seqno: "12345".toBytes(),
topicIDs: @["baz"]) topicIDs: @["baz"])
mCache.put(msg) mCache.put(msg)

View File

@ -1,5 +1,5 @@
import unittest, strformat import unittest, strformat
import chronos import chronos, stew/byteutils
import ../libp2p/errors import ../libp2p/errors
import ../libp2p/stream/bufferstream import ../libp2p/stream/bufferstream
@ -15,9 +15,8 @@ suite "BufferStream":
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard
let buff = newBufferStream(writeHandler, 16) let buff = newBufferStream(writeHandler, 16)
check buff.len == 0 check buff.len == 0
var data: seq[char] var data = "12345"
data.add(@"12345") await buff.pushTo(data.toBytes())
await buff.pushTo(cast[seq[byte]](data))
check buff.len == 5 check buff.len == 5
result = true result = true
@ -32,7 +31,7 @@ suite "BufferStream":
let buff = newBufferStream(writeHandler, 4) let buff = newBufferStream(writeHandler, 4)
check buff.len == 0 check buff.len == 0
let fut = buff.pushTo(cast[seq[byte]](@"12345")) let fut = buff.pushTo("12345".toBytes())
check buff.len == 4 check buff.len == 4
check buff.popFirst() == byte(ord('1')) check buff.popFirst() == byte(ord('1'))
await fut await fut
@ -51,10 +50,10 @@ suite "BufferStream":
let buff = newBufferStream(writeHandler, 10) let buff = newBufferStream(writeHandler, 10)
check buff.len == 0 check buff.len == 0
await buff.pushTo(cast[seq[byte]](@"12345")) await buff.pushTo("12345".toBytes())
var data = newSeq[byte](3) var data = newSeq[byte](3)
await buff.readExactly(addr data[0], 3) await buff.readExactly(addr data[0], 3)
check ['1', '2', '3'] == cast[string](data) check ['1', '2', '3'] == string.fromBytes(data)
result = true result = true
@ -69,11 +68,11 @@ suite "BufferStream":
let buff = newBufferStream(writeHandler, 10) let buff = newBufferStream(writeHandler, 10)
check buff.len == 0 check buff.len == 0
await buff.pushTo(cast[seq[byte]](@"12345")) await buff.pushTo("12345".toBytes())
check buff.len == 5 check buff.len == 5
var data: seq[byte] = newSeq[byte](2) var data: seq[byte] = newSeq[byte](2)
await buff.readExactly(addr data[0], 2) await buff.readExactly(addr data[0], 2)
check cast[string](data) == @['1', '2'] check string.fromBytes(data) == @['1', '2']
result = true result = true
@ -90,11 +89,11 @@ suite "BufferStream":
var data: seq[byte] = newSeq[byte](3) var data: seq[byte] = newSeq[byte](3)
let readFut = buff.readOnce(addr data[0], 5) let readFut = buff.readOnce(addr data[0], 5)
await buff.pushTo(cast[seq[byte]](@"123")) await buff.pushTo("123".toBytes())
check buff.len == 3 check buff.len == 3
check (await readFut) == 3 check (await readFut) == 3
check cast[string](data) == @['1', '2', '3'] check string.fromBytes(data) == @['1', '2', '3']
result = true result = true
@ -106,7 +105,7 @@ suite "BufferStream":
test "write ptr": test "write ptr":
proc testWritePtr(): Future[bool] {.async.} = proc testWritePtr(): Future[bool] {.async.} =
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = proc writeHandler(data: seq[byte]) {.async, gcsafe.} =
check cast[string](data) == "Hello!" check string.fromBytes(data) == "Hello!"
let buff = newBufferStream(writeHandler, 10) let buff = newBufferStream(writeHandler, 10)
check buff.len == 0 check buff.len == 0
@ -124,7 +123,7 @@ suite "BufferStream":
test "write string": test "write string":
proc testWritePtr(): Future[bool] {.async.} = proc testWritePtr(): Future[bool] {.async.} =
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = proc writeHandler(data: seq[byte]) {.async, gcsafe.} =
check cast[string](data) == "Hello!" check string.fromBytes(data) == "Hello!"
let buff = newBufferStream(writeHandler, 10) let buff = newBufferStream(writeHandler, 10)
check buff.len == 0 check buff.len == 0
@ -141,12 +140,12 @@ suite "BufferStream":
test "write bytes": test "write bytes":
proc testWritePtr(): Future[bool] {.async.} = proc testWritePtr(): Future[bool] {.async.} =
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = proc writeHandler(data: seq[byte]) {.async, gcsafe.} =
check cast[string](data) == "Hello!" check string.fromBytes(data) == "Hello!"
let buff = newBufferStream(writeHandler, 10) let buff = newBufferStream(writeHandler, 10)
check buff.len == 0 check buff.len == 0
await buff.write(cast[seq[byte]]("Hello!")) await buff.write("Hello!".toBytes())
result = true result = true
@ -159,7 +158,7 @@ suite "BufferStream":
proc testWritePtr(): Future[bool] {.async.} = proc testWritePtr(): Future[bool] {.async.} =
var count = 1 var count = 1
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = proc writeHandler(data: seq[byte]) {.async, gcsafe.} =
check cast[string](data) == &"Msg {$count}" check string.fromBytes(data) == &"Msg {$count}"
count.inc count.inc
let buff = newBufferStream(writeHandler, 10) let buff = newBufferStream(writeHandler, 10)
@ -189,32 +188,32 @@ suite "BufferStream":
let buff = newBufferStream(writeHandler, 10) let buff = newBufferStream(writeHandler, 10)
check buff.len == 0 check buff.len == 0
await buff.pushTo(cast[seq[byte]]("Msg 1")) await buff.pushTo("Msg 1".toBytes())
await buff.pushTo(cast[seq[byte]]("Msg 2")) await buff.pushTo("Msg 2".toBytes())
await buff.pushTo(cast[seq[byte]]("Msg 3")) await buff.pushTo("Msg 3".toBytes())
var data = newSeq[byte](5) var data = newSeq[byte](5)
await buff.readExactly(addr data[0], 5) await buff.readExactly(addr data[0], 5)
check cast[string](data) == "Msg 1" check string.fromBytes(data) == "Msg 1"
await buff.readExactly(addr data[0], 5) await buff.readExactly(addr data[0], 5)
check cast[string](data) == "Msg 2" check string.fromBytes(data) == "Msg 2"
await buff.readExactly(addr data[0], 5) await buff.readExactly(addr data[0], 5)
check cast[string](data) == "Msg 3" check string.fromBytes(data) == "Msg 3"
await buff.pushTo(cast[seq[byte]]("Msg 4")) await buff.pushTo("Msg 4".toBytes())
await buff.pushTo(cast[seq[byte]]("Msg 5")) await buff.pushTo("Msg 5".toBytes())
await buff.pushTo(cast[seq[byte]]("Msg 6")) await buff.pushTo("Msg 6".toBytes())
await buff.readExactly(addr data[0], 5) await buff.readExactly(addr data[0], 5)
check cast[string](data) == "Msg 4" check string.fromBytes(data) == "Msg 4"
await buff.readExactly(addr data[0], 5) await buff.readExactly(addr data[0], 5)
check cast[string](data) == "Msg 5" check string.fromBytes(data) == "Msg 5"
await buff.readExactly(addr data[0], 5) await buff.readExactly(addr data[0], 5)
check cast[string](data) == "Msg 6" check string.fromBytes(data) == "Msg 6"
result = true result = true
@ -232,12 +231,12 @@ suite "BufferStream":
var buf2 = newBufferStream(writeHandler2) var buf2 = newBufferStream(writeHandler2)
proc writeHandler1(data: seq[byte]) {.async, gcsafe.} = proc writeHandler1(data: seq[byte]) {.async, gcsafe.} =
var msg = cast[string](data) var msg = string.fromBytes(data)
check msg == "Hello!" check msg == "Hello!"
await buf2.pushTo(data) await buf2.pushTo(data)
proc writeHandler2(data: seq[byte]) {.async, gcsafe.} = proc writeHandler2(data: seq[byte]) {.async, gcsafe.} =
var msg = cast[string](data) var msg = string.fromBytes(data)
check msg == "Hello!" check msg == "Hello!"
await buf1.pushTo(data) await buf1.pushTo(data)
@ -247,14 +246,14 @@ suite "BufferStream":
var res2: seq[byte] = newSeq[byte](7) var res2: seq[byte] = newSeq[byte](7)
var readFut2 = buf2.readExactly(addr res2[0], 7) var readFut2 = buf2.readExactly(addr res2[0], 7)
await buf1.pushTo(cast[seq[byte]]("Hello2!")) await buf1.pushTo("Hello2!".toBytes())
await buf2.pushTo(cast[seq[byte]]("Hello1!")) await buf2.pushTo("Hello1!".toBytes())
await allFuturesThrowing(readFut1, readFut2) await allFuturesThrowing(readFut1, readFut2)
check: check:
res1 == cast[seq[byte]]("Hello2!") res1 == "Hello2!".toBytes()
res2 == cast[seq[byte]]("Hello1!") res2 == "Hello1!".toBytes()
result = true result = true
@ -271,11 +270,11 @@ suite "BufferStream":
var res1: seq[byte] = newSeq[byte](7) var res1: seq[byte] = newSeq[byte](7)
var readFut = buf2.readExactly(addr res1[0], 7) var readFut = buf2.readExactly(addr res1[0], 7)
await buf1.write(cast[seq[byte]]("Hello1!")) await buf1.write("Hello1!".toBytes())
await readFut await readFut
check: check:
res1 == cast[seq[byte]]("Hello1!") res1 == "Hello1!".toBytes()
result = true result = true
@ -298,13 +297,13 @@ suite "BufferStream":
var res2: seq[byte] = newSeq[byte](7) var res2: seq[byte] = newSeq[byte](7)
var readFut2 = buf2.readExactly(addr res2[0], 7) var readFut2 = buf2.readExactly(addr res2[0], 7)
await buf1.write(cast[seq[byte]]("Hello1!")) await buf1.write("Hello1!".toBytes())
await buf2.write(cast[seq[byte]]("Hello2!")) await buf2.write("Hello2!".toBytes())
await allFuturesThrowing(readFut1, readFut2) await allFuturesThrowing(readFut1, readFut2)
check: check:
res1 == cast[seq[byte]]("Hello2!") res1 == "Hello2!".toBytes()
res2 == cast[seq[byte]]("Hello1!") res2 == "Hello1!".toBytes()
result = true result = true
@ -324,14 +323,14 @@ suite "BufferStream":
result = newSeq[byte](6) result = newSeq[byte](6)
await buf1.readExactly(addr result[0], 6) await buf1.readExactly(addr result[0], 6)
proc writer(): Future[void] = buf1.write(cast[seq[byte]]("Hello!")) proc writer(): Future[void] = buf1.write("Hello!".toBytes())
var writerFut = writer() var writerFut = writer()
var readerFut = reader() var readerFut = reader()
await writerFut await writerFut
check: check:
(await readerFut) == cast[seq[byte]]("Hello!") (await readerFut) == "Hello!".toBytes()
result = true result = true
@ -347,11 +346,11 @@ suite "BufferStream":
var res1: seq[byte] = newSeq[byte](7) var res1: seq[byte] = newSeq[byte](7)
var readFut = buf2.readExactly(addr res1[0], 7) var readFut = buf2.readExactly(addr res1[0], 7)
await buf1.write(cast[seq[byte]]("Hello1!")) await buf1.write("Hello1!".toBytes())
await readFut await readFut
check: check:
res1 == cast[seq[byte]]("Hello1!") res1 == "Hello1!".toBytes()
result = true result = true
@ -374,13 +373,13 @@ suite "BufferStream":
var res2: seq[byte] = newSeq[byte](7) var res2: seq[byte] = newSeq[byte](7)
var readFut2 = buf2.readExactly(addr res2[0], 7) var readFut2 = buf2.readExactly(addr res2[0], 7)
await buf1.write(cast[seq[byte]]("Hello1!")) await buf1.write("Hello1!".toBytes())
await buf2.write(cast[seq[byte]]("Hello2!")) await buf2.write("Hello2!".toBytes())
await allFuturesThrowing(readFut1, readFut2) await allFuturesThrowing(readFut1, readFut2)
check: check:
res1 == cast[seq[byte]]("Hello2!") res1 == "Hello2!".toBytes()
res2 == cast[seq[byte]]("Hello1!") res2 == "Hello1!".toBytes()
result = true result = true
@ -400,14 +399,14 @@ suite "BufferStream":
result = newSeq[byte](6) result = newSeq[byte](6)
await buf1.readExactly(addr result[0], 6) await buf1.readExactly(addr result[0], 6)
proc writer(): Future[void] = buf1.write(cast[seq[byte]]("Hello!")) proc writer(): Future[void] = buf1.write("Hello!".toBytes())
var writerFut = writer() var writerFut = writer()
var readerFut = reader() var readerFut = reader()
await writerFut await writerFut
check: check:
(await readerFut) == cast[seq[byte]]("Hello!") (await readerFut) == "Hello!".toBytes()
result = true result = true
@ -431,7 +430,7 @@ suite "BufferStream":
proc writer() {.async.} = proc writer() {.async.} =
while count > 0: while count > 0:
await buf1.write(cast[seq[byte]]("Hello2!")) await buf1.write("Hello2!".toBytes())
count.dec count.dec
var writerFut = writer() var writerFut = writer()

View File

@ -65,7 +65,7 @@ proc testPubSubDaemonPublish(gossip: bool = false,
count: int = 1): Future[bool] {.async.} = count: int = 1): Future[bool] {.async.} =
var pubsubData = "TEST MESSAGE" var pubsubData = "TEST MESSAGE"
var testTopic = "test-topic" var testTopic = "test-topic"
var msgData = cast[seq[byte]](pubsubData) var msgData = pubsubData.toBytes()
var flags = {PSFloodSub} var flags = {PSFloodSub}
if gossip: if gossip:
@ -80,7 +80,7 @@ proc testPubSubDaemonPublish(gossip: bool = false,
var finished = false var finished = false
var times = 0 var times = 0
proc nativeHandler(topic: string, data: seq[byte]) {.async.} = proc nativeHandler(topic: string, data: seq[byte]) {.async.} =
let smsg = cast[string](data) let smsg = string.fromBytes(data)
check smsg == pubsubData check smsg == pubsubData
times.inc() times.inc()
if times >= count and not finished: if times >= count and not finished:
@ -116,7 +116,7 @@ proc testPubSubNodePublish(gossip: bool = false,
count: int = 1): Future[bool] {.async.} = count: int = 1): Future[bool] {.async.} =
var pubsubData = "TEST MESSAGE" var pubsubData = "TEST MESSAGE"
var testTopic = "test-topic" var testTopic = "test-topic"
var msgData = cast[seq[byte]](pubsubData) var msgData = pubsubData.toBytes()
var flags = {PSFloodSub} var flags = {PSFloodSub}
if gossip: if gossip:
@ -139,7 +139,7 @@ proc testPubSubNodePublish(gossip: bool = false,
proc pubsubHandler(api: DaemonAPI, proc pubsubHandler(api: DaemonAPI,
ticket: PubsubTicket, ticket: PubsubTicket,
message: PubSubMessage): Future[bool] {.async.} = message: PubSubMessage): Future[bool] {.async.} =
let smsg = cast[string](message.data) let smsg = string.fromBytes(message.data)
check smsg == pubsubData check smsg == pubsubData
times.inc() times.inc()
if times >= count and not finished: if times >= count and not finished:
@ -184,11 +184,11 @@ suite "Interop":
var testFuture = newFuture[void]("test.future") var testFuture = newFuture[void]("test.future")
proc daemonHandler(api: DaemonAPI, stream: P2PStream) {.async.} = proc daemonHandler(api: DaemonAPI, stream: P2PStream) {.async.} =
check cast[string](await stream.transp.readLp()) == "test 1" check string.fromBytes(await stream.transp.readLp()) == "test 1"
asyncDiscard stream.transp.writeLp("test 2") asyncDiscard stream.transp.writeLp("test 2")
await sleepAsync(10.millis) await sleepAsync(10.millis)
check cast[string](await stream.transp.readLp()) == "test 3" check string.fromBytes(await stream.transp.readLp()) == "test 3"
asyncDiscard stream.transp.writeLp("test 4") asyncDiscard stream.transp.writeLp("test 4")
testFuture.complete() testFuture.complete()
@ -197,11 +197,11 @@ suite "Interop":
daemonPeer.addresses), daemonPeer.addresses),
protos[0]) protos[0])
await conn.writeLp("test 1") await conn.writeLp("test 1")
check "test 2" == cast[string]((await conn.readLp(1024))) check "test 2" == string.fromBytes((await conn.readLp(1024)))
await sleepAsync(10.millis) await sleepAsync(10.millis)
await conn.writeLp("test 3") await conn.writeLp("test 3")
check "test 4" == cast[string]((await conn.readLp(1024))) check "test 4" == string.fromBytes((await conn.readLp(1024)))
await wait(testFuture, 10.secs) await wait(testFuture, 10.secs)
await conn.close() await conn.close()
@ -265,7 +265,7 @@ suite "Interop":
var testFuture = newFuture[string]("test.future") var testFuture = newFuture[string]("test.future")
proc nativeHandler(conn: Connection, proto: string) {.async.} = proc nativeHandler(conn: Connection, proto: string) {.async.} =
var line = cast[string](await conn.readLp(1024)) var line = string.fromBytes(await conn.readLp(1024))
check line == test check line == test
testFuture.complete(line) testFuture.complete(line)
await conn.close() await conn.close()
@ -300,11 +300,11 @@ suite "Interop":
var testFuture = newFuture[void]("test.future") var testFuture = newFuture[void]("test.future")
proc nativeHandler(conn: Connection, proto: string) {.async.} = proc nativeHandler(conn: Connection, proto: string) {.async.} =
check "test 1" == cast[string](await conn.readLp(1024)) check "test 1" == string.fromBytes(await conn.readLp(1024))
await conn.writeLp(cast[seq[byte]]("test 2")) await conn.writeLp("test 2".toBytes())
check "test 3" == cast[string](await conn.readLp(1024)) check "test 3" == string.fromBytes(await conn.readLp(1024))
await conn.writeLp(cast[seq[byte]]("test 4")) await conn.writeLp("test 4".toBytes())
testFuture.complete() testFuture.complete()
await conn.close() await conn.close()
@ -325,10 +325,10 @@ suite "Interop":
var stream = await daemonNode.openStream(nativePeer.peerId, protos) var stream = await daemonNode.openStream(nativePeer.peerId, protos)
asyncDiscard stream.transp.writeLp("test 1") asyncDiscard stream.transp.writeLp("test 1")
check "test 2" == cast[string](await stream.transp.readLp()) check "test 2" == string.fromBytes(await stream.transp.readLp())
asyncDiscard stream.transp.writeLp("test 3") asyncDiscard stream.transp.writeLp("test 3")
check "test 4" == cast[string](await stream.transp.readLp()) check "test 4" == string.fromBytes(await stream.transp.readLp())
await wait(testFuture, 10.secs) await wait(testFuture, 10.secs)
@ -349,9 +349,9 @@ suite "Interop":
var testFuture = newFuture[int]("test.future") var testFuture = newFuture[int]("test.future")
proc nativeHandler(conn: Connection, proto: string) {.async.} = proc nativeHandler(conn: Connection, proto: string) {.async.} =
while count < 10: while count < 10:
var line = cast[string](await conn.readLp(1024)) var line = string.fromBytes(await conn.readLp(1024))
check line == test check line == test
await conn.writeLp(cast[seq[byte]](test)) await conn.writeLp(test.toBytes())
count.inc() count.inc()
testFuture.complete(count) testFuture.complete(count)
@ -376,7 +376,7 @@ suite "Interop":
while count2 < 10: while count2 < 10:
discard await stream.transp.writeLp(test) discard await stream.transp.writeLp(test)
let line = await stream.transp.readLp() let line = await stream.transp.readLp()
check test == cast[string](line) check test == string.fromBytes(line)
inc(count2) inc(count2)
result = 10 == (await wait(testFuture, 1.minutes)) result = 10 == (await wait(testFuture, 1.minutes))

View File

@ -192,7 +192,7 @@ suite "Multistream select":
let conn = newConnection(newTestLsStream(testLsHandler)) let conn = newConnection(newTestLsStream(testLsHandler))
let done = newFuture[void]() let done = newFuture[void]()
proc testLsHandler(proto: seq[byte]) {.async, gcsafe.} = proc testLsHandler(proto: seq[byte]) {.async, gcsafe.} =
var strProto: string = cast[string](proto) var strProto: string = string.fromBytes(proto)
check strProto == "\x26/test/proto1/1.0.0\n/test/proto2/1.0.0\n" check strProto == "\x26/test/proto1/1.0.0\n/test/proto2/1.0.0\n"
await conn.close() await conn.close()
done.complete() done.complete()
@ -270,7 +270,7 @@ suite "Multistream select":
check (await msDial.select(conn, "/test/proto/1.0.0")) == true check (await msDial.select(conn, "/test/proto/1.0.0")) == true
let hello = cast[string](await conn.readLp(1024)) let hello = string.fromBytes(await conn.readLp(1024))
result = hello == "Hello!" result = hello == "Hello!"
await conn.close() await conn.close()
@ -366,7 +366,7 @@ suite "Multistream select":
check (await msDial.select(conn, check (await msDial.select(conn,
@["/test/proto/1.0.0", "/test/no/proto/1.0.0"])) == "/test/proto/1.0.0" @["/test/proto/1.0.0", "/test/no/proto/1.0.0"])) == "/test/proto/1.0.0"
let hello = cast[string](await conn.readLp(1024)) let hello = string.fromBytes(await conn.readLp(1024))
result = hello == "Hello!" result = hello == "Hello!"
await conn.close() await conn.close()
@ -405,7 +405,7 @@ suite "Multistream select":
check (await msDial.select(conn, @["/test/proto2/1.0.0", "/test/proto1/1.0.0"])) == "/test/proto2/1.0.0" check (await msDial.select(conn, @["/test/proto2/1.0.0", "/test/proto1/1.0.0"])) == "/test/proto2/1.0.0"
result = cast[string](await conn.readLp(1024)) == "Hello from /test/proto2/1.0.0!" result = string.fromBytes(await conn.readLp(1024)) == "Hello from /test/proto2/1.0.0!"
await conn.close() await conn.close()
await transport2.close() await transport2.close()

View File

@ -10,7 +10,7 @@
{.used.} {.used.}
import unittest, tables import unittest, tables
import chronos import chronos, stew/byteutils
import chronicles import chronicles
import nimcrypto/sysrand import nimcrypto/sysrand
import ../libp2p/crypto/crypto import ../libp2p/crypto/crypto
@ -41,7 +41,7 @@ type
method init(p: TestProto) {.gcsafe.} = method init(p: TestProto) {.gcsafe.} =
proc handle(conn: Connection, proto: string) {.async, gcsafe.} = proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
let msg = cast[string](await conn.readLp(1024)) let msg = string.fromBytes(await conn.readLp(1024))
check "Hello!" == msg check "Hello!" == msg
await conn.writeLp("Hello!") await conn.writeLp("Hello!")
await conn.close() await conn.close()
@ -107,7 +107,7 @@ suite "Noise":
await transport1.close() await transport1.close()
await transport2.close() await transport2.close()
result = cast[string](msg) == "Hello!" result = string.fromBytes(msg) == "Hello!"
check: check:
waitFor(testListenerDialer()) == true waitFor(testListenerDialer()) == true
@ -127,7 +127,7 @@ suite "Noise":
await conn.close() await conn.close()
var msg = newSeq[byte](6) var msg = newSeq[byte](6)
await sconn.readExactly(addr msg[0], 6) await sconn.readExactly(addr msg[0], 6)
check cast[string](msg) == "Hello!" check string.fromBytes(msg) == "Hello!"
readTask.complete() readTask.complete()
let let
@ -218,7 +218,7 @@ suite "Noise":
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
let conn = await switch2.dial(switch1.peerInfo, TestCodec) let conn = await switch2.dial(switch1.peerInfo, TestCodec)
await conn.writeLp("Hello!") await conn.writeLp("Hello!")
let msg = cast[string](await conn.readLp(1024)) let msg = string.fromBytes(await conn.readLp(1024))
check "Hello!" == msg check "Hello!" == msg
await conn.close() await conn.close()

View File

@ -1,7 +1,7 @@
{.used.} {.used.}
import unittest import unittest
import chronos import chronos, stew/byteutils
import ../libp2p/[connection, import ../libp2p/[connection,
transports/transport, transports/transport,
transports/tcptransport, transports/tcptransport,
@ -35,7 +35,7 @@ suite "TCP transport":
await streamTransport.closeWait() await streamTransport.closeWait()
await transport.close() await transport.close()
result = cast[string](msg) == "Hello!" result = string.fromBytes(msg) == "Hello!"
check: check:
waitFor(testListener()) == true waitFor(testListener()) == true
@ -47,7 +47,7 @@ suite "TCP transport":
proc connHandler(conn: Connection) {.async, gcsafe.} = proc connHandler(conn: Connection) {.async, gcsafe.} =
var msg = newSeq[byte](6) var msg = newSeq[byte](6)
await conn.readExactly(addr msg[0], 6) await conn.readExactly(addr msg[0], 6)
check cast[string](msg) == "Hello!" check string.fromBytes(msg) == "Hello!"
await conn.close() await conn.close()
handlerWait.complete() handlerWait.complete()
@ -87,7 +87,7 @@ suite "TCP transport":
let conn = await transport.dial(ma) let conn = await transport.dial(ma)
var msg = newSeq[byte](6) var msg = newSeq[byte](6)
await conn.readExactly(addr msg[0], 6) await conn.readExactly(addr msg[0], 6)
result = cast[string](msg) == "Hello!" result = string.fromBytes(msg) == "Hello!"
await handlerWait.wait(5000.millis) # when no issues will not wait that long! await handlerWait.wait(5000.millis) # when no issues will not wait that long!
@ -108,7 +108,7 @@ suite "TCP transport":
transp: StreamTransport) {.async, gcsafe.} = transp: StreamTransport) {.async, gcsafe.} =
var rstream = newAsyncStreamReader(transp) var rstream = newAsyncStreamReader(transp)
let msg = await rstream.read(6) let msg = await rstream.read(6)
check cast[string](msg) == "Hello!" check string.fromBytes(msg) == "Hello!"
await rstream.closeWait() await rstream.closeWait()
await transp.closeWait() await transp.closeWait()
@ -159,7 +159,7 @@ suite "TCP transport":
await transport2.close() await transport2.close()
await transport1.close() await transport1.close()
result = cast[string](msg) == "Hello!" result = string.fromBytes(msg) == "Hello!"
check: check:
waitFor(testListenerDialer()) == true waitFor(testListenerDialer()) == true
@ -171,7 +171,7 @@ suite "TCP transport":
proc connHandler(conn: Connection) {.async, gcsafe.} = proc connHandler(conn: Connection) {.async, gcsafe.} =
var msg = newSeq[byte](6) var msg = newSeq[byte](6)
await conn.readExactly(addr msg[0], 6) await conn.readExactly(addr msg[0], 6)
check cast[string](msg) == "Hello!" check string.fromBytes(msg) == "Hello!"
await conn.close() await conn.close()
handlerWait.complete() handlerWait.complete()