2020-04-07 22:19:23 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2020 Status Research & Development GmbH
|
|
|
|
## 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.
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
import
|
2020-04-07 22:07:35 +00:00
|
|
|
options, tables
|
2020-04-07 22:43:54 +00:00
|
|
|
import libp2p
|
2019-12-08 21:06:58 +00:00
|
|
|
|
2020-04-07 22:43:54 +00:00
|
|
|
export libp2p
|
2019-12-08 21:06:58 +00:00
|
|
|
|
|
|
|
proc newStandardSwitch*(privKey = none(PrivateKey),
|
|
|
|
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0"),
|
2019-12-23 18:45:12 +00:00
|
|
|
triggerSelf = false,
|
|
|
|
gossip = false): Switch =
|
2019-12-08 21:06:58 +00:00
|
|
|
proc createMplex(conn: Connection): Muxer =
|
|
|
|
result = newMplex(conn)
|
|
|
|
|
|
|
|
let
|
2020-01-07 08:04:33 +00:00
|
|
|
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA))
|
2020-02-11 17:53:39 +00:00
|
|
|
peerInfo = PeerInfo.init(seckey, [address])
|
2019-12-08 21:06:58 +00:00
|
|
|
mplexProvider = newMuxerProvider(createMplex, MplexCodec)
|
|
|
|
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)
|
|
|
|
|
|
|
|
result = newSwitch(peerInfo,
|
|
|
|
transports,
|
|
|
|
identify,
|
|
|
|
muxers,
|
|
|
|
secureManagers = secureManagers,
|
|
|
|
pubSub = some(pubSub))
|