2023-04-05 14:01:51 +00:00
|
|
|
import
|
|
|
|
std/options,
|
|
|
|
stew/results,
|
|
|
|
stew/shims/net,
|
|
|
|
chronos,
|
|
|
|
libp2p/switch,
|
|
|
|
libp2p/builders,
|
|
|
|
libp2p/nameresolving/nameresolver,
|
|
|
|
libp2p/crypto/crypto as libp2p_keys,
|
|
|
|
eth/keys as eth_keys
|
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../../../waku/waku_node,
|
|
|
|
../../../waku/node/peer_manager,
|
|
|
|
../../../waku/waku_enr,
|
|
|
|
../../../waku/waku_discv5,
|
2023-09-29 13:30:07 +00:00
|
|
|
../../apps/wakunode2/external_config,
|
2023-09-27 13:02:24 +00:00
|
|
|
../../apps/wakunode2/internal_config,
|
2023-04-05 14:01:51 +00:00
|
|
|
./common
|
|
|
|
|
|
|
|
|
|
|
|
# Waku node
|
|
|
|
|
2023-09-29 13:30:07 +00:00
|
|
|
proc defaultTestWakuNodeConf*(): WakuNodeConf =
|
|
|
|
WakuNodeConf(
|
2023-11-09 09:48:39 +00:00
|
|
|
cmd: noCommand,
|
2023-09-29 13:30:07 +00:00
|
|
|
tcpPort: Port(60000),
|
|
|
|
websocketPort: Port(8000),
|
|
|
|
listenAddress: ValidIpAddress.init("0.0.0.0"),
|
|
|
|
rpcAddress: ValidIpAddress.init("127.0.0.1"),
|
|
|
|
restAddress: ValidIpAddress.init("127.0.0.1"),
|
|
|
|
metricsServerAddress: ValidIpAddress.init("127.0.0.1"),
|
|
|
|
dnsAddrsNameServers: @[ValidIpAddress.init("1.1.1.1"), ValidIpAddress.init("1.0.0.1")],
|
|
|
|
nat: "any",
|
|
|
|
maxConnections: 50,
|
|
|
|
topics: @["/waku/2/default-waku/proto"],
|
|
|
|
relay: true
|
|
|
|
)
|
|
|
|
|
2023-04-05 14:01:51 +00:00
|
|
|
proc newTestWakuNode*(nodeKey: crypto.PrivateKey,
|
|
|
|
bindIp: ValidIpAddress,
|
|
|
|
bindPort: Port,
|
|
|
|
extIp = none(ValidIpAddress),
|
|
|
|
extPort = none(Port),
|
|
|
|
extMultiAddrs = newSeq[MultiAddress](),
|
|
|
|
peerStorage: PeerStorage = nil,
|
|
|
|
maxConnections = builders.MaxConnections,
|
|
|
|
wsBindPort: Port = (Port)8000,
|
|
|
|
wsEnabled: bool = false,
|
|
|
|
wssEnabled: bool = false,
|
|
|
|
secureKey: string = "",
|
|
|
|
secureCert: string = "",
|
|
|
|
wakuFlags = none(CapabilitiesBitfield),
|
|
|
|
nameResolver: NameResolver = nil,
|
|
|
|
sendSignedPeerRecord = false,
|
|
|
|
dns4DomainName = none(string),
|
|
|
|
discv5UdpPort = none(Port),
|
|
|
|
agentString = none(string),
|
2023-10-11 06:58:45 +00:00
|
|
|
clusterId: uint32 = 0.uint32,
|
2023-04-05 14:01:51 +00:00
|
|
|
peerStoreCapacity = none(int)): WakuNode =
|
2023-10-11 06:58:45 +00:00
|
|
|
|
2023-09-27 13:02:24 +00:00
|
|
|
var resolvedExtIp = extIp
|
|
|
|
|
2023-10-11 06:58:45 +00:00
|
|
|
# Update extPort to default value if it's missing and there's an extIp or a DNS domain
|
2023-09-27 13:02:24 +00:00
|
|
|
let extPort = if (extIp.isSome() or dns4DomainName.isSome()) and
|
|
|
|
extPort.isNone():
|
|
|
|
some(Port(60000))
|
|
|
|
else:
|
|
|
|
extPort
|
2023-10-11 06:58:45 +00:00
|
|
|
|
2023-09-27 13:02:24 +00:00
|
|
|
if dns4DomainName.isSome() and extIp.isNone():
|
|
|
|
let conf = defaultTestWakuNodeConf()
|
2023-10-11 06:58:45 +00:00
|
|
|
# If there's an error resolving the IP, an exception is thrown and test fails
|
2023-09-27 13:02:24 +00:00
|
|
|
let dnsRes = waitFor dnsResolve(dns4DomainName.get(), conf)
|
|
|
|
if dnsRes.isErr():
|
|
|
|
raise newException(Defect, $dnsRes.error)
|
|
|
|
else:
|
|
|
|
resolvedExtIp = some(ValidIpAddress.init(dnsRes.get()))
|
|
|
|
|
2023-04-05 14:01:51 +00:00
|
|
|
let netConfigRes = NetConfig.init(
|
|
|
|
bindIp = bindIp,
|
2023-10-11 06:58:45 +00:00
|
|
|
clusterId = clusterId,
|
2023-04-05 14:01:51 +00:00
|
|
|
bindPort = bindPort,
|
2023-09-27 13:02:24 +00:00
|
|
|
extIp = resolvedExtIp,
|
2023-04-05 14:01:51 +00:00
|
|
|
extPort = extPort,
|
|
|
|
extMultiAddrs = extMultiAddrs,
|
|
|
|
wsBindPort = wsBindPort,
|
|
|
|
wsEnabled = wsEnabled,
|
|
|
|
wssEnabled = wssEnabled,
|
|
|
|
wakuFlags = wakuFlags,
|
|
|
|
dns4DomainName = dns4DomainName,
|
|
|
|
discv5UdpPort = discv5UdpPort,
|
|
|
|
)
|
2023-06-28 12:57:10 +00:00
|
|
|
let netConf =
|
|
|
|
if netConfigRes.isErr():
|
|
|
|
raise newException(Defect, "Invalid network configuration: " & $netConfigRes.error)
|
|
|
|
else:
|
|
|
|
netConfigRes.get()
|
|
|
|
|
|
|
|
var enrBuilder = EnrBuilder.init(nodeKey)
|
|
|
|
|
|
|
|
enrBuilder.withIpAddressAndPorts(
|
|
|
|
ipAddr = netConf.enrIp,
|
|
|
|
tcpPort = netConf.enrPort,
|
|
|
|
udpPort = netConf.discv5UdpPort,
|
|
|
|
)
|
|
|
|
if netConf.wakuFlags.isSome():
|
|
|
|
enrBuilder.withWakuCapabilities(netConf.wakuFlags.get())
|
|
|
|
enrBuilder.withMultiaddrs(netConf.enrMultiaddrs)
|
|
|
|
|
|
|
|
let recordRes = enrBuilder.build()
|
|
|
|
let record =
|
|
|
|
if recordRes.isErr():
|
|
|
|
raise newException(Defect, "Invalid record: " & $recordRes.error)
|
|
|
|
else:
|
|
|
|
recordRes.get()
|
2023-04-05 14:01:51 +00:00
|
|
|
|
|
|
|
var builder = WakuNodeBuilder.init()
|
|
|
|
builder.withRng(rng())
|
|
|
|
builder.withNodeKey(nodeKey)
|
2023-06-28 12:57:10 +00:00
|
|
|
builder.withRecord(record)
|
2023-04-05 14:01:51 +00:00
|
|
|
builder.withNetworkConfiguration(netConfigRes.get())
|
|
|
|
builder.withPeerStorage(peerStorage, capacity = peerStoreCapacity)
|
|
|
|
builder.withSwitchConfiguration(
|
|
|
|
maxConnections = some(maxConnections),
|
|
|
|
nameResolver = nameResolver,
|
|
|
|
sendSignedPeerRecord = sendSignedPeerRecord,
|
|
|
|
secureKey = if secureKey != "": some(secureKey) else: none(string),
|
|
|
|
secureCert = if secureCert != "": some(secureCert) else: none(string),
|
|
|
|
agentString = agentString,
|
2023-06-23 13:30:28 +00:00
|
|
|
|
2023-04-05 14:01:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return builder.build().get()
|