diff --git a/libp2p/connection.nim b/libp2p/connection.nim index 5f822328a..e18f87689 100644 --- a/libp2p/connection.nim +++ b/libp2p/connection.nim @@ -63,7 +63,7 @@ declareGauge libp2p_open_connection, "open Connection instances" proc `$`*(conn: Connection): string = if not isNil(conn.peerInfo): - result = $(conn.peerInfo) + result = conn.peerInfo.id proc init[T: Connection](self: var T, stream: LPStream): T = ## create a new Connection for the specified async reader/writer diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 60a6d35b3..0d642e1fa 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -649,9 +649,11 @@ proc toString*(value: MultiAddress): MaResult[string] = res = "/" & parts.join("/") ok(res) -proc `$`*(value: MultiAddress): string {.raises: [Defect, ResultError[string]].} = +proc `$`*(value: MultiAddress): string {.raises: [Defect].} = ## Return string representation of MultiAddress ``value``. - value.toString().tryGet() + let s = value.toString() + if s.isErr: s.error + else: s[] proc protocols*(value: MultiAddress): MaResult[seq[MultiCodec]] = ## Returns list of protocol codecs inside of MultiAddress ``value``. diff --git a/libp2p/peerinfo.nim b/libp2p/peerinfo.nim index 2868ed0d5..e170a3491 100644 --- a/libp2p/peerinfo.nim +++ b/libp2p/peerinfo.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import options +import options, sequtils import chronos, chronicles import peer, multiaddress, crypto/crypto @@ -29,6 +29,8 @@ type addrs*: seq[MultiAddress] protocols*: seq[string] lifefut: Future[void] + protoVersion*: string + agentVersion*: string case keyType*: KeyType: of HasPrivate: privateKey*: PrivateKey @@ -38,14 +40,14 @@ type proc id*(p: PeerInfo): string = p.peerId.pretty() -proc `$`*(p: PeerInfo): string = - result.add(p.id) - - result.add(" ") - result.add($p.addrs) - - result.add(" ") - result.add($p.protocols) +proc shortLog*(p: PeerInfo): auto = + ( + id: p.id(), + addrs: mapIt(p.addrs, $it), + protocols: mapIt(p.protocols, $it), + protoVersion: p.protoVersion, + agentVersion: p.agentVersion, + ) template postInit(peerinfo: PeerInfo, addrs: openarray[MultiAddress], diff --git a/libp2p/protocols/identify.nim b/libp2p/protocols/identify.nim index 4d7b2985c..e5267863f 100644 --- a/libp2p/protocols/identify.nim +++ b/libp2p/protocols/identify.nim @@ -103,7 +103,7 @@ proc decodeMsg*(buf: seq[byte]): IdentifyInfo = var agentVersion = "" if pb.getString(6, agentVersion) > 0: trace "read agentVersion from message", agentVersion = agentVersion - result.agentVersion = some(protoVersion) + result.agentVersion = some(agentVersion) proc newIdentify*(peerInfo: PeerInfo): Identify = new result @@ -151,8 +151,6 @@ proc identify*(p: Identify, raise newException(IdentityNoMatchError, "Peer ids don't match") - trace "Identify for remote peer succeded" - proc push*(p: Identify, conn: Connection) {.async.} = await conn.write(IdentifyPushCodec) var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs()) diff --git a/libp2p/protocols/secure/noise.nim b/libp2p/protocols/secure/noise.nim index 0a13abd14..362f9da71 100644 --- a/libp2p/protocols/secure/noise.nim +++ b/libp2p/protocols/secure/noise.nim @@ -22,7 +22,7 @@ import secure, ../../stream/bufferstream logScope: - topic = "Noise" + topic = "noise" const # https://godoc.org/github.com/libp2p/go-libp2p-noise#pkg-constants @@ -422,7 +422,7 @@ method readMessage*(sconn: NoiseConnection): Future[seq[byte]] {.async.} = var besize: array[2, byte] await sconn.stream.readExactly(addr besize[0], besize.len) let size = uint16.fromBytesBE(besize).int # Cannot overflow - trace "receiveEncryptedMessage", size, peer = $sconn.peerInfo + trace "receiveEncryptedMessage", size, peer = $sconn if size > 0: var buffer = newSeq[byte](size) await sconn.stream.readExactly(addr buffer[0], buffer.len) @@ -450,7 +450,7 @@ method write*(sconn: NoiseConnection, message: seq[byte]): Future[void] {.async. lesize = cipher.len.uint16 besize = lesize.toBytesBE outbuf = newSeqOfCap[byte](cipher.len + 2) - trace "sendEncryptedMessage", size = lesize, peer = $sconn.peerInfo, left, offset + trace "sendEncryptedMessage", size = lesize, peer = $sconn, left, offset outbuf &= besize outbuf &= cipher await sconn.stream.write(outbuf) diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 7cb5e2787..9b43664e7 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -82,9 +82,15 @@ proc identify(s: Switch, conn: Connection): Future[PeerInfo] {.async, gcsafe.} = if info.addrs.len > 0: result.addrs = info.addrs + if info.agentVersion.isSome: + result.agentVersion = info.agentVersion.get() + + if info.protoVersion.isSome: + result.protoVersion = info.protoVersion.get() + if info.protos.len > 0: result.protocols = info.protos - + debug "identify", info = shortLog(result) except IdentityInvalidMsgError as exc: error "identify: invalid message", msg = exc.msg except IdentityNoMatchError as exc: @@ -126,8 +132,6 @@ proc mux(s: Switch, conn: Connection): Future[void] {.async, gcsafe.} = finally: await stream.close() # close identify stream - trace "connection's peerInfo", peerInfo = $conn.peerInfo - # store it in muxed connections if we have a peer for it if not isNil(conn.peerInfo): trace "adding muxer for peer", peer = conn.peerInfo.id @@ -299,7 +303,7 @@ proc mount*[T: LPProtocol](s: Switch, proto: T) {.gcsafe.} = s.ms.addHandler(proto.codec, proto) proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} = - trace "starting switch for peer", peerInfo = $s.peerInfo + trace "starting switch for peer", peerInfo = shortLog(s.peerInfo) proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} = try: @@ -432,7 +436,7 @@ proc newSwitch*(peerInfo: PeerInfo, let s = result # can't capture result result.streamHandler = proc(stream: Connection) {.async, gcsafe.} = try: - trace "handling connection for", peerInfo = $stream.peerInfo + trace "handling connection for", peerInfo = $stream try: await s.ms.handle(stream) # handle incoming connection finally: