2020-05-10 06:53:48 +00:00
|
|
|
# compile time options here
|
|
|
|
const
|
|
|
|
libp2p_secure {.strdefine.} = ""
|
2020-05-26 03:55:53 +00:00
|
|
|
libp2p_pubsub_sign {.booldefine.} = true
|
|
|
|
libp2p_pubsub_verify {.booldefine.} = true
|
2020-05-10 06:53:48 +00:00
|
|
|
|
|
|
|
import
|
2020-05-26 03:55:53 +00:00
|
|
|
options, tables, chronicles, chronos,
|
2020-07-13 10:08:03 +00:00
|
|
|
libp2p/[switch, peerinfo, multiaddress, crypto/crypto],
|
|
|
|
libp2p/stream/connection,
|
2020-05-10 06:53:48 +00:00
|
|
|
libp2p/transports/[transport, tcptransport],
|
|
|
|
libp2p/muxers/[muxer, mplex/mplex, mplex/types],
|
|
|
|
libp2p/protocols/[identify, secure/secure],
|
2020-05-27 04:07:11 +00:00
|
|
|
libp2p/protocols/pubsub/[pubsub, gossipsub],
|
2020-07-28 11:00:34 +00:00
|
|
|
../../protocol/v2/waku_protocol2
|
2020-05-10 06:53:48 +00:00
|
|
|
|
2020-06-15 03:05:37 +00:00
|
|
|
import
|
|
|
|
libp2p/protocols/secure/noise,
|
|
|
|
libp2p/protocols/secure/secio
|
2020-05-10 06:53:48 +00:00
|
|
|
|
|
|
|
export
|
2020-07-13 10:08:03 +00:00
|
|
|
switch, peerinfo, connection, multiaddress, crypto
|
2020-05-10 06:53:48 +00:00
|
|
|
|
2020-06-15 03:05:37 +00:00
|
|
|
type
|
|
|
|
SecureProtocol* {.pure.} = enum
|
|
|
|
Noise,
|
|
|
|
Secio
|
|
|
|
|
2020-05-10 06:53:48 +00:00
|
|
|
proc newStandardSwitch*(privKey = none(PrivateKey),
|
2020-06-01 03:15:37 +00:00
|
|
|
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
|
2020-05-10 06:53:48 +00:00
|
|
|
triggerSelf = false,
|
2020-07-13 10:08:03 +00:00
|
|
|
gossip = false,
|
2020-06-15 03:05:37 +00:00
|
|
|
secureManagers: openarray[SecureProtocol] = [
|
|
|
|
# NOTE below relates to Eth2
|
|
|
|
# TODO investigate why we're getting fewer peers on public testnets with noise
|
|
|
|
SecureProtocol.Secio,
|
|
|
|
SecureProtocol.Noise, # array cos order matters
|
|
|
|
],
|
2020-05-26 03:55:53 +00:00
|
|
|
verifySignature = libp2p_pubsub_verify,
|
|
|
|
sign = libp2p_pubsub_sign,
|
2020-07-13 10:08:03 +00:00
|
|
|
transportFlags: set[ServerFlags] = {},
|
2020-07-24 01:39:58 +00:00
|
|
|
rng = newRng(),
|
|
|
|
inTimeout: Duration = 1.minutes,
|
|
|
|
outTimeout: Duration = 1.minutes): Switch =
|
2020-05-18 06:03:15 +00:00
|
|
|
info "newStandardSwitch"
|
2020-05-10 06:53:48 +00:00
|
|
|
proc createMplex(conn: Connection): Muxer =
|
2020-07-24 01:39:58 +00:00
|
|
|
Mplex.init(
|
|
|
|
conn,
|
|
|
|
inTimeout = inTimeout,
|
|
|
|
outTimeout = outTimeout)
|
2020-05-10 06:53:48 +00:00
|
|
|
|
|
|
|
let
|
2020-07-13 10:08:03 +00:00
|
|
|
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA, rng[]).tryGet())
|
2020-05-10 06:53:48 +00:00
|
|
|
peerInfo = PeerInfo.init(seckey, [address])
|
|
|
|
mplexProvider = newMuxerProvider(createMplex, MplexCodec)
|
2020-05-26 03:55:53 +00:00
|
|
|
transports = @[Transport(TcpTransport.init(transportFlags))]
|
2020-05-10 06:53:48 +00:00
|
|
|
muxers = {MplexCodec: mplexProvider}.toTable
|
|
|
|
identify = newIdentify(peerInfo)
|
2020-06-15 03:05:37 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
secureManagerInstances: seq[Secure]
|
|
|
|
for sec in secureManagers:
|
|
|
|
case sec
|
|
|
|
of SecureProtocol.Noise:
|
2020-07-13 10:08:03 +00:00
|
|
|
secureManagerInstances &= newNoise(rng, seckey).Secure
|
2020-06-15 03:05:37 +00:00
|
|
|
of SecureProtocol.Secio:
|
2020-07-13 10:08:03 +00:00
|
|
|
secureManagerInstances &= newSecio(rng, seckey).Secure
|
2020-06-15 03:05:37 +00:00
|
|
|
|
2020-06-02 11:27:53 +00:00
|
|
|
let pubSub = PubSub newPubSub(WakuSub, peerInfo, triggerSelf)
|
2020-05-10 06:53:48 +00:00
|
|
|
|
|
|
|
result = newSwitch(peerInfo,
|
|
|
|
transports,
|
|
|
|
identify,
|
|
|
|
muxers,
|
2020-06-15 03:05:37 +00:00
|
|
|
secureManagers = secureManagerInstances,
|
2020-05-10 06:53:48 +00:00
|
|
|
pubSub = some(pubSub))
|