* use a builder pattern to build the switch (#551)

* use a builder pattern to build the switch

* with with

* more refs

* Merge master (#555)

* Revisit Floodsub (#543)

Fixes #525

add coverage to unsubscribeAll and testing

* add mounted protos to identify message (#546)

* add stable/unstable auto bumps

* fix auto-bump CI

* merge nbc auto bump with CI in order to bump only on CI success

* put conditional locks on nbc bump (#549)

* Fix minor exception issues (#550)

Makes code compatible with
https://github.com/status-im/nim-chronos/pull/166 without requiring it.

* fix nimbus ref for auto-bump stable's PR

* Split dialer (#542)

* extracting dialing logic to dialer

* exposing upgrade methods on transport

* cleanup

* fixing tests to use new interfaces

* add comments

* add base exception class and fix hierarchy

* fix imports

* `doAssert` is `ValueError` not `AssertionError`?

* revert back to `AssertionError`

Co-authored-by: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com>
Co-authored-by: Jacek Sieka <jacek@status.im>

* `doAssert` is `ValueError` not `AssertionError`?

* revert back to `AssertionError`

* fix builders

* more builder stuff

* more builders

Co-authored-by: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com>
Co-authored-by: Jacek Sieka <jacek@status.im>
This commit is contained in:
Dmitriy Ryajov 2021-04-05 13:37:14 -06:00
parent ee49b76478
commit 530e589e14
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
2 changed files with 10 additions and 77 deletions

View File

@ -14,7 +14,8 @@ import
switch, peerid, peerinfo, stream/connection, multiaddress, switch, peerid, peerinfo, stream/connection, multiaddress,
crypto/crypto, transports/[transport, tcptransport], crypto/crypto, transports/[transport, tcptransport],
muxers/[muxer, mplex/mplex], muxers/[muxer, mplex/mplex],
protocols/[identify, secure/secure, secure/noise] protocols/[identify, secure/secure, secure/noise],
connmanager, upgrademngrs/muxedupgrade
export export
switch, peerid, peerinfo, connection, multiaddress, crypto switch, peerid, peerinfo, connection, multiaddress, crypto
@ -39,6 +40,8 @@ type
mplexOpts: MplexOpts mplexOpts: MplexOpts
tcpTransportOpts: TcpTransportOpts tcpTransportOpts: TcpTransportOpts
rng: ref BrHmacDrbgContext rng: ref BrHmacDrbgContext
inTimeout: Duration
outTimeout: Duration
maxConnections: int maxConnections: int
maxIn: int maxIn: int
maxOut: int maxOut: int
@ -154,12 +157,15 @@ proc build*(b: SwitchBuilder): Switch
let let
identify = newIdentify(peerInfo) identify = newIdentify(peerInfo)
connManager = ConnManager.init(b.maxConnsPerPeer, b.maxConnections, b.maxIn, b.maxOut)
ms = newMultistream()
muxedUpgrade = MuxedUpgrade.init(identify, muxers, secureManagerInstances, connManager, ms)
let let
transports = block: transports = block:
var transports: seq[Transport] var transports: seq[Transport]
if b.tcpTransportOpts.enable: if b.tcpTransportOpts.enable:
transports.add(Transport(TcpTransport.init(b.tcpTransportOpts.flags))) transports.add(Transport(TcpTransport.init(b.tcpTransportOpts.flags, muxedUpgrade)))
transports transports
if b.secureManagers.len == 0: if b.secureManagers.len == 0:
@ -174,10 +180,8 @@ proc build*(b: SwitchBuilder): Switch
identity = identify, identity = identify,
muxers = muxers, muxers = muxers,
secureManagers = secureManagerInstances, secureManagers = secureManagerInstances,
maxConnections = b.maxConnections, connManager = connManager,
maxIn = b.maxIn, ms = ms)
maxOut = b.maxOut,
maxConnsPerPeer = b.maxConnsPerPeer)
return switch return switch

View File

@ -1,71 +0,0 @@
import
options, tables, chronos, bearssl,
switch, peerid, peerinfo, stream/connection, multiaddress,
crypto/crypto, transports/[transport, tcptransport],
muxers/[muxer, mplex/mplex],
protocols/[identify, secure/secure, secure/noise],
upgrademngrs/[upgrade, muxedupgrade], connmanager
export
switch, peerid, peerinfo, connection, multiaddress, crypto
type
SecureProtocol* {.pure.} = enum
Noise,
Secio {.deprecated.}
proc newStandardSwitch*(privKey = none(PrivateKey),
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
secureManagers: openarray[SecureProtocol] = [
SecureProtocol.Noise,
],
transportFlags: set[ServerFlags] = {},
rng = newRng(),
inTimeout: Duration = 5.minutes,
outTimeout: Duration = 5.minutes,
maxConnections = MaxConnections,
maxIn = -1,
maxOut = -1,
maxConnsPerPeer = MaxConnectionsPerPeer): Switch =
proc createMplex(conn: Connection): Muxer =
Mplex.init(
conn,
inTimeout = inTimeout,
outTimeout = outTimeout)
if rng == nil: # newRng could fail
raise (ref CatchableError)(msg: "Cannot initialize RNG")
let
seckey = privKey.get(otherwise = PrivateKey.random(rng[]).tryGet())
peerInfo = PeerInfo.init(seckey, [address])
var
secureManagerInstances: seq[Secure]
for sec in secureManagers:
case sec
of SecureProtocol.Noise:
secureManagerInstances &= newNoise(rng, seckey).Secure
of SecureProtocol.Secio:
quit("Secio is deprecated!") # use of secio is unsafe
let
mplexProvider = newMuxerProvider(createMplex, MplexCodec)
ms = newMultistream()
identify = newIdentify(peerInfo)
muxers = {MplexCodec: mplexProvider}.toTable
connManager = ConnManager.init(maxConnsPerPeer, maxConnections, maxIn, maxOut)
muxedUpgrade = MuxedUpgrade.init(identify, muxers, secureManagerInstances, connManager, ms)
transports = @[Transport(TcpTransport.init(transportFlags, muxedUpgrade))]
let switch = newSwitch(
peerInfo,
transports,
identify,
muxers,
secureManagers = secureManagerInstances,
connManager = connManager,
ms = ms)
return switch