2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
## The switch is the core of libp2p, which brings together the
|
|
|
|
## transports, the connection manager, the upgrader and other
|
|
|
|
## parts to allow programs to use libp2p
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-01-21 04:00:24 +00:00
|
|
|
import std/[tables,
|
|
|
|
options,
|
2021-11-24 20:01:12 +00:00
|
|
|
sequtils,
|
2021-01-21 04:00:24 +00:00
|
|
|
sets,
|
2023-05-18 08:24:17 +00:00
|
|
|
oids]
|
2020-06-24 15:08:44 +00:00
|
|
|
|
|
|
|
import chronos,
|
|
|
|
chronicles,
|
|
|
|
metrics
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
import stream/connection,
|
2019-09-11 19:03:30 +00:00
|
|
|
transports/transport,
|
2023-05-18 08:24:17 +00:00
|
|
|
upgrademngrs/upgrade,
|
2019-09-11 19:03:30 +00:00
|
|
|
multistream,
|
2020-06-17 04:14:02 +00:00
|
|
|
multiaddress,
|
2019-09-06 07:13:47 +00:00
|
|
|
protocols/protocol,
|
2019-09-14 13:55:52 +00:00
|
|
|
protocols/secure/secure,
|
2019-09-11 19:03:30 +00:00
|
|
|
peerinfo,
|
2021-01-04 18:59:05 +00:00
|
|
|
utils/semaphore,
|
2020-07-17 15:36:48 +00:00
|
|
|
connmanager,
|
2021-08-18 07:40:12 +00:00
|
|
|
nameresolving/nameresolver,
|
2020-07-24 19:24:31 +00:00
|
|
|
peerid,
|
2021-06-08 16:55:24 +00:00
|
|
|
peerstore,
|
2021-03-18 15:20:36 +00:00
|
|
|
errors,
|
2022-07-01 18:19:57 +00:00
|
|
|
utility,
|
2021-03-18 15:20:36 +00:00
|
|
|
dialer
|
2019-08-28 19:12:15 +00:00
|
|
|
|
2021-06-08 16:55:24 +00:00
|
|
|
export connmanager, upgrade, dialer, peerstore
|
2020-09-23 14:07:16 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p switch"
|
2019-09-12 02:10:38 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
#TODO: General note - use a finite state machine to manage the different
|
2019-12-08 21:06:58 +00:00
|
|
|
# steps of connections establishing and upgrading. This makes everything
|
|
|
|
# more robust and less prone to ordering attacks - i.e. muxing can come if
|
|
|
|
# and only if the channel has been secured (i.e. if a secure manager has been
|
2019-12-04 04:44:54 +00:00
|
|
|
# previously provided)
|
|
|
|
|
2021-01-04 18:59:05 +00:00
|
|
|
const
|
|
|
|
ConcurrentUpgrades* = 4
|
|
|
|
|
2019-08-28 19:12:15 +00:00
|
|
|
type
|
2022-07-01 18:19:57 +00:00
|
|
|
Switch* {.public.} = ref object of Dial
|
2019-08-30 05:17:07 +00:00
|
|
|
peerInfo*: PeerInfo
|
2021-01-15 04:48:03 +00:00
|
|
|
connManager*: ConnManager
|
2019-08-30 05:17:07 +00:00
|
|
|
transports*: seq[Transport]
|
2020-02-04 16:16:21 +00:00
|
|
|
ms*: MultistreamSelect
|
2020-11-19 02:06:42 +00:00
|
|
|
acceptFuts: seq[Future[void]]
|
2021-03-18 15:20:36 +00:00
|
|
|
dialer*: Dial
|
2021-06-08 16:55:24 +00:00
|
|
|
peerStore*: PeerStore
|
2021-08-18 07:40:12 +00:00
|
|
|
nameResolver*: NameResolver
|
2022-06-30 09:21:33 +00:00
|
|
|
started: bool
|
2022-12-16 11:32:00 +00:00
|
|
|
services*: seq[Service]
|
|
|
|
|
|
|
|
Service* = ref object of RootObj
|
|
|
|
inUse: bool
|
|
|
|
|
|
|
|
|
|
|
|
method setup*(self: Service, switch: Switch): Future[bool] {.base, async, gcsafe.} =
|
|
|
|
if self.inUse:
|
|
|
|
warn "service setup has already been called"
|
|
|
|
return false
|
|
|
|
self.inUse = true
|
|
|
|
return true
|
|
|
|
|
|
|
|
method run*(self: Service, switch: Switch) {.base, async, gcsafe.} =
|
|
|
|
doAssert(false, "Not implemented!")
|
|
|
|
|
|
|
|
method stop*(self: Service, switch: Switch): Future[bool] {.base, async, gcsafe.} =
|
|
|
|
if not self.inUse:
|
|
|
|
warn "service is already stopped"
|
|
|
|
return false
|
|
|
|
self.inUse = false
|
|
|
|
return true
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2020-08-08 06:52:20 +00:00
|
|
|
proc addConnEventHandler*(s: Switch,
|
2020-09-23 14:07:16 +00:00
|
|
|
handler: ConnEventHandler,
|
2022-07-01 18:19:57 +00:00
|
|
|
kind: ConnEventKind) {.public.} =
|
|
|
|
## Adds a ConnEventHandler, which will be triggered when
|
|
|
|
## a connection to a peer is created or dropped.
|
|
|
|
## There may be multiple connections per peer.
|
|
|
|
##
|
|
|
|
## The handler should not raise.
|
2020-09-23 14:07:16 +00:00
|
|
|
s.connManager.addConnEventHandler(handler, kind)
|
2020-07-24 19:24:31 +00:00
|
|
|
|
2020-08-08 06:52:20 +00:00
|
|
|
proc removeConnEventHandler*(s: Switch,
|
2020-09-23 14:07:16 +00:00
|
|
|
handler: ConnEventHandler,
|
2022-07-01 18:19:57 +00:00
|
|
|
kind: ConnEventKind) {.public.} =
|
2020-09-23 14:07:16 +00:00
|
|
|
s.connManager.removeConnEventHandler(handler, kind)
|
2020-06-24 15:08:44 +00:00
|
|
|
|
2020-09-15 20:19:22 +00:00
|
|
|
proc addPeerEventHandler*(s: Switch,
|
|
|
|
handler: PeerEventHandler,
|
2022-07-01 18:19:57 +00:00
|
|
|
kind: PeerEventKind) {.public.} =
|
|
|
|
## Adds a PeerEventHandler, which will be triggered when
|
|
|
|
## a peer connects or disconnects from us.
|
|
|
|
##
|
|
|
|
## The handler should not raise.
|
2020-09-23 14:07:16 +00:00
|
|
|
s.connManager.addPeerEventHandler(handler, kind)
|
2020-09-15 20:19:22 +00:00
|
|
|
|
|
|
|
proc removePeerEventHandler*(s: Switch,
|
|
|
|
handler: PeerEventHandler,
|
2022-07-01 18:19:57 +00:00
|
|
|
kind: PeerEventKind) {.public.} =
|
2020-09-23 14:07:16 +00:00
|
|
|
s.connManager.removePeerEventHandler(handler, kind)
|
2020-09-15 20:19:22 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
method addTransport*(s: Switch, t: Transport) =
|
2022-05-18 08:19:37 +00:00
|
|
|
s.transports &= t
|
|
|
|
s.dialer.addTransport(t)
|
|
|
|
|
2022-12-16 11:32:00 +00:00
|
|
|
proc connectedPeers*(s: Switch, dir: Direction): seq[PeerId] =
|
|
|
|
s.connManager.connectedPeers(dir)
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc isConnected*(s: Switch, peerId: PeerId): bool {.public.} =
|
2020-07-17 18:44:41 +00:00
|
|
|
## returns true if the peer has one or more
|
2022-07-01 18:19:57 +00:00
|
|
|
## associated connections
|
2020-07-17 18:44:41 +00:00
|
|
|
##
|
|
|
|
|
2020-08-06 07:29:27 +00:00
|
|
|
peerId in s.connManager
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc disconnect*(s: Switch, peerId: PeerId): Future[void] {.gcsafe, public.} =
|
|
|
|
## Disconnect from a peer, waiting for the connection(s) to be dropped
|
2020-08-06 07:29:27 +00:00
|
|
|
s.connManager.dropPeer(peerId)
|
2019-09-28 19:54:32 +00:00
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
method connect*(
|
|
|
|
s: Switch,
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId: PeerId,
|
2022-02-24 16:31:47 +00:00
|
|
|
addrs: seq[MultiAddress],
|
2023-01-25 10:19:03 +00:00
|
|
|
forceDial = false,
|
2023-04-14 14:23:19 +00:00
|
|
|
reuseConnection = true,
|
|
|
|
upgradeDir = Direction.Out): Future[void] {.public.} =
|
2022-07-01 18:19:57 +00:00
|
|
|
## Connects to a peer without opening a stream to it
|
|
|
|
|
2023-04-14 14:23:19 +00:00
|
|
|
s.dialer.connect(peerId, addrs, forceDial, reuseConnection, upgradeDir)
|
2020-02-25 22:13:05 +00:00
|
|
|
|
2022-09-05 12:31:14 +00:00
|
|
|
method connect*(
|
|
|
|
s: Switch,
|
2022-12-08 16:11:55 +00:00
|
|
|
address: MultiAddress,
|
|
|
|
allowUnknownPeerId = false): Future[PeerId] =
|
2022-09-05 12:31:14 +00:00
|
|
|
## Connects to a peer and retrieve its PeerId
|
2022-12-08 16:11:55 +00:00
|
|
|
##
|
|
|
|
## If the P2P part is missing from the MA and `allowUnknownPeerId` is set
|
|
|
|
## to true, this will discover the PeerId while connecting. This exposes
|
|
|
|
## you to MiTM attacks, so it shouldn't be used without care!
|
2022-09-05 12:31:14 +00:00
|
|
|
|
2022-12-08 16:11:55 +00:00
|
|
|
s.dialer.connect(address, allowUnknownPeerId)
|
2022-09-05 12:31:14 +00:00
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
method dial*(
|
|
|
|
s: Switch,
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId: PeerId,
|
2022-07-01 18:19:57 +00:00
|
|
|
protos: seq[string]): Future[Connection] {.public.} =
|
|
|
|
## Open a stream to a connected peer with the specified `protos`
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
s.dialer.dial(peerId, protos)
|
2020-09-21 09:16:29 +00:00
|
|
|
|
|
|
|
proc dial*(s: Switch,
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId: PeerId,
|
2022-07-01 18:19:57 +00:00
|
|
|
proto: string): Future[Connection] {.public.} =
|
|
|
|
## Open a stream to a connected peer with the specified `proto`
|
|
|
|
|
2021-01-21 04:00:24 +00:00
|
|
|
dial(s, peerId, @[proto])
|
2020-08-12 00:05:49 +00:00
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
method dial*(
|
|
|
|
s: Switch,
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId: PeerId,
|
2021-03-18 15:20:36 +00:00
|
|
|
addrs: seq[MultiAddress],
|
2022-02-24 16:31:47 +00:00
|
|
|
protos: seq[string],
|
2022-07-01 18:19:57 +00:00
|
|
|
forceDial = false): Future[Connection] {.public.} =
|
|
|
|
## Connected to a peer and open a stream
|
|
|
|
## with the specified `protos`
|
|
|
|
|
2022-02-24 16:31:47 +00:00
|
|
|
s.dialer.dial(peerId, addrs, protos, forceDial)
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
proc dial*(
|
|
|
|
s: Switch,
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId: PeerId,
|
2021-03-18 15:20:36 +00:00
|
|
|
addrs: seq[MultiAddress],
|
2022-07-01 18:19:57 +00:00
|
|
|
proto: string): Future[Connection] {.public.} =
|
|
|
|
## Connected to a peer and open a stream
|
|
|
|
## with the specified `proto`
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
dial(s, peerId, addrs, @[proto])
|
2020-09-21 09:16:29 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
proc mount*[T: LPProtocol](s: Switch, proto: T, matcher: Matcher = nil)
|
2023-06-07 11:12:49 +00:00
|
|
|
{.gcsafe, raises: [LPError], public.} =
|
2022-07-01 18:19:57 +00:00
|
|
|
## mount a protocol to the switch
|
|
|
|
|
2019-08-31 17:58:49 +00:00
|
|
|
if isNil(proto.handler):
|
2021-06-02 13:39:10 +00:00
|
|
|
raise newException(LPError,
|
2020-06-27 17:33:34 +00:00
|
|
|
"Protocol has to define a handle method or proc")
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
if proto.codec.len == 0:
|
2021-06-02 13:39:10 +00:00
|
|
|
raise newException(LPError,
|
2020-06-27 17:33:34 +00:00
|
|
|
"Protocol has to define a codec string")
|
2019-08-31 18:52:56 +00:00
|
|
|
|
2022-06-30 09:21:33 +00:00
|
|
|
if s.started and not proto.started:
|
|
|
|
raise newException(LPError, "Protocol not started")
|
|
|
|
|
2020-10-02 14:59:15 +00:00
|
|
|
s.ms.addHandler(proto.codecs, proto, matcher)
|
2021-03-15 21:29:05 +00:00
|
|
|
s.peerInfo.protocols.add(proto.codec)
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc upgrader(switch: Switch, trans: Transport, conn: Connection) {.async.} =
|
2023-04-14 14:23:19 +00:00
|
|
|
let muxed = await trans.upgrade(conn, Direction.In, Opt.none(PeerId))
|
2023-03-08 11:30:19 +00:00
|
|
|
switch.connManager.storeMuxer(muxed)
|
|
|
|
await switch.peerStore.identify(muxed)
|
|
|
|
trace "Connection upgrade succeeded"
|
|
|
|
|
|
|
|
proc upgradeMonitor(
|
|
|
|
switch: Switch,
|
|
|
|
trans: Transport,
|
|
|
|
conn: Connection,
|
|
|
|
upgrades: AsyncSemaphore) {.async.} =
|
2021-01-04 18:59:05 +00:00
|
|
|
try:
|
2023-03-08 11:30:19 +00:00
|
|
|
await switch.upgrader(trans, conn).wait(30.seconds)
|
2021-01-04 18:59:05 +00:00
|
|
|
except CatchableError as exc:
|
2023-03-08 11:30:19 +00:00
|
|
|
if exc isnot CancelledError:
|
|
|
|
libp2p_failed_upgrades_incoming.inc()
|
2021-01-07 23:06:41 +00:00
|
|
|
if not isNil(conn):
|
|
|
|
await conn.close()
|
2021-01-04 18:59:05 +00:00
|
|
|
trace "Exception awaiting connection upgrade", exc = exc.msg, conn
|
|
|
|
finally:
|
2023-03-08 11:30:19 +00:00
|
|
|
upgrades.release()
|
2021-01-04 18:59:05 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
proc accept(s: Switch, transport: Transport) {.async.} = # noraises
|
2021-01-04 18:59:05 +00:00
|
|
|
## switch accept loop, ran for every transport
|
2020-11-19 02:06:42 +00:00
|
|
|
##
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2021-01-14 09:11:12 +00:00
|
|
|
let upgrades = newAsyncSemaphore(ConcurrentUpgrades)
|
2020-11-19 02:06:42 +00:00
|
|
|
while transport.running:
|
|
|
|
var conn: Connection
|
2019-09-04 22:00:39 +00:00
|
|
|
try:
|
2020-11-19 02:06:42 +00:00
|
|
|
debug "About to accept incoming connection"
|
2021-01-04 18:59:05 +00:00
|
|
|
# remember to always release the slot when
|
|
|
|
# the upgrade succeeds or fails, this is
|
|
|
|
# currently done by the `upgradeMonitor`
|
2021-01-21 04:00:24 +00:00
|
|
|
await upgrades.acquire() # first wait for an upgrade slot to become available
|
2022-08-03 14:48:19 +00:00
|
|
|
let slot = await s.connManager.getIncomingSlot()
|
|
|
|
conn =
|
|
|
|
try:
|
|
|
|
await transport.accept()
|
|
|
|
except CatchableError as exc:
|
|
|
|
slot.release()
|
|
|
|
raise exc
|
|
|
|
slot.trackConnection(conn)
|
2021-01-04 18:59:05 +00:00
|
|
|
if isNil(conn):
|
2020-11-19 02:06:42 +00:00
|
|
|
# A nil connection means that we might have hit a
|
|
|
|
# file-handle limit (or another non-fatal error),
|
2023-06-14 15:55:56 +00:00
|
|
|
# we can get one on the next try
|
|
|
|
debug "Unable to get a connection"
|
2021-01-04 18:59:05 +00:00
|
|
|
continue
|
|
|
|
|
2021-03-02 23:23:40 +00:00
|
|
|
# set the direction of this bottom level transport
|
|
|
|
# in order to be able to consume this information in gossipsub if required
|
|
|
|
# gossipsub gives priority to connections we make
|
|
|
|
conn.transportDir = Direction.In
|
|
|
|
|
2021-01-04 18:59:05 +00:00
|
|
|
debug "Accepted an incoming connection", conn
|
2023-03-08 11:30:19 +00:00
|
|
|
asyncSpawn s.upgradeMonitor(transport, conn, upgrades)
|
2021-01-04 18:59:05 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
trace "releasing semaphore on cancellation"
|
|
|
|
upgrades.release() # always release the slot
|
2019-12-04 04:44:54 +00:00
|
|
|
except CatchableError as exc:
|
2023-06-14 15:55:56 +00:00
|
|
|
error "Exception in accept loop, exiting", exc = exc.msg
|
2021-01-04 18:59:05 +00:00
|
|
|
upgrades.release() # always release the slot
|
2020-11-19 02:06:42 +00:00
|
|
|
if not isNil(conn):
|
|
|
|
await conn.close()
|
|
|
|
return
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc stop*(s: Switch) {.async, public.} =
|
|
|
|
## Stop listening on every transport, and
|
|
|
|
## close every active connections
|
|
|
|
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "Stopping switch"
|
2020-06-29 15:15:31 +00:00
|
|
|
|
2022-06-30 09:21:33 +00:00
|
|
|
s.started = false
|
2023-01-17 15:18:38 +00:00
|
|
|
|
|
|
|
for service in s.services:
|
|
|
|
discard await service.stop(s)
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
# close and cleanup all connections
|
|
|
|
await s.connManager.close()
|
2019-09-06 07:13:47 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
for transp in s.transports:
|
2020-06-29 15:15:31 +00:00
|
|
|
try:
|
2021-12-16 10:05:20 +00:00
|
|
|
await transp.stop()
|
2020-06-29 15:15:31 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
2020-09-09 17:12:08 +00:00
|
|
|
warn "error cleaning up transports", msg = exc.msg
|
2020-06-29 15:15:31 +00:00
|
|
|
|
2021-01-04 18:59:05 +00:00
|
|
|
try:
|
|
|
|
await allFutures(s.acceptFuts)
|
|
|
|
.wait(1.seconds)
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Exception while stopping accept loops", exc = exc.msg
|
|
|
|
|
|
|
|
# check that all futures were properly
|
|
|
|
# stopped and otherwise cancel them
|
|
|
|
for a in s.acceptFuts:
|
|
|
|
if not a.finished:
|
|
|
|
a.cancel()
|
|
|
|
|
2022-12-16 11:32:00 +00:00
|
|
|
for service in s.services:
|
|
|
|
discard await service.stop(s)
|
|
|
|
|
2022-06-30 09:21:33 +00:00
|
|
|
await s.ms.stop()
|
|
|
|
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "Switch stopped"
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc start*(s: Switch) {.async, gcsafe, public.} =
|
|
|
|
## Start listening on every transport
|
|
|
|
|
2022-11-29 15:21:51 +00:00
|
|
|
if s.started:
|
|
|
|
warn "Switch has already been started"
|
|
|
|
return
|
|
|
|
|
2023-05-16 12:59:02 +00:00
|
|
|
debug "starting switch for peer", peerInfo = s.peerInfo
|
2021-07-19 10:51:27 +00:00
|
|
|
var startFuts: seq[Future[void]]
|
2021-11-24 20:01:12 +00:00
|
|
|
for t in s.transports:
|
2022-10-20 10:22:28 +00:00
|
|
|
let addrs = s.peerInfo.listenAddrs.filterIt(
|
2021-11-24 20:01:12 +00:00
|
|
|
t.handles(it)
|
|
|
|
)
|
|
|
|
|
2022-10-20 10:22:28 +00:00
|
|
|
s.peerInfo.listenAddrs.keepItIf(
|
2021-11-24 20:01:12 +00:00
|
|
|
it notin addrs
|
|
|
|
)
|
|
|
|
|
2022-05-18 08:19:37 +00:00
|
|
|
if addrs.len > 0 or t.running:
|
2021-11-24 20:01:12 +00:00
|
|
|
startFuts.add(t.start(addrs))
|
|
|
|
|
|
|
|
await allFutures(startFuts)
|
|
|
|
|
2022-09-15 07:06:32 +00:00
|
|
|
for fut in startFuts:
|
|
|
|
if fut.failed:
|
|
|
|
await s.stop()
|
|
|
|
raise fut.error
|
2021-11-24 20:01:12 +00:00
|
|
|
|
2021-07-19 10:51:27 +00:00
|
|
|
for t in s.transports: # for each transport
|
2022-05-18 08:19:37 +00:00
|
|
|
if t.addrs.len > 0 or t.running:
|
2021-11-24 20:01:12 +00:00
|
|
|
s.acceptFuts.add(s.accept(t))
|
2022-10-20 10:22:28 +00:00
|
|
|
s.peerInfo.listenAddrs &= t.addrs
|
2021-07-19 10:51:27 +00:00
|
|
|
|
2022-10-20 10:22:28 +00:00
|
|
|
await s.peerInfo.update()
|
2022-04-13 07:35:28 +00:00
|
|
|
|
2022-06-30 09:21:33 +00:00
|
|
|
await s.ms.start()
|
|
|
|
|
2022-12-16 11:32:00 +00:00
|
|
|
for service in s.services:
|
|
|
|
discard await service.setup(s)
|
|
|
|
|
2022-06-30 09:21:33 +00:00
|
|
|
s.started = true
|
|
|
|
|
2021-07-19 10:51:27 +00:00
|
|
|
debug "Started libp2p node", peer = s.peerInfo
|
2021-11-24 20:01:12 +00:00
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
proc newSwitch*(peerInfo: PeerInfo,
|
|
|
|
transports: seq[Transport],
|
2021-12-16 10:05:20 +00:00
|
|
|
secureManagers: openArray[Secure] = [],
|
2021-03-18 15:20:36 +00:00
|
|
|
connManager: ConnManager,
|
2021-08-18 07:40:12 +00:00
|
|
|
ms: MultistreamSelect,
|
2023-03-08 11:30:19 +00:00
|
|
|
peerStore: PeerStore,
|
2022-05-25 10:12:57 +00:00
|
|
|
nameResolver: NameResolver = nil,
|
2022-12-16 11:32:00 +00:00
|
|
|
services = newSeq[Service]()): Switch
|
2023-06-07 11:12:49 +00:00
|
|
|
{.raises: [LPError].} =
|
2020-07-07 11:14:11 +00:00
|
|
|
if secureManagers.len == 0:
|
2021-05-24 17:55:33 +00:00
|
|
|
raise newException(LPError, "Provide at least one secure manager")
|
2020-07-07 11:14:11 +00:00
|
|
|
|
2020-09-15 20:19:22 +00:00
|
|
|
let switch = Switch(
|
2020-07-07 11:14:11 +00:00
|
|
|
peerInfo: peerInfo,
|
2021-01-20 17:28:32 +00:00
|
|
|
ms: ms,
|
2020-07-07 11:14:11 +00:00
|
|
|
transports: transports,
|
2021-01-20 17:28:32 +00:00
|
|
|
connManager: connManager,
|
2022-05-25 10:12:57 +00:00
|
|
|
peerStore: peerStore,
|
2023-03-08 11:30:19 +00:00
|
|
|
dialer: Dialer.new(peerInfo.peerId, connManager, peerStore, transports, nameResolver),
|
2022-12-16 11:32:00 +00:00
|
|
|
nameResolver: nameResolver,
|
|
|
|
services: services)
|
2019-09-06 07:13:47 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
switch.connManager.peerStore = peerStore
|
2020-09-15 20:19:22 +00:00
|
|
|
return switch
|