Fix secure/noise securing explicitly, added noise to pubsub tests

This commit is contained in:
Giovanni Petrantoni 2020-04-23 10:27:29 +09:00 committed by Dmitriy Ryajov
parent 917b5f5c84
commit 8a22c073c7
5 changed files with 32 additions and 16 deletions

View File

@ -16,7 +16,7 @@ requires "nim >= 1.2.0",
"secp256k1",
"stew"
proc runTest(filename: string) =
proc runTest(filename: string, secure: string = "secio") =
exec "nim c -r --opt:speed -d:debug --verbosity:0 --hints:off tests/" & filename
rmFile "tests/" & filename.toExe
@ -25,9 +25,10 @@ proc buildSample(filename: string) =
rmFile "examples" & filename.toExe
task test, "Runs the test suite":
runTest "testnative"
runTest "testdaemon"
runTest "testinterop"
runTest("testnative")
runTest("testnative", "noise")
runTest("testdaemon")
runTest("testinterop")
task examples_build, "Build the samples":
buildSample "directchat"
buildSample("directchat")

View File

@ -521,6 +521,7 @@ method init*(p: Noise) {.gcsafe.} =
p.codec = NoiseCodec
method secure*(p: Noise, conn: Connection): Future[Connection] {.async, gcsafe.} =
trace "Noise.secure called", initiator=p.outgoing
try:
result = await p.handleConn(conn, p.outgoing)
except CatchableError as exc:

View File

@ -29,7 +29,7 @@ method writeMessage*(c: SecureConn, data: seq[byte]) {.async, base.} =
method handshake(s: Secure,
conn: Connection,
initiator: bool = false): Future[SecureConn] {.async, base.} =
initiator: bool): Future[SecureConn] {.async, base.} =
doAssert(false, "Not implemented!")
proc readLoop(sconn: SecureConn, conn: Connection) {.async.} =
@ -54,7 +54,7 @@ proc readLoop(sconn: SecureConn, conn: Connection) {.async.} =
await sconn.close()
trace "ending Secure readLoop"
proc handleConn*(s: Secure, conn: Connection, initiator: bool = false): Future[Connection] {.async, gcsafe.} =
proc handleConn*(s: Secure, conn: Connection, initiator: bool): Future[Connection] {.async, gcsafe.} =
var sconn = await s.handshake(conn, initiator)
proc writeHandler(data: seq[byte]) {.async, gcsafe.} =
trace "sending encrypted bytes", bytes = data.shortLog
@ -68,7 +68,7 @@ proc handleConn*(s: Secure, conn: Connection, initiator: bool = false): Future[C
method init*(s: Secure) {.gcsafe.} =
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
trace "handling connection"
trace "handling connection upgrade", proto
try:
# We don't need the result but we definitely need to await the handshake
discard await s.handleConn(conn, false)
@ -80,9 +80,9 @@ method init*(s: Secure) {.gcsafe.} =
s.handler = handle
method secure*(s: Secure, conn: Connection): Future[Connection] {.async, base, gcsafe.} =
method secure*(s: Secure, conn: Connection, initiator: bool): Future[Connection] {.async, base, gcsafe.} =
try:
result = await s.handleConn(conn, true)
result = await s.handleConn(conn, initiator)
except CatchableError as exc:
warn "securing connection failed", msg = exc.msg
if not conn.closed():

View File

@ -1,11 +1,20 @@
# compile time options here
const
libp2p_secure {.strdefine.} = ""
import
options, tables,
switch, peer, peerinfo, connection, multiaddress,
crypto/crypto, transports/[transport, tcptransport],
muxers/[muxer, mplex/mplex, mplex/types],
protocols/[identify, secure/secure, secure/secio],
protocols/[identify, secure/secure],
protocols/pubsub/[pubsub, gossipsub, floodsub]
when libp2p_secure == "noise":
import protocols/secure/noise
else:
import protocols/secure/secio
export
switch, peer, peerinfo, connection, multiaddress, crypto
@ -23,9 +32,14 @@ proc newStandardSwitch*(privKey = none(PrivateKey),
transports = @[Transport(newTransport(TcpTransport))]
muxers = {MplexCodec: mplexProvider}.toTable
identify = newIdentify(peerInfo)
secureManagers = {SecioCodec: Secure(newSecio seckey)}.toTable
pubSub = if gossip: PubSub newPubSub(GossipSub, peerInfo, triggerSelf)
else: PubSub newPubSub(FloodSub, peerInfo, triggerSelf)
when libp2p_secure == "noise":
let secureManagers = {NoiseCodec: newNoise(seckey).Secure}.toTable
else:
let secureManagers = {SecioCodec: newSecio(seckey).Secure}.toTable
let pubSub = if gossip:
PubSub newPubSub(GossipSub, peerInfo, triggerSelf)
else:
PubSub newPubSub(FloodSub, peerInfo, triggerSelf)
result = newSwitch(peerInfo,
transports,

View File

@ -62,7 +62,7 @@ proc secure(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} =
if manager.len == 0:
raise newException(CatchableError, "Unable to negotiate a secure channel!")
result = await s.secureManagers[manager].secure(conn)
result = await s.secureManagers[manager].secure(conn, true)
proc identify(s: Switch, conn: Connection): Future[PeerInfo] {.async, gcsafe.} =
## identify the connection
@ -191,7 +191,7 @@ proc upgradeIncoming(s: Switch, conn: Connection) {.async, gcsafe.} =
{.async, gcsafe, closure.} =
trace "Securing connection"
let secure = s.secureManagers[proto]
let sconn = await secure.secure(conn)
let sconn = await secure.secure(conn, false)
if not isNil(sconn):
# add the muxer
for muxer in s.muxers.values: