2023-04-05 16:01:51 +02:00
|
|
|
import
|
|
|
|
|
std/options,
|
2025-04-11 17:20:23 +02:00
|
|
|
results,
|
2023-04-05 16:01:51 +02:00
|
|
|
chronos,
|
|
|
|
|
libp2p/switch,
|
|
|
|
|
libp2p/builders,
|
|
|
|
|
libp2p/nameresolving/nameresolver,
|
|
|
|
|
libp2p/crypto/crypto as libp2p_keys,
|
|
|
|
|
eth/keys as eth_keys
|
|
|
|
|
import
|
2024-07-06 03:33:38 +05:30
|
|
|
waku/[
|
|
|
|
|
waku_node,
|
|
|
|
|
waku_core/topics,
|
|
|
|
|
node/peer_manager,
|
|
|
|
|
waku_enr,
|
|
|
|
|
discovery/waku_discv5,
|
|
|
|
|
factory/internal_config,
|
2025-05-08 07:05:35 +10:00
|
|
|
factory/waku_conf,
|
|
|
|
|
factory/conf_builder/conf_builder,
|
2024-07-06 03:33:38 +05:30
|
|
|
factory/builder,
|
|
|
|
|
],
|
2023-04-05 16:01:51 +02:00
|
|
|
./common
|
|
|
|
|
|
|
|
|
|
# Waku node
|
|
|
|
|
|
2025-05-08 07:05:35 +10:00
|
|
|
# TODO: migrate to usage of a test cluster conf
|
|
|
|
|
proc defaultTestWakuConfBuilder*(): WakuConfBuilder =
|
|
|
|
|
var builder = WakuConfBuilder.init()
|
|
|
|
|
builder.withP2pTcpPort(Port(60000))
|
|
|
|
|
builder.withP2pListenAddress(parseIpAddress("0.0.0.0"))
|
|
|
|
|
builder.restServerConf.withListenAddress(parseIpAddress("127.0.0.1"))
|
|
|
|
|
builder.withDnsAddrsNameServers(
|
|
|
|
|
@[parseIpAddress("1.1.1.1"), parseIpAddress("1.0.0.1")]
|
2023-09-29 16:30:07 +03:00
|
|
|
)
|
2025-05-08 07:05:35 +10:00
|
|
|
builder.withNatStrategy("any")
|
|
|
|
|
builder.withMaxConnections(50)
|
|
|
|
|
builder.withRelayServiceRatio("60:40")
|
|
|
|
|
builder.withMaxMessageSize("1024 KiB")
|
|
|
|
|
builder.withClusterId(DefaultClusterId)
|
|
|
|
|
builder.withShards(@[DefaultShardId])
|
|
|
|
|
builder.withRelay(true)
|
|
|
|
|
builder.withRendezvous(true)
|
|
|
|
|
builder.storeServiceConf.withDbMigration(false)
|
|
|
|
|
builder.storeServiceConf.withSupportV2(false)
|
|
|
|
|
builder.webSocketConf.withWebSocketPort(Port(8000))
|
|
|
|
|
builder.webSocketConf.withEnabled(true)
|
|
|
|
|
return builder
|
|
|
|
|
|
|
|
|
|
proc defaultTestWakuConf*(): WakuConf =
|
|
|
|
|
var builder = defaultTestWakuConfBuilder()
|
|
|
|
|
return builder.build().value
|
2023-09-29 16:30:07 +03:00
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
proc newTestWakuNode*(
|
|
|
|
|
nodeKey: crypto.PrivateKey,
|
|
|
|
|
bindIp: IpAddress,
|
|
|
|
|
bindPort: Port,
|
|
|
|
|
extIp = none(IpAddress),
|
|
|
|
|
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),
|
|
|
|
|
peerStoreCapacity = none(int),
|
2024-09-10 15:07:12 -06:00
|
|
|
clusterId = DefaultClusterId,
|
|
|
|
|
shards = @[DefaultShardId],
|
2024-03-16 00:08:47 +01:00
|
|
|
): WakuNode =
|
2023-09-27 16:02:24 +03:00
|
|
|
var resolvedExtIp = extIp
|
|
|
|
|
|
2023-10-11 08:58:45 +02:00
|
|
|
# Update extPort to default value if it's missing and there's an extIp or a DNS domain
|
2023-11-21 15:15:39 -05:00
|
|
|
let extPort =
|
2024-03-16 00:08:47 +01:00
|
|
|
if (extIp.isSome() or dns4DomainName.isSome()) and extPort.isNone():
|
|
|
|
|
some(Port(60000))
|
|
|
|
|
else:
|
|
|
|
|
extPort
|
2023-11-21 15:15:39 -05:00
|
|
|
|
2025-05-08 07:05:35 +10:00
|
|
|
var conf = defaultTestWakuConf()
|
2024-01-30 07:28:21 -05:00
|
|
|
|
|
|
|
|
conf.clusterId = clusterId
|
2024-09-10 15:07:12 -06:00
|
|
|
conf.shards = shards
|
2023-10-11 08:58:45 +02:00
|
|
|
|
2023-09-27 16:02:24 +03:00
|
|
|
if dns4DomainName.isSome() and extIp.isNone():
|
2023-10-11 08:58:45 +02:00
|
|
|
# If there's an error resolving the IP, an exception is thrown and test fails
|
2025-05-08 07:05:35 +10:00
|
|
|
let dns = (waitFor dnsResolve(dns4DomainName.get(), conf.dnsAddrsNameServers)).valueOr:
|
2023-11-21 15:15:39 -05:00
|
|
|
raise newException(Defect, error)
|
2024-03-16 00:08:47 +01:00
|
|
|
|
2023-12-14 07:16:39 +01:00
|
|
|
resolvedExtIp = some(parseIpAddress(dns))
|
2023-09-27 16:02:24 +03:00
|
|
|
|
2023-11-21 15:15:39 -05:00
|
|
|
let netConf = NetConfig.init(
|
2024-09-10 15:07:12 -06:00
|
|
|
clusterId = conf.clusterId,
|
2025-05-08 07:05:35 +10:00
|
|
|
bindIp = bindIp,
|
2023-04-05 16:01:51 +02:00
|
|
|
bindPort = bindPort,
|
2023-09-27 16:02:24 +03:00
|
|
|
extIp = resolvedExtIp,
|
2023-04-05 16:01:51 +02:00
|
|
|
extPort = extPort,
|
|
|
|
|
extMultiAddrs = extMultiAddrs,
|
2025-05-08 07:05:35 +10:00
|
|
|
wsBindPort = some(wsBindPort),
|
2023-04-05 16:01:51 +02:00
|
|
|
wsEnabled = wsEnabled,
|
|
|
|
|
wssEnabled = wssEnabled,
|
|
|
|
|
dns4DomainName = dns4DomainName,
|
|
|
|
|
discv5UdpPort = discv5UdpPort,
|
2025-05-08 07:05:35 +10:00
|
|
|
wakuFlags = wakuFlags,
|
2023-11-21 15:15:39 -05:00
|
|
|
).valueOr:
|
|
|
|
|
raise newException(Defect, "Invalid network configuration: " & error)
|
2023-06-28 08:57:10 -04:00
|
|
|
|
|
|
|
|
var enrBuilder = EnrBuilder.init(nodeKey)
|
|
|
|
|
|
2024-09-10 15:07:12 -06:00
|
|
|
enrBuilder.withWakuRelaySharding(
|
|
|
|
|
RelayShards(clusterId: conf.clusterId, shardIds: conf.shards)
|
|
|
|
|
).isOkOr:
|
|
|
|
|
raise newException(Defect, "Invalid record: " & $error)
|
2023-11-21 15:15:39 -05:00
|
|
|
|
2023-06-28 08:57:10 -04:00
|
|
|
enrBuilder.withIpAddressAndPorts(
|
2024-03-16 00:08:47 +01:00
|
|
|
ipAddr = netConf.enrIp, tcpPort = netConf.enrPort, udpPort = netConf.discv5UdpPort
|
2023-06-28 08:57:10 -04:00
|
|
|
)
|
2023-11-21 15:15:39 -05:00
|
|
|
|
|
|
|
|
enrBuilder.withMultiaddrs(netConf.enrMultiaddrs)
|
|
|
|
|
|
2023-06-28 08:57:10 -04:00
|
|
|
if netConf.wakuFlags.isSome():
|
|
|
|
|
enrBuilder.withWakuCapabilities(netConf.wakuFlags.get())
|
|
|
|
|
|
2023-11-21 15:15:39 -05:00
|
|
|
let record = enrBuilder.build().valueOr:
|
2024-03-16 00:08:47 +01:00
|
|
|
raise newException(Defect, "Invalid record: " & $error)
|
2023-04-05 16:01:51 +02:00
|
|
|
|
|
|
|
|
var builder = WakuNodeBuilder.init()
|
|
|
|
|
builder.withRng(rng())
|
|
|
|
|
builder.withNodeKey(nodeKey)
|
2023-06-28 08:57:10 -04:00
|
|
|
builder.withRecord(record)
|
2023-11-21 15:15:39 -05:00
|
|
|
builder.withNetworkConfiguration(netConf)
|
2023-04-05 16:01:51 +02:00
|
|
|
builder.withPeerStorage(peerStorage, capacity = peerStoreCapacity)
|
|
|
|
|
builder.withSwitchConfiguration(
|
|
|
|
|
maxConnections = some(maxConnections),
|
|
|
|
|
nameResolver = nameResolver,
|
|
|
|
|
sendSignedPeerRecord = sendSignedPeerRecord,
|
2024-03-16 00:08:47 +01:00
|
|
|
secureKey =
|
|
|
|
|
if secureKey != "":
|
|
|
|
|
some(secureKey)
|
|
|
|
|
else:
|
2024-09-11 11:51:42 +10:00
|
|
|
none(string),
|
2024-03-16 00:08:47 +01:00
|
|
|
secureCert =
|
|
|
|
|
if secureCert != "":
|
|
|
|
|
some(secureCert)
|
|
|
|
|
else:
|
2024-09-11 11:51:42 +10:00
|
|
|
none(string),
|
2023-04-05 16:01:51 +02:00
|
|
|
agentString = agentString,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return builder.build().get()
|