Observedaddr (#217)

* send correct observerAddr

* run tests with info log level

* set observedaddr for channels
This commit is contained in:
Dmitriy Ryajov 2020-06-12 19:56:17 -06:00 committed by GitHub
parent f97e2deec7
commit 85b56d0b3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 16 deletions

View File

@ -17,7 +17,7 @@ requires "nim >= 1.2.0",
"stew >= 0.1.0"
proc runTest(filename: string, verify: bool = true, sign: bool = true) =
var excstr: string = "nim c -r --opt:speed -d:debug --verbosity:0 --hints:off"
var excstr: string = "nim c -r --opt:speed -d:debug --verbosity:0 --hints:off -d:chronicles_log_level=info"
excstr.add(" ")
excstr.add("-d:libp2p_pubsub_sign=" & $sign)
excstr.add(" ")

View File

@ -29,7 +29,7 @@ type
Connection* = ref object of LPStream
peerInfo*: PeerInfo
stream*: LPStream
observedAddrs*: Multiaddress
observedAddr*: Multiaddress
ConnectionTracker* = ref object of TrackerBase
opened*: uint64
@ -117,8 +117,8 @@ method close*(s: Connection) {.async, gcsafe.} =
s.isClosed = true
trace "about to close connection", closed = s.closed,
conn = $s,
oid = s.oid
conn = $s,
oid = s.oid
if not isNil(s.stream) and not s.stream.closed:
@ -137,7 +137,3 @@ method close*(s: Connection) {.async, gcsafe.} =
libp2p_open_connection.dec()
except CatchableError as exc:
trace "exception closing connections", exc = exc.msg
method getObservedAddrs*(c: Connection): Future[MultiAddress] {.base, async, gcsafe.} =
## get resolved multiaddresses for the connection
result = c.observedAddrs

View File

@ -100,6 +100,7 @@ method handle*(m: Mplex) {.async, gcsafe.} =
let stream = newConnection(channel)
m.conns.add(stream)
stream.peerInfo = m.connection.peerInfo
stream.observedAddr = m.connection.observedAddr
var fut = newFuture[void]()
proc handler() {.async.} =
@ -191,6 +192,7 @@ method newStream*(m: Mplex,
await channel.open()
result = newConnection(channel)
result.peerInfo = m.connection.peerInfo
result.observedAddr = m.connection.observedAddr
asyncCheck m.cleanupChann(channel)

View File

@ -44,7 +44,7 @@ type
Identify* = ref object of LPProtocol
peerInfo*: PeerInfo
proc encodeMsg*(peerInfo: PeerInfo, observedAddrs: Multiaddress): ProtoBuffer =
proc encodeMsg*(peerInfo: PeerInfo, observedAddr: Multiaddress): ProtoBuffer =
result = initProtoBuffer()
result.write(initProtoField(1, peerInfo.publicKey.get().getBytes().tryGet()))
@ -55,7 +55,7 @@ proc encodeMsg*(peerInfo: PeerInfo, observedAddrs: Multiaddress): ProtoBuffer =
for proto in peerInfo.protocols:
result.write(initProtoField(3, proto))
result.write(initProtoField(4, observedAddrs.data.buffer))
result.write(initProtoField(4, observedAddr.data.buffer))
let protoVersion = ProtoVersion
result.write(initProtoField(5, protoVersion))
@ -114,10 +114,11 @@ method init*(p: Identify) =
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
try:
try:
trace "handling identify request"
var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs())
trace "handling identify request", oid = conn.oid
var pb = encodeMsg(p.peerInfo, conn.observedAddr)
await conn.writeLp(pb.buffer)
finally:
trace "exiting identify handler", oid = conn.oid
await conn.close()
except CatchableError as exc:
trace "exception in identify handler", exc = exc.msg
@ -148,10 +149,9 @@ proc identify*(p: Identify,
remote = peer.pretty(),
local = remotePeerInfo.id
raise newException(IdentityNoMatchError,
"Peer ids don't match")
raise newException(IdentityNoMatchError, "Peer ids don't match")
proc push*(p: Identify, conn: Connection) {.async.} =
await conn.write(IdentifyPushCodec)
var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs())
var pb = encodeMsg(p.peerInfo, conn.observedAddr)
await conn.writeLp(pb.buffer)

View File

@ -32,6 +32,7 @@ proc handleConn*(s: Secure, conn: Connection, initiator: bool): Future[Connectio
var sconn = await s.handshake(conn, initiator)
result = sconn
result.observedAddr = conn.observedAddr
if not isNil(sconn.peerInfo) and sconn.peerInfo.publicKey.isSome:
result.peerInfo = PeerInfo.init(sconn.peerInfo.publicKey.get())

View File

@ -64,7 +64,7 @@ proc connHandler*(t: TcpTransport,
initiator: bool): Connection =
trace "handling connection", address = $client.remoteAddress
let conn: Connection = newConnection(newChronosStream(client))
conn.observedAddrs = MultiAddress.init(client.remoteAddress).tryGet()
conn.observedAddr = MultiAddress.init(client.remoteAddress).tryGet()
if not initiator:
if not isNil(t.handler):
t.handlers &= t.handler(conn)