diff --git a/libp2p/multistream.nim b/libp2p/multistream.nim index f9b0e0c0c..1221c2ec1 100644 --- a/libp2p/multistream.nim +++ b/libp2p/multistream.nim @@ -7,8 +7,8 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import strutils, stew/byteutils -import chronos, chronicles +import strutils +import chronos, chronicles, stew/byteutils import connection, vbuffer, errors, @@ -58,7 +58,7 @@ proc select*(m: MultistreamSelect, trace "selecting proto", proto = 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") if result != Codec: notice "handshake failed", codec = result.toHex() diff --git a/libp2p/muxers/mplex/coder.nim b/libp2p/muxers/mplex/coder.nim index 9df47e954..850b177bb 100644 --- a/libp2p/muxers/mplex/coder.nim +++ b/libp2p/muxers/mplex/coder.nim @@ -8,7 +8,7 @@ ## those terms. import chronos -import nimcrypto/utils, chronicles +import nimcrypto/utils, chronicles, stew/byteutils import types, ../../connection, ../../utility, @@ -74,7 +74,7 @@ proc writeMsg*(conn: Connection, msgType: MessageType, data: string) {.async, gcsafe.} = # 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. # DON'T DO IT! - result = conn.writeMsg(id, msgType, cast[seq[byte]](data)) + result = conn.writeMsg(id, msgType, data.toBytes()) diff --git a/libp2p/muxers/mplex/mplex.nim b/libp2p/muxers/mplex/mplex.nim index 6dd53b5b7..d4b21120b 100644 --- a/libp2p/muxers/mplex/mplex.nim +++ b/libp2p/muxers/mplex/mplex.nim @@ -8,7 +8,7 @@ ## those terms. import tables, sequtils, oids -import chronos, chronicles +import chronos, chronicles, stew/byteutils import ../muxer, ../../connection, ../../stream/lpstream, @@ -89,7 +89,7 @@ method handle*(m: Mplex) {.async, gcsafe.} = case msgType: of MessageType.New: - let name = cast[string](data) + let name = string.fromBytes(data) channel = await m.newStreamInternal(false, id, name) trace "created channel", id = id, name = name, diff --git a/libp2p/protocols/pubsub/rpc/message.nim b/libp2p/protocols/pubsub/rpc/message.nim index cc5103b24..ba16dc275 100644 --- a/libp2p/protocols/pubsub/rpc/message.nim +++ b/libp2p/protocols/pubsub/rpc/message.nim @@ -8,7 +8,7 @@ ## those terms. import options -import chronicles +import chronicles, stew/byteutils import nimcrypto/sysrand import messages, protobuf, ../../../peer, @@ -23,7 +23,7 @@ const PubSubPrefix = "libp2p-pubsub:" proc msgIdProvider(m: Message): string = ## 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 = ## calls the ``msgIdProvider`` from @@ -41,7 +41,7 @@ proc sign*(msg: Message, p: PeerInfo): Message {.gcsafe.} = if buff.buffer.len > 0: result = msg result.signature = p.privateKey. - sign(cast[seq[byte]](PubSubPrefix) & buff.buffer).tryGet(). + sign(PubSubPrefix.toBytes() & buff.buffer).tryGet(). getBytes() proc verify*(m: Message, p: PeerInfo): bool = @@ -57,7 +57,7 @@ proc verify*(m: Message, p: PeerInfo): bool = var key: PublicKey if remote.init(m.signature) and key.init(m.key): 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, data: seq[byte], diff --git a/tests/pubsub/testfloodsub.nim b/tests/pubsub/testfloodsub.nim index 169917a6d..583787333 100644 --- a/tests/pubsub/testfloodsub.nim +++ b/tests/pubsub/testfloodsub.nim @@ -10,7 +10,7 @@ {.used.} import unittest, sequtils, options, tables, sets -import chronos +import chronos, stew/byteutils import utils, ../../libp2p/[errors, switch, @@ -60,7 +60,7 @@ suite "FloodSub": await nodes[1].subscribe("foobar", handler) 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) @@ -91,7 +91,7 @@ suite "FloodSub": await nodes[0].subscribe("foobar", handler) 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) @@ -126,7 +126,7 @@ suite "FloodSub": 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 await allFuturesThrowing( @@ -160,7 +160,7 @@ suite "FloodSub": nodes[1].addValidator("foobar", validator) - await nodes[0].publish("foobar", cast[seq[byte]]("Hello!")) + await nodes[0].publish("foobar", "Hello!".toBytes()) await allFuturesThrowing( nodes[0].stop(), @@ -198,8 +198,8 @@ suite "FloodSub": nodes[1].addValidator("foo", "bar", validator) - await nodes[0].publish("foo", cast[seq[byte]]("Hello!")) - await nodes[0].publish("bar", cast[seq[byte]]("Hello!")) + await nodes[0].publish("foo", "Hello!".toBytes()) + await nodes[0].publish("bar", "Hello!".toBytes()) await allFuturesThrowing( nodes[0].stop(), @@ -252,7 +252,7 @@ suite "FloodSub": var pubs: seq[Future[void]] for i in 0.. 0: - await buf1.write(cast[seq[byte]]("Hello2!")) + await buf1.write("Hello2!".toBytes()) count.dec var writerFut = writer() diff --git a/tests/testinterop.nim b/tests/testinterop.nim index c33d26826..8ce56e06d 100644 --- a/tests/testinterop.nim +++ b/tests/testinterop.nim @@ -65,7 +65,7 @@ proc testPubSubDaemonPublish(gossip: bool = false, count: int = 1): Future[bool] {.async.} = var pubsubData = "TEST MESSAGE" var testTopic = "test-topic" - var msgData = cast[seq[byte]](pubsubData) + var msgData = pubsubData.toBytes() var flags = {PSFloodSub} if gossip: @@ -80,7 +80,7 @@ proc testPubSubDaemonPublish(gossip: bool = false, var finished = false var times = 0 proc nativeHandler(topic: string, data: seq[byte]) {.async.} = - let smsg = cast[string](data) + let smsg = string.fromBytes(data) check smsg == pubsubData times.inc() if times >= count and not finished: @@ -116,7 +116,7 @@ proc testPubSubNodePublish(gossip: bool = false, count: int = 1): Future[bool] {.async.} = var pubsubData = "TEST MESSAGE" var testTopic = "test-topic" - var msgData = cast[seq[byte]](pubsubData) + var msgData = pubsubData.toBytes() var flags = {PSFloodSub} if gossip: @@ -139,7 +139,7 @@ proc testPubSubNodePublish(gossip: bool = false, proc pubsubHandler(api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage): Future[bool] {.async.} = - let smsg = cast[string](message.data) + let smsg = string.fromBytes(message.data) check smsg == pubsubData times.inc() if times >= count and not finished: @@ -184,11 +184,11 @@ suite "Interop": var testFuture = newFuture[void]("test.future") 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") 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") testFuture.complete() @@ -197,11 +197,11 @@ suite "Interop": daemonPeer.addresses), protos[0]) 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 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 conn.close() @@ -265,7 +265,7 @@ suite "Interop": var testFuture = newFuture[string]("test.future") 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 testFuture.complete(line) await conn.close() @@ -300,11 +300,11 @@ suite "Interop": var testFuture = newFuture[void]("test.future") proc nativeHandler(conn: Connection, proto: string) {.async.} = - check "test 1" == cast[string](await conn.readLp(1024)) - await conn.writeLp(cast[seq[byte]]("test 2")) + check "test 1" == string.fromBytes(await conn.readLp(1024)) + await conn.writeLp("test 2".toBytes()) - check "test 3" == cast[string](await conn.readLp(1024)) - await conn.writeLp(cast[seq[byte]]("test 4")) + check "test 3" == string.fromBytes(await conn.readLp(1024)) + await conn.writeLp("test 4".toBytes()) testFuture.complete() await conn.close() @@ -325,10 +325,10 @@ suite "Interop": var stream = await daemonNode.openStream(nativePeer.peerId, protos) 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") - check "test 4" == cast[string](await stream.transp.readLp()) + check "test 4" == string.fromBytes(await stream.transp.readLp()) await wait(testFuture, 10.secs) @@ -349,9 +349,9 @@ suite "Interop": var testFuture = newFuture[int]("test.future") proc nativeHandler(conn: Connection, proto: string) {.async.} = while count < 10: - var line = cast[string](await conn.readLp(1024)) + var line = string.fromBytes(await conn.readLp(1024)) check line == test - await conn.writeLp(cast[seq[byte]](test)) + await conn.writeLp(test.toBytes()) count.inc() testFuture.complete(count) @@ -376,7 +376,7 @@ suite "Interop": while count2 < 10: discard await stream.transp.writeLp(test) let line = await stream.transp.readLp() - check test == cast[string](line) + check test == string.fromBytes(line) inc(count2) result = 10 == (await wait(testFuture, 1.minutes)) diff --git a/tests/testmultistream.nim b/tests/testmultistream.nim index ecb64f58c..217225877 100644 --- a/tests/testmultistream.nim +++ b/tests/testmultistream.nim @@ -192,7 +192,7 @@ suite "Multistream select": let conn = newConnection(newTestLsStream(testLsHandler)) let done = newFuture[void]() 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" await conn.close() done.complete() @@ -270,7 +270,7 @@ suite "Multistream select": 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!" await conn.close() @@ -366,7 +366,7 @@ suite "Multistream select": check (await msDial.select(conn, @["/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!" 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" - 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 transport2.close() diff --git a/tests/testnoise.nim b/tests/testnoise.nim index 778328564..c77ac86cf 100644 --- a/tests/testnoise.nim +++ b/tests/testnoise.nim @@ -10,7 +10,7 @@ {.used.} import unittest, tables -import chronos +import chronos, stew/byteutils import chronicles import nimcrypto/sysrand import ../libp2p/crypto/crypto @@ -41,7 +41,7 @@ type method init(p: TestProto) {.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 await conn.writeLp("Hello!") await conn.close() @@ -107,7 +107,7 @@ suite "Noise": await transport1.close() await transport2.close() - result = cast[string](msg) == "Hello!" + result = string.fromBytes(msg) == "Hello!" check: waitFor(testListenerDialer()) == true @@ -127,7 +127,7 @@ suite "Noise": await conn.close() var msg = newSeq[byte](6) await sconn.readExactly(addr msg[0], 6) - check cast[string](msg) == "Hello!" + check string.fromBytes(msg) == "Hello!" readTask.complete() let @@ -218,7 +218,7 @@ suite "Noise": awaiters.add(await switch2.start()) let conn = await switch2.dial(switch1.peerInfo, TestCodec) await conn.writeLp("Hello!") - let msg = cast[string](await conn.readLp(1024)) + let msg = string.fromBytes(await conn.readLp(1024)) check "Hello!" == msg await conn.close() diff --git a/tests/testtransport.nim b/tests/testtransport.nim index d7c3fc74b..d943c10ac 100644 --- a/tests/testtransport.nim +++ b/tests/testtransport.nim @@ -1,7 +1,7 @@ {.used.} import unittest -import chronos +import chronos, stew/byteutils import ../libp2p/[connection, transports/transport, transports/tcptransport, @@ -35,7 +35,7 @@ suite "TCP transport": await streamTransport.closeWait() await transport.close() - result = cast[string](msg) == "Hello!" + result = string.fromBytes(msg) == "Hello!" check: waitFor(testListener()) == true @@ -47,7 +47,7 @@ suite "TCP transport": proc connHandler(conn: Connection) {.async, gcsafe.} = var msg = newSeq[byte](6) await conn.readExactly(addr msg[0], 6) - check cast[string](msg) == "Hello!" + check string.fromBytes(msg) == "Hello!" await conn.close() handlerWait.complete() @@ -87,7 +87,7 @@ suite "TCP transport": let conn = await transport.dial(ma) var msg = newSeq[byte](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! @@ -108,7 +108,7 @@ suite "TCP transport": transp: StreamTransport) {.async, gcsafe.} = var rstream = newAsyncStreamReader(transp) let msg = await rstream.read(6) - check cast[string](msg) == "Hello!" + check string.fromBytes(msg) == "Hello!" await rstream.closeWait() await transp.closeWait() @@ -159,7 +159,7 @@ suite "TCP transport": await transport2.close() await transport1.close() - result = cast[string](msg) == "Hello!" + result = string.fromBytes(msg) == "Hello!" check: waitFor(testListenerDialer()) == true @@ -171,7 +171,7 @@ suite "TCP transport": proc connHandler(conn: Connection) {.async, gcsafe.} = var msg = newSeq[byte](6) await conn.readExactly(addr msg[0], 6) - check cast[string](msg) == "Hello!" + check string.fromBytes(msg) == "Hello!" await conn.close() handlerWait.complete()