grab agentversion/protoversion (#211)

This commit is contained in:
Jacek Sieka 2020-06-09 20:42:52 +02:00 committed by GitHub
parent 35ff99829e
commit 8d9e231a74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 23 deletions

View File

@ -63,7 +63,7 @@ declareGauge libp2p_open_connection, "open Connection instances"
proc `$`*(conn: Connection): string = proc `$`*(conn: Connection): string =
if not isNil(conn.peerInfo): if not isNil(conn.peerInfo):
result = $(conn.peerInfo) result = conn.peerInfo.id
proc init[T: Connection](self: var T, stream: LPStream): T = proc init[T: Connection](self: var T, stream: LPStream): T =
## create a new Connection for the specified async reader/writer ## create a new Connection for the specified async reader/writer

View File

@ -649,9 +649,11 @@ proc toString*(value: MultiAddress): MaResult[string] =
res = "/" & parts.join("/") res = "/" & parts.join("/")
ok(res) ok(res)
proc `$`*(value: MultiAddress): string {.raises: [Defect, ResultError[string]].} = proc `$`*(value: MultiAddress): string {.raises: [Defect].} =
## Return string representation of MultiAddress ``value``. ## 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]] = proc protocols*(value: MultiAddress): MaResult[seq[MultiCodec]] =
## Returns list of protocol codecs inside of MultiAddress ``value``. ## Returns list of protocol codecs inside of MultiAddress ``value``.

View File

@ -7,7 +7,7 @@
## 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 options import options, sequtils
import chronos, chronicles import chronos, chronicles
import peer, multiaddress, crypto/crypto import peer, multiaddress, crypto/crypto
@ -29,6 +29,8 @@ type
addrs*: seq[MultiAddress] addrs*: seq[MultiAddress]
protocols*: seq[string] protocols*: seq[string]
lifefut: Future[void] lifefut: Future[void]
protoVersion*: string
agentVersion*: string
case keyType*: KeyType: case keyType*: KeyType:
of HasPrivate: of HasPrivate:
privateKey*: PrivateKey privateKey*: PrivateKey
@ -38,14 +40,14 @@ type
proc id*(p: PeerInfo): string = proc id*(p: PeerInfo): string =
p.peerId.pretty() p.peerId.pretty()
proc `$`*(p: PeerInfo): string = proc shortLog*(p: PeerInfo): auto =
result.add(p.id) (
id: p.id(),
result.add(" ") addrs: mapIt(p.addrs, $it),
result.add($p.addrs) protocols: mapIt(p.protocols, $it),
protoVersion: p.protoVersion,
result.add(" ") agentVersion: p.agentVersion,
result.add($p.protocols) )
template postInit(peerinfo: PeerInfo, template postInit(peerinfo: PeerInfo,
addrs: openarray[MultiAddress], addrs: openarray[MultiAddress],

View File

@ -103,7 +103,7 @@ proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
var agentVersion = "" var agentVersion = ""
if pb.getString(6, agentVersion) > 0: if pb.getString(6, agentVersion) > 0:
trace "read agentVersion from message", agentVersion = agentVersion trace "read agentVersion from message", agentVersion = agentVersion
result.agentVersion = some(protoVersion) result.agentVersion = some(agentVersion)
proc newIdentify*(peerInfo: PeerInfo): Identify = proc newIdentify*(peerInfo: PeerInfo): Identify =
new result new result
@ -151,8 +151,6 @@ proc identify*(p: Identify,
raise newException(IdentityNoMatchError, raise newException(IdentityNoMatchError,
"Peer ids don't match") "Peer ids don't match")
trace "Identify for remote peer succeded"
proc push*(p: Identify, conn: Connection) {.async.} = proc push*(p: Identify, conn: Connection) {.async.} =
await conn.write(IdentifyPushCodec) await conn.write(IdentifyPushCodec)
var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs()) var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs())

View File

@ -22,7 +22,7 @@ import secure,
../../stream/bufferstream ../../stream/bufferstream
logScope: logScope:
topic = "Noise" topic = "noise"
const const
# https://godoc.org/github.com/libp2p/go-libp2p-noise#pkg-constants # 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] var besize: array[2, byte]
await sconn.stream.readExactly(addr besize[0], besize.len) await sconn.stream.readExactly(addr besize[0], besize.len)
let size = uint16.fromBytesBE(besize).int # Cannot overflow let size = uint16.fromBytesBE(besize).int # Cannot overflow
trace "receiveEncryptedMessage", size, peer = $sconn.peerInfo trace "receiveEncryptedMessage", size, peer = $sconn
if size > 0: if size > 0:
var buffer = newSeq[byte](size) var buffer = newSeq[byte](size)
await sconn.stream.readExactly(addr buffer[0], buffer.len) 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 lesize = cipher.len.uint16
besize = lesize.toBytesBE besize = lesize.toBytesBE
outbuf = newSeqOfCap[byte](cipher.len + 2) 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 &= besize
outbuf &= cipher outbuf &= cipher
await sconn.stream.write(outbuf) await sconn.stream.write(outbuf)

View File

@ -82,9 +82,15 @@ proc identify(s: Switch, conn: Connection): Future[PeerInfo] {.async, gcsafe.} =
if info.addrs.len > 0: if info.addrs.len > 0:
result.addrs = info.addrs 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: if info.protos.len > 0:
result.protocols = info.protos result.protocols = info.protos
debug "identify", info = shortLog(result)
except IdentityInvalidMsgError as exc: except IdentityInvalidMsgError as exc:
error "identify: invalid message", msg = exc.msg error "identify: invalid message", msg = exc.msg
except IdentityNoMatchError as exc: except IdentityNoMatchError as exc:
@ -126,8 +132,6 @@ proc mux(s: Switch, conn: Connection): Future[void] {.async, gcsafe.} =
finally: finally:
await stream.close() # close identify stream 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 # store it in muxed connections if we have a peer for it
if not isNil(conn.peerInfo): if not isNil(conn.peerInfo):
trace "adding muxer for peer", peer = conn.peerInfo.id 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) s.ms.addHandler(proto.codec, proto)
proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} = 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.} = proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} =
try: try:
@ -432,7 +436,7 @@ proc newSwitch*(peerInfo: PeerInfo,
let s = result # can't capture result let s = result # can't capture result
result.streamHandler = proc(stream: Connection) {.async, gcsafe.} = result.streamHandler = proc(stream: Connection) {.async, gcsafe.} =
try: try:
trace "handling connection for", peerInfo = $stream.peerInfo trace "handling connection for", peerInfo = $stream
try: try:
await s.ms.handle(stream) # handle incoming connection await s.ms.handle(stream) # handle incoming connection
finally: finally: