diff --git a/Makefile b/Makefile index 28a4d302d..daa8a8ad7 100644 --- a/Makefile +++ b/Makefile @@ -147,6 +147,9 @@ NIM_PARAMS := $(NIM_PARAMS) -d:disable_libbacktrace # enable experimental exit is dest feature in libp2p mix NIM_PARAMS := $(NIM_PARAMS) -d:libp2p_mix_experimental_exit_is_dest +# enable libp2p's QUIC transport +NIM_PARAMS := $(NIM_PARAMS) -d:libp2p_quic_support + ifeq ($(POSTGRES), 1) NIM_PARAMS := $(NIM_PARAMS) -d:postgres -d:nimDebugDlOpen endif diff --git a/apps/chat2/chat2.nim b/apps/chat2/chat2.nim index 23ab1f03e..968d4582a 100644 --- a/apps/chat2/chat2.nim +++ b/apps/chat2/chat2.nim @@ -305,7 +305,7 @@ proc readInput(wfd: AsyncFD) {.thread, raises: [Defect, CatchableError].} = {.pop.} # @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError -proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} = +proc processInput(rfd: AsyncFD, rng: crypto.Rng) {.async.} = let transp = fromPipe(rfd) conf = Chat2Conf.load() @@ -313,7 +313,7 @@ proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} = if conf.nodekey.isSome(): conf.nodekey.get() else: - PrivateKey.random(Secp256k1, rng[]).tryGet() + PrivateKey.random(Secp256k1, rng).tryGet() # set log level if conf.logLevel != LogLevel.NONE: @@ -568,7 +568,7 @@ proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} = runForever() -proc main(rng: ref HmacDrbgContext) {.async.} = +proc main(rng: crypto.Rng) {.async.} = let (rfd, wfd) = createAsyncPipe() if rfd == asyncInvalidPipe or wfd == asyncInvalidPipe: raise newException(ValueError, "Could not initialize pipe!") diff --git a/apps/chat2bridge/chat2bridge.nim b/apps/chat2bridge/chat2bridge.nim index 4e8842dab..e97eba033 100644 --- a/apps/chat2bridge/chat2bridge.nim +++ b/apps/chat2bridge/chat2bridge.nim @@ -272,7 +272,7 @@ when isMainModule: if conf.nodekey.isSome(): conf.nodekey.get() else: - crypto.PrivateKey.random(Secp256k1, rng[]).tryGet() + crypto.PrivateKey.random(Secp256k1, rng).tryGet() let bridge = Chat2Matterbridge.new( mbHostUri = "http://" & $initTAddress(conf.mbHostAddress, Port(conf.mbHostPort)), diff --git a/apps/chat2mix/chat2mix.nim b/apps/chat2mix/chat2mix.nim index 8f48396cf..9d9c723e0 100644 --- a/apps/chat2mix/chat2mix.nim +++ b/apps/chat2mix/chat2mix.nim @@ -388,7 +388,7 @@ proc maintainSubscription( {.pop.} # @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError -proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} = +proc processInput(rfd: AsyncFD, rng: crypto.Rng) {.async.} = let transp = fromPipe(rfd) conf = Chat2Conf.load() @@ -396,7 +396,7 @@ proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} = if conf.nodekey.isSome(): conf.nodekey.get() else: - PrivateKey.random(Secp256k1, rng[]).tryGet() + PrivateKey.random(Secp256k1, rng).tryGet() # set log level if conf.logLevel != LogLevel.NONE: @@ -660,7 +660,7 @@ proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} = runForever() -proc main(rng: ref HmacDrbgContext) {.async.} = +proc main(rng: crypto.Rng) {.async.} = let (rfd, wfd) = createAsyncPipe() if rfd == asyncInvalidPipe or wfd == asyncInvalidPipe: raise newException(ValueError, "Could not initialize pipe!") diff --git a/apps/networkmonitor/networkmonitor.nim b/apps/networkmonitor/networkmonitor.nim index 4fed6da01..4c2d047ae 100644 --- a/apps/networkmonitor/networkmonitor.nim +++ b/apps/networkmonitor/networkmonitor.nim @@ -423,7 +423,7 @@ proc initAndStartApp( let # some hardcoded parameters rng = keys.newRng() - key = crypto.PrivateKey.random(Secp256k1, rng[])[] + key = crypto.PrivateKey.random(Secp256k1, rng)[] nodeTcpPort = Port(60000) nodeUdpPort = Port(9000) flags = CapabilitiesBitfield.init( diff --git a/apps/wakucanary/wakucanary.nim b/apps/wakucanary/wakucanary.nim index 7d5581d8e..44a3548d6 100644 --- a/apps/wakucanary/wakucanary.nim +++ b/apps/wakucanary/wakucanary.nim @@ -161,7 +161,7 @@ proc pingNode( error "Failed to ping the peer", peer = peerInfo, err = msg return false -proc main(rng: ref HmacDrbgContext): Future[int] {.async.} = +proc main(rng: Rng): Future[int] {.async.} = let conf: WakuCanaryConf = WakuCanaryConf.load() # create dns resolver @@ -192,7 +192,7 @@ proc main(rng: ref HmacDrbgContext): Future[int] {.async.} = quit(QuitFailure) let - nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[] + nodeKey = crypto.PrivateKey.random(Secp256k1, rng)[] bindIp = parseIpAddress("0.0.0.0") wsBindPort = Port(conf.nodePort + WebSocketPortOffset) nodeTcpPort = Port(conf.nodePort) diff --git a/examples/lightpush_mix/lightpush_publisher_mix.nim b/examples/lightpush_mix/lightpush_publisher_mix.nim index ae022e226..573d6549e 100644 --- a/examples/lightpush_mix/lightpush_publisher_mix.nim +++ b/examples/lightpush_mix/lightpush_publisher_mix.nim @@ -7,8 +7,8 @@ import confutils, libp2p/crypto/crypto, libp2p/crypto/curve25519, - libp2p/protocols/mix, - libp2p/protocols/mix/curve25519, + libp2p_mix, + libp2p_mix/curve25519, libp2p/multiaddress, eth/keys, eth/p2p/discoveryv5/enr, diff --git a/library/kernel_api/discovery_api.nim b/library/kernel_api/discovery_api.nim index 847e21efb..9dec4575a 100644 --- a/library/kernel_api/discovery_api.nim +++ b/library/kernel_api/discovery_api.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/json import chronos, chronicles, results, strutils, libp2p/multiaddress, ffi import diff --git a/library/kernel_api/node_lifecycle_api.nim b/library/kernel_api/node_lifecycle_api.nim index 71e3805ea..8ee735f52 100644 --- a/library/kernel_api/node_lifecycle_api.nim +++ b/library/kernel_api/node_lifecycle_api.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[options, json, strutils, net] import chronos, chronicles, results, confutils, confutils/std/net, ffi diff --git a/library/kernel_api/peer_manager_api.nim b/library/kernel_api/peer_manager_api.nim index 0a5bae3ea..775e58cae 100644 --- a/library/kernel_api/peer_manager_api.nim +++ b/library/kernel_api/peer_manager_api.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[sequtils, strutils, tables] import chronicles, chronos, results, options, json, ffi import diff --git a/library/kernel_api/protocols/filter_api.nim b/library/kernel_api/protocols/filter_api.nim index ac1f372be..5e2628953 100644 --- a/library/kernel_api/protocols/filter_api.nim +++ b/library/kernel_api/protocols/filter_api.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import options, std/[strutils, sequtils] import chronicles, chronos, results, ffi import diff --git a/library/kernel_api/protocols/lightpush_api.nim b/library/kernel_api/protocols/lightpush_api.nim index da32ab7b2..dc9197481 100644 --- a/library/kernel_api/protocols/lightpush_api.nim +++ b/library/kernel_api/protocols/lightpush_api.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import options, std/[json, strformat] import chronicles, chronos, results, ffi import diff --git a/library/kernel_api/protocols/relay_api.nim b/library/kernel_api/protocols/relay_api.nim index 739856104..ae7e29ceb 100644 --- a/library/kernel_api/protocols/relay_api.nim +++ b/library/kernel_api/protocols/relay_api.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[net, sequtils, strutils, json], strformat import chronicles, chronos, stew/byteutils, results, ffi import diff --git a/library/kernel_api/protocols/store_api.nim b/library/kernel_api/protocols/store_api.nim index c66b64839..22d514d14 100644 --- a/library/kernel_api/protocols/store_api.nim +++ b/library/kernel_api/protocols/store_api.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[json, sugar, strutils, options] import chronos, chronicles, results, stew/byteutils, ffi import diff --git a/library/liblogosdelivery.nim b/library/liblogosdelivery.nim index 86d8e0948..08541634a 100644 --- a/library/liblogosdelivery.nim +++ b/library/liblogosdelivery.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[atomics, options, macros] import chronicles, chronos, chronos/threadsync, ffi import diff --git a/logos_delivery.nimble b/logos_delivery.nimble index dce50ccf8..8f9bbd8d9 100644 --- a/logos_delivery.nimble +++ b/logos_delivery.nimble @@ -28,7 +28,7 @@ requires "nim >= 2.2.4", "toml_serialization", "faststreams", # Networking & P2P - "https://github.com/vacp2p/nim-libp2p.git#ff8d51857b4b79a68468e7bcc27b2026cca02996", + "https://github.com/vacp2p/nim-libp2p.git#v2.0.0", "eth", "nat_traversal", "dnsdisc", @@ -40,7 +40,7 @@ requires "nim >= 2.2.4", "secp256k1", "bearssl", # RPC & APIs - "https://github.com/status-im/nim-json-rpc.git#43bbf499143eb45046c83ac9794c9e3280a2b8e7", + "https://github.com/status-im/nim-json-rpc.git#v0.6.1", "presto", "web3", # Database @@ -67,8 +67,9 @@ requires "https://github.com/logos-messaging/nim-sds.git#abdd40cc645f1b024c3ee99 requires "https://github.com/NagyZoltanPeter/nim-brokers.git#v3.1.1" -requires "https://github.com/vacp2p/nim-lsquic" +requires "https://github.com/vacp2p/nim-lsquic.git#v0.5.1" requires "https://github.com/vacp2p/nim-jwt.git#057ec95eb5af0eea9c49bfe9025b3312c95dc5f2" +requires "https://github.com/logos-co/nim-libp2p-mix#380513117d556bf8f70066f5e72a7fd74fe36ba6" proc getMyCPU(): string = ## Need to set cpu more explicit manner to avoid arch issues between dependencies diff --git a/logos_delivery/channels/reliable_channel.nim b/logos_delivery/channels/reliable_channel.nim index 4f4851aca..5edacc619 100644 --- a/logos_delivery/channels/reliable_channel.nim +++ b/logos_delivery/channels/reliable_channel.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## Reliable Channel type. ## ## A `ReliableChannel` orchestrates segmentation, SDS (end-to-end @@ -93,7 +94,7 @@ type channelId: ChannelId contentTopic: ContentTopic senderId: SdsParticipantID - rng: ref HmacDrbgContext + rng: libp2p_crypto.Rng segmentation: SegmentationHandler sdsHandler: SdsHandler rateLimit: RateLimitManager diff --git a/logos_delivery/messaging/delivery_service/recv_service/recv_service.nim b/logos_delivery/messaging/delivery_service/recv_service/recv_service.nim index 09bcaa815..55446c2a9 100644 --- a/logos_delivery/messaging/delivery_service/recv_service/recv_service.nim +++ b/logos_delivery/messaging/delivery_service/recv_service/recv_service.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## This module is in charge of taking care of the messages that this node is expecting to ## receive and is backed by store-v3 requests to get an additional degree of certainty ## diff --git a/logos_delivery/messaging/delivery_service/send_service/delivery_task.nim b/logos_delivery/messaging/delivery_service/send_service/delivery_task.nim index e56536baa..1e38fcee1 100644 --- a/logos_delivery/messaging/delivery_service/send_service/delivery_task.nim +++ b/logos_delivery/messaging/delivery_service/send_service/delivery_task.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[options, times], chronos import brokers/broker_context import diff --git a/logos_delivery/messaging/delivery_service/send_service/lightpush_processor.nim b/logos_delivery/messaging/delivery_service/send_service/lightpush_processor.nim index 100c27f52..4105ba48d 100644 --- a/logos_delivery/messaging/delivery_service/send_service/lightpush_processor.nim +++ b/logos_delivery/messaging/delivery_service/send_service/lightpush_processor.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import chronicles, chronos, results import std/options import brokers/broker_context diff --git a/logos_delivery/messaging/delivery_service/send_service/relay_processor.nim b/logos_delivery/messaging/delivery_service/send_service/relay_processor.nim index 2fdc39f8e..23b5b846c 100644 --- a/logos_delivery/messaging/delivery_service/send_service/relay_processor.nim +++ b/logos_delivery/messaging/delivery_service/send_service/relay_processor.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/options import chronos, chronicles import brokers/broker_context diff --git a/logos_delivery/messaging/delivery_service/send_service/send_service.nim b/logos_delivery/messaging/delivery_service/send_service/send_service.nim index 43b73fa26..92026be5d 100644 --- a/logos_delivery/messaging/delivery_service/send_service/send_service.nim +++ b/logos_delivery/messaging/delivery_service/send_service/send_service.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## This module reinforces the publish operation with regular store-v3 requests. ## diff --git a/logos_delivery/waku/api/api.nim b/logos_delivery/waku/api/api.nim index 74cf5888b..5be1e2086 100644 --- a/logos_delivery/waku/api/api.nim +++ b/logos_delivery/waku/api/api.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[net, options] import chronicles, chronos, libp2p/peerid, results diff --git a/logos_delivery/waku/api/api_conf.nim b/logos_delivery/waku/api/api_conf.nim index b01fae8bd..5810b1eb2 100644 --- a/logos_delivery/waku/api/api_conf.nim +++ b/logos_delivery/waku/api/api_conf.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[net, options] import results diff --git a/logos_delivery/waku/api/types.nim b/logos_delivery/waku/api/types.nim index 6b2b621de..d51d46994 100644 --- a/logos_delivery/waku/api/types.nim +++ b/logos_delivery/waku/api/types.nim @@ -1,3 +1,5 @@ +import logos_delivery/waku/compat/option_valueor +import libp2p/crypto/crypto {.push raises: [].} import bearssl/rand, std/times, chronos @@ -23,7 +25,7 @@ type PartiallyConnected Connected -proc new*(T: typedesc[RequestId], rng: ref HmacDrbgContext): T = +proc new*(T: typedesc[RequestId], rng: crypto.Rng): T = ## Generate a new RequestId using the provided RNG. RequestId(request_utils.generateRequestId(rng)) diff --git a/logos_delivery/waku/common/databases/db_postgres/dbconn.nim b/logos_delivery/waku/common/databases/db_postgres/dbconn.nim index f24f3d4dd..ce0eaa213 100644 --- a/logos_delivery/waku/common/databases/db_postgres/dbconn.nim +++ b/logos_delivery/waku/common/databases/db_postgres/dbconn.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[times, strutils, os, sets, strformat, tables], results, diff --git a/logos_delivery/waku/common/databases/db_postgres/pgasyncpool.nim b/logos_delivery/waku/common/databases/db_postgres/pgasyncpool.nim index 0b298084e..b04319a65 100644 --- a/logos_delivery/waku/common/databases/db_postgres/pgasyncpool.nim +++ b/logos_delivery/waku/common/databases/db_postgres/pgasyncpool.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor # Simple async pool driver for postgress. # Inspired by: https://github.com/treeform/pg/ {.push raises: [].} diff --git a/logos_delivery/waku/common/databases/db_sqlite.nim b/logos_delivery/waku/common/databases/db_sqlite.nim index e398ea5ac..e8237bfe6 100644 --- a/logos_delivery/waku/common/databases/db_sqlite.nim +++ b/logos_delivery/waku/common/databases/db_sqlite.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} # The code in this file is an adaptation of the Sqlite KV Store found in nim-eth. # https://github.com/status-im/nim-eth/blob/master/eth/db/kvstore_sqlite3.nim diff --git a/logos_delivery/waku/common/enr/typed_record.nim b/logos_delivery/waku/common/enr/typed_record.nim index 1db357621..94353d01d 100644 --- a/logos_delivery/waku/common/enr/typed_record.nim +++ b/logos_delivery/waku/common/enr/typed_record.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import std/options, results, eth/keys as eth_keys, libp2p/crypto/crypto as libp2p_crypto diff --git a/logos_delivery/waku/common/rate_limit/request_limiter.nim b/logos_delivery/waku/common/rate_limit/request_limiter.nim index bc318e151..9fccf1c29 100644 --- a/logos_delivery/waku/common/rate_limit/request_limiter.nim +++ b/logos_delivery/waku/common/rate_limit/request_limiter.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## RequestRateLimiter ## ## RequestRateLimiter is a general service protection mechanism. diff --git a/logos_delivery/waku/common/utils/parse_size_units.nim b/logos_delivery/waku/common/utils/parse_size_units.nim index 14f41a933..3b8c1d1a7 100644 --- a/logos_delivery/waku/common/utils/parse_size_units.nim +++ b/logos_delivery/waku/common/utils/parse_size_units.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[strutils, math], results, regex proc parseMsgSize*(input: string): Result[uint64, string] = diff --git a/logos_delivery/waku/compat/option_valueor.nim b/logos_delivery/waku/compat/option_valueor.nim new file mode 100644 index 000000000..96a9276d5 --- /dev/null +++ b/logos_delivery/waku/compat/option_valueor.nim @@ -0,0 +1,32 @@ +## Polyfill: `valueOr` / `withValue` templates for `std/options.Option[T]`. +## +## Previously provided transitively by `libp2p/utility`, removed in +## nim-libp2p PR #2162 (commit 8a9943145). logos-delivery uses these +## templates pervasively on `Option[T]`. + +{.push raises: [].} + +import std/[macros, options] + +template valueOr*[T](self: Option[T], body: untyped): untyped = + let temp = (self) + if temp.isSome: + temp.get() + else: + body + +template withValue*[T](self: Option[T], value, body: untyped): untyped = + let temp = (self) + if temp.isSome: + let `value` {.inject.} = temp.get() + body + +macro withValue*[T](self: Option[T], value, body, elseStmt: untyped): untyped = + let elseBody = elseStmt[0] + quote: + let temp = (`self`) + if temp.isSome: + let `value` {.inject.} = temp.get() + `body` + else: + `elseBody` diff --git a/logos_delivery/waku/discovery/autonat_service.nim b/logos_delivery/waku/discovery/autonat_service.nim index fb1f7dbc3..9f6ecaafd 100644 --- a/logos_delivery/waku/discovery/autonat_service.nim +++ b/logos_delivery/waku/discovery/autonat_service.nim @@ -1,3 +1,4 @@ +import libp2p/crypto/crypto import chronos, chronicles, @@ -7,7 +8,7 @@ import const AutonatCheckInterval = Opt.some(chronos.seconds(30)) -proc getAutonatService*(rng: ref HmacDrbgContext): AutonatService = +proc getAutonatService*(rng: crypto.Rng): AutonatService = ## AutonatService request other peers to dial us back ## flagging us as Reachable or NotReachable. ## minConfidence is used as threshold to determine the state. diff --git a/logos_delivery/waku/discovery/waku_discv5.nim b/logos_delivery/waku/discovery/waku_discv5.nim index d1b22a6d4..ade874733 100644 --- a/logos_delivery/waku/discovery/waku_discv5.nim +++ b/logos_delivery/waku/discovery/waku_discv5.nim @@ -1,3 +1,6 @@ +import libp2p/crypto/crypto +import libp2p/crypto/rng +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -82,7 +85,7 @@ proc shardingPredicate*( proc new*( T: type WakuDiscoveryV5, - rng: ref HmacDrbgContext, + rng: crypto.Rng, conf: WakuDiscoveryV5Config, record: Option[waku_enr.Record], peerManager: Option[PeerManager] = none(PeerManager), @@ -90,7 +93,7 @@ proc new*( newAsyncEventQueue[SubscriptionEvent](30), ): T = let protocol = newProtocol( - rng = rng, + rng = rng.bearSslDrbgRef, config = conf.discv5Config.get(protocol.defaultDiscoveryConfig), bindPort = conf.port, bindIp = conf.address, @@ -407,7 +410,7 @@ proc setupDiscoveryV5*( nodeTopicSubscriptionQueue: AsyncEventQueue[SubscriptionEvent], conf: Discv5Conf, dynamicBootstrapNodes: seq[RemotePeerInfo], - rng: ref HmacDrbgContext, + rng: crypto.Rng, key: crypto.PrivateKey, p2pListenAddress: IpAddress, portsShift: uint16, @@ -463,7 +466,7 @@ proc setupAndStartDiscv5*( nodeTopicSubscriptionQueue: AsyncEventQueue[SubscriptionEvent], conf: Discv5Conf, dynamicBootstrapNodes: seq[RemotePeerInfo], - rng: ref HmacDrbgContext, + rng: crypto.Rng, key: crypto.PrivateKey, p2pListenAddress: IpAddress, portsShift: uint16, diff --git a/logos_delivery/waku/discovery/waku_dnsdisc.nim b/logos_delivery/waku/discovery/waku_dnsdisc.nim index 0d0a82948..d0167de06 100644 --- a/logos_delivery/waku/discovery/waku_dnsdisc.nim +++ b/logos_delivery/waku/discovery/waku_dnsdisc.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} ## A set of utilities to integrate EIP-1459 DNS-based discovery diff --git a/logos_delivery/waku/discovery/waku_kademlia.nim b/logos_delivery/waku/discovery/waku_kademlia.nim index 6c64edace..92ff4d94a 100644 --- a/logos_delivery/waku/discovery/waku_kademlia.nim +++ b/logos_delivery/waku/discovery/waku_kademlia.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import std/[options, sequtils] @@ -9,9 +10,9 @@ import libp2p/[peerid, multiaddress, switch], libp2p/extended_peer_record, libp2p/crypto/curve25519, - libp2p/protocols/[kademlia, kad_disco], - libp2p/protocols/kademlia_discovery/types as kad_types, - libp2p/protocols/mix/mix_protocol + libp2p/protocols/[kademlia, service_discovery], + libp2p/protocols/service_discovery/types as svdisc_types, + libp2p_mix/mix_protocol import logos_delivery/waku/waku_core, logos_delivery/waku/node/peer_manager @@ -32,7 +33,7 @@ type advertiseMix*: bool = false WakuKademlia* = ref object - protocol*: KademliaDiscovery + protocol*: ServiceDiscovery peerManager: PeerManager discoveryLoop: Future[void] running*: bool @@ -50,13 +51,15 @@ proc new*( if params.bootstrapNodes.len == 0: info "creating kademlia discovery as seed node (no bootstrap nodes)" - let kademlia = KademliaDiscovery.new( + let kademlia = ServiceDiscovery.new( switch, bootstrapNodes = params.bootstrapNodes, config = KadDHTConfig.new( - validator = kad_types.ExtEntryValidator(), selector = kad_types.ExtEntrySelector() + validator = svdisc_types.ExtEntryValidator(), + selector = svdisc_types.ExtEntrySelector(), ), - codec = ExtendedKademliaDiscoveryCodec, + rng = switch.rng, + codec = ExtendedServiceDiscoveryCodec, ) try: @@ -68,11 +71,9 @@ proc new*( # initial self-signed peer record published to the DHT if params.advertiseMix: if params.mixPubKey.isSome(): - let alreadyAdvertising = kademlia.startAdvertising( + kademlia.startAdvertising( ServiceInfo(id: MixProtocolID, data: @(params.mixPubKey.get())) ) - if alreadyAdvertising: - warn "mix service was already being advertised" debug "extended kademlia advertising mix service", keyHex = byteutils.toHex(params.mixPubKey.get()), bootstrapNodes = params.bootstrapNodes.len @@ -164,7 +165,9 @@ proc lookupMixPeers*( let mixService = ServiceInfo(id: MixProtocolID, data: @[]) var records: seq[ExtendedPeerRecord] try: - records = await wk.protocol.lookup(mixService) + let advertisements = (await wk.protocol.lookup(mixService)).valueOr: + return err("mix peer lookup failed: " & $error) + records = advertisements.mapIt(it.data) except CatchableError: return err("mix peer lookup failed: " & getCurrentExceptionMsg()) @@ -202,7 +205,7 @@ proc runDiscoveryLoop( var records: seq[ExtendedPeerRecord] try: - records = await wk.protocol.randomRecords() + records = await wk.protocol.lookupRandom() except CatchableError as e: warn "extended kademlia discovery failed", error = e.msg await sleepAsync(interval) diff --git a/logos_delivery/waku/factory/builder.nim b/logos_delivery/waku/factory/builder.nim index 953d95a34..aff49e873 100644 --- a/logos_delivery/waku/factory/builder.nim +++ b/logos_delivery/waku/factory/builder.nim @@ -15,6 +15,7 @@ import ../waku_enr, ../discovery/waku_discv5, ../waku_node, + ../node/waku_switch, ../net/net_config, ../node/peer_manager, ../common/rate_limit/setting, @@ -22,7 +23,7 @@ import type WakuNodeBuilder* = object # General - nodeRng: Option[ref crypto.HmacDrbgContext] + nodeRng: Option[crypto.Rng] nodeKey: Option[crypto.PrivateKey] netConfig: Option[NetConfig] record: Option[enr.Record] @@ -58,7 +59,7 @@ proc init*(T: type WakuNodeBuilder): WakuNodeBuilder = ## General -proc withRng*(builder: var WakuNodeBuilder, rng: ref crypto.HmacDrbgContext) = +proc withRng*(builder: var WakuNodeBuilder, rng: crypto.Rng) = builder.nodeRng = some(rng) proc withNodeKey*(builder: var WakuNodeBuilder, nodeKey: crypto.PrivateKey) = @@ -158,7 +159,7 @@ proc withSwitchConfiguration*( ## Build proc build*(builder: WakuNodeBuilder): Result[WakuNode, string] = - var rng: ref crypto.HmacDrbgContext + var rng: crypto.Rng if builder.nodeRng.isNone(): rng = crypto.newRng() else: @@ -191,7 +192,7 @@ proc build*(builder: WakuNodeBuilder): Result[WakuNode, string] = wsAddress = builder.netConfig.get().wsHostAddress, transportFlags = {ServerFlags.ReuseAddr, ServerFlags.TcpNoDelay}, rng = rng, - maxConnections = builder.switchMaxConnections.get(builders.MaxConnections), + maxConnections = builder.switchMaxConnections.get(MaxConnections), wssEnabled = builder.netConfig.get().wssEnabled, secureKeyPath = builder.switchSslSecureKey.get(""), secureCertPath = builder.switchSslSecureCert.get(""), @@ -211,7 +212,7 @@ proc build*(builder: WakuNodeBuilder): Result[WakuNode, string] = maxServicePeers = some(builder.maxServicePeers), colocationLimit = builder.colocationLimit, shardedPeerManagement = builder.shardAware, - maxConnections = builder.switchMaxConnections.get(builders.MaxConnections), + maxConnections = builder.switchMaxConnections.get(MaxConnections), ) var node: WakuNode diff --git a/logos_delivery/waku/factory/conf_builder/kademlia_discovery_conf_builder.nim b/logos_delivery/waku/factory/conf_builder/kademlia_discovery_conf_builder.nim index 62fe42e27..f498235b8 100644 --- a/logos_delivery/waku/factory/conf_builder/kademlia_discovery_conf_builder.nim +++ b/logos_delivery/waku/factory/conf_builder/kademlia_discovery_conf_builder.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import chronicles, std/options, results import libp2p/[peerid, multiaddress, peerinfo] import logos_delivery/waku/factory/waku_conf diff --git a/logos_delivery/waku/factory/conf_builder/mix_conf_builder.nim b/logos_delivery/waku/factory/conf_builder/mix_conf_builder.nim index 68aca5f68..fb2b17dde 100644 --- a/logos_delivery/waku/factory/conf_builder/mix_conf_builder.nim +++ b/logos_delivery/waku/factory/conf_builder/mix_conf_builder.nim @@ -1,5 +1,6 @@ +import logos_delivery/waku/compat/option_valueor import chronicles, std/options, results -import libp2p/crypto/crypto, libp2p/crypto/curve25519, libp2p/protocols/mix/curve25519 +import libp2p/crypto/crypto, libp2p/crypto/curve25519, libp2p_mix/curve25519 import ../waku_conf, logos_delivery/waku/waku_mix logScope: diff --git a/logos_delivery/waku/factory/conf_builder/rate_limit_conf_builder.nim b/logos_delivery/waku/factory/conf_builder/rate_limit_conf_builder.nim index f3a28c5dc..e4a6d260b 100644 --- a/logos_delivery/waku/factory/conf_builder/rate_limit_conf_builder.nim +++ b/logos_delivery/waku/factory/conf_builder/rate_limit_conf_builder.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import chronicles, std/[net, options], results import logos_delivery/waku/common/rate_limit/setting diff --git a/logos_delivery/waku/factory/conf_builder/store_service_conf_builder.nim b/logos_delivery/waku/factory/conf_builder/store_service_conf_builder.nim index 1df3da61f..20507e7d0 100644 --- a/logos_delivery/waku/factory/conf_builder/store_service_conf_builder.nim +++ b/logos_delivery/waku/factory/conf_builder/store_service_conf_builder.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[options, strutils, sequtils] import chronicles, results, chronos import ../waku_conf, ./store_sync_conf_builder diff --git a/logos_delivery/waku/factory/conf_builder/waku_conf_builder.nim b/logos_delivery/waku/factory/conf_builder/waku_conf_builder.nim index d034e0bd0..4bbb0145b 100644 --- a/logos_delivery/waku/factory/conf_builder/waku_conf_builder.nim +++ b/logos_delivery/waku/factory/conf_builder/waku_conf_builder.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import libp2p/crypto/crypto, libp2p/multiaddress, @@ -327,13 +328,13 @@ proc withStaticNodes*(builder: var WakuConfBuilder, staticNodes: seq[string]) = ## Building proc nodeKey( - builder: WakuConfBuilder, rng: ref HmacDrbgContext + builder: WakuConfBuilder, rng: crypto.Rng ): Result[crypto.PrivateKey, string] = if builder.nodeKey.isSome(): return ok(builder.nodeKey.get()) else: warn "missing node key, generating new set" - let nodeKey = crypto.PrivateKey.random(Secp256k1, rng[]).valueOr: + let nodeKey = crypto.PrivateKey.random(Secp256k1, rng).valueOr: error "Failed to generate key", error = error return err("Failed to generate key: " & $error) return ok(nodeKey) @@ -526,7 +527,7 @@ proc enforceSecurityConstraints(builder: WakuConfBuilder): Result[void, string] ok() proc build*( - builder: var WakuConfBuilder, rng: ref HmacDrbgContext = crypto.newRng() + builder: var WakuConfBuilder, rng: crypto.Rng = crypto.newRng() ): Result[WakuConf, string] = ## Return a WakuConf that contains all mandatory parameters ## Applies some sane defaults that are applicable across any usage diff --git a/logos_delivery/waku/factory/internal_config.nim b/logos_delivery/waku/factory/internal_config.nim index a3306eadc..75e5be1ad 100644 --- a/logos_delivery/waku/factory/internal_config.nim +++ b/logos_delivery/waku/factory/internal_config.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import chronicles, chronos, diff --git a/logos_delivery/waku/factory/node_factory.nim b/logos_delivery/waku/factory/node_factory.nim index 2a2b6e5d9..f19e9632b 100644 --- a/logos_delivery/waku/factory/node_factory.nim +++ b/logos_delivery/waku/factory/node_factory.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[options, sequtils], chronicles, @@ -55,7 +56,7 @@ proc setupPeerStorage(): Result[Option[WakuPeerStorage], string] = proc initNode( conf: WakuConf, netConfig: NetConfig, - rng: ref HmacDrbgContext, + rng: crypto.Rng, record: enr.Record, peerStore: Option[WakuPeerStorage], relay: Relay, @@ -459,7 +460,7 @@ proc startNode*( return ok() proc setupNode*( - wakuConf: WakuConf, rng: ref HmacDrbgContext = crypto.newRng(), relay: Relay + wakuConf: WakuConf, rng: crypto.Rng = crypto.newRng(), relay: Relay ): Future[Result[WakuNode, string]] {.async.} = let netConfig = ( await networkConfiguration( diff --git a/logos_delivery/waku/factory/waku.nim b/logos_delivery/waku/factory/waku.nim index 56e2748e2..2eee46103 100644 --- a/logos_delivery/waku/factory/waku.nim +++ b/logos_delivery/waku/factory/waku.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -62,7 +63,7 @@ const git_version* {.strdefine.} = "n/a" type Waku* = ref object stateInfo*: WakuStateInfo conf*: WakuConf - rng*: ref HmacDrbgContext + rng*: crypto.Rng key: crypto.PrivateKey @@ -86,7 +87,7 @@ type Waku* = ref object brokerCtx*: BrokerContext proc setupSwitchServices( - waku: Waku, conf: WakuConf, circuitRelay: Relay, rng: ref HmacDrbgContext + waku: Waku, conf: WakuConf, circuitRelay: Relay, rng: crypto.Rng ) = proc onReservation(addresses: seq[MultiAddress]) {.gcsafe, raises: [].} = info "circuit relay handler new reserve event", @@ -114,6 +115,17 @@ proc setupSwitchServices( else: waku.node.switch.services = @[Service(autonatService)] + # libp2p 2.0.0 split Service.setup out of Service.start: the switch runs setup + # only at build time (SwitchBuilder.setupServices), while switch.start calls + # just start. These services are created and attached post-build, so setup must + # be invoked explicitly here -- otherwise AutonatService.addressMapper stays nil + # and the peerInfo.update() inside start dereferences it (SIGSEGV). + for service in waku.node.switch.services: + try: + service.setup(waku.node.switch) + except ServiceSetupError as e: + error "failed to set up libp2p switch service", error = e.msg + ## Initialisation proc newCircuitRelay(isRelayClient: bool): Relay = diff --git a/logos_delivery/waku/incentivization/eligibility_manager.nim b/logos_delivery/waku/incentivization/eligibility_manager.nim index ad0f3b67e..ed882f524 100644 --- a/logos_delivery/waku/incentivization/eligibility_manager.nim +++ b/logos_delivery/waku/incentivization/eligibility_manager.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[options, sets], chronos, web3, stew/byteutils, stint, results, chronicles import logos_delivery/waku/incentivization/rpc, tests/waku_rln_relay/utils_onchain diff --git a/logos_delivery/waku/node/health_monitor/node_health_monitor.nim b/logos_delivery/waku/node/health_monitor/node_health_monitor.nim index 74f3defec..6e77816f0 100644 --- a/logos_delivery/waku/node/health_monitor/node_health_monitor.nim +++ b/logos_delivery/waku/node/health_monitor/node_health_monitor.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/node/peer_manager/peer_manager.nim b/logos_delivery/waku/node/peer_manager/peer_manager.nim index 6c916c3c6..60bd2acb1 100644 --- a/logos_delivery/waku/node/peer_manager/peer_manager.nim +++ b/logos_delivery/waku/node/peer_manager/peer_manager.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -25,6 +26,7 @@ import common/callbacks, common/utils/parse_size_units, node/health_monitor/online_monitor, + node/waku_switch, ], ./peer_store/peer_storage, ./waku_peer_store diff --git a/logos_delivery/waku/node/peer_manager/peer_store/waku_peer_storage.nim b/logos_delivery/waku/node/peer_manager/peer_store/waku_peer_storage.nim index dc1452618..bea265d31 100644 --- a/logos_delivery/waku/node/peer_manager/peer_store/waku_peer_storage.nim +++ b/logos_delivery/waku/node/peer_manager/peer_store/waku_peer_storage.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/node/peer_manager/waku_peer_store.nim b/logos_delivery/waku/node/peer_manager/waku_peer_store.nim index 93ac9ad2e..e4e48f20e 100644 --- a/logos_delivery/waku/node/peer_manager/waku_peer_store.nim +++ b/logos_delivery/waku/node/peer_manager/waku_peer_store.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -7,7 +8,8 @@ import eth/p2p/discoveryv5/enr, libp2p/builders, libp2p/peerstore, - libp2p/crypto/curve25519 + libp2p/crypto/curve25519, + libp2p_mix/pool import ../../waku_core, diff --git a/logos_delivery/waku/node/subscription_manager.nim b/logos_delivery/waku/node/subscription_manager.nim index a8acb58dd..15b582ea6 100644 --- a/logos_delivery/waku/node/subscription_manager.nim +++ b/logos_delivery/waku/node/subscription_manager.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[sequtils, sets, tables, options], chronos, chronicles, metrics, results import libp2p/[peerid, peerinfo] import brokers/broker_context diff --git a/logos_delivery/waku/node/waku_metrics.nim b/logos_delivery/waku/node/waku_metrics.nim index feb767878..08eecbe5c 100644 --- a/logos_delivery/waku/node/waku_metrics.nim +++ b/logos_delivery/waku/node/waku_metrics.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import chronicles, chronos, metrics, metrics/chronos_httpserver diff --git a/logos_delivery/waku/node/waku_node.nim b/logos_delivery/waku/node/waku_node.nim index a852fa13e..da7de70fa 100644 --- a/logos_delivery/waku/node/waku_node.nim +++ b/logos_delivery/waku/node/waku_node.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -23,8 +24,8 @@ import libp2p/transports/wstransport, libp2p/utility, libp2p/utils/offsettedseq, - libp2p/protocols/mix, - libp2p/protocols/mix/mix_protocol, + libp2p_mix, + libp2p_mix/mix_protocol, brokers/broker_context, brokers/request_broker @@ -120,7 +121,7 @@ type wakuAutoSharding*: Option[Sharding] enr*: enr.Record libp2pPing*: Ping - rng*: ref rand.HmacDrbgContext + rng*: crypto.Rng brokerCtx*: BrokerContext wakuRendezvous*: WakuRendezVous wakuRendezvousClient*: rendezvous_client.WakuRendezVousClient @@ -215,7 +216,7 @@ proc new*( peerManager: PeerManager, rateLimitSettings: ProtocolRateLimitSettings = DefaultProtocolRateLimit, # TODO: make this argument required after tests are updated - rng: ref HmacDrbgContext = crypto.newRng(), + rng: crypto.Rng = crypto.newRng(), ): T {.raises: [Defect, LPError, IOError, TLSStreamProtocolError].} = ## Creates a Waku Node instance. diff --git a/logos_delivery/waku/node/waku_node/filter.nim b/logos_delivery/waku/node/waku_node/filter.nim index 0db4875b0..4b2ea8494 100644 --- a/logos_delivery/waku/node/waku_node/filter.nim +++ b/logos_delivery/waku/node/waku_node/filter.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/node/waku_node/lightpush.nim b/logos_delivery/waku/node/waku_node/lightpush.nim index ffe2afdac..10f27e32f 100644 --- a/logos_delivery/waku/node/waku_node/lightpush.nim +++ b/logos_delivery/waku/node/waku_node/lightpush.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -17,7 +18,7 @@ import libp2p/transports/tcptransport, libp2p/transports/wstransport, libp2p/utility, - libp2p/protocols/mix + libp2p_mix import ../waku_node, diff --git a/logos_delivery/waku/node/waku_node/peer_exchange.nim b/logos_delivery/waku/node/waku_node/peer_exchange.nim index 1cb6bd3bb..cd6e6146c 100644 --- a/logos_delivery/waku/node/waku_node/peer_exchange.nim +++ b/logos_delivery/waku/node/waku_node/peer_exchange.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/node/waku_node/ping.nim b/logos_delivery/waku/node/waku_node/ping.nim index 9dc649fd8..e7e011477 100644 --- a/logos_delivery/waku/node/waku_node/ping.nim +++ b/logos_delivery/waku/node/waku_node/ping.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/node/waku_node/relay.nim b/logos_delivery/waku/node/waku_node/relay.nim index b90992e9b..57904dc94 100644 --- a/logos_delivery/waku/node/waku_node/relay.nim +++ b/logos_delivery/waku/node/waku_node/relay.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/node/waku_node/store.nim b/logos_delivery/waku/node/waku_node/store.nim index fcf0dfc89..189ef9052 100644 --- a/logos_delivery/waku/node/waku_node/store.nim +++ b/logos_delivery/waku/node/waku_node/store.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/node/waku_switch.nim b/logos_delivery/waku/node/waku_switch.nim index d1af77662..31177591e 100644 --- a/logos_delivery/waku/node/waku_switch.nim +++ b/logos_delivery/waku/node/waku_switch.nim @@ -18,10 +18,12 @@ import # override nim-libp2p default value (which is also 1) const MaxConnectionsPerPeer* = 1 +const MaxConnections* = 50 + proc withWsTransport*(b: SwitchBuilder): SwitchBuilder = b.withTransport( - proc(upgr: Upgrade, privateKey: crypto.PrivateKey): Transport = - WsTransport.new(upgr) + proc(config: TransportConfig): Transport = + WsTransport.new(config.upgr, rng = config.rng) ) proc getSecureKey(path: string): TLSPrivateKey {.raises: [Defect, IOError].} = @@ -59,7 +61,7 @@ proc newWakuSwitch*( wsAddress = none(MultiAddress), secureManagers: openarray[SecureProtocol] = [SecureProtocol.Noise], transportFlags: set[ServerFlags] = {}, - rng: ref HmacDrbgContext, + rng: crypto.Rng, inTimeout: Duration = 5.minutes, outTimeout: Duration = 5.minutes, maxConnections = MaxConnections, @@ -79,9 +81,6 @@ proc newWakuSwitch*( var b = SwitchBuilder .new() .withRng(rng) - .withMaxConnections(maxConnections) - .withMaxIn(maxIn) - .withMaxOut(maxOut) .withMaxConnsPerPeer(maxConnsPerPeer) .withYamux() .withMplex(inTimeout, outTimeout) @@ -92,6 +91,15 @@ proc newWakuSwitch*( .withCircuitRelay(circuitRelay) .withAutonat() + # libp2p 2.0.0 folded withMaxConnections and withMaxInOut into a single + # `limits` field: they are mutually exclusive (last one wins), and + # ConnectionLimits.maxInOut asserts maxIn/maxOut > 0. So apply explicit in/out + # limits only when both are provided (>0); otherwise use the shared total cap. + if maxIn > 0 and maxOut > 0: + b = b.withMaxInOut(maxIn, maxOut) + else: + b = b.withMaxConnections(maxConnections) + if peerStoreCapacity.isSome(): b = b.withPeerStore(peerStoreCapacity.get()) else: @@ -112,6 +120,6 @@ proc newWakuSwitch*( b = b.withAddress(address) if not rendezvous.isNil(): - b = b.withRendezVous(rendezvous) + b = b.withRendezVous() b.build() diff --git a/logos_delivery/waku/persistency/persistency.nim b/logos_delivery/waku/persistency/persistency.nim index 1e070dbb5..fca6ad34d 100644 --- a/logos_delivery/waku/persistency/persistency.nim +++ b/logos_delivery/waku/persistency/persistency.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## Public facade and main driver types for the persistency library. ## ## ``Persistency`` is the per-root coordinator; one instance owns one diff --git a/logos_delivery/waku/rest_api/endpoint/admin/handlers.nim b/logos_delivery/waku/rest_api/endpoint/admin/handlers.nim index c82401d87..f49b75652 100644 --- a/logos_delivery/waku/rest_api/endpoint/admin/handlers.nim +++ b/logos_delivery/waku/rest_api/endpoint/admin/handlers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/rest_api/endpoint/builder.nim b/logos_delivery/waku/rest_api/endpoint/builder.nim index 6762aebdf..4610720ee 100644 --- a/logos_delivery/waku/rest_api/endpoint/builder.nim +++ b/logos_delivery/waku/rest_api/endpoint/builder.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import net, tables diff --git a/logos_delivery/waku/rest_api/endpoint/debug/handlers.nim b/logos_delivery/waku/rest_api/endpoint/debug/handlers.nim index 43b6fbbf1..ee63511f5 100644 --- a/logos_delivery/waku/rest_api/endpoint/debug/handlers.nim +++ b/logos_delivery/waku/rest_api/endpoint/debug/handlers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import chronicles, json_serialization, presto/route diff --git a/logos_delivery/waku/rest_api/endpoint/filter/handlers.nim b/logos_delivery/waku/rest_api/endpoint/filter/handlers.nim index 61d7eb96f..60a16dfb3 100644 --- a/logos_delivery/waku/rest_api/endpoint/filter/handlers.nim +++ b/logos_delivery/waku/rest_api/endpoint/filter/handlers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/rest_api/endpoint/health/handlers.nim b/logos_delivery/waku/rest_api/endpoint/health/handlers.nim index dc7588d16..955e599d9 100644 --- a/logos_delivery/waku/rest_api/endpoint/health/handlers.nim +++ b/logos_delivery/waku/rest_api/endpoint/health/handlers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import chronicles, json_serialization, presto/route diff --git a/logos_delivery/waku/rest_api/endpoint/health/types.nim b/logos_delivery/waku/rest_api/endpoint/health/types.nim index 00e101133..331460e7a 100644 --- a/logos_delivery/waku/rest_api/endpoint/health/types.nim +++ b/logos_delivery/waku/rest_api/endpoint/health/types.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import results diff --git a/logos_delivery/waku/rest_api/endpoint/legacy_lightpush/handlers.nim b/logos_delivery/waku/rest_api/endpoint/legacy_lightpush/handlers.nim index 4344f0481..058b84b8e 100644 --- a/logos_delivery/waku/rest_api/endpoint/legacy_lightpush/handlers.nim +++ b/logos_delivery/waku/rest_api/endpoint/legacy_lightpush/handlers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/rest_api/endpoint/lightpush/handlers.nim b/logos_delivery/waku/rest_api/endpoint/lightpush/handlers.nim index 2afe3c7e7..5980c028c 100644 --- a/logos_delivery/waku/rest_api/endpoint/lightpush/handlers.nim +++ b/logos_delivery/waku/rest_api/endpoint/lightpush/handlers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/rest_api/endpoint/origin_handler.nim b/logos_delivery/waku/rest_api/endpoint/origin_handler.nim index 9752bfb56..148dd3962 100644 --- a/logos_delivery/waku/rest_api/endpoint/origin_handler.nim +++ b/logos_delivery/waku/rest_api/endpoint/origin_handler.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/rest_api/endpoint/relay/handlers.nim b/logos_delivery/waku/rest_api/endpoint/relay/handlers.nim index 48565422f..9075c61df 100644 --- a/logos_delivery/waku/rest_api/endpoint/relay/handlers.nim +++ b/logos_delivery/waku/rest_api/endpoint/relay/handlers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/rest_api/endpoint/rest_serdes.nim b/logos_delivery/waku/rest_api/endpoint/rest_serdes.nim index 8dcb7c8f1..3441b6b26 100644 --- a/logos_delivery/waku/rest_api/endpoint/rest_serdes.nim +++ b/logos_delivery/waku/rest_api/endpoint/rest_serdes.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/rest_api/endpoint/store/handlers.nim b/logos_delivery/waku/rest_api/endpoint/store/handlers.nim index 7d37191fb..072041f29 100644 --- a/logos_delivery/waku/rest_api/endpoint/store/handlers.nim +++ b/logos_delivery/waku/rest_api/endpoint/store/handlers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/rest_api/endpoint/store/types.nim b/logos_delivery/waku/rest_api/endpoint/store/types.nim index 99818b5ff..c53797233 100644 --- a/logos_delivery/waku/rest_api/endpoint/store/types.nim +++ b/logos_delivery/waku/rest_api/endpoint/store/types.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/rest_api/handlers.nim b/logos_delivery/waku/rest_api/handlers.nim index 4fc922f36..520c0518a 100644 --- a/logos_delivery/waku/rest_api/handlers.nim +++ b/logos_delivery/waku/rest_api/handlers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import chronos, std/[options, sequtils], results diff --git a/logos_delivery/waku/utils/requests.nim b/logos_delivery/waku/utils/requests.nim index d9afd2887..a7fd9a2a9 100644 --- a/logos_delivery/waku/utils/requests.nim +++ b/logos_delivery/waku/utils/requests.nim @@ -2,9 +2,9 @@ {.push raises: [].} -import bearssl/rand, stew/byteutils +import libp2p/crypto/crypto, stew/byteutils -proc generateRequestId*(rng: ref HmacDrbgContext): string = +proc generateRequestId*(rng: crypto.Rng): string = var bytes: array[10, byte] - hmacDrbgGenerate(rng[], bytes) + rng.generate(bytes) return byteutils.toHex(bytes) diff --git a/logos_delivery/waku/waku_archive/archive.nim b/logos_delivery/waku/waku_archive/archive.nim index a257a55b1..ea5e2bf02 100644 --- a/logos_delivery/waku/waku_archive/archive.nim +++ b/logos_delivery/waku/waku_archive/archive.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_archive/driver/builder.nim b/logos_delivery/waku/waku_archive/driver/builder.nim index 811b16999..632468a00 100644 --- a/logos_delivery/waku/waku_archive/driver/builder.nim +++ b/logos_delivery/waku/waku_archive/driver/builder.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import results, chronicles, chronos diff --git a/logos_delivery/waku/waku_archive/driver/postgres_driver/migrations.nim b/logos_delivery/waku/waku_archive/driver/postgres_driver/migrations.nim index a1a3e93fe..f216e75b8 100644 --- a/logos_delivery/waku/waku_archive/driver/postgres_driver/migrations.nim +++ b/logos_delivery/waku/waku_archive/driver/postgres_driver/migrations.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import std/strutils, results, chronicles, chronos diff --git a/logos_delivery/waku/waku_archive/driver/postgres_driver/postgres_driver.nim b/logos_delivery/waku/waku_archive/driver/postgres_driver/postgres_driver.nim index 82c8ec2ae..e909f953f 100644 --- a/logos_delivery/waku/waku_archive/driver/postgres_driver/postgres_driver.nim +++ b/logos_delivery/waku/waku_archive/driver/postgres_driver/postgres_driver.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_archive/driver/queue_driver/queue_driver.nim b/logos_delivery/waku/waku_archive/driver/queue_driver/queue_driver.nim index 2ffc9ab00..a810d231a 100644 --- a/logos_delivery/waku/waku_archive/driver/queue_driver/queue_driver.nim +++ b/logos_delivery/waku/waku_archive/driver/queue_driver/queue_driver.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import std/options, results, stew/sorted_set, chronicles, chronos diff --git a/logos_delivery/waku/waku_archive/driver/sqlite_driver/sqlite_driver.nim b/logos_delivery/waku/waku_archive/driver/sqlite_driver/sqlite_driver.nim index ff7b0e7d3..913f3d307 100644 --- a/logos_delivery/waku/waku_archive/driver/sqlite_driver/sqlite_driver.nim +++ b/logos_delivery/waku/waku_archive/driver/sqlite_driver/sqlite_driver.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor # The code in this file is an adaptation of the Sqlite KV Store found in nim-eth. # https://github.com/status-im/nim-eth/blob/master/eth/db/kvstore_sqlite3.nim {.push raises: [].} diff --git a/logos_delivery/waku/waku_archive/retention_policy/builder.nim b/logos_delivery/waku/waku_archive/retention_policy/builder.nim index 7e777f4a0..ad49ed45e 100644 --- a/logos_delivery/waku/waku_archive/retention_policy/builder.nim +++ b/logos_delivery/waku/waku_archive/retention_policy/builder.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import std/[strutils, options], regex, results diff --git a/logos_delivery/waku/waku_archive/retention_policy/retention_policy_capacity.nim b/logos_delivery/waku/waku_archive/retention_policy/retention_policy_capacity.nim index ff4da6861..088508b9b 100644 --- a/logos_delivery/waku/waku_archive/retention_policy/retention_policy_capacity.nim +++ b/logos_delivery/waku/waku_archive/retention_policy/retention_policy_capacity.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import results, chronicles, chronos diff --git a/logos_delivery/waku/waku_core/peers.nim b/logos_delivery/waku/waku_core/peers.nim index c4b8b593e..d6cc2db36 100644 --- a/logos_delivery/waku/waku_core/peers.nim +++ b/logos_delivery/waku/waku_core/peers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_core/topics/content_topic.nim b/logos_delivery/waku/waku_core/topics/content_topic.nim index 3eeb35771..23da9a24a 100644 --- a/logos_delivery/waku/waku_core/topics/content_topic.nim +++ b/logos_delivery/waku/waku_core/topics/content_topic.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## Waku content topics definition and namespacing utils ## ## See 23/WAKU2-TOPICS RFC: https://rfc.vac.dev/spec/23/ diff --git a/logos_delivery/waku/waku_core/topics/sharding.nim b/logos_delivery/waku/waku_core/topics/sharding.nim index 1cb5b37b3..704180554 100644 --- a/logos_delivery/waku/waku_core/topics/sharding.nim +++ b/logos_delivery/waku/waku_core/topics/sharding.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## Waku autosharding utils ## ## See 51/WAKU2-RELAY-SHARDING RFC: https://rfc.vac.dev/spec/51/#automatic-sharding diff --git a/logos_delivery/waku/waku_enr/capabilities.nim b/logos_delivery/waku/waku_enr/capabilities.nim index 26899fbb4..42008e9c7 100644 --- a/logos_delivery/waku/waku_enr/capabilities.nim +++ b/logos_delivery/waku/waku_enr/capabilities.nim @@ -1,9 +1,10 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import std/[options, bitops, sequtils, net, tables], results, eth/keys, libp2p/crypto/crypto import ../common/enr, ../waku_core/codecs -import libp2p/protocols/mix +import libp2p_mix const CapabilitiesEnrField* = "waku2" diff --git a/logos_delivery/waku/waku_enr/multiaddr.nim b/logos_delivery/waku/waku_enr/multiaddr.nim index 4d6e9baa7..a275b2599 100644 --- a/logos_delivery/waku/waku_enr/multiaddr.nim +++ b/logos_delivery/waku/waku_enr/multiaddr.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_enr/sharding.nim b/logos_delivery/waku/waku_enr/sharding.nim index 2aeb96a9d..682772539 100644 --- a/logos_delivery/waku/waku_enr/sharding.nim +++ b/logos_delivery/waku/waku_enr/sharding.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_filter_v2/client.nim b/logos_delivery/waku/waku_filter_v2/client.nim index 6fb473e27..ddae363a3 100644 --- a/logos_delivery/waku/waku_filter_v2/client.nim +++ b/logos_delivery/waku/waku_filter_v2/client.nim @@ -1,3 +1,5 @@ +import logos_delivery/waku/compat/option_valueor +import libp2p/crypto/crypto ## Waku Filter client for subscribing and receiving filtered messages {.push raises: [].} @@ -23,13 +25,13 @@ logScope: type WakuFilterClient* = ref object of LPProtocol brokerCtx: BrokerContext - rng: ref HmacDrbgContext + rng: crypto.Rng peerManager: PeerManager pushHandlers: seq[FilterPushHandler] -func generateRequestId(rng: ref HmacDrbgContext): string = +func generateRequestId(rng: crypto.Rng): string = var bytes: array[10, byte] - hmacDrbgGenerate(rng[], bytes) + rng.generate(bytes) return byteutils.toHex(bytes) proc sendSubscribeRequest( @@ -210,9 +212,7 @@ proc initProtocolHandler(wfc: WakuFilterClient) = wfc.handler = handler wfc.codec = WakuFilterPushCodec -proc new*( - T: type WakuFilterClient, peerManager: PeerManager, rng: ref HmacDrbgContext -): T = +proc new*(T: type WakuFilterClient, peerManager: PeerManager, rng: crypto.Rng): T = let brokerCtx = globalBrokerContext() let wfc = WakuFilterClient( brokerCtx: brokerCtx, rng: rng, peerManager: peerManager, pushHandlers: @[] diff --git a/logos_delivery/waku/waku_filter_v2/protocol.nim b/logos_delivery/waku/waku_filter_v2/protocol.nim index 35620b6cd..ab77ce2dc 100644 --- a/logos_delivery/waku/waku_filter_v2/protocol.nim +++ b/logos_delivery/waku/waku_filter_v2/protocol.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## Waku Filter protocol for subscribing and filtering messages {.push raises: [].} diff --git a/logos_delivery/waku/waku_filter_v2/subscriptions.nim b/logos_delivery/waku/waku_filter_v2/subscriptions.nim index 0e66899d0..5c942cbf5 100644 --- a/logos_delivery/waku/waku_filter_v2/subscriptions.nim +++ b/logos_delivery/waku/waku_filter_v2/subscriptions.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_keystore/keystore.nim b/logos_delivery/waku/waku_keystore/keystore.nim index 158f1a98e..757024591 100644 --- a/logos_delivery/waku/waku_keystore/keystore.nim +++ b/logos_delivery/waku/waku_keystore/keystore.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import options, json, strutils, sequtils, std/[tables, os] diff --git a/logos_delivery/waku/waku_lightpush/callbacks.nim b/logos_delivery/waku/waku_lightpush/callbacks.nim index a9d795d6e..dd910326e 100644 --- a/logos_delivery/waku/waku_lightpush/callbacks.nim +++ b/logos_delivery/waku/waku_lightpush/callbacks.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import results diff --git a/logos_delivery/waku/waku_lightpush/client.nim b/logos_delivery/waku/waku_lightpush/client.nim index fd12c49d2..680970a51 100644 --- a/logos_delivery/waku/waku_lightpush/client.nim +++ b/logos_delivery/waku/waku_lightpush/client.nim @@ -1,3 +1,5 @@ +import libp2p/crypto/crypto +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import std/options, results, chronicles, chronos, metrics, bearssl/rand, stew/byteutils @@ -16,12 +18,10 @@ logScope: topics = "waku lightpush client" type WakuLightPushClient* = ref object - rng*: ref rand.HmacDrbgContext + rng*: crypto.Rng peerManager*: PeerManager -proc new*( - T: type WakuLightPushClient, peerManager: PeerManager, rng: ref rand.HmacDrbgContext -): T = +proc new*(T: type WakuLightPushClient, peerManager: PeerManager, rng: crypto.Rng): T = WakuLightPushClient(peerManager: peerManager, rng: rng) proc ensureTimestampSet(message: var WakuMessage) = diff --git a/logos_delivery/waku/waku_lightpush/protocol.nim b/logos_delivery/waku/waku_lightpush/protocol.nim index 8336f4dfc..7c6c55928 100644 --- a/logos_delivery/waku/waku_lightpush/protocol.nim +++ b/logos_delivery/waku/waku_lightpush/protocol.nim @@ -1,3 +1,5 @@ +import libp2p/crypto/crypto +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -22,7 +24,7 @@ logScope: topics = "waku lightpush" type WakuLightPush* = ref object of LPProtocol - rng*: ref rand.HmacDrbgContext + rng*: crypto.Rng peerManager*: PeerManager pushHandler*: PushMessageHandler requestRateLimiter*: RequestRateLimiter @@ -156,7 +158,7 @@ proc initProtocolHandler(wl: WakuLightPush) = proc new*( T: type WakuLightPush, peerManager: PeerManager, - rng: ref rand.HmacDrbgContext, + rng: crypto.Rng, pushHandler: PushMessageHandler, autoSharding: Option[Sharding], rateLimitSetting: Option[RateLimitSetting] = none[RateLimitSetting](), diff --git a/logos_delivery/waku/waku_lightpush_legacy/client.nim b/logos_delivery/waku/waku_lightpush_legacy/client.nim index ab489bec9..511c3f543 100644 --- a/logos_delivery/waku/waku_lightpush_legacy/client.nim +++ b/logos_delivery/waku/waku_lightpush_legacy/client.nim @@ -1,3 +1,5 @@ +import libp2p/crypto/crypto +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import std/options, results, chronicles, chronos, metrics, bearssl/rand, stew/byteutils @@ -17,12 +19,10 @@ logScope: type WakuLegacyLightPushClient* = ref object peerManager*: PeerManager - rng*: ref rand.HmacDrbgContext + rng*: crypto.Rng proc new*( - T: type WakuLegacyLightPushClient, - peerManager: PeerManager, - rng: ref rand.HmacDrbgContext, + T: type WakuLegacyLightPushClient, peerManager: PeerManager, rng: crypto.Rng ): T = WakuLegacyLightPushClient(peerManager: peerManager, rng: rng) diff --git a/logos_delivery/waku/waku_lightpush_legacy/protocol.nim b/logos_delivery/waku/waku_lightpush_legacy/protocol.nim index f5ed60134..b3d728f9c 100644 --- a/logos_delivery/waku/waku_lightpush_legacy/protocol.nim +++ b/logos_delivery/waku/waku_lightpush_legacy/protocol.nim @@ -1,3 +1,4 @@ +import libp2p/crypto/crypto {.push raises: [].} import std/options, results, stew/byteutils, chronicles, chronos, metrics, bearssl/rand @@ -14,7 +15,7 @@ logScope: topics = "waku lightpush legacy" type WakuLegacyLightPush* = ref object of LPProtocol - rng*: ref rand.HmacDrbgContext + rng*: crypto.Rng peerManager*: PeerManager pushHandler*: PushMessageHandler requestRateLimiter*: RequestRateLimiter @@ -116,7 +117,7 @@ proc initProtocolHandler(wl: WakuLegacyLightPush) = proc new*( T: type WakuLegacyLightPush, peerManager: PeerManager, - rng: ref rand.HmacDrbgContext, + rng: crypto.Rng, pushHandler: PushMessageHandler, rateLimitSetting: Option[RateLimitSetting] = none[RateLimitSetting](), ): T = diff --git a/logos_delivery/waku/waku_metadata/protocol.nim b/logos_delivery/waku/waku_metadata/protocol.nim index 7c72a6934..d080ff2b7 100644 --- a/logos_delivery/waku/waku_metadata/protocol.nim +++ b/logos_delivery/waku/waku_metadata/protocol.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_mix/protocol.nim b/logos_delivery/waku/waku_mix/protocol.nim index c0c3adadc..613b6e3c7 100644 --- a/logos_delivery/waku/waku_mix/protocol.nim +++ b/logos_delivery/waku/waku_mix/protocol.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import chronicles, std/options, chronos, results, metrics @@ -5,11 +6,11 @@ import chronicles, std/options, chronos, results, metrics import libp2p/crypto/curve25519, libp2p/crypto/crypto, - libp2p/protocols/mix, - libp2p/protocols/mix/mix_node, - libp2p/protocols/mix/mix_protocol, - libp2p/protocols/mix/mix_metrics, - libp2p/protocols/mix/delay_strategy, + libp2p_mix, + libp2p_mix/mix_node, + libp2p_mix/mix_protocol, + libp2p_mix/mix_metrics, + libp2p_mix/delay_strategy, libp2p/[multiaddress, peerid], eth/common/keys @@ -91,8 +92,11 @@ proc new*( procCall MixProtocol(m).init( localMixNodeInfo, peermgr.switch, - delayStrategy = - ExponentialDelayStrategy.new(meanDelayMs = 50, rng = crypto.newRng()), + delayStrategy = Opt.some( + DelayStrategy( + ExponentialDelayStrategy.new(meanDelay = 50'u16, rng = crypto.newRng()) + ) + ), ) processBootNodes(bootnodes, peermgr, m) diff --git a/logos_delivery/waku/waku_peer_exchange/client.nim b/logos_delivery/waku/waku_peer_exchange/client.nim index 15426c464..1f15d76fc 100644 --- a/logos_delivery/waku/waku_peer_exchange/client.nim +++ b/logos_delivery/waku/waku_peer_exchange/client.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/options, results, chronicles, chronos, metrics import ./common, ./rpc, ./rpc_codec, ../node/peer_manager diff --git a/logos_delivery/waku/waku_peer_exchange/protocol.nim b/logos_delivery/waku/waku_peer_exchange/protocol.nim index b99f5eabf..a389938f8 100644 --- a/logos_delivery/waku/waku_peer_exchange/protocol.nim +++ b/logos_delivery/waku/waku_peer_exchange/protocol.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import std/[options, sequtils, random], results, diff --git a/logos_delivery/waku/waku_relay/protocol.nim b/logos_delivery/waku/waku_relay/protocol.nim index c35854be3..e677ec5a0 100644 --- a/logos_delivery/waku/waku_relay/protocol.nim +++ b/logos_delivery/waku/waku_relay/protocol.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## Waku Relay module. Thin layer on top of GossipSub. ## ## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-relay.md @@ -366,6 +367,7 @@ proc new*( triggerSelf = true, msgIdProvider = defaultMessageIdProvider, maxMessageSize = maxMessageSize, + rng = switch.rng, parameters = GossipsubParameters, ) w.brokerCtx = globalBrokerContext() diff --git a/logos_delivery/waku/waku_rendezvous/client.nim b/logos_delivery/waku/waku_rendezvous/client.nim index 433ce361e..c4aada9e6 100644 --- a/logos_delivery/waku/waku_rendezvous/client.nim +++ b/logos_delivery/waku/waku_rendezvous/client.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -7,8 +8,7 @@ import chronicles, libp2p/protocols/rendezvous, libp2p/crypto/curve25519, - libp2p/switch, - libp2p/utils/semaphore + libp2p/switch import metrics except collect @@ -107,10 +107,12 @@ proc new*( switch: switch, rng: rng, sema: newAsyncSemaphore(MaxSimultanesousAdvertisements), - minDuration: rendezvous.MinimumAcceptedDuration, - maxDuration: rendezvous.MaximumDuration, - minTTL: rendezvous.MinimumAcceptedDuration.seconds.uint64, - maxTTL: rendezvous.MaximumDuration.seconds.uint64, + config: RendezVousConfig( + minDuration: rendezvous.MinimumAcceptedDuration, + maxDuration: rendezvous.MaximumDuration, + minTTL: rendezvous.MinimumAcceptedDuration.seconds.uint64, + maxTTL: rendezvous.MaximumDuration.seconds.uint64, + ), peers: @[], # Will be populated from selectPeer calls cookiesSaved: initTable[PeerId, Table[string, seq[byte]]](), peerRecordValidator: checkWakuPeerRecord, diff --git a/logos_delivery/waku/waku_rendezvous/common.nim b/logos_delivery/waku/waku_rendezvous/common.nim index 18c633efb..9776fd3a7 100644 --- a/logos_delivery/waku/waku_rendezvous/common.nim +++ b/logos_delivery/waku/waku_rendezvous/common.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import std/options, chronos diff --git a/logos_delivery/waku/waku_rendezvous/protocol.nim b/logos_delivery/waku/waku_rendezvous/protocol.nim index 89433f533..253fa5a12 100644 --- a/logos_delivery/waku/waku_rendezvous/protocol.nim +++ b/logos_delivery/waku/waku_rendezvous/protocol.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -8,7 +9,6 @@ import stew/byteutils, libp2p/protocols/rendezvous, libp2p/protocols/rendezvous/protobuf, - libp2p/utils/semaphore, libp2p/utils/offsettedseq, libp2p/crypto/curve25519, libp2p/switch, @@ -51,7 +51,7 @@ proc advertise*( self: WakuRendezVous, namespace: string, peers: seq[PeerId], - ttl: timer.Duration = self.minDuration, + ttl: timer.Duration = self.config.minDuration, ): Future[Result[void, string]] {.async: (raises: []).} = trace "advertising via waku rendezvous", namespace = namespace, ttl = ttl, peers = $peers, peerRecord = $self.getPeerRecord() @@ -154,14 +154,16 @@ proc new*( let rng = newRng() let wrv = T( rng: rng, - salt: string.fromBytes(generateBytes(rng[], 8)), + salt: string.fromBytes(generateBytes(rng, 8)), registered: initOffsettedSeq[RegisteredData](), expiredDT: Moment.now() - 1.days, sema: newAsyncSemaphore(SemaphoreDefaultSize), - minDuration: rendezvous.MinimumAcceptedDuration, - maxDuration: rendezvous.MaximumDuration, - minTTL: rendezvous.MinimumAcceptedDuration.seconds.uint64, - maxTTL: rendezvous.MaximumDuration.seconds.uint64, + config: RendezVousConfig( + minDuration: rendezvous.MinimumAcceptedDuration, + maxDuration: rendezvous.MaximumDuration, + minTTL: rendezvous.MinimumAcceptedDuration.seconds.uint64, + maxTTL: rendezvous.MaximumDuration.seconds.uint64, + ), peerRecordValidator: checkWakuPeerRecord, ) diff --git a/logos_delivery/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim b/logos_delivery/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim index 2d6b57c4b..258e9d133 100644 --- a/logos_delivery/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim +++ b/logos_delivery/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_rln_relay/rln/rln_interface.nim b/logos_delivery/waku/waku_rln_relay/rln/rln_interface.nim index 612d1a2cc..00d1fc0ba 100644 --- a/logos_delivery/waku/waku_rln_relay/rln/rln_interface.nim +++ b/logos_delivery/waku/waku_rln_relay/rln/rln_interface.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## Nim wrappers for librln (zerokit v2.0.2, safer-ffi typed handles). ## ## Built against the `stateless` zerokit feature: tree-mutation FFI is not diff --git a/logos_delivery/waku/waku_rln_relay/rln/wrappers.nim b/logos_delivery/waku/waku_rln_relay/rln/wrappers.nim index 4fc8c1542..611deeb5d 100644 --- a/logos_delivery/waku/waku_rln_relay/rln/wrappers.nim +++ b/logos_delivery/waku/waku_rln_relay/rln/wrappers.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor import chronicles, eth/keys, stew/[arrayops, endians2], stint, results import ./rln_interface, ../conversion_utils, ../protocol_types, ../protocol_metrics diff --git a/logos_delivery/waku/waku_rln_relay/rln_relay.nim b/logos_delivery/waku/waku_rln_relay/rln_relay.nim index 9c48b12fb..7c325d3d5 100644 --- a/logos_delivery/waku/waku_rln_relay/rln_relay.nim +++ b/logos_delivery/waku/waku_rln_relay/rln_relay.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_store/client.nim b/logos_delivery/waku/waku_store/client.nim index b49662811..21e2699e3 100644 --- a/logos_delivery/waku/waku_store/client.nim +++ b/logos_delivery/waku/waku_store/client.nim @@ -1,3 +1,5 @@ +import libp2p/crypto/crypto +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import @@ -20,11 +22,11 @@ const MaxQueryRetries = 5 # Maximum number of store peers to try before giving u type WakuStoreClient* = ref object peerManager: PeerManager - rng: ref rand.HmacDrbgContext + rng: crypto.Rng storeMsgMetricsPerShard*: Table[string, float64] proc new*( - T: type WakuStoreClient, peerManager: PeerManager, rng: ref rand.HmacDrbgContext + T: type WakuStoreClient, peerManager: PeerManager, rng: crypto.Rng ): T {.gcsafe.} = WakuStoreClient(peerManager: peerManager, rng: rng) diff --git a/logos_delivery/waku/waku_store/protocol.nim b/logos_delivery/waku/waku_store/protocol.nim index 17b7fb214..d09532d43 100644 --- a/logos_delivery/waku/waku_store/protocol.nim +++ b/logos_delivery/waku/waku_store/protocol.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## Waku Store protocol for historical messaging support. ## See spec for more details: ## https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-store.md @@ -30,7 +31,7 @@ type StoreQueryRequestHandler* = type WakuStore* = ref object of LPProtocol peerManager: PeerManager - rng: ref rand.HmacDrbgContext + rng: crypto.Rng requestHandler*: StoreQueryRequestHandler requestRateLimiter*: RequestRateLimiter @@ -156,7 +157,7 @@ proc initProtocolHandler(self: WakuStore) = proc new*( T: type WakuStore, peerManager: PeerManager, - rng: ref rand.HmacDrbgContext, + rng: crypto.Rng, requestHandler: StoreQueryRequestHandler, rateLimitSetting: Option[RateLimitSetting] = none[RateLimitSetting](), ): T = diff --git a/logos_delivery/waku/waku_store/resume.nim b/logos_delivery/waku/waku_store/resume.nim index b7864da94..9350bf6b7 100644 --- a/logos_delivery/waku/waku_store/resume.nim +++ b/logos_delivery/waku/waku_store/resume.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_store/self_req_handler.nim b/logos_delivery/waku/waku_store/self_req_handler.nim index 315961307..acd933724 100644 --- a/logos_delivery/waku/waku_store/self_req_handler.nim +++ b/logos_delivery/waku/waku_store/self_req_handler.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor ## ## This file is aimed to attend the requests that come directly ## from the 'self' node. It is expected to attend the store requests that diff --git a/logos_delivery/waku/waku_store_sync/reconciliation.nim b/logos_delivery/waku/waku_store_sync/reconciliation.nim index b18251fff..6f301d633 100644 --- a/logos_delivery/waku/waku_store_sync/reconciliation.nim +++ b/logos_delivery/waku/waku_store_sync/reconciliation.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/logos_delivery/waku/waku_store_sync/transfer.nim b/logos_delivery/waku/waku_store_sync/transfer.nim index 5d20afb18..aa02adfdc 100644 --- a/logos_delivery/waku/waku_store_sync/transfer.nim +++ b/logos_delivery/waku/waku_store_sync/transfer.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.push raises: [].} import diff --git a/nimble.lock b/nimble.lock index 4bdb8bb82..67e4296ff 100644 --- a/nimble.lock +++ b/nimble.lock @@ -50,8 +50,8 @@ } }, "jwt": { - "version": "#18f8378de52b241f321c1f9ea905456e89b95c6f", - "vcsRevision": "18f8378de52b241f321c1f9ea905456e89b95c6f", + "version": "#057ec95eb5af0eea9c49bfe9025b3312c95dc5f2", + "vcsRevision": "057ec95eb5af0eea9c49bfe9025b3312c95dc5f2", "url": "https://github.com/vacp2p/nim-jwt.git", "downloadMethod": "git", "dependencies": [ @@ -60,7 +60,7 @@ "bearssl_pkey_decoder" ], "checksums": { - "sha1": "bcfd6fc9c5e10a52b87117219b7ab5c98136bc8e" + "sha1": "3cd368666fd2bc7f99f253452289e827abcac13c" } }, "testutils": { @@ -434,8 +434,8 @@ } }, "websock": { - "version": "0.3.0", - "vcsRevision": "c105d98e6522e0e2cbe3dfa11b07a273e9fd0e7b", + "version": "0.4.0", + "vcsRevision": "387a8eb7e961e8fdd3b1a717d36bc53b55e4dc5d", "url": "https://github.com/status-im/nim-websock", "downloadMethod": "git", "dependencies": [ @@ -450,12 +450,12 @@ "zlib" ], "checksums": { - "sha1": "1294a66520fa4541e261dec8a6a84f774fb8c0ac" + "sha1": "fd17a854686d9a19af40aba51f05d06b82fc30ec" } }, "json_rpc": { - "version": "#43bbf499143eb45046c83ac9794c9e3280a2b8e7", - "vcsRevision": "43bbf499143eb45046c83ac9794c9e3280a2b8e7", + "version": "0.6.1", + "vcsRevision": "6f1fff8ba685c9192fab153a9d66484ad9066e78", "url": "https://github.com/status-im/nim-json-rpc.git", "downloadMethod": "git", "dependencies": [ @@ -472,12 +472,12 @@ "unittest2" ], "checksums": { - "sha1": "30ff6ead115b88c79862c5c7e37b1c9852eea59f" + "sha1": "596db0aafcb3c83f5dba6d42993f2276e0d00eb5" } }, "lsquic": { - "version": "0.0.1", - "vcsRevision": "4fb03ee7bfb39aecb3316889fdcb60bec3d0936f", + "version": "0.5.1", + "vcsRevision": "2f01046bf1d513de8b5f8296c3d8bec819ab0cb9", "url": "https://github.com/vacp2p/nim-lsquic", "downloadMethod": "git", "dependencies": [ @@ -487,10 +487,11 @@ "chronos", "nimcrypto", "unittest2", - "chronicles" + "chronicles", + "boringssl" ], "checksums": { - "sha1": "f465fa994346490d0924d162f53d9b5aec62f948" + "sha1": "959df1a9ac2a574d6fe30a4faf37c37b443e1cfb" } }, "secp256k1": { @@ -582,8 +583,8 @@ } }, "libp2p": { - "version": "#ff8d51857b4b79a68468e7bcc27b2026cca02996", - "vcsRevision": "ff8d51857b4b79a68468e7bcc27b2026cca02996", + "version": "2.0.0", + "vcsRevision": "c43199378f46d0aaf61be1cad1ee1d63e8f665d6", "url": "https://github.com/vacp2p/nim-libp2p.git", "downloadMethod": "git", "dependencies": [ @@ -591,20 +592,22 @@ "nimcrypto", "dnsclient", "bearssl", + "boringssl", "chronicles", "chronos", "metrics", "secp256k1", "stew", - "websock", "unittest2", "results", "serialization", "lsquic", + "protobuf_serialization", + "websock", "jwt" ], "checksums": { - "sha1": "fa2a7552c6ec860717b77ce34cf0b7afe4570234" + "sha1": "327dc7a0cb7e9d0be3d6083841bd496c4cbc48dc" } }, "taskpools": { @@ -653,6 +656,67 @@ "checksums": { "sha1": "6f9d49375ea1dc71add55c72ac80a808f238e5b0" } + }, + "boringssl": { + "version": "0.0.8", + "vcsRevision": "e77caabae78fbc9aa5b78a0a521181b077c82571", + "url": "https://github.com/vacp2p/nim-boringssl", + "downloadMethod": "git", + "dependencies": [ + "nim" + ], + "checksums": { + "sha1": "2f603bb6d70683393bbb091bf6bd325d9c52be9f" + } + }, + "protobuf_serialization": { + "version": "0.4.0", + "vcsRevision": "38d24eb3bd93e605fb88199da71d36b1ec0ad60d", + "url": "https://github.com/status-im/nim-protobuf-serialization", + "downloadMethod": "git", + "dependencies": [ + "nim", + "stew", + "faststreams", + "serialization", + "npeg", + "unittest2" + ], + "checksums": { + "sha1": "5a7a80fb8cca29e41899ce9540b74e49c874f8fd" + } + }, + "npeg": { + "version": "1.3.0", + "vcsRevision": "409f6796d0e880b3f0222c964d1da7de6e450811", + "url": "https://github.com/zevv/npeg", + "downloadMethod": "git", + "dependencies": [ + "nim" + ], + "checksums": { + "sha1": "64f15c85a059c889cb11c5fe72372677c50da621" + } + }, + "libp2p_mix": { + "version": "0.1.0", + "vcsRevision": "380513117d556bf8f70066f5e72a7fd74fe36ba6", + "url": "https://github.com/logos-co/nim-libp2p-mix", + "downloadMethod": "git", + "dependencies": [ + "nim", + "libp2p", + "chronicles", + "chronos", + "metrics", + "nimcrypto", + "stew", + "results", + "unittest2" + ], + "checksums": { + "sha1": "ccfb0f0160ac15ac970471964c730d57edacad91" + } } }, "tasks": {} diff --git a/nix/deps.nix b/nix/deps.nix index 8e4453675..1f2735457 100644 --- a/nix/deps.nix +++ b/nix/deps.nix @@ -26,8 +26,8 @@ jwt = pkgs.fetchgit { url = "https://github.com/vacp2p/nim-jwt.git"; - rev = "18f8378de52b241f321c1f9ea905456e89b95c6f"; - sha256 = "1986czmszdxj6g9yr7xn1fx8y2y9mwpb3f1bn9nc6973qawsdm0p"; + rev = "057ec95eb5af0eea9c49bfe9025b3312c95dc5f2"; + sha256 = "1hnxsl5762fdn80hl4xxfdqrcgmxrrfck66js1iqaqfxc4m0fv63"; fetchSubmodules = true; }; @@ -159,8 +159,8 @@ brokers = pkgs.fetchgit { url = "https://github.com/NagyZoltanPeter/nim-brokers.git"; - rev = "2093ca4d50e581adda73fee7fd16231f990f4cbe"; - sha256 = "0a4ix2q6riqfrd0hfnajisy159qdmk5imwzymppj23rwc8n7d2dx"; + rev = "a7316a35f1b62e3497ae8ee0fc1aace74df0beb2"; + sha256 = "1990270n88jm0i48g07zr4vq2nn79g7gymf28f3g5ak42g33l7rm"; fetchSubmodules = true; }; @@ -215,22 +215,22 @@ websock = pkgs.fetchgit { url = "https://github.com/status-im/nim-websock"; - rev = "c105d98e6522e0e2cbe3dfa11b07a273e9fd0e7b"; - sha256 = "1zrigw27nwcmg7mw9867581ipcp3ckrqq3cwl2snabcjhkp5dm2c"; + rev = "387a8eb7e961e8fdd3b1a717d36bc53b55e4dc5d"; + sha256 = "1v0m3x96fbp9jdzsys6mbxxc2xw3k3dqiv7wksfla89gc6z8w377"; fetchSubmodules = true; }; json_rpc = pkgs.fetchgit { url = "https://github.com/status-im/nim-json-rpc.git"; - rev = "43bbf499143eb45046c83ac9794c9e3280a2b8e7"; - sha256 = "1c1msxg958jm2ggvs875b6wh6n829d3lh7x4ch6dcxawda16qf95"; + rev = "6f1fff8ba685c9192fab153a9d66484ad9066e78"; + sha256 = "1r4xlis5fxcmp1cdqskb25nzmxckfkl8lndshvl76kcqrb0hl88d"; fetchSubmodules = true; }; lsquic = pkgs.fetchgit { url = "https://github.com/vacp2p/nim-lsquic"; - rev = "4fb03ee7bfb39aecb3316889fdcb60bec3d0936f"; - sha256 = "0qdhcd4hyp185szc9sv3jvwdwc9zp3j0syy7glxv13k9bchfmkfg"; + rev = "2f01046bf1d513de8b5f8296c3d8bec819ab0cb9"; + sha256 = "16xj0hx13nc95x7scck8lyir0kzx4wqnd6d4h79aai8j9hlcmbsl"; fetchSubmodules = true; }; @@ -264,8 +264,8 @@ libp2p = pkgs.fetchgit { url = "https://github.com/vacp2p/nim-libp2p.git"; - rev = "ff8d51857b4b79a68468e7bcc27b2026cca02996"; - sha256 = "08y4s0zhqzsd780bwaixfqbi79km0mcq5g8nyw7awfvcbjqsa53l"; + rev = "c43199378f46d0aaf61be1cad1ee1d63e8f665d6"; + sha256 = "0q1hkwwz08zfdwwz7cfql1hqil0iyv3dn8jypdwqmg7497l1bmxk"; fetchSubmodules = true; }; @@ -278,8 +278,8 @@ sds = pkgs.fetchgit { url = "https://github.com/logos-messaging/nim-sds.git"; - rev = "2e9a7683f0e180bf112135fae3a3803eed8490d4"; - sha256 = "1dbpvp3zhvdlfxdyggz5waga1vg3b6ndd3acfzhnx8k1wdr01c6f"; + rev = "abdd40cc645f1b024c3ee99cced7e287c4e4c441"; + sha256 = "01k49sljxnzjy82jljcffwqkaqvhpj1aiz605gv429sbzgyfr8mm"; fetchSubmodules = true; }; @@ -290,4 +290,32 @@ fetchSubmodules = true; }; + boringssl = pkgs.fetchgit { + url = "https://github.com/vacp2p/nim-boringssl"; + rev = "e77caabae78fbc9aa5b78a0a521181b077c82571"; + sha256 = "15k4dqh1hlcpq9zm30lpr728h7apdmgm22xzqhdm3clq9kia6fr8"; + fetchSubmodules = true; + }; + + protobuf_serialization = pkgs.fetchgit { + url = "https://github.com/status-im/nim-protobuf-serialization"; + rev = "38d24eb3bd93e605fb88199da71d36b1ec0ad60d"; + sha256 = "0jr0a41b4r444si6xfa7bclw8mjsk6id10lrdvbxzp99750zspb9"; + fetchSubmodules = true; + }; + + npeg = pkgs.fetchgit { + url = "https://github.com/zevv/npeg"; + rev = "409f6796d0e880b3f0222c964d1da7de6e450811"; + sha256 = "1h2f5znbpa3svk7wsw2axn8f7f59d23xq85z148kiv6fqh0ffwbm"; + fetchSubmodules = true; + }; + + libp2p_mix = pkgs.fetchgit { + url = "https://github.com/logos-co/nim-libp2p-mix"; + rev = "380513117d556bf8f70066f5e72a7fd74fe36ba6"; + sha256 = "05zjf98nl2hxx62m9blk4yip2f31y44r5x4n98lmm5hghb7wbcpk"; + fetchSubmodules = true; + }; + } diff --git a/tests/common/test_ratelimit_setting.nim b/tests/common/test_ratelimit_setting.nim index e33f13db5..d8140381e 100644 --- a/tests/common/test_ratelimit_setting.nim +++ b/tests/common/test_ratelimit_setting.nim @@ -1,3 +1,4 @@ +import libp2p/crypto/crypto # Chronos Test Suite # (c) Copyright 2022-Present # Status Research & Development GmbH @@ -17,9 +18,10 @@ import ../../logos_delivery/waku/common/rate_limit/timed_map let proto = "ProtocolDescriptor" -let conn1 = Connection(peerId: PeerId.random().tryGet()) -let conn2 = Connection(peerId: PeerId.random().tryGet()) -let conn3 = Connection(peerId: PeerId.random().tryGet()) +let rng = newRng() +let conn1 = Connection(peerId: PeerId.random(rng).tryGet()) +let conn2 = Connection(peerId: PeerId.random(rng).tryGet()) +let conn3 = Connection(peerId: PeerId.random(rng).tryGet()) suite "RateLimitSetting": test "Parse rate limit setting - ok": diff --git a/tests/common/test_requestratelimiter.nim b/tests/common/test_requestratelimiter.nim index 9a662d0bb..bd055c0f6 100644 --- a/tests/common/test_requestratelimiter.nim +++ b/tests/common/test_requestratelimiter.nim @@ -1,3 +1,4 @@ +import libp2p/crypto/crypto # Chronos Test Suite # (c) Copyright 2022-Present # Status Research & Development GmbH @@ -17,9 +18,10 @@ import ../../logos_delivery/waku/common/rate_limit/timed_map let proto = "ProtocolDescriptor" -let conn1 = Connection(peerId: PeerId.random().tryGet()) -let conn2 = Connection(peerId: PeerId.random().tryGet()) -let conn3 = Connection(peerId: PeerId.random().tryGet()) +let rng = newRng() +let conn1 = Connection(peerId: PeerId.random(rng).tryGet()) +let conn2 = Connection(peerId: PeerId.random(rng).tryGet()) +let conn3 = Connection(peerId: PeerId.random(rng).tryGet()) suite "RequestRateLimiter": test "RequestRateLimiter Allow up to main bucket": diff --git a/tests/node/test_wakunode_filter.nim b/tests/node/test_wakunode_filter.nim index ebc529bfb..65c162c0a 100644 --- a/tests/node/test_wakunode_filter.nim +++ b/tests/node/test_wakunode_filter.nim @@ -1,3 +1,4 @@ +import libp2p/crypto/crypto {.used.} import @@ -20,9 +21,9 @@ import ../testlib/[common, wakucore, wakunode, testasync, futures, testutils], ../waku_filter_v2/waku_filter_utils -proc generateRequestId(rng: ref HmacDrbgContext): string = +proc generateRequestId(rng: crypto.Rng): string = var bytes: array[10, byte] - hmacDrbgGenerate(rng[], bytes) + rng.generate(bytes) return toHex(bytes) proc createRequest( @@ -30,7 +31,7 @@ proc createRequest( pubsubTopic = none(PubsubTopic), contentTopics = newSeq[ContentTopic](), ): FilterSubscribeRequest = - let requestId = generateRequestId(rng) + let requestId = generateRequestId(common.rng()) return FilterSubscribeRequest( requestId: requestId, diff --git a/tests/node/test_wakunode_legacy_lightpush.nim b/tests/node/test_wakunode_legacy_lightpush.nim index ec61c2cdf..4dfe21e9c 100644 --- a/tests/node/test_wakunode_legacy_lightpush.nim +++ b/tests/node/test_wakunode_legacy_lightpush.nim @@ -118,7 +118,10 @@ suite "RLN Proofs as a Lightpush Service": manager = waitFor setupOnchainGroupManager(deployContracts = false) # mount rln-relay - let wakuRlnConfig = getWakuRlnConfig(manager = manager, index = MembershipIndex(1)) + # match prod epoch window to reduce test flake + let wakuRlnConfig = getWakuRlnConfig( + manager = manager, index = MembershipIndex(1), epochSizeSec = 600 + ) await allFutures(server.start(), client.start()) await server.start() diff --git a/tests/node/test_wakunode_lightpush.nim b/tests/node/test_wakunode_lightpush.nim index be36b8d98..df6c7f8e0 100644 --- a/tests/node/test_wakunode_lightpush.nim +++ b/tests/node/test_wakunode_lightpush.nim @@ -116,7 +116,10 @@ suite "RLN Proofs as a Lightpush Service": manager = waitFor setupOnchainGroupManager(deployContracts = false) # mount rln-relay - let wakuRlnConfig = getWakuRlnConfig(manager = manager, index = MembershipIndex(1)) + # match prod epoch window to reduce test flake + let wakuRlnConfig = getWakuRlnConfig( + manager = manager, index = MembershipIndex(1), epochSizeSec = 600 + ) await allFutures(server.start(), client.start()) await server.start() diff --git a/tests/node/test_wakunode_peer_manager.nim b/tests/node/test_wakunode_peer_manager.nim index 09fa4cbe9..a43f76d97 100644 --- a/tests/node/test_wakunode_peer_manager.nim +++ b/tests/node/test_wakunode_peer_manager.nim @@ -27,8 +27,12 @@ import ../waku_discv5/utils, ./peer_manager/peer_store/utils -const DEFAULT_PROTOCOLS: seq[string] = - @["/ipfs/id/1.0.0", "/libp2p/autonat/1.0.0", "/libp2p/circuit/relay/0.2.0/hop"] +# nim-libp2p 2.0.0 enables the IdentifyPush protocol by default, so every node +# now also advertises "/ipfs/id/push/1.0.0" (prepended to the identify list). +const DEFAULT_PROTOCOLS: seq[string] = @[ + "/ipfs/id/push/1.0.0", "/ipfs/id/1.0.0", "/libp2p/autonat/1.0.0", + "/libp2p/circuit/relay/0.2.0/hop", +] let listenIp = parseIpAddress("0.0.0.0") @@ -375,7 +379,7 @@ suite "Peer Manager": chainedComparison( clientPeerStore[AgentBook][serverPeerId], # FIXME: Not assigned serverRemotePeerInfo.agent, - "nim-libp2p/0.0.1", + "nim-libp2p", ) chainedComparison( clientPeerStore[ProtoVersionBook][serverPeerId], # FIXME: Not assigned @@ -441,7 +445,7 @@ suite "Peer Manager": chainedComparison( clientPeerStore[AgentBook][serverPeerId], # FIXME: Not assigned serverRemotePeerInfo.agent, - "nim-libp2p/0.0.1", + "nim-libp2p", ) chainedComparison( clientPeerStore[ProtoVersionBook][serverPeerId], # FIXME: Not assigned @@ -492,7 +496,7 @@ suite "Peer Manager": chainedComparison( clientPeerStore[AgentBook][server2PeerId], # FIXME: Not assigned server2RemotePeerInfo.agent, - "nim-libp2p/0.0.1", + "nim-libp2p", ) chainedComparison( clientPeerStore[ProtoVersionBook][server2PeerId], # FIXME: Not assigned diff --git a/tests/node/test_wakunode_relay_rln.nim b/tests/node/test_wakunode_relay_rln.nim index 0105e9a2f..c53b3d571 100644 --- a/tests/node/test_wakunode_relay_rln.nim +++ b/tests/node/test_wakunode_relay_rln.nim @@ -31,10 +31,10 @@ import proc buildRandomIdentityCredentials(): IdentityCredential = # We generate a random identity credential (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen) let - idTrapdoor = randomSeqByte(rng[], 32) - idNullifier = randomSeqByte(rng[], 32) - idSecretHash = randomSeqByte(rng[], 32) - idCommitment = randomSeqByte(rng[], 32) + idTrapdoor = randomSeqByte(rng, 32) + idNullifier = randomSeqByte(rng, 32) + idSecretHash = randomSeqByte(rng, 32) + idCommitment = randomSeqByte(rng, 32) IdentityCredential( idTrapdoor: idTrapdoor, diff --git a/tests/test_helpers.nim b/tests/test_helpers.nim index bd1d837b6..a4bb69fbc 100644 --- a/tests/test_helpers.nim +++ b/tests/test_helpers.nim @@ -1,3 +1,4 @@ +import libp2p/crypto/rng import chronos, bearssl/rand, eth/[keys, p2p] import libp2p/crypto/crypto @@ -9,10 +10,10 @@ proc localAddress*(port: int): Address = result = Address(udpPort: port, tcpPort: port, ip: parseIpAddress("127.0.0.1")) proc setupTestNode*( - rng: ref HmacDrbgContext, capabilities: varargs[ProtocolInfo, `protocolInfo`] + rng: crypto.Rng, capabilities: varargs[ProtocolInfo, `protocolInfo`] ): EthereumNode = let - keys1 = keys.KeyPair.random(rng[]) + keys1 = keys.KeyPair.random(keys.newRng()[]) address = localAddress(nextPort) result = newEthereumNode( keys1, @@ -29,11 +30,11 @@ proc setupTestNode*( # Copied from here: https://github.com/status-im/nim-libp2p/blob/d522537b19a532bc4af94fcd146f779c1f23bad0/tests/helpers.nim#L28 type RngWrap = object - rng: ref rand.HmacDrbgContext + rng: crypto.Rng var rngVar: RngWrap -proc getRng(): ref rand.HmacDrbgContext = +proc getRng(): crypto.Rng = # TODO if `rngVar` is a threadvar like it should be, there are random and # spurious compile failures on mac - this is not gcsafe but for the # purpose of the tests, it's ok as long as we only use a single thread @@ -42,5 +43,5 @@ proc getRng(): ref rand.HmacDrbgContext = rngVar.rng = crypto.newRng() rngVar.rng -template rng*(): ref rand.HmacDrbgContext = +template rng*(): crypto.Rng = getRng() diff --git a/tests/test_peer_manager.nim b/tests/test_peer_manager.nim index 1ad29b90b..1505a686f 100644 --- a/tests/test_peer_manager.nim +++ b/tests/test_peer_manager.nim @@ -1,7 +1,7 @@ {.used.} import - std/[sequtils, times, sugar, net], + std/[sequtils, times, sugar, net, options], testutils/unittests, chronos, json_rpc/rpcserver, @@ -955,7 +955,8 @@ procSuite "Peer Manager": # Create peer manager let pm = PeerManager.new( - switch = SwitchBuilder.new().withRng(rng).withMplex().withNoise().build(), + switch = + SwitchBuilder.new().withRng(crypto.newRng()).withMplex().withNoise().build(), storage = nil, ) @@ -1013,7 +1014,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(peerStoreSize) @@ -1027,7 +1028,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(25) @@ -1040,7 +1041,9 @@ procSuite "Peer Manager": # Create 30 peers and add them to the peerstore let peers = toSeq(1 .. 30) - .mapIt(parsePeerInfo("/ip4/0.0.0.0/tcp/0/p2p/" & $PeerId.random().get())) + .mapIt( + parsePeerInfo("/ip4/0.0.0.0/tcp/0/p2p/" & $PeerId.random(crypto.newRng()).get()) + ) .filterIt(it.isOk()) .mapIt(it.value) for p in peers: @@ -1091,7 +1094,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(25) @@ -1148,7 +1151,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(25) @@ -1164,7 +1167,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(25) @@ -1178,7 +1181,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(25) diff --git a/tests/test_waku_dnsdisc.nim b/tests/test_waku_dnsdisc.nim index 848f2c441..d0430adb0 100644 --- a/tests/test_waku_dnsdisc.nim +++ b/tests/test_waku_dnsdisc.nim @@ -1,3 +1,4 @@ +import libp2p/crypto/rng {.used.} import @@ -52,7 +53,7 @@ suite "Waku DNS Discovery": ) .get() # No link entries - let treeKeys = keys.KeyPair.random(rng[]) + let treeKeys = keys.KeyPair.random(keys.newRng()[]) # Sign tree check: diff --git a/tests/test_waku_keepalive.nim b/tests/test_waku_keepalive.nim index 8c4845f55..d8412f2c5 100644 --- a/tests/test_waku_keepalive.nim +++ b/tests/test_waku_keepalive.nim @@ -43,7 +43,7 @@ suite "Waku Keepalive": (await node2.mountRelay()).isOkOr: assert false, "Failed to mount relay" - let pingProto = Ping.new(handler = pingHandler) + let pingProto = Ping.new(handler = pingHandler, rng = crypto.newRng()) await pingProto.start() node2.switch.mount(pingProto) diff --git a/tests/test_waku_keystore.nim b/tests/test_waku_keystore.nim index 89304380a..718248c59 100644 --- a/tests/test_waku_keystore.nim +++ b/tests/test_waku_keystore.nim @@ -44,10 +44,10 @@ procSuite "Credentials test suite": # We generate a random identity credential (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen) var - idTrapdoor = randomSeqByte(rng[], 32) - idNullifier = randomSeqByte(rng[], 32) - idSecretHash = randomSeqByte(rng[], 32) - idCommitment = randomSeqByte(rng[], 32) + idTrapdoor = randomSeqByte(rng, 32) + idNullifier = randomSeqByte(rng, 32) + idSecretHash = randomSeqByte(rng, 32) + idCommitment = randomSeqByte(rng, 32) var idCredential = IdentityCredential( idTrapdoor: idTrapdoor, @@ -83,10 +83,10 @@ procSuite "Credentials test suite": # We generate two random identity credentials (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen) var - idTrapdoor = randomSeqByte(rng[], 32) - idNullifier = randomSeqByte(rng[], 32) - idSecretHash = randomSeqByte(rng[], 32) - idCommitment = randomSeqByte(rng[], 32) + idTrapdoor = randomSeqByte(rng, 32) + idNullifier = randomSeqByte(rng, 32) + idSecretHash = randomSeqByte(rng, 32) + idCommitment = randomSeqByte(rng, 32) idCredential = IdentityCredential( idTrapdoor: idTrapdoor, idNullifier: idNullifier, @@ -139,10 +139,10 @@ procSuite "Credentials test suite": # We generate random identity credentials (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen) let - idTrapdoor = randomSeqByte(rng[], 32) - idNullifier = randomSeqByte(rng[], 32) - idSecretHash = randomSeqByte(rng[], 32) - idCommitment = randomSeqByte(rng[], 32) + idTrapdoor = randomSeqByte(rng, 32) + idNullifier = randomSeqByte(rng, 32) + idSecretHash = randomSeqByte(rng, 32) + idCommitment = randomSeqByte(rng, 32) idCredential = IdentityCredential( idTrapdoor: idTrapdoor, idNullifier: idNullifier, @@ -191,10 +191,10 @@ procSuite "Credentials test suite": # We generate random identity credentials (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen) let - idTrapdoor = randomSeqByte(rng[], 32) - idNullifier = randomSeqByte(rng[], 32) - idSecretHash = randomSeqByte(rng[], 32) - idCommitment = randomSeqByte(rng[], 32) + idTrapdoor = randomSeqByte(rng, 32) + idNullifier = randomSeqByte(rng, 32) + idSecretHash = randomSeqByte(rng, 32) + idCommitment = randomSeqByte(rng, 32) idCredential = IdentityCredential( idTrapdoor: idTrapdoor, idNullifier: idNullifier, @@ -252,10 +252,10 @@ procSuite "Credentials test suite": # We generate random identity credentials (inter-value constrains are not enforced, otherwise we need to load e.g. zerokit RLN keygen) let - idTrapdoor = randomSeqByte(rng[], 32) - idNullifier = randomSeqByte(rng[], 32) - idSecretHash = randomSeqByte(rng[], 32) - idCommitment = randomSeqByte(rng[], 32) + idTrapdoor = randomSeqByte(rng, 32) + idNullifier = randomSeqByte(rng, 32) + idSecretHash = randomSeqByte(rng, 32) + idCommitment = randomSeqByte(rng, 32) idCredential = IdentityCredential( idTrapdoor: idTrapdoor, idNullifier: idNullifier, diff --git a/tests/test_waku_keystore_keyfile.nim b/tests/test_waku_keystore_keyfile.nim index 8c19f82f3..445cf6541 100644 --- a/tests/test_waku_keystore_keyfile.nim +++ b/tests/test_waku_keystore_keyfile.nim @@ -14,7 +14,7 @@ suite "KeyFile test suite": removeFile(filepath) # The secret - var secret = randomSeqByte(rng[], 300) + var secret = randomSeqByte(rng, 300) # We create a keyfile encrypting the secret with password let keyfile = createKeyFileJson(secret, password) @@ -40,9 +40,9 @@ suite "KeyFile test suite": test "Create/Save/Load multiple keyfiles in same file": # We set different passwords for different keyfiles that will be stored in same file - let password1 = string.fromBytes(randomSeqByte(rng[], 20)) + let password1 = string.fromBytes(randomSeqByte(rng, 20)) let password2 = "" - let password3 = string.fromBytes(randomSeqByte(rng[], 20)) + let password3 = string.fromBytes(randomSeqByte(rng, 20)) var keyfile: KfResult[JsonNode] let filepath = "./test.keyfile" @@ -51,40 +51,40 @@ suite "KeyFile test suite": # We generate 6 different secrets and we encrypt them using 3 different passwords, and we store the obtained keystore - let secret1 = randomSeqByte(rng[], 300) + let secret1 = randomSeqByte(rng, 300) keyfile = createKeyFileJson(secret1, password1) check: keyfile.isOk() saveKeyFile(filepath, keyfile.get()).isOk() - let secret2 = randomSeqByte(rng[], 300) + let secret2 = randomSeqByte(rng, 300) keyfile = createKeyFileJson(secret2, password2) check: keyfile.isOk() saveKeyFile(filepath, keyfile.get()).isOk() - let secret3 = randomSeqByte(rng[], 300) + let secret3 = randomSeqByte(rng, 300) keyfile = createKeyFileJson(secret3, password3) check: keyfile.isOk() saveKeyFile(filepath, keyfile.get()).isOk() # We encrypt secret4 with password3 - let secret4 = randomSeqByte(rng[], 300) + let secret4 = randomSeqByte(rng, 300) keyfile = createKeyFileJson(secret4, password3) check: keyfile.isOk() saveKeyFile(filepath, keyfile.get()).isOk() # We encrypt secret5 with password1 - let secret5 = randomSeqByte(rng[], 300) + let secret5 = randomSeqByte(rng, 300) keyfile = createKeyFileJson(secret5, password1) check: keyfile.isOk() saveKeyFile(filepath, keyfile.get()).isOk() # We encrypt secret6 with password1 - let secret6 = randomSeqByte(rng[], 300) + let secret6 = randomSeqByte(rng, 300) keyfile = createKeyFileJson(secret6, password1) check: keyfile.isOk() @@ -341,7 +341,7 @@ suite "KeyFile test suite (adapted from nim-eth keyfile tests)": test "Scrypt keyfiles": let - expectedSecret = randomSeqByte(rng[], 300) + expectedSecret = randomSeqByte(rng, 300) password = "miawmiawcat" # By default, keyfiles' encryption key is derived from password using PBKDF2. diff --git a/tests/test_waku_netconfig.nim b/tests/test_waku_netconfig.nim index 697777233..7e8cc1a2b 100644 --- a/tests/test_waku_netconfig.nim +++ b/tests/test_waku_netconfig.nim @@ -1,3 +1,4 @@ +import std/options {.used.} import chronos, confutils/toml/std/net, libp2p/multiaddress, testutils/unittests diff --git a/tests/test_wakunode.nim b/tests/test_wakunode.nim index 574e35d7b..e52a8089f 100644 --- a/tests/test_wakunode.nim +++ b/tests/test_wakunode.nim @@ -1,7 +1,7 @@ {.used.} import - std/[sequtils, strutils, net], + std/[sequtils, strutils, net, options], stew/byteutils, testutils/unittests, chronicles, @@ -294,8 +294,8 @@ suite "WakuNode": # custom agent string expectedAgentString1 = "node1-agent-string" - # bump when updating nim-libp2p - expectedAgentString2 = "nim-libp2p/0.0.1" + # bump when updating nim-libp2p (2.0.0 default AgentVersion is "nim-libp2p") + expectedAgentString2 = "nim-libp2p" let # node with custom agent string nodeKey1 = generateSecp256k1Key() diff --git a/tests/testlib/common.nim b/tests/testlib/common.nim index ddd80b71b..1ad9d8178 100644 --- a/tests/testlib/common.nim +++ b/tests/testlib/common.nim @@ -1,4 +1,4 @@ -import std/[times, random], bearssl/rand, libp2p/crypto/crypto +import std/[times, random], bearssl/rand, libp2p/crypto/crypto, libp2p/crypto/rng ## Randomization @@ -12,13 +12,13 @@ proc randomize*() = # Copied from here: https://github.com/status-im/nim-libp2p/blob/d522537b19a532bc4af94fcd146f779c1f23bad0/tests/helpers.nim#L28 type Rng = object - rng: ref HmacDrbgContext + rng: crypto.Rng # Typically having a module variable is considered bad design. This case should # be considered as an exception and it should be used only in the tests. var rngVar: Rng -proc getRng(): ref HmacDrbgContext = +proc getRng(): crypto.Rng = # TODO: if `rngVar` is a threadvar like it should be, there are random and # spurious compile failures on mac - this is not gcsafe but for the # purpose of the tests, it's ok as long as we only use a single thread @@ -28,10 +28,10 @@ proc getRng(): ref HmacDrbgContext = rngVar.rng -template rng*(): ref HmacDrbgContext = +template rng*(): crypto.Rng = getRng() -proc randomSeqByte*(rng: var HmacDrbgContext, size: int): seq[byte] = +proc randomSeqByte*(rng: crypto.Rng, size: int): seq[byte] = var output = newSeq[byte](size.uint32) - hmacDrbgGenerate(rng, output) + hmacDrbgGenerate(rng.bearSslDrbg, output) return output diff --git a/tests/testlib/wakucore.nim b/tests/testlib/wakucore.nim index d681adc31..e1d0f8143 100644 --- a/tests/testlib/wakucore.nim +++ b/tests/testlib/wakucore.nim @@ -22,17 +22,44 @@ proc ts*(offset = 0, origin = now()): Timestamp = # Switch proc generateEcdsaKey*(): libp2p_keys.PrivateKey = - libp2p_keys.PrivateKey.random(ECDSA, rng[]).get() + libp2p_keys.PrivateKey.random(ECDSA, common.rng()).get() proc generateEcdsaKeyPair*(): libp2p_keys.KeyPair = - libp2p_keys.KeyPair.random(ECDSA, rng[]).get() + libp2p_keys.KeyPair.random(ECDSA, common.rng()).get() proc generateSecp256k1Key*(): libp2p_keys.PrivateKey = - libp2p_keys.PrivateKey.random(Secp256k1, rng[]).get() + libp2p_keys.PrivateKey.random(Secp256k1, common.rng()).get() proc ethSecp256k1Key*(hex: string): eth_keys.PrivateKey = eth_keys.PrivateKey.fromHex(hex).get() +proc newStandardSwitch*( + privKey = Opt.none(libp2p_keys.PrivateKey), + addrs: MultiAddress = MultiAddress.init("/ip4/127.0.0.1/tcp/0").get(), +): Switch = + ## Bare libp2p switch for tests. Replaces nim-libp2p's `newStandardSwitch`, + ## removed in 2.0.0. Mirrors the *substrate* of the production switch + ## (`newWakuSwitch`, waku/node/waku_switch.nim): same transport (TCP only — no + ## QUIC yet, like prod), security (Noise), and muxers (yamux first so it is the + ## one negotiated, then mplex). Tests therefore run on the same kernel peers + ## actually use in prod. NB: the removed libp2p `newStandardSwitch` was + ## mplex-only; mounting yamux here is intentional (match prod's muxer) — do not + ## "restore" it to mplex-only. Deliberately omits the prod services (autonat, circuit + ## relay, name resolver, NAT, signed peer record, connection limits): they add + ## port grief, network delays, and nondeterminism with no value to unit tests. + ## For a full-stack node, use newTestWakuNode / newWakuSwitch instead. + var b = SwitchBuilder + .new() + .withRng(common.rng()) + .withAddress(addrs) + .withTcpTransport() + .withYamux() + .withMplex() + .withNoise() + if privKey.isSome(): + b = b.withPrivateKey(privKey.get()) + b.build() + proc newTestSwitch*( key = none(libp2p_keys.PrivateKey), address = none(MultiAddress) ): Switch = diff --git a/tests/testlib/wakunode.nim b/tests/testlib/wakunode.nim index bab8c5764..aeb7037ee 100644 --- a/tests/testlib/wakunode.nim +++ b/tests/testlib/wakunode.nim @@ -12,6 +12,7 @@ import waku_node, net/net_config, waku_core/topics, + node/waku_switch, node/peer_manager, waku_enr, discovery/waku_discv5, @@ -56,7 +57,7 @@ proc newTestWakuNode*( extPort = none(Port), extMultiAddrs = newSeq[MultiAddress](), peerStorage: PeerStorage = nil, - maxConnections = builders.MaxConnections, + maxConnections = MaxConnections, wsBindPort: Port = (Port) 8000, wsEnabled: bool = false, wssEnabled: bool = false, diff --git a/tests/waku_discv5/test_waku_discv5.nim b/tests/waku_discv5/test_waku_discv5.nim index e7c53a58d..b5d34486f 100644 --- a/tests/waku_discv5/test_waku_discv5.nim +++ b/tests/waku_discv5/test_waku_discv5.nim @@ -1,3 +1,4 @@ +import libp2p/crypto/rng {.used.} import @@ -37,8 +38,8 @@ suite "Waku Discovery v5": let rng = eth_keys.newRng() - pk1 = eth_keys.PrivateKey.random(rng[]) - pk2 = eth_keys.PrivateKey.random(rng[]) + pk1 = eth_keys.PrivateKey.random(eth_keys.newRng()[]) + pk2 = eth_keys.PrivateKey.random(eth_keys.newRng()[]) suite "shardingPredicate": var @@ -422,7 +423,7 @@ suite "Waku Discovery v5": let myRng = libp2p_keys.newRng() var confBuilder = defaultTestWakuConfBuilder() - confBuilder.withNodeKey(libp2p_keys.PrivateKey.random(Secp256k1, myRng[])[]) + confBuilder.withNodeKey(libp2p_keys.PrivateKey.random(Secp256k1, myRng)[]) confBuilder.discv5Conf.withEnabled(true) confBuilder.discv5Conf.withUdpPort(9000.Port) let conf = confBuilder.build().valueOr: @@ -433,7 +434,7 @@ suite "Waku Discovery v5": (waitFor waku0.start()).isOkOr: raiseAssert error - confBuilder.withNodeKey(crypto.PrivateKey.random(Secp256k1, myRng[])[]) + confBuilder.withNodeKey(crypto.PrivateKey.random(Secp256k1, myRng)[]) confBuilder.discv5Conf.withBootstrapNodes(@[waku0.node.enr.toURI()]) confBuilder.discv5Conf.withEnabled(true) confBuilder.discv5Conf.withUdpPort(9001.Port) @@ -453,7 +454,7 @@ suite "Waku Discovery v5": confBuilder.discv5Conf.withBootstrapNodes(@[waku1.node.enr.toURI()]) confBuilder.withP2pTcpPort(60003.Port) confBuilder.discv5Conf.withUdpPort(9003.Port) - confBuilder.withNodeKey(crypto.PrivateKey.random(Secp256k1, myRng[])[]) + confBuilder.withNodeKey(crypto.PrivateKey.random(Secp256k1, myRng)[]) let conf2 = confBuilder.build().valueOr: raiseAssert error diff --git a/tests/waku_filter_v2/waku_filter_utils.nim b/tests/waku_filter_v2/waku_filter_utils.nim index fce601a09..d03922998 100644 --- a/tests/waku_filter_v2/waku_filter_utils.nim +++ b/tests/waku_filter_v2/waku_filter_utils.nim @@ -36,7 +36,7 @@ proc newTestWakuFilter*( proc newTestWakuFilterClient*(switch: Switch): Future[WakuFilterClient] {.async.} = let peerManager = PeerManager.new(switch) - proto = WakuFilterClient.new(peerManager, rng) + proto = WakuFilterClient.new(peerManager, common.rng()) await proto.start() switch.mount(proto) diff --git a/tests/waku_lightpush/lightpush_utils.nim b/tests/waku_lightpush/lightpush_utils.nim index 8eb133d99..d4a8b12e2 100644 --- a/tests/waku_lightpush/lightpush_utils.nim +++ b/tests/waku_lightpush/lightpush_utils.nim @@ -20,7 +20,7 @@ proc newTestWakuLightpushNode*( peerManager = PeerManager.new(switch) wakuAutoSharding = Sharding(clusterId: 1, shardCountGenZero: 8) proto = WakuLightPush.new( - peerManager, rng, handler, some(wakuAutoSharding), rateLimitSetting + peerManager, crypto.newRng(), handler, some(wakuAutoSharding), rateLimitSetting ) await proto.start() @@ -30,4 +30,4 @@ proc newTestWakuLightpushNode*( proc newTestWakuLightpushClient*(switch: Switch): WakuLightPushClient = let peerManager = PeerManager.new(switch) - WakuLightPushClient.new(peerManager, rng) + WakuLightPushClient.new(peerManager, crypto.newRng()) diff --git a/tests/waku_lightpush_legacy/lightpush_utils.nim b/tests/waku_lightpush_legacy/lightpush_utils.nim index 1e549859b..e64f5d69f 100644 --- a/tests/waku_lightpush_legacy/lightpush_utils.nim +++ b/tests/waku_lightpush_legacy/lightpush_utils.nim @@ -19,7 +19,8 @@ proc newTestWakuLegacyLightpushNode*( ): Future[WakuLegacyLightPush] {.async.} = let peerManager = PeerManager.new(switch) - proto = WakuLegacyLightPush.new(peerManager, rng, handler, rateLimitSetting) + proto = + WakuLegacyLightPush.new(peerManager, crypto.newRng(), handler, rateLimitSetting) await proto.start() switch.mount(proto) @@ -28,4 +29,4 @@ proc newTestWakuLegacyLightpushNode*( proc newTestWakuLegacyLightpushClient*(switch: Switch): WakuLegacyLightPushClient = let peerManager = PeerManager.new(switch) - WakuLegacyLightPushClient.new(peerManager, rng) + WakuLegacyLightPushClient.new(peerManager, crypto.newRng()) diff --git a/tests/waku_relay/test_protocol.nim b/tests/waku_relay/test_protocol.nim index 77466d32a..a2042627e 100644 --- a/tests/waku_relay/test_protocol.nim +++ b/tests/waku_relay/test_protocol.nim @@ -1,7 +1,7 @@ {.used.} import - std/[options, strformat], + std/[options, strformat, sets, tables], testutils/unittests, chronos, libp2p/protocols/pubsub/[pubsub, gossipsub], @@ -21,6 +21,23 @@ import ./utils, ../resources/payloads +proc hasMeshPeer(relay: WakuRelay, topic: PubsubTopic, peer: PeerId): bool = + for p in relay.mesh.getOrDefault(topic): + if p.peerId == peer: + return true + false + +proc waitForMeshPeer( + relay: WakuRelay, topic: PubsubTopic, peer: PeerId, timeout = 10.seconds +): Future[bool] {.async.} = + ## Wait until `relay`'s gossipsub mesh for `topic` has GRAFTed `peer`. + let deadline = Moment.now() + timeout + while Moment.now() < deadline: + if relay.hasMeshPeer(topic, peer): + return true + await sleepAsync(50.milliseconds) + false + suite "Waku Relay": var messageSeq {.threadvar.}: seq[(PubsubTopic, WakuMessage)] var handlerFuture {.threadvar.}: Future[(PubsubTopic, WakuMessage)] @@ -677,6 +694,11 @@ suite "Waku Relay": anotherPeerManager.switch.isConnected(otherPeerId) otherPeerManager.switch.isConnected(anotherPeerId) + # Wait for the mesh to re-form before publishing (else it races the 1s FUTURE_TIMEOUT). + check: + await otherNode.waitForMeshPeer(pubsubTopicC, anotherPeerId) + await anotherNode.waitForMeshPeer(pubsubTopicC, otherPeerId) + # When publishing a message in anotherNode for each of the pubsub topics handlerFuture = newPushHandlerFuture() handlerFuture2 = newPushHandlerFuture() @@ -1214,101 +1236,6 @@ suite "Waku Relay": await allFutures(otherSwitch.stop(), otherNode.stop()) suite "Security and Privacy": - asyncTest "Relay can receive messages after reboot and reconnect": - # Given a second node connected to the first one - let - otherSwitch = newTestSwitch() - otherPeerManager = PeerManager.new(otherSwitch) - otherNode = await newTestWakuRelay(otherSwitch) - - await otherSwitch.start() - let - otherRemotePeerInfo = otherSwitch.peerInfo.toRemotePeerInfo() - otherPeerId = otherRemotePeerInfo.peerId - - check await peerManager.connectPeer(otherRemotePeerInfo) - - # Given both are subscribed to the same pubsub topic - var otherHandlerFuture = newPushHandlerFuture() - proc otherSimpleFutureHandler( - topic: PubsubTopic, message: WakuMessage - ) {.async, gcsafe.} = - otherHandlerFuture.complete((topic, message)) - - otherNode.subscribe(pubsubTopic, otherSimpleFutureHandler) - node.subscribe(pubsubTopic, simpleFutureHandler) - check: - node.subscribedTopics == pubsubTopicSeq - otherNode.subscribedTopics == pubsubTopicSeq - await sleepAsync(500.millis) - - # Given other node is stopped and restarted - await otherSwitch.stop() - await otherSwitch.start() - - check await peerManager.connectPeer(otherRemotePeerInfo) - - # FIXME: Once stopped and started, nodes are not considered connected, nor do they reconnect after running connectPeer, as below - # check await otherPeerManager.connectPeer(otherRemotePeerInfo) - - # When sending a message from node - let msg1 = fakeWakuMessage(testMessage, pubsubTopic) - discard await node.publish(pubsubTopic, msg1) - - # Then the message is received in both nodes - check: - await handlerFuture.withTimeout(FUTURE_TIMEOUT) - await otherHandlerFuture.withTimeout(FUTURE_TIMEOUT) - (pubsubTopic, msg1) == handlerFuture.read() - (pubsubTopic, msg1) == otherHandlerFuture.read() - - # When sending a message from other node - handlerFuture = newPushHandlerFuture() - otherHandlerFuture = newPushHandlerFuture() - let msg2 = fakeWakuMessage(testMessage, pubsubTopic) - discard await otherNode.publish(pubsubTopic, msg2) - - # Then the message is received in both nodes - check: - await handlerFuture.withTimeout(FUTURE_TIMEOUT) - await otherHandlerFuture.withTimeout(FUTURE_TIMEOUT) - (pubsubTopic, msg2) == handlerFuture.read() - (pubsubTopic, msg2) == otherHandlerFuture.read() - - # Given node is stopped and restarted - await switch.stop() - await switch.start() - check await peerManager.connectPeer(otherRemotePeerInfo) - - # When sending a message from node - handlerFuture = newPushHandlerFuture() - otherHandlerFuture = newPushHandlerFuture() - let msg3 = fakeWakuMessage(testMessage, pubsubTopic) - discard await node.publish(pubsubTopic, msg3) - - # Then the message is received in both nodes - check: - await handlerFuture.withTimeout(FUTURE_TIMEOUT) - await otherHandlerFuture.withTimeout(FUTURE_TIMEOUT) - (pubsubTopic, msg3) == handlerFuture.read() - (pubsubTopic, msg3) == otherHandlerFuture.read() - - # When sending a message from other node - handlerFuture = newPushHandlerFuture() - otherHandlerFuture = newPushHandlerFuture() - let msg4 = fakeWakuMessage(testMessage, pubsubTopic) - discard await otherNode.publish(pubsubTopic, msg4) - - # Then the message is received in both nodes - check: - await handlerFuture.withTimeout(FUTURE_TIMEOUT) - await otherHandlerFuture.withTimeout(FUTURE_TIMEOUT) - (pubsubTopic, msg4) == handlerFuture.read() - (pubsubTopic, msg4) == otherHandlerFuture.read() - - # Finally stop the other node - await allFutures(otherSwitch.stop(), otherNode.stop()) - asyncTest "Relay can't receive messages after subscribing and stopping without unsubscribing": # Given a second node connected to the first one let diff --git a/tests/waku_relay/test_wakunode_relay.nim b/tests/waku_relay/test_wakunode_relay.nim index 58e4b734f..28d35e883 100644 --- a/tests/waku_relay/test_wakunode_relay.nim +++ b/tests/waku_relay/test_wakunode_relay.nim @@ -1,7 +1,7 @@ {.used.} import - std/[os, strutils, sequtils, sysrand, math], + std/[os, strutils, sequtils, sysrand, math, options], stew/byteutils, testutils/unittests, chronos, diff --git a/tests/waku_relay/utils.nim b/tests/waku_relay/utils.nim index 6fcf3b571..0391d47bc 100644 --- a/tests/waku_relay/utils.nim +++ b/tests/waku_relay/utils.nim @@ -1,7 +1,7 @@ {.used.} import - std/[strutils, sequtils, tempfiles], + std/[strutils, sequtils, tempfiles, options], stew/byteutils, chronos, chronicles, diff --git a/tests/waku_rln_relay/rln/waku_rln_relay_utils.nim b/tests/waku_rln_relay/rln/waku_rln_relay_utils.nim index c1539aef3..81032eab9 100644 --- a/tests/waku_rln_relay/rln/waku_rln_relay_utils.nim +++ b/tests/waku_rln_relay/rln/waku_rln_relay_utils.nim @@ -1,3 +1,4 @@ +import std/options import std/tempfiles import diff --git a/tests/waku_rln_relay/utils_offchain.nim b/tests/waku_rln_relay/utils_offchain.nim index 04f27dfe0..593f31c3f 100644 --- a/tests/waku_rln_relay/utils_offchain.nim +++ b/tests/waku_rln_relay/utils_offchain.nim @@ -1,7 +1,7 @@ {.used.} import - std/[sequtils, tempfiles], + std/[sequtils, tempfiles, options], stew/byteutils, chronos, chronicles, diff --git a/tests/waku_rln_relay/utils_onchain.nim b/tests/waku_rln_relay/utils_onchain.nim index f8fa11d28..79c850c93 100644 --- a/tests/waku_rln_relay/utils_onchain.nim +++ b/tests/waku_rln_relay/utils_onchain.nim @@ -1,3 +1,4 @@ +import libp2p/crypto/rng {.used.} {.push raises: [].} @@ -446,7 +447,7 @@ proc createEthAccount*( let gasPrice = Quantity(await web3.provider.eth_gasPrice()) web3.defaultAccount = accounts[0] - let pk = keys.PrivateKey.random(rng[]) + let pk = keys.PrivateKey.random(keys.newRng()[]) let acc = Address(toCanonicalAddress(pk.toPublicKey())) var tx: TransactionArgs @@ -464,7 +465,7 @@ proc createEthAccount*( return (pk, acc) proc createEthAccount*(web3: Web3): (keys.PrivateKey, Address) = - let pk = keys.PrivateKey.random(rng[]) + let pk = keys.PrivateKey.random(keys.newRng()[]) let acc = Address(toCanonicalAddress(pk.toPublicKey())) return (pk, acc) diff --git a/tests/waku_store/store_utils.nim b/tests/waku_store/store_utils.nim index 1e241ec95..f7640ca04 100644 --- a/tests/waku_store/store_utils.nim +++ b/tests/waku_store/store_utils.nim @@ -11,7 +11,7 @@ proc newTestWakuStore*( ): Future[WakuStore] {.async.} = let peerManager = PeerManager.new(switch) - proto = WakuStore.new(peerManager, rng, handler) + proto = WakuStore.new(peerManager, common.rng(), handler) await proto.start() switch.mount(proto) @@ -20,4 +20,4 @@ proc newTestWakuStore*( proc newTestWakuStoreClient*(switch: Switch): WakuStoreClient {.gcsafe.} = let peerManager = PeerManager.new(switch) - WakuStoreClient.new(peerManager, rng) + WakuStoreClient.new(peerManager, common.rng()) diff --git a/tests/waku_store/test_wakunode_store.nim b/tests/waku_store/test_wakunode_store.nim index 478a91178..0447bcdfe 100644 --- a/tests/waku_store/test_wakunode_store.nim +++ b/tests/waku_store/test_wakunode_store.nim @@ -1,7 +1,7 @@ {.used.} import - std/sequtils, + std/[sequtils, options], testutils/unittests, chronicles, chronos, diff --git a/tests/wakunode2/test_app.nim b/tests/wakunode2/test_app.nim index 45a268ed6..c59802ba3 100644 --- a/tests/wakunode2/test_app.nim +++ b/tests/wakunode2/test_app.nim @@ -1,3 +1,4 @@ +import logos_delivery/waku/compat/option_valueor {.used.} import diff --git a/tests/wakunode2/test_validators.nim b/tests/wakunode2/test_validators.nim index dc6e8eb28..7a84c365a 100644 --- a/tests/wakunode2/test_validators.nim +++ b/tests/wakunode2/test_validators.nim @@ -1,7 +1,7 @@ {.used.} import - std/[sequtils, sysrand, math], + std/[sequtils, sysrand, math, options], testutils/unittests, chronos, libp2p/crypto/crypto, diff --git a/tests/wakunode_rest/test_rest_cors.nim b/tests/wakunode_rest/test_rest_cors.nim index 2d198f878..5a57e0a9a 100644 --- a/tests/wakunode_rest/test_rest_cors.nim +++ b/tests/wakunode_rest/test_rest_cors.nim @@ -1,3 +1,4 @@ +import std/options {.used.} import @@ -22,7 +23,7 @@ type TestResponseTuple = tuple[status: int, data: string, headers: HttpTable] proc testWakuNode(): WakuNode = let - privkey = crypto.PrivateKey.random(Secp256k1, rng[]).tryGet() + privkey = crypto.PrivateKey.random(Secp256k1, rng).tryGet() bindIp = parseIpAddress("0.0.0.0") extIp = parseIpAddress("127.0.0.1") port = Port(0) diff --git a/tests/wakunode_rest/test_rest_debug.nim b/tests/wakunode_rest/test_rest_debug.nim index 923f28fa4..8885c8230 100644 --- a/tests/wakunode_rest/test_rest_debug.nim +++ b/tests/wakunode_rest/test_rest_debug.nim @@ -25,7 +25,7 @@ import proc testWakuNode(): WakuNode = let - privkey = crypto.PrivateKey.random(Secp256k1, rng[]).tryGet() + privkey = crypto.PrivateKey.random(Secp256k1, rng).tryGet() bindIp = parseIpAddress("0.0.0.0") extIp = parseIpAddress("127.0.0.1") port = Port(0) diff --git a/tests/wakunode_rest/test_rest_debug_serdes.nim b/tests/wakunode_rest/test_rest_debug_serdes.nim index f14912b85..bdc3ed57f 100644 --- a/tests/wakunode_rest/test_rest_debug_serdes.nim +++ b/tests/wakunode_rest/test_rest_debug_serdes.nim @@ -1,3 +1,4 @@ +import std/options {.used.} import results, stew/byteutils, testutils/unittests, json_serialization diff --git a/tests/wakunode_rest/test_rest_filter.nim b/tests/wakunode_rest/test_rest_filter.nim index 76de0112e..b20e67e59 100644 --- a/tests/wakunode_rest/test_rest_filter.nim +++ b/tests/wakunode_rest/test_rest_filter.nim @@ -1,3 +1,4 @@ +import std/options {.used.} import diff --git a/tests/wakunode_rest/test_rest_health.nim b/tests/wakunode_rest/test_rest_health.nim index 1f51877ec..f2d94f6e5 100644 --- a/tests/wakunode_rest/test_rest_health.nim +++ b/tests/wakunode_rest/test_rest_health.nim @@ -1,7 +1,7 @@ {.used.} import - std/[tempfiles, osproc], + std/[tempfiles, osproc, options], testutils/unittests, presto, presto/client as presto_client, @@ -29,7 +29,7 @@ import proc testWakuNode(): WakuNode = let - privkey = crypto.PrivateKey.random(Secp256k1, rng[]).tryGet() + privkey = crypto.PrivateKey.random(Secp256k1, rng).tryGet() bindIp = parseIpAddress("0.0.0.0") extIp = parseIpAddress("127.0.0.1") port = Port(0) diff --git a/tests/wakunode_rest/test_rest_lightpush.nim b/tests/wakunode_rest/test_rest_lightpush.nim index ae417d11a..ff602328a 100644 --- a/tests/wakunode_rest/test_rest_lightpush.nim +++ b/tests/wakunode_rest/test_rest_lightpush.nim @@ -1,3 +1,4 @@ +import std/options {.used.} import diff --git a/tests/wakunode_rest/test_rest_lightpush_legacy.nim b/tests/wakunode_rest/test_rest_lightpush_legacy.nim index 5b2e3494e..5f29146c8 100644 --- a/tests/wakunode_rest/test_rest_lightpush_legacy.nim +++ b/tests/wakunode_rest/test_rest_lightpush_legacy.nim @@ -1,3 +1,4 @@ +import std/options {.used.} import diff --git a/tests/wakunode_rest/test_rest_relay.nim b/tests/wakunode_rest/test_rest_relay.nim index def933021..e90ccb31d 100644 --- a/tests/wakunode_rest/test_rest_relay.nim +++ b/tests/wakunode_rest/test_rest_relay.nim @@ -1,7 +1,7 @@ {.used.} import - std/[sequtils, strformat, tempfiles, osproc], + std/[sequtils, strformat, tempfiles, osproc, options], stew/byteutils, testutils/unittests, presto, diff --git a/tests/wakunode_rest/test_rest_relay_serdes.nim b/tests/wakunode_rest/test_rest_relay_serdes.nim index 24ce80b9e..ca0e45c30 100644 --- a/tests/wakunode_rest/test_rest_relay_serdes.nim +++ b/tests/wakunode_rest/test_rest_relay_serdes.nim @@ -1,3 +1,4 @@ +import std/options {.used.} import results, stew/byteutils, unittest2, json_serialization