nwaku/tests/v2/standard_setup.nim

61 lines
2.3 KiB
Nim
Raw Normal View History

# compile time options here
const
libp2p_secure {.strdefine.} = ""
libp2p_pubsub_sign {.booldefine.} = true
libp2p_pubsub_verify {.booldefine.} = true
import
options, tables, chronicles, chronos,
libp2p/[switch, peer, peerinfo, connection, multiaddress, crypto/crypto],
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-05-14 02:42:04 +00:00
../../waku/protocol/v2/waku_protocol
when libp2p_secure == "noise":
import libp2p/protocols/secure/noise
else:
import libp2p/protocols/secure/secio
export
switch, peer, peerinfo, connection, multiaddress, crypto
proc newStandardSwitch*(privKey = none(PrivateKey),
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
triggerSelf = false,
gossip = false,
verifySignature = libp2p_pubsub_verify,
sign = libp2p_pubsub_sign,
transportFlags: set[ServerFlags] = {}): Switch =
info "newStandardSwitch"
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)
let
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA).tryGet())
peerInfo = PeerInfo.init(seckey, [address])
mplexProvider = newMuxerProvider(createMplex, MplexCodec)
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
let pubSub = if gossip:
PubSub newPubSub(GossipSub, peerInfo, triggerSelf)
else:
# Creating switch from generate node
# XXX: Hacky test, hijacking WakuSub here
2020-05-15 03:35:32 +00:00
debug "Using WakuSub here"
2020-05-14 03:18:20 +00:00
#PubSub newPubSub(FloodSub, peerInfo, triggerSelf)
PubSub newPubSub(WakuSub, peerInfo, triggerSelf)
result = newSwitch(peerInfo,
transports,
identify,
muxers,
secureManagers = secureManagers,
pubSub = some(pubSub))