mirror of
https://github.com/waku-org/nwaku.git
synced 2025-02-27 14:31:01 +00:00
Import standard_setup and utils
Will be used for new PubSub protocol
This commit is contained in:
parent
eb880f5b8b
commit
824bef65ad
50
waku/protocol/v2/standard_setup.nim
Normal file
50
waku/protocol/v2/standard_setup.nim
Normal file
@ -0,0 +1,50 @@
|
||||
# compile time options here
|
||||
const
|
||||
libp2p_secure {.strdefine.} = ""
|
||||
|
||||
import
|
||||
options, tables,
|
||||
libp2p/[switch, peer, peerinfo, connection, multiaddress, crypto/crypto],
|
||||
libp2p/transports/[transport, tcptransport],
|
||||
libp2p/muxers/[muxer, mplex/mplex, mplex/types],
|
||||
libp2p/protocols/[identify, secure/secure],
|
||||
libp2p/protocols/pubsub/[pubsub, gossipsub, floodsub]
|
||||
|
||||
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"),
|
||||
triggerSelf = false,
|
||||
gossip = false): Switch =
|
||||
proc createMplex(conn: Connection): Muxer =
|
||||
result = newMplex(conn)
|
||||
|
||||
let
|
||||
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA))
|
||||
peerInfo = PeerInfo.init(seckey, [address])
|
||||
mplexProvider = newMuxerProvider(createMplex, MplexCodec)
|
||||
transports = @[Transport(newTransport(TcpTransport))]
|
||||
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
|
||||
PubSub newPubSub(FloodSub, peerInfo, triggerSelf)
|
||||
|
||||
result = newSwitch(peerInfo,
|
||||
transports,
|
||||
identify,
|
||||
muxers,
|
||||
secureManagers = secureManagers,
|
||||
pubSub = some(pubSub))
|
15
waku/protocol/v2/utils.nim
Normal file
15
waku/protocol/v2/utils.nim
Normal file
@ -0,0 +1,15 @@
|
||||
import chronos
|
||||
import standard_setup
|
||||
export standard_setup
|
||||
|
||||
proc generateNodes*(num: Natural, gossip: bool = false): seq[Switch] =
|
||||
for i in 0..<num:
|
||||
result.add(newStandardSwitch(gossip = gossip))
|
||||
|
||||
proc subscribeNodes*(nodes: seq[Switch]) {.async.} =
|
||||
var dials: seq[Future[void]]
|
||||
for dialer in nodes:
|
||||
for node in nodes:
|
||||
if dialer.peerInfo.peerId != node.peerInfo.peerId:
|
||||
dials.add(dialer.connect(node.peerInfo))
|
||||
await allFutures(dials)
|
Loading…
x
Reference in New Issue
Block a user