From 549834203dfb1412baac38c71f37c8d072175706 Mon Sep 17 00:00:00 2001 From: Fabiana Cecin Date: Tue, 2 Jun 2026 15:42:58 -0300 Subject: [PATCH] Bump to nim-libp2p 2.0.0 * bump libp2p pin to release/v2.0.0 (c43199378) * pin nimble.lock: lsquic/websock/boringssl/protobuf_serialization/npeg/jwt * add libp2p_mix dep and point libp2p/protocols/mix -> libp2p_mix * migrate rng to libp2p Rng type (prod, channels, noise, tests) * noise: take Rng, extract bearSslDrbg internally * waku_switch: TransportConfig factory; withMaxInOut; local MaxConnections * waku_relay/rendezvous/discv5/kademlia: v2.0.0 API (rng, config, ServiceDiscovery) * tests: newStandardSwitch shim; PeerId.random(rng); common.rng()/crypto.newRng() * drop libp2p/utils/semaphore (use chronos AsyncSemaphore) * add waku/compat/option_valueor shim where needed * add std/options where transitive re-export dropped --- apps/chat2/chat2.nim | 6 +- apps/chat2bridge/chat2bridge.nim | 2 +- apps/chat2mix/chat2mix.nim | 6 +- apps/networkmonitor/networkmonitor.nim | 2 +- apps/wakucanary/wakucanary.nim | 4 +- channels/reliable_channel.nim | 3 +- .../lightpush_mix/lightpush_publisher_mix.nim | 4 +- library/kernel_api/discovery_api.nim | 1 + library/kernel_api/node_lifecycle_api.nim | 1 + library/kernel_api/peer_manager_api.nim | 1 + library/kernel_api/protocols/filter_api.nim | 1 + .../kernel_api/protocols/lightpush_api.nim | 1 + library/kernel_api/protocols/relay_api.nim | 1 + library/kernel_api/protocols/store_api.nim | 1 + library/libwaku.nim | 1 + nimble.lock | 92 +++++++-- tests/common/test_ratelimit_setting.nim | 7 +- tests/common/test_requestratelimiter.nim | 7 +- tests/node/test_wakunode_filter.nim | 7 +- tests/node/test_wakunode_relay_rln.nim | 8 +- tests/test_helpers.nim | 11 +- tests/test_peer_manager.nim | 18 +- tests/test_waku_dnsdisc.nim | 3 +- tests/test_waku_keepalive.nim | 2 +- tests/test_waku_keystore.nim | 40 ++-- tests/test_waku_keystore_keyfile.nim | 20 +- tests/test_waku_netconfig.nim | 1 + tests/test_waku_noise.nim | 178 +++++++++--------- tests/test_waku_noise_sessions.nim | 38 ++-- tests/test_wakunode.nim | 2 +- tests/testlib/common.nim | 6 +- tests/testlib/wakucore.nim | 21 ++- tests/testlib/wakunode.nim | 3 +- tests/waku_discv5/test_waku_discv5.nim | 11 +- tests/waku_filter_v2/waku_filter_utils.nim | 2 +- tests/waku_lightpush/lightpush_utils.nim | 4 +- .../waku_lightpush_legacy/lightpush_utils.nim | 4 +- tests/waku_relay/test_wakunode_relay.nim | 2 +- tests/waku_relay/utils.nim | 2 +- .../rln/waku_rln_relay_utils.nim | 1 + tests/waku_rln_relay/utils_offchain.nim | 2 +- tests/waku_rln_relay/utils_onchain.nim | 5 +- tests/waku_store/store_utils.nim | 4 +- tests/waku_store/test_wakunode_store.nim | 2 +- tests/wakunode2/test_app.nim | 1 + tests/wakunode2/test_validators.nim | 2 +- tests/wakunode_rest/test_rest_cors.nim | 3 +- tests/wakunode_rest/test_rest_debug.nim | 2 +- .../wakunode_rest/test_rest_debug_serdes.nim | 1 + tests/wakunode_rest/test_rest_filter.nim | 1 + tests/wakunode_rest/test_rest_health.nim | 4 +- tests/wakunode_rest/test_rest_lightpush.nim | 1 + .../test_rest_lightpush_legacy.nim | 1 + tests/wakunode_rest/test_rest_relay.nim | 2 +- .../wakunode_rest/test_rest_relay_serdes.nim | 1 + waku.nimble | 3 +- waku/api/api.nim | 1 + waku/api/api_conf.nim | 1 + waku/api/types.nim | 4 +- waku/common/databases/db_postgres/dbconn.nim | 1 + .../databases/db_postgres/pgasyncpool.nim | 1 + waku/common/databases/db_sqlite.nim | 1 + waku/common/enr/typed_record.nim | 1 + waku/common/rate_limit/request_limiter.nim | 1 + waku/common/utils/parse_size_units.nim | 1 + waku/compat/option_valueor.nim | 35 ++++ waku/discovery/autonat_service.nim | 3 +- waku/discovery/waku_discv5.nim | 11 +- waku/discovery/waku_dnsdisc.nim | 1 + waku/discovery/waku_kademlia.nim | 26 +-- waku/factory/builder.nim | 11 +- .../kademlia_discovery_conf_builder.nim | 1 + .../factory/conf_builder/mix_conf_builder.nim | 3 +- .../conf_builder/rate_limit_conf_builder.nim | 1 + .../store_service_conf_builder.nim | 1 + .../conf_builder/waku_conf_builder.nim | 7 +- waku/factory/internal_config.nim | 1 + waku/factory/node_factory.nim | 5 +- waku/factory/waku.nim | 5 +- waku/incentivization/eligibility_manager.nim | 1 + .../recv_service/recv_service.nim | 1 + .../send_service/delivery_task.nim | 1 + .../send_service/lightpush_processor.nim | 1 + .../send_service/relay_processor.nim | 1 + .../send_service/send_service.nim | 1 + .../delivery_service/subscription_manager.nim | 1 + .../health_monitor/node_health_monitor.nim | 1 + waku/node/kernel_api/filter.nim | 1 + waku/node/kernel_api/lightpush.nim | 3 +- waku/node/kernel_api/peer_exchange.nim | 1 + waku/node/kernel_api/ping.nim | 1 + waku/node/kernel_api/relay.nim | 1 + waku/node/kernel_api/store.nim | 1 + waku/node/peer_manager/peer_manager.nim | 2 + .../peer_store/waku_peer_storage.nim | 1 + waku/node/peer_manager/waku_peer_store.nim | 4 +- waku/node/waku_metrics.nim | 1 + waku/node/waku_node.nim | 9 +- waku/node/waku_switch.nim | 13 +- waku/persistency/persistency.nim | 1 + waku/rest_api/endpoint/admin/handlers.nim | 1 + waku/rest_api/endpoint/builder.nim | 1 + waku/rest_api/endpoint/debug/handlers.nim | 1 + waku/rest_api/endpoint/filter/handlers.nim | 1 + waku/rest_api/endpoint/health/handlers.nim | 1 + waku/rest_api/endpoint/health/types.nim | 1 + .../endpoint/legacy_lightpush/handlers.nim | 1 + waku/rest_api/endpoint/lightpush/handlers.nim | 1 + waku/rest_api/endpoint/origin_handler.nim | 1 + waku/rest_api/endpoint/relay/handlers.nim | 1 + waku/rest_api/endpoint/rest_serdes.nim | 1 + waku/rest_api/endpoint/store/handlers.nim | 1 + waku/rest_api/endpoint/store/types.nim | 1 + waku/rest_api/handlers.nim | 1 + waku/utils/noise.nim | 1 + waku/utils/requests.nim | 6 +- waku/waku_archive/archive.nim | 1 + waku/waku_archive/driver/builder.nim | 1 + .../driver/postgres_driver/migrations.nim | 1 + .../postgres_driver/postgres_driver.nim | 1 + .../driver/queue_driver/queue_driver.nim | 1 + .../driver/sqlite_driver/sqlite_driver.nim | 1 + .../waku_archive/retention_policy/builder.nim | 1 + .../retention_policy_capacity.nim | 1 + waku/waku_core/peers.nim | 1 + waku/waku_core/topics/content_topic.nim | 1 + waku/waku_core/topics/sharding.nim | 1 + waku/waku_enr/capabilities.nim | 3 +- waku/waku_enr/multiaddr.nim | 1 + waku/waku_enr/sharding.nim | 1 + waku/waku_filter_v2/client.nim | 10 +- waku/waku_filter_v2/protocol.nim | 1 + waku/waku_filter_v2/subscriptions.nim | 1 + waku/waku_keystore/keystore.nim | 1 + waku/waku_lightpush/callbacks.nim | 1 + waku/waku_lightpush/client.nim | 6 +- waku/waku_lightpush/protocol.nim | 6 +- waku/waku_lightpush_legacy/client.nim | 6 +- waku/waku_lightpush_legacy/protocol.nim | 5 +- waku/waku_metadata/protocol.nim | 1 + waku/waku_mix/protocol.nim | 16 +- waku/waku_noise/noise.nim | 6 +- .../waku_noise/noise_handshake_processing.nim | 5 +- waku/waku_noise/noise_utils.nim | 21 ++- waku/waku_peer_exchange/client.nim | 1 + waku/waku_peer_exchange/protocol.nim | 1 + waku/waku_relay/protocol.nim | 2 + waku/waku_rendezvous/client.nim | 14 +- waku/waku_rendezvous/common.nim | 1 + waku/waku_rendezvous/protocol.nim | 16 +- .../group_manager/on_chain/group_manager.nim | 1 + waku/waku_rln_relay/rln/rln_interface.nim | 1 + waku/waku_rln_relay/rln/wrappers.nim | 1 + waku/waku_rln_relay/rln_relay.nim | 1 + waku/waku_store/client.nim | 6 +- waku/waku_store/protocol.nim | 5 +- waku/waku_store/resume.nim | 1 + waku/waku_store/self_req_handler.nim | 1 + waku/waku_store_sync/reconciliation.nim | 1 + waku/waku_store_sync/transfer.nim | 1 + 160 files changed, 583 insertions(+), 324 deletions(-) create mode 100644 waku/compat/option_valueor.nim diff --git a/apps/chat2/chat2.nim b/apps/chat2/chat2.nim index 282e17bfd..60ef3beaf 100644 --- a/apps/chat2/chat2.nim +++ b/apps/chat2/chat2.nim @@ -302,7 +302,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() @@ -310,7 +310,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: @@ -565,7 +565,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 eeeea328b..084004b02 100644 --- a/apps/chat2bridge/chat2bridge.nim +++ b/apps/chat2bridge/chat2bridge.nim @@ -271,7 +271,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 8b786d7b6..8a4baf11f 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 23607b118..5b37e80f4 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 e7b1ff9aa..a3f0bd91c 100644 --- a/apps/wakucanary/wakucanary.nim +++ b/apps/wakucanary/wakucanary.nim @@ -159,7 +159,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 @@ -190,7 +190,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/channels/reliable_channel.nim b/channels/reliable_channel.nim index e32b57e36..4a5ad7a80 100644 --- a/channels/reliable_channel.nim +++ b/channels/reliable_channel.nim @@ -1,3 +1,4 @@ +import 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/examples/lightpush_mix/lightpush_publisher_mix.nim b/examples/lightpush_mix/lightpush_publisher_mix.nim index 104de8552..893b15368 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 f61b7bad1..589cad181 100644 --- a/library/kernel_api/discovery_api.nim +++ b/library/kernel_api/discovery_api.nim @@ -1,3 +1,4 @@ +import 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 8f3e99b24..6c3f88f5a 100644 --- a/library/kernel_api/node_lifecycle_api.nim +++ b/library/kernel_api/node_lifecycle_api.nim @@ -1,3 +1,4 @@ +import 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 f0ae37f00..84d806ebc 100644 --- a/library/kernel_api/peer_manager_api.nim +++ b/library/kernel_api/peer_manager_api.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[sequtils, strutils, tables] import chronicles, chronos, results, options, json, ffi import waku/factory/waku, waku/node/waku_node, waku/node/peer_manager, ../declare_lib diff --git a/library/kernel_api/protocols/filter_api.nim b/library/kernel_api/protocols/filter_api.nim index c4f99510a..177d52b68 100644 --- a/library/kernel_api/protocols/filter_api.nim +++ b/library/kernel_api/protocols/filter_api.nim @@ -1,3 +1,4 @@ +import 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 e9251a3f3..4c87292b6 100644 --- a/library/kernel_api/protocols/lightpush_api.nim +++ b/library/kernel_api/protocols/lightpush_api.nim @@ -1,3 +1,4 @@ +import 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 b184d6011..a83418886 100644 --- a/library/kernel_api/protocols/relay_api.nim +++ b/library/kernel_api/protocols/relay_api.nim @@ -1,3 +1,4 @@ +import 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 0df4d9b1f..e9da7da67 100644 --- a/library/kernel_api/protocols/store_api.nim +++ b/library/kernel_api/protocols/store_api.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[json, sugar, strutils, options] import chronos, chronicles, results, stew/byteutils, ffi import diff --git a/library/libwaku.nim b/library/libwaku.nim index dd1ee9fd9..2576d1713 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[atomics, options, atomics, macros] import chronicles, chronos, chronos/threadsync, ffi import diff --git a/nimble.lock b/nimble.lock index cd533001e..d246ebe84 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,7 +450,7 @@ "zlib" ], "checksums": { - "sha1": "1294a66520fa4541e261dec8a6a84f774fb8c0ac" + "sha1": "fd17a854686d9a19af40aba51f05d06b82fc30ec" } }, "json_rpc": { @@ -476,8 +476,8 @@ } }, "lsquic": { - "version": "0.0.1", - "vcsRevision": "4fb03ee7bfb39aecb3316889fdcb60bec3d0936f", + "version": "0.4.1", + "vcsRevision": "00e4b7dfaa197cd120267aa897b33b0914166b45", "url": "https://github.com/vacp2p/nim-lsquic", "downloadMethod": "git", "dependencies": [ @@ -487,10 +487,11 @@ "chronos", "nimcrypto", "unittest2", - "chronicles" + "chronicles", + "boringssl" ], "checksums": { - "sha1": "f465fa994346490d0924d162f53d9b5aec62f948" + "sha1": "5e36783661eab239bd4d8566ff3195c8d14f1923" } }, "secp256k1": { @@ -582,8 +583,8 @@ } }, "libp2p": { - "version": "#ff8d51857b4b79a68468e7bcc27b2026cca02996", - "vcsRevision": "ff8d51857b4b79a68468e7bcc27b2026cca02996", + "version": "#c43199378f46d0aaf61be1cad1ee1d63e8f665d6", + "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.4", + "vcsRevision": "1c91a4a2579123597df43bbdea70cc575957bdea", + "url": "https://github.com/vacp2p/nim-boringssl", + "downloadMethod": "git", + "dependencies": [ + "nim" + ], + "checksums": { + "sha1": "3655faa30099538293cb7d60e726783dff9d979a" + } + }, + "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": "7cb7556d9a228573fc5622af7ffb2dd11741e043", + "url": "https://github.com/logos-co/nim-libp2p-mix", + "downloadMethod": "git", + "dependencies": [ + "nim", + "libp2p", + "chronicles", + "chronos", + "metrics", + "nimcrypto", + "stew", + "results", + "unittest2" + ], + "checksums": { + "sha1": "8287dda862a4398966cd66a5b3d7d66834c9a9a6" + } } }, "tasks": {} diff --git a/tests/common/test_ratelimit_setting.nim b/tests/common/test_ratelimit_setting.nim index 2bc95fbfb..c9385ea57 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,9 @@ import ../../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 conn1 = Connection(peerId: PeerId.random(newRng()).tryGet()) +let conn2 = Connection(peerId: PeerId.random(newRng()).tryGet()) +let conn3 = Connection(peerId: PeerId.random(newRng()).tryGet()) suite "RateLimitSetting": test "Parse rate limit setting - ok": diff --git a/tests/common/test_requestratelimiter.nim b/tests/common/test_requestratelimiter.nim index be910b38e..e3801f853 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,9 @@ import ../../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 conn1 = Connection(peerId: PeerId.random(newRng()).tryGet()) +let conn2 = Connection(peerId: PeerId.random(newRng()).tryGet()) +let conn3 = Connection(peerId: PeerId.random(newRng()).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 2777b0124..c94c8fe94 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 @@ -21,9 +22,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( @@ -31,7 +32,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_relay_rln.nim b/tests/node/test_wakunode_relay_rln.nim index 3a2a8a67c..9e1520aab 100644 --- a/tests/node/test_wakunode_relay_rln.nim +++ b/tests/node/test_wakunode_relay_rln.nim @@ -35,10 +35,10 @@ from ../../waku/waku_noise/noise_utils import randomSeqByte 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 608889d32..e60ab3c8d 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,7 @@ 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 +1013,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(peerStoreSize) @@ -1027,7 +1027,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(25) @@ -1040,7 +1040,7 @@ 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 +1091,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(25) @@ -1148,7 +1148,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(25) @@ -1164,7 +1164,7 @@ procSuite "Peer Manager": let pm = PeerManager.new( switch = SwitchBuilder .new() - .withRng(rng) + .withRng(crypto.newRng()) .withMplex() .withNoise() .withPeerStore(25) @@ -1178,7 +1178,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 758bdb3ca..43e00da80 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 5d8402268..7b7586db1 100644 --- a/tests/test_waku_keepalive.nim +++ b/tests/test_waku_keepalive.nim @@ -38,7 +38,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 8fd8ad297..83536b062 100644 --- a/tests/test_waku_keystore.nim +++ b/tests/test_waku_keystore.nim @@ -46,10 +46,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, @@ -85,10 +85,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, @@ -141,10 +141,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, @@ -193,10 +193,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, @@ -254,10 +254,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 afdb7e44b..cd187baf6 100644 --- a/tests/test_waku_keystore_keyfile.nim +++ b/tests/test_waku_keystore_keyfile.nim @@ -16,7 +16,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) @@ -42,9 +42,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" @@ -53,40 +53,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() @@ -343,7 +343,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 0aff64121..d637f8b2b 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_waku_noise.nim b/tests/test_waku_noise.nim index 6566f9eed..6dbb3a2f9 100644 --- a/tests/test_waku_noise.nim +++ b/tests/test_waku_noise.nim @@ -27,7 +27,7 @@ procSuite "Waku Noise": let maxMessageLength = 3 * NoisePaddingBlockSize for messageLen in 0 .. maxMessageLength: let - message = randomSeqByte(rng[], messageLen) + message = randomSeqByte(rng, messageLen) padded = pkcs7_pad(message, NoisePaddingBlockSize) unpadded = pkcs7_unpad(padded, NoisePaddingBlockSize) @@ -37,11 +37,11 @@ procSuite "Waku Noise": message == unpadded test "ChaChaPoly Encryption/Decryption: random byte sequences": - let cipherState = randomChaChaPolyCipherState(rng[]) + let cipherState = randomChaChaPolyCipherState(rng) # We encrypt/decrypt random byte sequences let - plaintext: seq[byte] = randomSeqByte(rng[], rand(1 .. 128)) + plaintext: seq[byte] = randomSeqByte(rng, rand(1 .. 128)) ciphertext: ChaChaPolyCiphertext = encrypt(cipherState, plaintext) decryptedCiphertext: seq[byte] = decrypt(cipherState, ciphertext) @@ -49,7 +49,7 @@ procSuite "Waku Noise": plaintext == decryptedCiphertext test "ChaChaPoly Encryption/Decryption: random strings": - let cipherState = randomChaChaPolyCipherState(rng[]) + let cipherState = randomChaChaPolyCipherState(rng) # We encrypt/decrypt random strings var plaintext: string @@ -64,10 +64,10 @@ procSuite "Waku Noise": plaintext.toBytes() == decryptedCiphertext test "Noise public keys: encrypt and decrypt a public key": - let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[]) + let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng) let - cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng[]) + cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng) encryptedPk: NoisePublicKey = encryptNoisePublicKey(cs, noisePublicKey) decryptedPk: NoisePublicKey = decryptNoisePublicKey(cs, encryptedPk) @@ -75,20 +75,20 @@ procSuite "Waku Noise": noisePublicKey == decryptedPk test "Noise public keys: decrypt an unencrypted public key": - let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[]) + let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng) let - cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng[]) + cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng) decryptedPk: NoisePublicKey = decryptNoisePublicKey(cs, noisePublicKey) check: noisePublicKey == decryptedPk test "Noise public keys: encrypt an encrypted public key": - let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[]) + let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng) let - cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng[]) + cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng) encryptedPk: NoisePublicKey = encryptNoisePublicKey(cs, noisePublicKey) encryptedPk2: NoisePublicKey = encryptNoisePublicKey(cs, encryptedPk) @@ -96,10 +96,10 @@ procSuite "Waku Noise": encryptedPk == encryptedPk2 test "Noise public keys: encrypt, decrypt and decrypt a public key": - let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[]) + let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng) let - cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng[]) + cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng) encryptedPk: NoisePublicKey = encryptNoisePublicKey(cs, noisePublicKey) decryptedPk: NoisePublicKey = decryptNoisePublicKey(cs, encryptedPk) decryptedPk2: NoisePublicKey = decryptNoisePublicKey(cs, decryptedPk) @@ -109,7 +109,7 @@ procSuite "Waku Noise": test "Noise public keys: serialize and deserialize an unencrypted public key": let - noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[]) + noisePublicKey: NoisePublicKey = genNoisePublicKey(rng) serializedNoisePublicKey: seq[byte] = serializeNoisePublicKey(noisePublicKey) deserializedNoisePublicKey: NoisePublicKey = intoNoisePublicKey(serializedNoisePublicKey) @@ -118,10 +118,10 @@ procSuite "Waku Noise": noisePublicKey == deserializedNoisePublicKey test "Noise public keys: encrypt, serialize, deserialize and decrypt a public key": - let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[]) + let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng) let - cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng[]) + cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng) encryptedPk: NoisePublicKey = encryptNoisePublicKey(cs, noisePublicKey) serializedNoisePublicKey: seq[byte] = serializeNoisePublicKey(encryptedPk) deserializedNoisePublicKey: NoisePublicKey = @@ -134,7 +134,7 @@ procSuite "Waku Noise": test "PayloadV2: serialize/deserialize PayloadV2 to byte sequence": let - payload2: PayloadV2 = randomPayloadV2(rng[]) + payload2: PayloadV2 = randomPayloadV2(rng) serializedPayload = serializePayloadV2(payload2) check: @@ -149,7 +149,7 @@ procSuite "Waku Noise": test "PayloadV2: Encode/Decode a Waku Message (version 2) to a PayloadV2": # We encode to a WakuMessage a random PayloadV2 let - payload2 = randomPayloadV2(rng[]) + payload2 = randomPayloadV2(rng) msg = encodePayloadV2(payload2) check: @@ -173,8 +173,8 @@ procSuite "Waku Noise": test "Noise State Machine: Diffie-Hellman operation": #We generate random keypairs let - aliceKey = genKeyPair(rng[]) - bobKey = genKeyPair(rng[]) + aliceKey = genKeyPair(rng) + bobKey = genKeyPair(rng) # A Diffie-Hellman operation between Alice's private key and Bob's public key must be equal to # a Diffie-hellman operation between Alice's public key and Bob's private key @@ -188,10 +188,10 @@ procSuite "Waku Noise": test "Noise State Machine: Cipher State primitives": # We generate a random Cipher State, associated data ad and plaintext var - cipherState: CipherState = randomCipherState(rng[]) + cipherState: CipherState = randomCipherState(rng) nonce: uint64 = uint64(rand(0 .. int.high)) - ad: seq[byte] = randomSeqByte(rng[], rand(1 .. 128)) - plaintext: seq[byte] = randomSeqByte(rng[], rand(1 .. 128)) + ad: seq[byte] = randomSeqByte(rng, rand(1 .. 128)) + plaintext: seq[byte] = randomSeqByte(rng, rand(1 .. 128)) # We set the random nonce generated in the cipher state setNonce(cipherState, nonce) @@ -218,7 +218,7 @@ procSuite "Waku Noise": setCipherStateKey(cipherState, EmptyKey) nonce = getNonce(cipherState) - plaintext = randomSeqByte(rng[], rand(1 .. 128)) + plaintext = randomSeqByte(rng, rand(1 .. 128)) ciphertext = encryptWithAd(cipherState, ad, plaintext) check: @@ -230,7 +230,7 @@ procSuite "Waku Noise": nonce = getNonce(cipherState) # Note that we set ciphertext minimum length to 16 to not trigger checks on authentication tag length - ciphertext = randomSeqByte(rng[], rand(16 .. 128)) + ciphertext = randomSeqByte(rng, rand(16 .. 128)) plaintext = decryptWithAd(cipherState, ad, ciphertext) check: @@ -241,9 +241,9 @@ procSuite "Waku Noise": # Note that NonceMax is uint64.high - 1 = 2^64-1-1 and that nonce is increased after each encryption and decryption operation # We generate a test Cipher State with nonce set to MaxNonce - cipherState = randomCipherState(rng[]) + cipherState = randomCipherState(rng) setNonce(cipherState, NonceMax) - plaintext = randomSeqByte(rng[], rand(1 .. 128)) + plaintext = randomSeqByte(rng, rand(1 .. 128)) # We test if encryption fails with a NoiseNonceMaxError error. Any subsequent encryption call over the Cipher State should fail similarly and leave the nonce unchanged for _ in [1 .. 5]: @@ -257,9 +257,9 @@ procSuite "Waku Noise": # Since nonce is increased after decryption as well, we need to generate a proper ciphertext in order to test MaxNonceError error handling # We cannot call encryptWithAd to encrypt a plaintext using a nonce equal MaxNonce, since this will trigger a MaxNonceError. # To perform such test, we then need to encrypt a test plaintext using directly ChaChaPoly primitive - cipherState = randomCipherState(rng[]) + cipherState = randomCipherState(rng) setNonce(cipherState, NonceMax) - plaintext = randomSeqByte(rng[], rand(1 .. 128)) + plaintext = randomSeqByte(rng, rand(1 .. 128)) # We perform encryption using the Cipher State key, NonceMax and ad # By Noise specification the nonce is 8 bytes long out of the 12 bytes supported by ChaChaPoly, thus we copy the Little endian conversion of the nonce to a ChaChaPolyNonce @@ -308,7 +308,7 @@ procSuite "Waku Noise": ######################################## # We generate a random byte sequence and execute a mixHash over it - mixHash(symmetricState, randomSeqByte(rng[], rand(1 .. 128))) + mixHash(symmetricState, randomSeqByte(rng, rand(1 .. 128))) # mixHash changes only the handshake hash value of the Symmetric state check: @@ -324,7 +324,7 @@ procSuite "Waku Noise": ######################################## # We generate random input key material and we execute mixKey - var inputKeyMaterial = randomSeqByte(rng[], rand(1 .. 128)) + var inputKeyMaterial = randomSeqByte(rng, rand(1 .. 128)) mixKey(symmetricState, inputKeyMaterial) # mixKey changes the Symmetric State's chaining key and encryption key of the embedded Cipher State @@ -345,7 +345,7 @@ procSuite "Waku Noise": ######################################## # We generate random input key material and we execute mixKeyAndHash - inputKeyMaterial = randomSeqByte(rng[], rand(1 .. 128)) + inputKeyMaterial = randomSeqByte(rng, rand(1 .. 128)) mixKeyAndHash(symmetricState, inputKeyMaterial) # mixKeyAndHash executes a mixKey and a mixHash using the input key material @@ -368,7 +368,7 @@ procSuite "Waku Noise": var initialSymmetricState = symmetricState # We generate random plaintext and we execute encryptAndHash - var plaintext = randomChaChaPolyKey(rng[]) + var plaintext = randomChaChaPolyKey(rng) var nonce = getNonce(getCipherState(symmetricState)) var ciphertext = encryptAndHash(symmetricState, plaintext) @@ -421,11 +421,11 @@ procSuite "Waku Noise": let hsPattern = NoiseHandshakePatterns["XX"] # We initialize Alice's and Bob's Handshake State - let aliceStaticKey = genKeyPair(rng[]) + let aliceStaticKey = genKeyPair(rng) var aliceHS = initialize(hsPattern = hsPattern, staticKey = aliceStaticKey, initiator = true) - let bobStaticKey = genKeyPair(rng[]) + let bobStaticKey = genKeyPair(rng) var bobHS = initialize(hsPattern = hsPattern, staticKey = bobStaticKey) var @@ -440,15 +440,15 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message # and the (encrypted) transport message aliceStep = - stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, aliceHS, transportMessage = sentTransportMessage).get() # Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him - bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get() + bobStep = stepHandshake(rng, bobHS, readPayloadV2 = aliceStep.payload2).get() check: bobStep.transportMessage == sentTransportMessage @@ -458,13 +458,13 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # At this step, Bob writes and returns a payload - bobStep = stepHandshake(rng[], bobHS, transportMessage = sentTransportMessage).get() + bobStep = stepHandshake(rng, bobHS, transportMessage = sentTransportMessage).get() # While Alice reads and returns the (decrypted) transport message - aliceStep = stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep.payload2).get() + aliceStep = stepHandshake(rng, aliceHS, readPayloadV2 = bobStep.payload2).get() check: aliceStep.transportMessage == sentTransportMessage @@ -474,14 +474,14 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # Similarly as in first step, Alice writes a Waku2 payload containing the handshake message and the (encrypted) transport message aliceStep = - stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, aliceHS, transportMessage = sentTransportMessage).get() # Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him - bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get() + bobStep = stepHandshake(rng, bobHS, readPayloadV2 = aliceStep.payload2).get() check: bobStep.transportMessage == sentTransportMessage @@ -494,13 +494,13 @@ procSuite "Waku Noise": let prevBobHS = bobHS let bobStep1 = - stepHandshake(rng[], bobHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, bobHS, transportMessage = sentTransportMessage).get() let aliceStep1 = - stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep1.payload2).get() + stepHandshake(rng, aliceHS, readPayloadV2 = bobStep1.payload2).get() let aliceStep2 = - stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, aliceHS, transportMessage = sentTransportMessage).get() let bobStep2 = - stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep2.payload2).get() + stepHandshake(rng, bobHS, readPayloadV2 = aliceStep2.payload2).get() check: aliceStep1 == default(HandshakeStepResult) @@ -529,7 +529,7 @@ procSuite "Waku Noise": for _ in 0 .. 10: # Alice writes to Bob - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage(aliceHSResult, message, defaultMessageNametagBuffer) readMessage = readMessage(bobHSResult, payload2, defaultMessageNametagBuffer).get() @@ -538,7 +538,7 @@ procSuite "Waku Noise": message == readMessage # Bob writes to Alice - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage(bobHSResult, message, defaultMessageNametagBuffer) readMessage = readMessage(aliceHSResult, payload2, defaultMessageNametagBuffer).get() @@ -550,15 +550,15 @@ procSuite "Waku Noise": let hsPattern = NoiseHandshakePatterns["XXpsk0"] # We generate a random psk - let psk = randomSeqByte(rng[], 32) + let psk = randomSeqByte(rng, 32) # We initialize Alice's and Bob's Handshake State - let aliceStaticKey = genKeyPair(rng[]) + let aliceStaticKey = genKeyPair(rng) var aliceHS = initialize( hsPattern = hsPattern, staticKey = aliceStaticKey, psk = psk, initiator = true ) - let bobStaticKey = genKeyPair(rng[]) + let bobStaticKey = genKeyPair(rng) var bobHS = initialize(hsPattern = hsPattern, staticKey = bobStaticKey, psk = psk) var @@ -573,15 +573,15 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message # and the (encrypted) transport message aliceStep = - stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, aliceHS, transportMessage = sentTransportMessage).get() # Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him - bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get() + bobStep = stepHandshake(rng, bobHS, readPayloadV2 = aliceStep.payload2).get() check: bobStep.transportMessage == sentTransportMessage @@ -591,13 +591,13 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # At this step, Bob writes and returns a payload - bobStep = stepHandshake(rng[], bobHS, transportMessage = sentTransportMessage).get() + bobStep = stepHandshake(rng, bobHS, transportMessage = sentTransportMessage).get() # While Alice reads and returns the (decrypted) transport message - aliceStep = stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep.payload2).get() + aliceStep = stepHandshake(rng, aliceHS, readPayloadV2 = bobStep.payload2).get() check: aliceStep.transportMessage == sentTransportMessage @@ -607,14 +607,14 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # Similarly as in first step, Alice writes a Waku2 payload containing the handshake message and the (encrypted) transport message aliceStep = - stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, aliceHS, transportMessage = sentTransportMessage).get() # Bob reads Alice's payloads, and returns the (decrypted) transportMessage alice sent to him - bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get() + bobStep = stepHandshake(rng, bobHS, readPayloadV2 = aliceStep.payload2).get() check: bobStep.transportMessage == sentTransportMessage @@ -640,7 +640,7 @@ procSuite "Waku Noise": for _ in 0 .. 10: # Alice writes to Bob - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage(aliceHSResult, message, defaultMessageNametagBuffer) readMessage = readMessage(bobHSResult, payload2, defaultMessageNametagBuffer).get() @@ -649,7 +649,7 @@ procSuite "Waku Noise": message == readMessage # Bob writes to Alice - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage(bobHSResult, message, defaultMessageNametagBuffer) readMessage = readMessage(aliceHSResult, payload2, defaultMessageNametagBuffer).get() @@ -661,8 +661,8 @@ procSuite "Waku Noise": let hsPattern = NoiseHandshakePatterns["K1K1"] # We initialize Alice's and Bob's Handshake State - let aliceStaticKey = genKeyPair(rng[]) - let bobStaticKey = genKeyPair(rng[]) + let aliceStaticKey = genKeyPair(rng) + let bobStaticKey = genKeyPair(rng) # This handshake has the following pre-message pattern: # -> s @@ -696,15 +696,15 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message # and the (encrypted) transport message aliceStep = - stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, aliceHS, transportMessage = sentTransportMessage).get() # Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him - bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get() + bobStep = stepHandshake(rng, bobHS, readPayloadV2 = aliceStep.payload2).get() check: bobStep.transportMessage == sentTransportMessage @@ -714,13 +714,13 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # At this step, Bob writes and returns a payload - bobStep = stepHandshake(rng[], bobHS, transportMessage = sentTransportMessage).get() + bobStep = stepHandshake(rng, bobHS, transportMessage = sentTransportMessage).get() # While Alice reads and returns the (decrypted) transport message - aliceStep = stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep.payload2).get() + aliceStep = stepHandshake(rng, aliceHS, readPayloadV2 = bobStep.payload2).get() check: aliceStep.transportMessage == sentTransportMessage @@ -730,14 +730,14 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # Similarly as in first step, Alice writes a Waku2 payload containing the handshake_message and the (encrypted) transportMessage aliceStep = - stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, aliceHS, transportMessage = sentTransportMessage).get() # Bob reads Alice's payloads, and returns the (decrypted) transportMessage alice sent to him - bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get() + bobStep = stepHandshake(rng, bobHS, readPayloadV2 = aliceStep.payload2).get() check: bobStep.transportMessage == sentTransportMessage @@ -763,7 +763,7 @@ procSuite "Waku Noise": for _ in 0 .. 10: # Alice writes to Bob - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage(aliceHSResult, message, defaultMessageNametagBuffer) readMessage = readMessage(bobHSResult, payload2, defaultMessageNametagBuffer).get() @@ -772,7 +772,7 @@ procSuite "Waku Noise": message == readMessage # Bob writes to Alice - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage(bobHSResult, message, defaultMessageNametagBuffer) readMessage = readMessage(aliceHSResult, payload2, defaultMessageNametagBuffer).get() @@ -784,8 +784,8 @@ procSuite "Waku Noise": let hsPattern = NoiseHandshakePatterns["XK1"] # We initialize Alice's and Bob's Handshake State - let aliceStaticKey = genKeyPair(rng[]) - let bobStaticKey = genKeyPair(rng[]) + let aliceStaticKey = genKeyPair(rng) + let bobStaticKey = genKeyPair(rng) # This handshake has the following pre-message pattern: # <- s @@ -816,15 +816,15 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message # and the (encrypted) transport message aliceStep = - stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, aliceHS, transportMessage = sentTransportMessage).get() # Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him - bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get() + bobStep = stepHandshake(rng, bobHS, readPayloadV2 = aliceStep.payload2).get() check: bobStep.transportMessage == sentTransportMessage @@ -834,13 +834,13 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # At this step, Bob writes and returns a payload - bobStep = stepHandshake(rng[], bobHS, transportMessage = sentTransportMessage).get() + bobStep = stepHandshake(rng, bobHS, transportMessage = sentTransportMessage).get() # While Alice reads and returns the (decrypted) transport message - aliceStep = stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep.payload2).get() + aliceStep = stepHandshake(rng, aliceHS, readPayloadV2 = bobStep.payload2).get() check: aliceStep.transportMessage == sentTransportMessage @@ -850,14 +850,14 @@ procSuite "Waku Noise": ############### # We generate a random transport message - sentTransportMessage = randomSeqByte(rng[], 32) + sentTransportMessage = randomSeqByte(rng, 32) # Similarly as in first step, Alice writes a Waku2 payload containing the handshake message and the (encrypted) transport message aliceStep = - stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get() + stepHandshake(rng, aliceHS, transportMessage = sentTransportMessage).get() # Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him - bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get() + bobStep = stepHandshake(rng, bobHS, readPayloadV2 = aliceStep.payload2).get() check: bobStep.transportMessage == sentTransportMessage @@ -883,7 +883,7 @@ procSuite "Waku Noise": for _ in 0 .. 10: # Alice writes to Bob - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage(aliceHSResult, message, defaultMessageNametagBuffer) readMessage = readMessage(bobHSResult, payload2, defaultMessageNametagBuffer).get() @@ -892,7 +892,7 @@ procSuite "Waku Noise": message == readMessage # Bob writes to Alice - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage(bobHSResult, message, defaultMessageNametagBuffer) readMessage = readMessage(aliceHSResult, payload2, defaultMessageNametagBuffer).get() diff --git a/tests/test_waku_noise_sessions.nim b/tests/test_waku_noise_sessions.nim index 543653982..61eaa0038 100644 --- a/tests/test_waku_noise_sessions.nim +++ b/tests/test_waku_noise_sessions.nim @@ -25,22 +25,22 @@ procSuite "Waku Noise Sessions": let hsPattern = NoiseHandshakePatterns["WakuPairing"] # Alice static/ephemeral key initialization and commitment - let aliceStaticKey = genKeyPair(rng[]) - let aliceEphemeralKey = genKeyPair(rng[]) - let s = randomSeqByte(rng[], 32) + let aliceStaticKey = genKeyPair(rng) + let aliceEphemeralKey = genKeyPair(rng) + let s = randomSeqByte(rng, 32) let aliceCommittedStaticKey = commitPublicKey(getPublicKey(aliceStaticKey), s) # Bob static/ephemeral key initialization and commitment - let bobStaticKey = genKeyPair(rng[]) - let bobEphemeralKey = genKeyPair(rng[]) - let r = randomSeqByte(rng[], 32) + let bobStaticKey = genKeyPair(rng) + let bobEphemeralKey = genKeyPair(rng) + let r = randomSeqByte(rng, 32) let bobCommittedStaticKey = commitPublicKey(getPublicKey(bobStaticKey), r) # Content Topic information let applicationName = "waku-noise-sessions" let applicationVersion = "0.1" let shardId = "10" - let qrMessageNametag = randomSeqByte(rng[], MessageNametagLength) + let qrMessageNametag = randomSeqByte(rng, MessageNametagLength) # Out-of-band Communication @@ -133,7 +133,7 @@ procSuite "Waku Noise Sessions": # and the (encrypted) transport message # The message is sent with a messageNametag equal to the one received through the QR code aliceStep = stepHandshake( - rng[], + rng, aliceHS, transportMessage = sentTransportMessage, messageNametag = qrMessageNametag, @@ -168,7 +168,7 @@ procSuite "Waku Noise Sessions": # Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him # Note that Bob verifies if the received payloadv2 has the expected messageNametag set bobStep = stepHandshake( - rng[], bobHS, readPayloadV2 = readPayloadV2, messageNametag = qrMessageNametag + rng, bobHS, readPayloadV2 = readPayloadV2, messageNametag = qrMessageNametag ) .get() @@ -199,7 +199,7 @@ procSuite "Waku Noise Sessions": # At this step, Bob writes and returns a payload bobStep = stepHandshake( - rng[], + rng, bobHS, transportMessage = sentTransportMessage, messageNametag = bobMessageNametag, @@ -233,7 +233,7 @@ procSuite "Waku Noise Sessions": # While Alice reads and returns the (decrypted) transport message aliceStep = stepHandshake( - rng[], + rng, aliceHS, readPayloadV2 = readPayloadV2, messageNametag = aliceMessageNametag, @@ -265,7 +265,7 @@ procSuite "Waku Noise Sessions": # Similarly as in first step, Alice writes a Waku2 payload containing the handshake message and the (encrypted) transport message aliceStep = stepHandshake( - rng[], + rng, aliceHS, transportMessage = sentTransportMessage, messageNametag = aliceMessageNametag, @@ -299,7 +299,7 @@ procSuite "Waku Noise Sessions": # Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him bobStep = stepHandshake( - rng[], bobHS, readPayloadV2 = readPayloadV2, messageNametag = bobMessageNametag + rng, bobHS, readPayloadV2 = readPayloadV2, messageNametag = bobMessageNametag ) .get() @@ -333,7 +333,7 @@ procSuite "Waku Noise Sessions": # Note that we exchange more than the number of messages contained in the nametag buffer to test if they are filled correctly as the communication proceeds for i in 0 .. 10 * MessageNametagBufferSize: # Alice writes to Bob - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage( aliceHSResult, message, @@ -350,7 +350,7 @@ procSuite "Waku Noise Sessions": message == readMessage # Bob writes to Alice - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage( bobHSResult, message, @@ -368,13 +368,13 @@ procSuite "Waku Noise Sessions": # We test how nametag buffers help in detecting lost messages # Alice writes two messages to Bob, but only the second is received - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage( aliceHSResult, message, outboundMessageNametagBuffer = aliceHSResult.nametagsOutbound, ) - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage( aliceHSResult, message, @@ -390,7 +390,7 @@ procSuite "Waku Noise Sessions": # We adjust bob nametag buffer for next test (i.e. the missed message is correctly recovered) delete(bobHSResult.nametagsInbound, 2) - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage( bobHSResult, message, outboundMessageNametagBuffer = bobHSResult.nametagsOutbound ) @@ -405,7 +405,7 @@ procSuite "Waku Noise Sessions": message == readMessage # We test if a missing nametag is correctly detected - message = randomSeqByte(rng[], 32) + message = randomSeqByte(rng, 32) payload2 = writeMessage( aliceHSResult, message, diff --git a/tests/test_wakunode.nim b/tests/test_wakunode.nim index a7f1084fb..081ad02e1 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, diff --git a/tests/testlib/common.nim b/tests/testlib/common.nim index 216320692..d3692e137 100644 --- a/tests/testlib/common.nim +++ b/tests/testlib/common.nim @@ -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,5 +28,5 @@ proc getRng(): ref HmacDrbgContext = rngVar.rng -template rng*(): ref HmacDrbgContext = +template rng*(): crypto.Rng = getRng() diff --git a/tests/testlib/wakucore.nim b/tests/testlib/wakucore.nim index 0c5b5199d..c0d59b0fc 100644 --- a/tests/testlib/wakucore.nim +++ b/tests/testlib/wakucore.nim @@ -22,17 +22,32 @@ 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 = + var b = SwitchBuilder + .new() + .withRng(common.rng()) + .withAddress(addrs) + .withTcpTransport() + .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 77c017d96..54f0b1ed8 100644 --- a/tests/testlib/wakunode.nim +++ b/tests/testlib/wakunode.nim @@ -11,6 +11,7 @@ import waku/[ waku_node, waku_core/topics, + node/waku_switch, node/peer_manager, waku_enr, discovery/waku_discv5, @@ -55,7 +56,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 936c01826..640d60e7d 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 @@ -38,8 +39,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 @@ -423,7 +424,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: @@ -434,7 +435,7 @@ suite "Waku Discovery v5": (waitFor startWaku(addr waku0)).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) @@ -454,7 +455,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 2f04ceb36..239099765 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 7bd44a311..074dc8754 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 d5602173a..5636afbee 100644 --- a/tests/waku_lightpush_legacy/lightpush_utils.nim +++ b/tests/waku_lightpush_legacy/lightpush_utils.nim @@ -19,7 +19,7 @@ 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 +28,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_wakunode_relay.nim b/tests/waku_relay/test_wakunode_relay.nim index a687119bd..dabc0e119 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 069600106..0bc7a3143 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 4bdcbbdc3..5737f95ea 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 e0e1bd1f7..e550989b2 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 db07d3cd6..41f616157 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 4586a0631..bbcb2f7c2 100644 --- a/tests/waku_store/store_utils.nim +++ b/tests/waku_store/store_utils.nim @@ -10,7 +10,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) @@ -19,4 +19,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 fa73cd16d..cce3e3b02 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 7621ab1e7..6cd7712c7 100644 --- a/tests/wakunode2/test_app.nim +++ b/tests/wakunode2/test_app.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.used.} import diff --git a/tests/wakunode2/test_validators.nim b/tests/wakunode2/test_validators.nim index 83928e4df..dfc3b847d 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 0393b0d72..1880cee9e 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 1171f5878..df3804114 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 d3232e571..b654665e1 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 1a4731d6a..75d06b73e 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 0bdb93123..e3250127e 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 deba7de22..9afb0eb6f 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 4043eeed9..339638038 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 a98b75520..135ff90d9 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 21d21e281..87ff37c32 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 diff --git a/waku.nimble b/waku.nimble index da5b87eb6..3c50ce54c 100644 --- a/waku.nimble +++ b/waku.nimble @@ -27,7 +27,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#c43199378f46d0aaf61be1cad1ee1d63e8f665d6", "eth", "nat_traversal", "dnsdisc", @@ -75,6 +75,7 @@ requires "https://github.com/NagyZoltanPeter/nim-brokers.git#v2.0.1" requires "https://github.com/vacp2p/nim-lsquic" requires "https://github.com/vacp2p/nim-jwt.git#057ec95eb5af0eea9c49bfe9025b3312c95dc5f2" +requires "https://github.com/logos-co/nim-libp2p-mix#7cb7556d9a228573fc5622af7ffb2dd11741e043" proc getMyCPU(): string = ## Need to set cpu more explicit manner to avoid arch issues between dependencies diff --git a/waku/api/api.nim b/waku/api/api.nim index 1eee982fd..3ee3acadc 100644 --- a/waku/api/api.nim +++ b/waku/api/api.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import chronicles, chronos, results import waku/factory/waku diff --git a/waku/api/api_conf.nim b/waku/api/api_conf.nim index 3606be596..74ed1dabf 100644 --- a/waku/api/api_conf.nim +++ b/waku/api/api_conf.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[net, options] import results diff --git a/waku/api/types.nim b/waku/api/types.nim index 2b7edd616..2dcba3f24 100644 --- a/waku/api/types.nim +++ b/waku/api/types.nim @@ -1,3 +1,5 @@ +import 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/waku/common/databases/db_postgres/dbconn.nim b/waku/common/databases/db_postgres/dbconn.nim index f24f3d4dd..a5975a0bf 100644 --- a/waku/common/databases/db_postgres/dbconn.nim +++ b/waku/common/databases/db_postgres/dbconn.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[times, strutils, os, sets, strformat, tables], results, diff --git a/waku/common/databases/db_postgres/pgasyncpool.nim b/waku/common/databases/db_postgres/pgasyncpool.nim index 0b298084e..1e151080f 100644 --- a/waku/common/databases/db_postgres/pgasyncpool.nim +++ b/waku/common/databases/db_postgres/pgasyncpool.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor # Simple async pool driver for postgress. # Inspired by: https://github.com/treeform/pg/ {.push raises: [].} diff --git a/waku/common/databases/db_sqlite.nim b/waku/common/databases/db_sqlite.nim index e398ea5ac..60d45f54c 100644 --- a/waku/common/databases/db_sqlite.nim +++ b/waku/common/databases/db_sqlite.nim @@ -1,3 +1,4 @@ +import 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/waku/common/enr/typed_record.nim b/waku/common/enr/typed_record.nim index 1db357621..475f301ac 100644 --- a/waku/common/enr/typed_record.nim +++ b/waku/common/enr/typed_record.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import std/options, results, eth/keys as eth_keys, libp2p/crypto/crypto as libp2p_crypto diff --git a/waku/common/rate_limit/request_limiter.nim b/waku/common/rate_limit/request_limiter.nim index bc318e151..7d6d432e8 100644 --- a/waku/common/rate_limit/request_limiter.nim +++ b/waku/common/rate_limit/request_limiter.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor ## RequestRateLimiter ## ## RequestRateLimiter is a general service protection mechanism. diff --git a/waku/common/utils/parse_size_units.nim b/waku/common/utils/parse_size_units.nim index 14f41a933..58fc6279f 100644 --- a/waku/common/utils/parse_size_units.nim +++ b/waku/common/utils/parse_size_units.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[strutils, math], results, regex proc parseMsgSize*(input: string): Result[uint64, string] = diff --git a/waku/compat/option_valueor.nim b/waku/compat/option_valueor.nim new file mode 100644 index 000000000..12c2cb271 --- /dev/null +++ b/waku/compat/option_valueor.nim @@ -0,0 +1,35 @@ +## 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]`. The shim restores them here while +## we adapt to upstream churn; collocated under `waku/compat/` so the +## category is explicit (compatibility with upstream API drift), not a +## generic dumping ground. + +{.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/waku/discovery/autonat_service.nim b/waku/discovery/autonat_service.nim index fb1f7dbc3..9f6ecaafd 100644 --- a/waku/discovery/autonat_service.nim +++ b/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/waku/discovery/waku_discv5.nim b/waku/discovery/waku_discv5.nim index c1b253c8c..631f4a60b 100644 --- a/waku/discovery/waku_discv5.nim +++ b/waku/discovery/waku_discv5.nim @@ -1,3 +1,6 @@ +import libp2p/crypto/crypto +import libp2p/crypto/rng +import waku/compat/option_valueor {.push raises: [].} import @@ -80,7 +83,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), @@ -88,7 +91,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, @@ -405,7 +408,7 @@ proc setupDiscoveryV5*( nodeTopicSubscriptionQueue: AsyncEventQueue[SubscriptionEvent], conf: Discv5Conf, dynamicBootstrapNodes: seq[RemotePeerInfo], - rng: ref HmacDrbgContext, + rng: crypto.Rng, key: crypto.PrivateKey, p2pListenAddress: IpAddress, portsShift: uint16, @@ -461,7 +464,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/waku/discovery/waku_dnsdisc.nim b/waku/discovery/waku_dnsdisc.nim index 0d0a82948..4754dcce0 100644 --- a/waku/discovery/waku_dnsdisc.nim +++ b/waku/discovery/waku_dnsdisc.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} ## A set of utilities to integrate EIP-1459 DNS-based discovery diff --git a/waku/discovery/waku_kademlia.nim b/waku/discovery/waku_kademlia.nim index 94b63a321..22fe4d7b8 100644 --- a/waku/discovery/waku_kademlia.nim +++ b/waku/discovery/waku_kademlia.nim @@ -1,3 +1,4 @@ +import 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 waku/waku_core, 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,14 @@ 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 +70,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 +164,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 +204,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/waku/factory/builder.nim b/waku/factory/builder.nim index 4212cb92d..184937ab5 100644 --- a/waku/factory/builder.nim +++ b/waku/factory/builder.nim @@ -15,13 +15,14 @@ import ../waku_enr, ../discovery/waku_discv5, ../waku_node, + ../node/waku_switch, ../node/peer_manager, ../common/rate_limit/setting, ../common/utils/parse_size_units type WakuNodeBuilder* = object # General - nodeRng: Option[ref crypto.HmacDrbgContext] + nodeRng: Option[crypto.Rng] nodeKey: Option[crypto.PrivateKey] netConfig: Option[NetConfig] record: Option[enr.Record] @@ -57,7 +58,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) = @@ -157,7 +158,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: @@ -190,7 +191,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(""), @@ -210,7 +211,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/waku/factory/conf_builder/kademlia_discovery_conf_builder.nim b/waku/factory/conf_builder/kademlia_discovery_conf_builder.nim index 916d71be1..9d60ed621 100644 --- a/waku/factory/conf_builder/kademlia_discovery_conf_builder.nim +++ b/waku/factory/conf_builder/kademlia_discovery_conf_builder.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import chronicles, std/options, results import libp2p/[peerid, multiaddress, peerinfo] import waku/factory/waku_conf diff --git a/waku/factory/conf_builder/mix_conf_builder.nim b/waku/factory/conf_builder/mix_conf_builder.nim index 145ccb76e..956a9b52b 100644 --- a/waku/factory/conf_builder/mix_conf_builder.nim +++ b/waku/factory/conf_builder/mix_conf_builder.nim @@ -1,5 +1,6 @@ +import 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, waku/waku_mix logScope: diff --git a/waku/factory/conf_builder/rate_limit_conf_builder.nim b/waku/factory/conf_builder/rate_limit_conf_builder.nim index b2edbef03..78e069ccf 100644 --- a/waku/factory/conf_builder/rate_limit_conf_builder.nim +++ b/waku/factory/conf_builder/rate_limit_conf_builder.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import chronicles, std/[net, options], results import waku/common/rate_limit/setting diff --git a/waku/factory/conf_builder/store_service_conf_builder.nim b/waku/factory/conf_builder/store_service_conf_builder.nim index f1b0b1402..0a0e4af82 100644 --- a/waku/factory/conf_builder/store_service_conf_builder.nim +++ b/waku/factory/conf_builder/store_service_conf_builder.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[options, strutils, sequtils] import chronicles, results, chronos import ../waku_conf, ./store_sync_conf_builder diff --git a/waku/factory/conf_builder/waku_conf_builder.nim b/waku/factory/conf_builder/waku_conf_builder.nim index 96e34eeed..83cfd9086 100644 --- a/waku/factory/conf_builder/waku_conf_builder.nim +++ b/waku/factory/conf_builder/waku_conf_builder.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import libp2p/crypto/crypto, libp2p/multiaddress, @@ -293,13 +294,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) @@ -443,7 +444,7 @@ proc applyNetworkConf(builder: var WakuConfBuilder) = warn "Failed to process entry nodes from network conf", error = processed.error() 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/waku/factory/internal_config.nim b/waku/factory/internal_config.nim index fa36aff57..c2928103f 100644 --- a/waku/factory/internal_config.nim +++ b/waku/factory/internal_config.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import chronicles, chronos, diff --git a/waku/factory/node_factory.nim b/waku/factory/node_factory.nim index 52b719b8f..5a4a2d2fd 100644 --- a/waku/factory/node_factory.nim +++ b/waku/factory/node_factory.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[options, sequtils], chronicles, @@ -54,7 +55,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, @@ -458,7 +459,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/waku/factory/waku.nim b/waku/factory/waku.nim index 6a5567f8c..62c33b12a 100644 --- a/waku/factory/waku.nim +++ b/waku/factory/waku.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import @@ -60,7 +61,7 @@ const git_version* {.strdefine.} = "n/a" type Waku* = ref object stateInfo*: WakuStateInfo conf*: WakuConf - rng*: ref HmacDrbgContext + rng*: crypto.Rng key: crypto.PrivateKey @@ -82,7 +83,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", diff --git a/waku/incentivization/eligibility_manager.nim b/waku/incentivization/eligibility_manager.nim index cbbf4774c..cd0b09303 100644 --- a/waku/incentivization/eligibility_manager.nim +++ b/waku/incentivization/eligibility_manager.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[options, sets], chronos, web3, stew/byteutils, stint, results, chronicles import waku/incentivization/rpc, tests/waku_rln_relay/utils_onchain diff --git a/waku/node/delivery_service/recv_service/recv_service.nim b/waku/node/delivery_service/recv_service/recv_service.nim index 899f80f71..43c6ae70c 100644 --- a/waku/node/delivery_service/recv_service/recv_service.nim +++ b/waku/node/delivery_service/recv_service/recv_service.nim @@ -1,3 +1,4 @@ +import 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/waku/node/delivery_service/send_service/delivery_task.nim b/waku/node/delivery_service/send_service/delivery_task.nim index aa1dc17d7..a8b55138c 100644 --- a/waku/node/delivery_service/send_service/delivery_task.nim +++ b/waku/node/delivery_service/send_service/delivery_task.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[options, times], chronos import brokers/broker_context import waku/waku_core, waku/api/types, waku/requests/node_requests diff --git a/waku/node/delivery_service/send_service/lightpush_processor.nim b/waku/node/delivery_service/send_service/lightpush_processor.nim index 7a9f65c71..bbed1a2a1 100644 --- a/waku/node/delivery_service/send_service/lightpush_processor.nim +++ b/waku/node/delivery_service/send_service/lightpush_processor.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import chronicles, chronos, results import std/options import brokers/broker_context diff --git a/waku/node/delivery_service/send_service/relay_processor.nim b/waku/node/delivery_service/send_service/relay_processor.nim index e06b664fb..4fe428921 100644 --- a/waku/node/delivery_service/send_service/relay_processor.nim +++ b/waku/node/delivery_service/send_service/relay_processor.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/options import chronos, chronicles import brokers/broker_context diff --git a/waku/node/delivery_service/send_service/send_service.nim b/waku/node/delivery_service/send_service/send_service.nim index 88ec802cf..209f4f958 100644 --- a/waku/node/delivery_service/send_service/send_service.nim +++ b/waku/node/delivery_service/send_service/send_service.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor ## This module reinforces the publish operation with regular store-v3 requests. ## diff --git a/waku/node/delivery_service/subscription_manager.nim b/waku/node/delivery_service/subscription_manager.nim index 393a61eae..6a731b621 100644 --- a/waku/node/delivery_service/subscription_manager.nim +++ b/waku/node/delivery_service/subscription_manager.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[sequtils, sets, tables, options, strutils], chronos, chronicles, results import libp2p/[peerid, peerinfo] import brokers/broker_context diff --git a/waku/node/health_monitor/node_health_monitor.nim b/waku/node/health_monitor/node_health_monitor.nim index c652f7cea..c1bdc6572 100644 --- a/waku/node/health_monitor/node_health_monitor.nim +++ b/waku/node/health_monitor/node_health_monitor.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/node/kernel_api/filter.nim b/waku/node/kernel_api/filter.nim index 948035f14..fdc1efba7 100644 --- a/waku/node/kernel_api/filter.nim +++ b/waku/node/kernel_api/filter.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/node/kernel_api/lightpush.nim b/waku/node/kernel_api/lightpush.nim index ffe2afdac..edc579301 100644 --- a/waku/node/kernel_api/lightpush.nim +++ b/waku/node/kernel_api/lightpush.nim @@ -1,3 +1,4 @@ +import 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/waku/node/kernel_api/peer_exchange.nim b/waku/node/kernel_api/peer_exchange.nim index a4bec727b..f52818378 100644 --- a/waku/node/kernel_api/peer_exchange.nim +++ b/waku/node/kernel_api/peer_exchange.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/node/kernel_api/ping.nim b/waku/node/kernel_api/ping.nim index 9dc649fd8..eddd69530 100644 --- a/waku/node/kernel_api/ping.nim +++ b/waku/node/kernel_api/ping.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/node/kernel_api/relay.nim b/waku/node/kernel_api/relay.nim index f1b80cf19..8fc6bda1f 100644 --- a/waku/node/kernel_api/relay.nim +++ b/waku/node/kernel_api/relay.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/node/kernel_api/store.nim b/waku/node/kernel_api/store.nim index fcf0dfc89..1da9477e3 100644 --- a/waku/node/kernel_api/store.nim +++ b/waku/node/kernel_api/store.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/node/peer_manager/peer_manager.nim b/waku/node/peer_manager/peer_manager.nim index 6602c049b..81a030da5 100644 --- a/waku/node/peer_manager/peer_manager.nim +++ b/waku/node/peer_manager/peer_manager.nim @@ -1,3 +1,4 @@ +import 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/waku/node/peer_manager/peer_store/waku_peer_storage.nim b/waku/node/peer_manager/peer_store/waku_peer_storage.nim index dc1452618..c11274431 100644 --- a/waku/node/peer_manager/peer_store/waku_peer_storage.nim +++ b/waku/node/peer_manager/peer_store/waku_peer_storage.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/node/peer_manager/waku_peer_store.nim b/waku/node/peer_manager/waku_peer_store.nim index 93ac9ad2e..b4a668ccf 100644 --- a/waku/node/peer_manager/waku_peer_store.nim +++ b/waku/node/peer_manager/waku_peer_store.nim @@ -1,3 +1,4 @@ +import 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/waku/node/waku_metrics.nim b/waku/node/waku_metrics.nim index af74b1532..3fc8bc585 100644 --- a/waku/node/waku_metrics.nim +++ b/waku/node/waku_metrics.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import chronicles, chronos, metrics, metrics/chronos_httpserver diff --git a/waku/node/waku_node.nim b/waku/node/waku_node.nim index 26a2b5a57..c10f7e0f3 100644 --- a/waku/node/waku_node.nim +++ b/waku/node/waku_node.nim @@ -1,3 +1,4 @@ +import 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 @@ -126,7 +127,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 @@ -207,7 +208,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/waku/node/waku_switch.nim b/waku/node/waku_switch.nim index d1af77662..6e02b8b8b 100644 --- a/waku/node/waku_switch.nim +++ b/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, @@ -80,8 +82,7 @@ proc newWakuSwitch*( .new() .withRng(rng) .withMaxConnections(maxConnections) - .withMaxIn(maxIn) - .withMaxOut(maxOut) + .withMaxInOut(maxIn, maxOut) .withMaxConnsPerPeer(maxConnsPerPeer) .withYamux() .withMplex(inTimeout, outTimeout) @@ -112,6 +113,6 @@ proc newWakuSwitch*( b = b.withAddress(address) if not rendezvous.isNil(): - b = b.withRendezVous(rendezvous) + b = b.withRendezVous() b.build() diff --git a/waku/persistency/persistency.nim b/waku/persistency/persistency.nim index 916f3ac8b..ef00bd6f6 100644 --- a/waku/persistency/persistency.nim +++ b/waku/persistency/persistency.nim @@ -1,3 +1,4 @@ +import 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/waku/rest_api/endpoint/admin/handlers.nim b/waku/rest_api/endpoint/admin/handlers.nim index 304fdabf8..371d75df8 100644 --- a/waku/rest_api/endpoint/admin/handlers.nim +++ b/waku/rest_api/endpoint/admin/handlers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/rest_api/endpoint/builder.nim b/waku/rest_api/endpoint/builder.nim index 9b4ecf662..56876db3a 100644 --- a/waku/rest_api/endpoint/builder.nim +++ b/waku/rest_api/endpoint/builder.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import net, tables diff --git a/waku/rest_api/endpoint/debug/handlers.nim b/waku/rest_api/endpoint/debug/handlers.nim index 43b6fbbf1..d033b905d 100644 --- a/waku/rest_api/endpoint/debug/handlers.nim +++ b/waku/rest_api/endpoint/debug/handlers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import chronicles, json_serialization, presto/route diff --git a/waku/rest_api/endpoint/filter/handlers.nim b/waku/rest_api/endpoint/filter/handlers.nim index 61d7eb96f..c5c2639a6 100644 --- a/waku/rest_api/endpoint/filter/handlers.nim +++ b/waku/rest_api/endpoint/filter/handlers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/rest_api/endpoint/health/handlers.nim b/waku/rest_api/endpoint/health/handlers.nim index 865133245..a7456a7b3 100644 --- a/waku/rest_api/endpoint/health/handlers.nim +++ b/waku/rest_api/endpoint/health/handlers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import chronicles, json_serialization, presto/route diff --git a/waku/rest_api/endpoint/health/types.nim b/waku/rest_api/endpoint/health/types.nim index 88fa736a8..c7f9ec60f 100644 --- a/waku/rest_api/endpoint/health/types.nim +++ b/waku/rest_api/endpoint/health/types.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import results diff --git a/waku/rest_api/endpoint/legacy_lightpush/handlers.nim b/waku/rest_api/endpoint/legacy_lightpush/handlers.nim index 7a3c5b1ed..cb953ff49 100644 --- a/waku/rest_api/endpoint/legacy_lightpush/handlers.nim +++ b/waku/rest_api/endpoint/legacy_lightpush/handlers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/rest_api/endpoint/lightpush/handlers.nim b/waku/rest_api/endpoint/lightpush/handlers.nim index 342053e72..e5b657f72 100644 --- a/waku/rest_api/endpoint/lightpush/handlers.nim +++ b/waku/rest_api/endpoint/lightpush/handlers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/rest_api/endpoint/origin_handler.nim b/waku/rest_api/endpoint/origin_handler.nim index 9752bfb56..0edf7ec9a 100644 --- a/waku/rest_api/endpoint/origin_handler.nim +++ b/waku/rest_api/endpoint/origin_handler.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/rest_api/endpoint/relay/handlers.nim b/waku/rest_api/endpoint/relay/handlers.nim index 4a1415361..a65373a75 100644 --- a/waku/rest_api/endpoint/relay/handlers.nim +++ b/waku/rest_api/endpoint/relay/handlers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/rest_api/endpoint/rest_serdes.nim b/waku/rest_api/endpoint/rest_serdes.nim index 8dcb7c8f1..be7de9df0 100644 --- a/waku/rest_api/endpoint/rest_serdes.nim +++ b/waku/rest_api/endpoint/rest_serdes.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/rest_api/endpoint/store/handlers.nim b/waku/rest_api/endpoint/store/handlers.nim index 7d37191fb..183dd2493 100644 --- a/waku/rest_api/endpoint/store/handlers.nim +++ b/waku/rest_api/endpoint/store/handlers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/rest_api/endpoint/store/types.nim b/waku/rest_api/endpoint/store/types.nim index 99818b5ff..e097ff2df 100644 --- a/waku/rest_api/endpoint/store/types.nim +++ b/waku/rest_api/endpoint/store/types.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/rest_api/handlers.nim b/waku/rest_api/handlers.nim index 4fc922f36..ed4018a32 100644 --- a/waku/rest_api/handlers.nim +++ b/waku/rest_api/handlers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import chronos, std/[options, sequtils], results diff --git a/waku/utils/noise.nim b/waku/utils/noise.nim index c225ad9bf..e04fc8dba 100644 --- a/waku/utils/noise.nim +++ b/waku/utils/noise.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import results diff --git a/waku/utils/requests.nim b/waku/utils/requests.nim index d9afd2887..a7fd9a2a9 100644 --- a/waku/utils/requests.nim +++ b/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/waku/waku_archive/archive.nim b/waku/waku_archive/archive.nim index 976d7d035..e29913b04 100644 --- a/waku/waku_archive/archive.nim +++ b/waku/waku_archive/archive.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_archive/driver/builder.nim b/waku/waku_archive/driver/builder.nim index 811b16999..b2bb410a0 100644 --- a/waku/waku_archive/driver/builder.nim +++ b/waku/waku_archive/driver/builder.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import results, chronicles, chronos diff --git a/waku/waku_archive/driver/postgres_driver/migrations.nim b/waku/waku_archive/driver/postgres_driver/migrations.nim index 700f2a162..2e0f3519b 100644 --- a/waku/waku_archive/driver/postgres_driver/migrations.nim +++ b/waku/waku_archive/driver/postgres_driver/migrations.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import std/strutils, results, chronicles, chronos diff --git a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim index 82c8ec2ae..6d2e471a1 100644 --- a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim +++ b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_archive/driver/queue_driver/queue_driver.nim b/waku/waku_archive/driver/queue_driver/queue_driver.nim index 2ffc9ab00..6ab5ff104 100644 --- a/waku/waku_archive/driver/queue_driver/queue_driver.nim +++ b/waku/waku_archive/driver/queue_driver/queue_driver.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import std/options, results, stew/sorted_set, chronicles, chronos diff --git a/waku/waku_archive/driver/sqlite_driver/sqlite_driver.nim b/waku/waku_archive/driver/sqlite_driver/sqlite_driver.nim index ff7b0e7d3..8a1d775ff 100644 --- a/waku/waku_archive/driver/sqlite_driver/sqlite_driver.nim +++ b/waku/waku_archive/driver/sqlite_driver/sqlite_driver.nim @@ -1,3 +1,4 @@ +import 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/waku/waku_archive/retention_policy/builder.nim b/waku/waku_archive/retention_policy/builder.nim index 7e777f4a0..d274e01d9 100644 --- a/waku/waku_archive/retention_policy/builder.nim +++ b/waku/waku_archive/retention_policy/builder.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import std/[strutils, options], regex, results diff --git a/waku/waku_archive/retention_policy/retention_policy_capacity.nim b/waku/waku_archive/retention_policy/retention_policy_capacity.nim index ff4da6861..94502014e 100644 --- a/waku/waku_archive/retention_policy/retention_policy_capacity.nim +++ b/waku/waku_archive/retention_policy/retention_policy_capacity.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import results, chronicles, chronos diff --git a/waku/waku_core/peers.nim b/waku/waku_core/peers.nim index c4b8b593e..d2db1b6c4 100644 --- a/waku/waku_core/peers.nim +++ b/waku/waku_core/peers.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_core/topics/content_topic.nim b/waku/waku_core/topics/content_topic.nim index 3eeb35771..7e1942f25 100644 --- a/waku/waku_core/topics/content_topic.nim +++ b/waku/waku_core/topics/content_topic.nim @@ -1,3 +1,4 @@ +import 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/waku/waku_core/topics/sharding.nim b/waku/waku_core/topics/sharding.nim index 1cb5b37b3..a34e56fbf 100644 --- a/waku/waku_core/topics/sharding.nim +++ b/waku/waku_core/topics/sharding.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor ## Waku autosharding utils ## ## See 51/WAKU2-RELAY-SHARDING RFC: https://rfc.vac.dev/spec/51/#automatic-sharding diff --git a/waku/waku_enr/capabilities.nim b/waku/waku_enr/capabilities.nim index 26899fbb4..901b4d8f3 100644 --- a/waku/waku_enr/capabilities.nim +++ b/waku/waku_enr/capabilities.nim @@ -1,9 +1,10 @@ +import 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/waku/waku_enr/multiaddr.nim b/waku/waku_enr/multiaddr.nim index 4d6e9baa7..0e1e8e390 100644 --- a/waku/waku_enr/multiaddr.nim +++ b/waku/waku_enr/multiaddr.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_enr/sharding.nim b/waku/waku_enr/sharding.nim index 2aeb96a9d..a48571cc4 100644 --- a/waku/waku_enr/sharding.nim +++ b/waku/waku_enr/sharding.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_filter_v2/client.nim b/waku/waku_filter_v2/client.nim index 7798f41b7..b4c873d26 100644 --- a/waku/waku_filter_v2/client.nim +++ b/waku/waku_filter_v2/client.nim @@ -1,3 +1,5 @@ +import 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( @@ -211,7 +213,7 @@ proc initProtocolHandler(wfc: WakuFilterClient) = wfc.codec = WakuFilterPushCodec proc new*( - T: type WakuFilterClient, peerManager: PeerManager, rng: ref HmacDrbgContext + T: type WakuFilterClient, peerManager: PeerManager, rng: crypto.Rng ): T = let brokerCtx = globalBrokerContext() let wfc = WakuFilterClient( diff --git a/waku/waku_filter_v2/protocol.nim b/waku/waku_filter_v2/protocol.nim index 35620b6cd..b24e9e0c7 100644 --- a/waku/waku_filter_v2/protocol.nim +++ b/waku/waku_filter_v2/protocol.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor ## Waku Filter protocol for subscribing and filtering messages {.push raises: [].} diff --git a/waku/waku_filter_v2/subscriptions.nim b/waku/waku_filter_v2/subscriptions.nim index 0e66899d0..687756572 100644 --- a/waku/waku_filter_v2/subscriptions.nim +++ b/waku/waku_filter_v2/subscriptions.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_keystore/keystore.nim b/waku/waku_keystore/keystore.nim index 158f1a98e..e33ac78ed 100644 --- a/waku/waku_keystore/keystore.nim +++ b/waku/waku_keystore/keystore.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import options, json, strutils, sequtils, std/[tables, os] diff --git a/waku/waku_lightpush/callbacks.nim b/waku/waku_lightpush/callbacks.nim index ac2e562b6..fe3c8378f 100644 --- a/waku/waku_lightpush/callbacks.nim +++ b/waku/waku_lightpush/callbacks.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import results diff --git a/waku/waku_lightpush/client.nim b/waku/waku_lightpush/client.nim index fd12c49d2..a3e25cb9d 100644 --- a/waku/waku_lightpush/client.nim +++ b/waku/waku_lightpush/client.nim @@ -1,3 +1,5 @@ +import libp2p/crypto/crypto +import waku/compat/option_valueor {.push raises: [].} import std/options, results, chronicles, chronos, metrics, bearssl/rand, stew/byteutils @@ -16,11 +18,11 @@ 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: type WakuLightPushClient, peerManager: PeerManager, rng: crypto.Rng ): T = WakuLightPushClient(peerManager: peerManager, rng: rng) diff --git a/waku/waku_lightpush/protocol.nim b/waku/waku_lightpush/protocol.nim index 8336f4dfc..dd4970fdc 100644 --- a/waku/waku_lightpush/protocol.nim +++ b/waku/waku_lightpush/protocol.nim @@ -1,3 +1,5 @@ +import libp2p/crypto/crypto +import 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/waku/waku_lightpush_legacy/client.nim b/waku/waku_lightpush_legacy/client.nim index ab489bec9..a566ac87c 100644 --- a/waku/waku_lightpush_legacy/client.nim +++ b/waku/waku_lightpush_legacy/client.nim @@ -1,3 +1,5 @@ +import libp2p/crypto/crypto +import waku/compat/option_valueor {.push raises: [].} import std/options, results, chronicles, chronos, metrics, bearssl/rand, stew/byteutils @@ -17,12 +19,12 @@ 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, + rng: crypto.Rng, ): T = WakuLegacyLightPushClient(peerManager: peerManager, rng: rng) diff --git a/waku/waku_lightpush_legacy/protocol.nim b/waku/waku_lightpush_legacy/protocol.nim index f5ed60134..b3d728f9c 100644 --- a/waku/waku_lightpush_legacy/protocol.nim +++ b/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/waku/waku_metadata/protocol.nim b/waku/waku_metadata/protocol.nim index 7c72a6934..30f77bd73 100644 --- a/waku/waku_metadata/protocol.nim +++ b/waku/waku_metadata/protocol.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_mix/protocol.nim b/waku/waku_mix/protocol.nim index ac8b69eaf..b458a5ecf 100644 --- a/waku/waku_mix/protocol.nim +++ b/waku/waku_mix/protocol.nim @@ -1,3 +1,4 @@ +import 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,9 @@ 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/waku/waku_noise/noise.nim b/waku/waku_noise/noise.nim index 9c5dd76e7..23c3de2a3 100644 --- a/waku/waku_noise/noise.nim +++ b/waku/waku_noise/noise.nim @@ -8,6 +8,8 @@ {.push raises: [].} +import libp2p/crypto/rng +import libp2p/crypto/crypto import std/[options, strutils] import stew/byteutils import chronos @@ -168,9 +170,9 @@ proc setCipherStateKey*(cs: var CipherState, key: ChaChaPolyKey) = cs.k = key # Generates a random Symmetric Cipher State for test purposes -proc randomCipherState*(rng: var HmacDrbgContext, nonce: uint64 = 0): CipherState = +proc randomCipherState*(rng: Rng, nonce: uint64 = 0): CipherState = var randomCipherState: CipherState - hmacDrbgGenerate(rng, randomCipherState.k) + hmacDrbgGenerate(rng.bearSslDrbg, randomCipherState.k) setNonce(randomCipherState, nonce) return randomCipherState diff --git a/waku/waku_noise/noise_handshake_processing.nim b/waku/waku_noise/noise_handshake_processing.nim index 8b84bf958..e366668ad 100644 --- a/waku/waku_noise/noise_handshake_processing.nim +++ b/waku/waku_noise/noise_handshake_processing.nim @@ -4,6 +4,7 @@ {.push raises: [].} +import libp2p/crypto/rng import std/[options, strutils, tables] import chronos import chronicles @@ -237,7 +238,7 @@ proc processMessagePatternPayload( # We process an input handshake message according to current handshake state and we return the next handshake step's handshake message proc processMessagePatternTokens( - rng: var rand.HmacDrbgContext, + rng: Rng, hs: var HandshakeState, inputHandshakeMessage: seq[NoisePublicKey] = @[], ): Result[seq[NoisePublicKey], cstring] {. @@ -480,7 +481,7 @@ proc initialize*( # If the user is writing the handshake message, the transport message (if not empty) and eventually a non-empty message nametag has to be passed to transportMessage and messageNametag and readPayloadV2 can be left to its default value # It the user is reading the handshake message, the read payload v2 has to be passed to readPayloadV2 and the transportMessage can be left to its default values. Decryption is skipped if the payloadv2 read doesn't have a message nametag equal to messageNametag (empty input nametags are converted to all-0 MessageNametagLength bytes arrays) proc stepHandshake*( - rng: var rand.HmacDrbgContext, + rng: Rng, hs: var HandshakeState, readPayloadV2: PayloadV2 = default(PayloadV2), transportMessage: seq[byte] = @[], diff --git a/waku/waku_noise/noise_utils.nim b/waku/waku_noise/noise_utils.nim index 177dc6557..ea4cdb7c1 100644 --- a/waku/waku_noise/noise_utils.nim +++ b/waku/waku_noise/noise_utils.nim @@ -5,6 +5,7 @@ {.push raises: [].} +import libp2p/crypto/rng import std/[algorithm, base64, oids, options, strutils, tables, sequtils] import chronos import chronicles @@ -28,9 +29,9 @@ logScope: ################################# # Generates random byte sequences of given size -proc randomSeqByte*(rng: var HmacDrbgContext, size: int): seq[byte] = +proc randomSeqByte*(rng: Rng, size: int): seq[byte] = var output = newSeq[byte](size.uint32) - hmacDrbgGenerate(rng, output) + hmacDrbgGenerate(rng.bearSslDrbg, output) return output # Pads a payload according to PKCS#7 as per RFC 5652 https://datatracker.ietf.org/doc/html/rfc5652#section-6.3 @@ -149,7 +150,7 @@ proc isDefault*[T](value: T): bool = ################################# # Generate random (public, private) Elliptic Curve key pairs -proc genKeyPair*(rng: var HmacDrbgContext): KeyPair = +proc genKeyPair*(rng: Rng): KeyPair = var keyPair: KeyPair keyPair.privateKey = EllipticCurveKey.random(rng) keyPair.publicKey = keyPair.privateKey.public() @@ -319,18 +320,18 @@ proc dh*(private: EllipticCurveKey, public: EllipticCurveKey): EllipticCurveKey ################################# # Generates a random ChaChaPolyKey for testing encryption/decryption -proc randomChaChaPolyKey*(rng: var HmacDrbgContext): ChaChaPolyKey = +proc randomChaChaPolyKey*(rng: Rng): ChaChaPolyKey = var key: ChaChaPolyKey - hmacDrbgGenerate(rng, key) + hmacDrbgGenerate(rng.bearSslDrbg, key) return key # Generates a random ChaChaPoly Cipher State for testing encryption/decryption -proc randomChaChaPolyCipherState*(rng: var HmacDrbgContext): ChaChaPolyCipherState = +proc randomChaChaPolyCipherState*(rng: Rng): ChaChaPolyCipherState = var randomCipherState: ChaChaPolyCipherState randomCipherState.k = randomChaChaPolyKey(rng) - hmacDrbgGenerate(rng, randomCipherState.nonce) + hmacDrbgGenerate(rng.bearSslDrbg, randomCipherState.nonce) randomCipherState.ad = newSeq[byte](32) - hmacDrbgGenerate(rng, randomCipherState.ad) + hmacDrbgGenerate(rng.bearSslDrbg, randomCipherState.ad) return randomCipherState ################################################################# @@ -351,7 +352,7 @@ proc toNoisePublicKey*(publicKey: EllipticCurveKey): NoisePublicKey = return noisePublicKey # Generates a random Noise public key -proc genNoisePublicKey*(rng: var HmacDrbgContext): NoisePublicKey = +proc genNoisePublicKey*(rng: Rng): NoisePublicKey = var noisePublicKey: NoisePublicKey # We generate a random key pair let keyPair: KeyPair = genKeyPair(rng) @@ -446,7 +447,7 @@ proc `==`*(p1, p2: PayloadV2): bool = (p1.transportMessage == p2.transportMessage) # Generates a random PayloadV2 -proc randomPayloadV2*(rng: var HmacDrbgContext): PayloadV2 = +proc randomPayloadV2*(rng: Rng): PayloadV2 = var payload2: PayloadV2 # We set a random messageNametag let randMessageNametag = randomSeqByte(rng, MessageNametagLength) diff --git a/waku/waku_peer_exchange/client.nim b/waku/waku_peer_exchange/client.nim index 15426c464..b1940b424 100644 --- a/waku/waku_peer_exchange/client.nim +++ b/waku/waku_peer_exchange/client.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/options, results, chronicles, chronos, metrics import ./common, ./rpc, ./rpc_codec, ../node/peer_manager diff --git a/waku/waku_peer_exchange/protocol.nim b/waku/waku_peer_exchange/protocol.nim index b99f5eabf..fd7896e83 100644 --- a/waku/waku_peer_exchange/protocol.nim +++ b/waku/waku_peer_exchange/protocol.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor import std/[options, sequtils, random], results, diff --git a/waku/waku_relay/protocol.nim b/waku/waku_relay/protocol.nim index d0b1ddb48..8c59bf250 100644 --- a/waku/waku_relay/protocol.nim +++ b/waku/waku_relay/protocol.nim @@ -1,3 +1,4 @@ +import 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/waku/waku_rendezvous/client.nim b/waku/waku_rendezvous/client.nim index 09e789774..15561c944 100644 --- a/waku/waku_rendezvous/client.nim +++ b/waku/waku_rendezvous/client.nim @@ -1,3 +1,4 @@ +import 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/waku/waku_rendezvous/common.nim b/waku/waku_rendezvous/common.nim index 18c633efb..e78efc025 100644 --- a/waku/waku_rendezvous/common.nim +++ b/waku/waku_rendezvous/common.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import std/options, chronos diff --git a/waku/waku_rendezvous/protocol.nim b/waku/waku_rendezvous/protocol.nim index 89433f533..b848ab167 100644 --- a/waku/waku_rendezvous/protocol.nim +++ b/waku/waku_rendezvous/protocol.nim @@ -1,3 +1,4 @@ +import 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/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim index 02317a056..e9a9db519 100644 --- a/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim +++ b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_rln_relay/rln/rln_interface.nim b/waku/waku_rln_relay/rln/rln_interface.nim index 612d1a2cc..e735ab0f3 100644 --- a/waku/waku_rln_relay/rln/rln_interface.nim +++ b/waku/waku_rln_relay/rln/rln_interface.nim @@ -1,3 +1,4 @@ +import 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/waku/waku_rln_relay/rln/wrappers.nim b/waku/waku_rln_relay/rln/wrappers.nim index 4fc8c1542..42f10d53e 100644 --- a/waku/waku_rln_relay/rln/wrappers.nim +++ b/waku/waku_rln_relay/rln/wrappers.nim @@ -1,3 +1,4 @@ +import 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/waku/waku_rln_relay/rln_relay.nim b/waku/waku_rln_relay/rln_relay.nim index 7c36300b2..48be77521 100644 --- a/waku/waku_rln_relay/rln_relay.nim +++ b/waku/waku_rln_relay/rln_relay.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_store/client.nim b/waku/waku_store/client.nim index b49662811..22c521235 100644 --- a/waku/waku_store/client.nim +++ b/waku/waku_store/client.nim @@ -1,3 +1,5 @@ +import libp2p/crypto/crypto +import 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/waku/waku_store/protocol.nim b/waku/waku_store/protocol.nim index 17b7fb214..535a5fe9c 100644 --- a/waku/waku_store/protocol.nim +++ b/waku/waku_store/protocol.nim @@ -1,3 +1,4 @@ +import 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/waku/waku_store/resume.nim b/waku/waku_store/resume.nim index b7864da94..cddd9a7fb 100644 --- a/waku/waku_store/resume.nim +++ b/waku/waku_store/resume.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_store/self_req_handler.nim b/waku/waku_store/self_req_handler.nim index 315961307..c4358f322 100644 --- a/waku/waku_store/self_req_handler.nim +++ b/waku/waku_store/self_req_handler.nim @@ -1,3 +1,4 @@ +import 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/waku/waku_store_sync/reconciliation.nim b/waku/waku_store_sync/reconciliation.nim index b18251fff..bcf93f058 100644 --- a/waku/waku_store_sync/reconciliation.nim +++ b/waku/waku_store_sync/reconciliation.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import diff --git a/waku/waku_store_sync/transfer.nim b/waku/waku_store_sync/transfer.nim index 5d20afb18..3f819bd09 100644 --- a/waku/waku_store_sync/transfer.nim +++ b/waku/waku_store_sync/transfer.nim @@ -1,3 +1,4 @@ +import waku/compat/option_valueor {.push raises: [].} import