diff --git a/examples/directchat.nim b/examples/directchat.nim index 318820d..cacb612 100644 --- a/examples/directchat.nim +++ b/examples/directchat.nim @@ -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, diff --git a/libp2p.nimble b/libp2p.nimble index 6b0bba9..e6a685d 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -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" diff --git a/libp2p/standard_setup.nim b/libp2p/standard_setup.nim index b7312e4..240bf33 100644 --- a/libp2p/standard_setup.nim +++ b/libp2p/standard_setup.nim @@ -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)) diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 9aac42b..6e53a49 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -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 diff --git a/tests/testinterop.nim b/tests/testinterop.nim index 015be34..17490cc 100644 --- a/tests/testinterop.nim +++ b/tests/testinterop.nim @@ -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: diff --git a/tests/testnoise.nim b/tests/testnoise.nim index 958cc6c..948301d 100644 --- a/tests/testnoise.nim +++ b/tests/testnoise.nim @@ -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() diff --git a/tests/testswitch.nim b/tests/testswitch.nim index 0fb9b64..dc7e7c3 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -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,