Allow custom identify agent string (#451)

* allow custom agent version string

* rework tests and add test for custom agent version
This commit is contained in:
Dmitriy Ryajov 2020-11-25 07:42:02 -06:00 committed by GitHub
parent 164892776b
commit 7b1e652224
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 71 deletions

View File

@ -55,7 +55,10 @@ proc encodeMsg*(peerInfo: PeerInfo, observedAddr: Multiaddress): ProtoBuffer =
result.write(4, observedAddr.data.buffer) result.write(4, observedAddr.data.buffer)
let protoVersion = ProtoVersion let protoVersion = ProtoVersion
result.write(5, protoVersion) result.write(5, protoVersion)
let agentVersion = AgentVersion let agentVersion = if peerInfo.agentVersion.len <= 0:
AgentVersion
else:
peerInfo.agentVersion
result.write(6, agentVersion) result.write(6, agentVersion)
result.finish() result.finish()

View File

@ -49,21 +49,21 @@ template checkTrackers*() =
template asyncTeardown*(body: untyped): untyped = template asyncTeardown*(body: untyped): untyped =
teardown: teardown:
waitFor(( waitFor((
proc() {.async.} = proc() {.async, gcsafe.} =
body body
)()) )())
template asyncSetup*(body: untyped): untyped = template asyncSetup*(body: untyped): untyped =
setup: setup:
waitFor(( waitFor((
proc() {.async.} = proc() {.async, gcsafe.} =
body body
)()) )())
template asyncTest*(name: string, body: untyped): untyped = template asyncTest*(name: string, body: untyped): untyped =
test name: test name:
waitFor(( waitFor((
proc() {.async.} = proc() {.async, gcsafe.} =
body body
)()) )())

View File

@ -17,55 +17,87 @@ suite "Identify":
teardown: teardown:
checkTrackers() checkTrackers()
asyncTest "handle identify message": suite "handle identify message":
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet() var
let remoteSecKey = PrivateKey.random(ECDSA, rng[]).get() ma {.threadvar.}: MultiAddress
let remotePeerInfo = PeerInfo.init( remoteSecKey {.threadvar.}: PrivateKey
remotePeerInfo {.threadvar.}: PeerInfo
serverFut {.threadvar.}: Future[void]
acceptFut {.threadvar.}: Future[void]
identifyProto1 {.threadvar.}: Identify
identifyProto2 {.threadvar.}: Identify
transport1 {.threadvar.}: Transport
transport2 {.threadvar.}: Transport
msListen {.threadvar.}: MultistreamSelect
msDial {.threadvar.}: MultistreamSelect
conn {.threadvar.}: Connection
asyncSetup:
ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
remoteSecKey = PrivateKey.random(ECDSA, rng[]).get()
remotePeerInfo = PeerInfo.init(
remoteSecKey, [ma], ["/test/proto1/1.0.0", "/test/proto2/1.0.0"]) remoteSecKey, [ma], ["/test/proto1/1.0.0", "/test/proto2/1.0.0"])
var serverFut: Future[void]
let identifyProto1 = newIdentify(remotePeerInfo)
let msListen = newMultistream()
msListen.addHandler(IdentifyCodec, identifyProto1) transport1 = TcpTransport.init()
transport2 = TcpTransport.init()
var transport1 = TcpTransport.init() identifyProto1 = newIdentify(remotePeerInfo)
serverFut = transport1.start(ma) identifyProto2 = newIdentify(remotePeerInfo)
proc acceptHandler(): Future[void] {.async, gcsafe.} = msListen = newMultistream()
let conn = await transport1.accept() msDial = newMultistream()
await msListen.handle(conn)
let acceptFut = acceptHandler()
let msDial = newMultistream()
let transport2: TcpTransport = TcpTransport.init()
let conn = await transport2.dial(transport1.ma)
var peerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get(), [ma])
let identifyProto2 = newIdentify(peerInfo)
discard await msDial.select(conn, IdentifyCodec)
let id = await identifyProto2.identify(conn, remotePeerInfo)
check id.pubKey.get() == remoteSecKey.getKey().get()
check id.addrs[0] == ma
check id.protoVersion.get() == ProtoVersion
# check id.agentVersion.get() == AgentVersion
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
asyncTeardown:
await conn.close() await conn.close()
await acceptFut await acceptFut
await transport1.stop() await transport1.stop()
await serverFut await serverFut
await transport2.stop() await transport2.stop()
asyncTest "handle failed identify": asyncTest "default agent version":
let ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet() msListen.addHandler(IdentifyCodec, identifyProto1)
var remotePeerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get(), [ma]) serverFut = transport1.start(ma)
let identifyProto1 = newIdentify(remotePeerInfo) proc acceptHandler(): Future[void] {.async, gcsafe.} =
let msListen = newMultistream() let c = await transport1.accept()
await msListen.handle(c)
acceptFut = acceptHandler()
conn = await transport2.dial(transport1.ma)
discard await msDial.select(conn, IdentifyCodec)
let id = await identifyProto2.identify(conn, remotePeerInfo)
check id.pubKey.get() == remoteSecKey.getKey().get()
check id.addrs[0] == ma
check id.protoVersion.get() == ProtoVersion
check id.agentVersion.get() == AgentVersion
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
asyncTest "custom agent version":
const customAgentVersion = "MY CUSTOM AGENT STRING"
remotePeerInfo.agentVersion = customAgentVersion
msListen.addHandler(IdentifyCodec, identifyProto1) msListen.addHandler(IdentifyCodec, identifyProto1)
let transport1: TcpTransport = TcpTransport.init() serverFut = transport1.start(ma)
proc acceptHandler(): Future[void] {.async, gcsafe.} =
let c = await transport1.accept()
await msListen.handle(c)
acceptFut = acceptHandler()
conn = await transport2.dial(transport1.ma)
discard await msDial.select(conn, IdentifyCodec)
let id = await identifyProto2.identify(conn, remotePeerInfo)
check id.pubKey.get() == remoteSecKey.getKey().get()
check id.addrs[0] == ma
check id.protoVersion.get() == ProtoVersion
check id.agentVersion.get() == customAgentVersion
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
asyncTest "handle failed identify":
msListen.addHandler(IdentifyCodec, identifyProto1)
asyncCheck transport1.start(ma) asyncCheck transport1.start(ma)
proc acceptHandler() {.async.} = proc acceptHandler() {.async.} =
@ -78,19 +110,10 @@ suite "Identify":
finally: finally:
await conn.close() await conn.close()
let acceptFut = acceptHandler() acceptFut = acceptHandler()
let msDial = newMultistream() conn = await transport2.dial(transport1.ma)
let transport2: TcpTransport = TcpTransport.init()
let conn = await transport2.dial(transport1.ma)
var localPeerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get(), [ma])
let identifyProto2 = newIdentify(localPeerInfo)
expect IdentityNoMatchError: expect IdentityNoMatchError:
let pi2 = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get()) let pi2 = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get())
discard await msDial.select(conn, IdentifyCodec) discard await msDial.select(conn, IdentifyCodec)
discard await identifyProto2.identify(conn, pi2) discard await identifyProto2.identify(conn, pi2)
await conn.close()
await acceptFut.wait(5000.millis) # when no issues will not wait that long!
await transport2.stop()
await transport1.stop()