diff --git a/libp2p/multistream.nim b/libp2p/multistream.nim index f49cda49b..fbf19813b 100644 --- a/libp2p/multistream.nim +++ b/libp2p/multistream.nim @@ -58,40 +58,45 @@ proc select*(m: MultistreamSelect, trace "selecting proto", proto = proto[0] await conn.writeLp((proto[0] & "\n")) # select proto - result = string.fromBytes((await conn.readLp(1024))) # read ms header - result.removeSuffix("\n") - if result != Codec: - notice "handshake failed", codec = result.toHex() + var s = string.fromBytes((await conn.readLp(1024))) # read ms header + s.removeSuffix("\n") + if s != Codec: + notice "handshake failed", codec = s.toHex() raise newMultistreamHandshakeException() if proto.len() == 0: # no protocols, must be a handshake call - return - - result = string.fromBytes(await conn.readLp(1024)) # read the first proto - trace "reading first requested proto" - result.removeSuffix("\n") - if result == proto[0]: - trace "successfully selected ", proto = proto[0] - return - - let protos = proto[1.. 1: + # Try to negotiate alternatives + let protos = proto[1.. 0: - result = (await m.select(conn, @[proto])) == proto + return (await m.select(conn, @[proto])) == proto else: - result = (await m.select(conn, @[])) == Codec + return (await m.select(conn, @[])) == Codec proc select*(m: MultistreamSelect, conn: Connection): Future[bool] = m.select(conn, "") diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 56045c03e..909a766e0 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -63,7 +63,7 @@ proc secure(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} = raise newException(CatchableError, "No secure managers registered!") let manager = await s.ms.select(conn, s.secureManagers.mapIt(it.codec)) - if manager.len == 0 or manager == "na": + if manager.len == 0: raise newException(CatchableError, "Unable to negotiate a secure channel!") trace "securing connection", codec=manager diff --git a/tests/testmultistream.nim b/tests/testmultistream.nim index 390419132..4cd693d1b 100644 --- a/tests/testmultistream.nim +++ b/tests/testmultistream.nim @@ -57,7 +57,7 @@ proc newTestSelectStream(): TestSelectStream = type LsHandler = proc(procs: seq[byte]): Future[void] {.gcsafe.} - TestLsStream = ref object of LPStream + TestLsStream = ref object of Connection step*: int ls*: LsHandler @@ -103,7 +103,7 @@ proc newTestLsStream(ls: LsHandler): TestLsStream {.gcsafe.} = type NaHandler = proc(procs: string): Future[void] {.gcsafe.} - TestNaStream = ref object of LPStream + TestNaStream = ref object of Connection step*: int na*: NaHandler diff --git a/tests/testnative.nim b/tests/testnative.nim index fbba97f2d..53e4a244e 100644 --- a/tests/testnative.nim +++ b/tests/testnative.nim @@ -14,7 +14,7 @@ import testmultibase, testpeer import testtransport, - # testmultistream, + testmultistream, testbufferstream, testidentify, testswitch,