Secure managers are now sorted, giving priority to noise (#191)

* Secure managers are now sorted, giving priority to noise

* fix nimble test command

* Fix native tests

* fix directchat sample

* Could not write to connection - reduce verbosity

* fix interop testing

* Remove more tables

* test interop fixes

* directchat fix

* fix interop/remove some deprecation
This commit is contained in:
Giovanni Petrantoni 2020-06-01 15:41:32 +09:00 committed by GitHub
parent 6affcda937
commit 37b98ad45c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 43 deletions

View File

@ -178,7 +178,7 @@ proc processInput(rfd: AsyncFD) {.async.} =
let transports = @[Transport(TcpTransport.init())]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let identify = newIdentify(peerInfo)
let secureManagers = [(SecioCodec, Secure(newSecio(seckey)))].toTable()
let secureManagers = [Secure(newSecio(seckey))]
let switch = newSwitch(peerInfo,
transports,
identify,

View File

@ -16,11 +16,9 @@ requires "nim >= 1.2.0",
"secp256k1",
"stew"
proc runTest(filename: string, secure: string = "secio", verify: bool = true, sign: bool = true) =
proc runTest(filename: string, verify: bool = true, sign: bool = true) =
var excstr: string = "nim c -r --opt:speed -d:debug --verbosity:0 --hints:off"
excstr.add(" ")
excstr.add("-d:libp2p_secure=" & $secure)
excstr.add(" ")
excstr.add("-d:libp2p_pubsub_sign=" & $sign)
excstr.add(" ")
excstr.add("-d:libp2p_pubsub_verify=" & $verify)
@ -45,11 +43,9 @@ task testinterop, "Runs interop tests":
task testpubsub, "Runs pubsub tests":
runTest("pubsub/testpubsub")
runTest("pubsub/testpubsub", sign = false, verify = false)
# runTest("pubsub/testpubsub", "noise")
task test, "Runs the test suite":
exec "nimble testnative"
# runTest("testnative", "noise")
exec "nimble testpubsub"
exec "nimble testdaemon"
exec "nimble testinterop"

View File

@ -1,6 +1,5 @@
# compile time options here
const
libp2p_secure {.strdefine.} = ""
libp2p_pubsub_sign {.booldefine.} = true
libp2p_pubsub_verify {.booldefine.} = true
@ -12,23 +11,31 @@ import
protocols/[identify, secure/secure],
protocols/pubsub/[pubsub, gossipsub, floodsub]
when libp2p_secure == "noise":
import protocols/secure/noise
else:
import protocols/secure/secio
import
protocols/secure/noise,
protocols/secure/secio
export
switch, peer, peerinfo, connection, multiaddress, crypto
type
SecureProtocol* {.pure.} = enum
Noise,
Secio
proc newStandardSwitch*(privKey = none(PrivateKey),
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
triggerSelf = false,
gossip = false,
secureManagers: openarray[SecureProtocol] = [
SecureProtocol.Noise, # array cos order matters
SecureProtocol.Secio
],
verifySignature = libp2p_pubsub_verify,
sign = libp2p_pubsub_sign,
transportFlags: set[ServerFlags] = {}): Switch =
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)
newMplex(conn)
let
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA).tryGet())
@ -37,27 +44,33 @@ proc newStandardSwitch*(privKey = none(PrivateKey),
transports = @[Transport(TcpTransport.init(transportFlags))]
muxers = {MplexCodec: mplexProvider}.toTable
identify = newIdentify(peerInfo)
when libp2p_secure == "noise":
let secureManagers = {NoiseCodec: newNoise(seckey).Secure}.toTable
else:
let secureManagers = {SecioCodec: newSecio(seckey).Secure}.toTable
var
secureManagerInstances: seq[Secure]
for sec in secureManagers:
case sec
of SecureProtocol.Noise:
secureManagerInstances &= newNoise(seckey).Secure
of SecureProtocol.Secio:
secureManagerInstances &= newSecio(seckey).Secure
let pubSub = if gossip:
PubSub newPubSub(GossipSub,
peerInfo = peerInfo,
triggerSelf = triggerSelf,
verifySignature = verifySignature,
sign = sign)
newPubSub(GossipSub,
peerInfo = peerInfo,
triggerSelf = triggerSelf,
verifySignature = verifySignature,
sign = sign).PubSub
else:
PubSub newPubSub(FloodSub,
peerInfo = peerInfo,
triggerSelf = triggerSelf,
verifySignature = verifySignature,
sign = sign)
newPubSub(FloodSub,
peerInfo = peerInfo,
triggerSelf = triggerSelf,
verifySignature = verifySignature,
sign = sign).PubSub
result = newSwitch(peerInfo,
transports,
identify,
muxers,
secureManagers = secureManagers,
pubSub = some(pubSub))
newSwitch(
peerInfo,
transports,
identify,
muxers,
secureManagers = secureManagerInstances,
pubSub = some(pubSub))

View File

@ -44,7 +44,7 @@ type
ms*: MultistreamSelect
identity*: Identify
streamHandler*: StreamHandler
secureManagers*: Table[string, Secure]
secureManagers*: OrderedTable[string, Secure]
pubSub*: Option[PubSub]
dialedPubSubPeers: HashSet[string]
@ -412,7 +412,7 @@ proc newSwitch*(peerInfo: PeerInfo,
transports: seq[Transport],
identity: Identify,
muxers: Table[string, MuxerProvider],
secureManagers: Table[string, Secure] = initTable[string, Secure](),
secureManagers: openarray[Secure] = [],
pubSub: Option[PubSub] = none(PubSub)): Switch =
new result
result.peerInfo = peerInfo
@ -422,7 +422,7 @@ proc newSwitch*(peerInfo: PeerInfo,
result.muxed = initTable[string, Muxer]()
result.identity = identity
result.muxers = muxers
result.secureManagers = initTable[string, Secure]()
result.secureManagers = initOrderedTable[string, Secure]()
result.dialedPubSubPeers = initHashSet[string]()
let s = result # can't capture result
@ -448,9 +448,9 @@ proc newSwitch*(peerInfo: PeerInfo,
# try establishing a pubsub connection
await s.subscribeToPeer(muxer.connection.peerInfo)
for k in secureManagers.keys:
trace "adding secure manager ", codec = secureManagers[k].codec
result.secureManagers[k] = secureManagers[k]
for proto in secureManagers:
trace "adding secure manager ", codec = proto.codec
result.secureManagers[proto.codec] = proto
if result.secureManagers.len == 0:
# use plain text if no secure managers are provided

View File

@ -73,7 +73,7 @@ proc createNode*(privKey: Option[PrivateKey] = none(PrivateKey),
let transports = @[Transport(TcpTransport.init())]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let identify = newIdentify(peerInfo)
let secureManagers = [(SecioCodec, Secure(newSecio(seckey.get())))].toTable()
let secureManagers = [Secure(newSecio(seckey.get()))]
var pubSub: Option[PubSub]
if gossip:

View File

@ -60,7 +60,7 @@ proc createSwitch(ma: MultiAddress; outgoing: bool): (Switch, PeerInfo) =
let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(TcpTransport.init())]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let secureManagers = [(NoiseCodec, Secure(newNoise(peerInfo.privateKey, outgoing = outgoing)))].toTable()
let secureManagers = [Secure(newNoise(peerInfo.privateKey, outgoing = outgoing))]
let switch = newSwitch(peerInfo,
transports,
identify,
@ -86,7 +86,7 @@ suite "Noise":
defer:
await sconn.close()
await conn.close()
await sconn.write(cstring("Hello!"), 6)
await sconn.write("Hello!")
let
transport1: TcpTransport = TcpTransport.init()
@ -141,7 +141,7 @@ suite "Noise":
conn = await transport2.dial(transport1.ma)
sconn = await clientNoise.secure(conn, true)
await sconn.write("Hello!".cstring, 6)
await sconn.write("Hello!")
await readTask
await sconn.close()
await conn.close()

View File

@ -41,7 +41,7 @@ proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) =
let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(TcpTransport.init())]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let secureManagers = [(SecioCodec, Secure(newSecio(peerInfo.privateKey)))].toTable()
let secureManagers = [Secure(newSecio(peerInfo.privateKey))]
let switch = newSwitch(peerInfo,
transports,
identify,