Fabiana Cecin 6837ae0c1f
feat: bump nim-libp2p to v2.0.0 (#3929)
* bump nim-libp2p pin to v2.0.0 tag
* bump json_rpc to v0.6.1, lsquic to v0.5.1, boringssl to v0.0.8 (latest tags)
* add libp2p_mix dep; repoint libp2p/protocols/mix -> libp2p_mix
* pin nimble.lock: websock / protobuf_serialization / npeg / jwt
* Makefile: add -d:libp2p_quic_support
* regenerate nix/deps.nix (adds libp2p_mix, refreshes pins)
* migrate rng ref HmacDrbgContext -> libp2p Rng across prod/channels/tests (interface-only; same DRBG)
* waku_switch: TransportConfig factory; unified 2.0.0 connection limits (withMaxInOut, withMaxConnections); local MaxConnections
* waku_relay/rendezvous/discv5/kademlia: v2.0.0 API (rng, config, ServiceDiscovery rename)
* call Service.setup() on post-build switch services (2.0.0 split setup/start)
* drop libp2p/utils/semaphore -> chronos AsyncSemaphore
* add logos_delivery/waku/compat/option_valueor shim (Option[T] valueOr/withValue, dropped upstream)
* add std/options where a transitive re-export was removed
* add newStandardSwitch shim (libp2p removed it in 2.0.0); mounts yamux+mplex to match prod muxer
* PeerId.random(rng); common.rng()/crypto.newRng(); hoist shared rng (instantiation cleanup)
* update expectations for 2.0.0 defaults: DEFAULT_PROTOCOLS += /ipfs/id/push/1.0.0; agent "nim-libp2p"
* drop relay reboot/reconnect test (asserted a Switch restart capability that is simply not supported)
* fix up a few tests that were flaking on MacOS (libp2p upgrade may have exposed these)
2026-06-15 09:56:15 -03:00

104 lines
3.1 KiB
Nim

import
std/[options, times],
results,
stew/byteutils,
chronos,
libp2p/switch,
libp2p/builders,
libp2p/crypto/crypto as libp2p_keys,
eth/keys as eth_keys
import logos_delivery/waku/waku_core, ./common
export switch
# Time
proc now*(): Timestamp =
getNanosecondTime(getTime().toUnixFloat())
proc ts*(offset = 0, origin = now()): Timestamp =
origin + getNanosecondTime(int64(offset))
# Switch
proc generateEcdsaKey*(): libp2p_keys.PrivateKey =
libp2p_keys.PrivateKey.random(ECDSA, common.rng()).get()
proc generateEcdsaKeyPair*(): libp2p_keys.KeyPair =
libp2p_keys.KeyPair.random(ECDSA, common.rng()).get()
proc generateSecp256k1Key*(): libp2p_keys.PrivateKey =
libp2p_keys.PrivateKey.random(Secp256k1, common.rng()).get()
proc ethSecp256k1Key*(hex: string): eth_keys.PrivateKey =
eth_keys.PrivateKey.fromHex(hex).get()
proc newStandardSwitch*(
privKey = Opt.none(libp2p_keys.PrivateKey),
addrs: MultiAddress = MultiAddress.init("/ip4/127.0.0.1/tcp/0").get(),
): Switch =
## Bare libp2p switch for tests. Replaces nim-libp2p's `newStandardSwitch`,
## removed in 2.0.0. Mirrors the *substrate* of the production switch
## (`newWakuSwitch`, waku/node/waku_switch.nim): same transport (TCP only — no
## QUIC yet, like prod), security (Noise), and muxers (yamux first so it is the
## one negotiated, then mplex). Tests therefore run on the same kernel peers
## actually use in prod. NB: the removed libp2p `newStandardSwitch` was
## mplex-only; mounting yamux here is intentional (match prod's muxer) — do not
## "restore" it to mplex-only. Deliberately omits the prod services (autonat, circuit
## relay, name resolver, NAT, signed peer record, connection limits): they add
## port grief, network delays, and nondeterminism with no value to unit tests.
## For a full-stack node, use newTestWakuNode / newWakuSwitch instead.
var b = SwitchBuilder
.new()
.withRng(common.rng())
.withAddress(addrs)
.withTcpTransport()
.withYamux()
.withMplex()
.withNoise()
if privKey.isSome():
b = b.withPrivateKey(privKey.get())
b.build()
proc newTestSwitch*(
key = none(libp2p_keys.PrivateKey), address = none(MultiAddress)
): Switch =
let peerKey = key.get(generateSecp256k1Key())
let peerAddr = address.get(MultiAddress.init("/ip4/127.0.0.1/tcp/0").get())
return newStandardSwitch(privKey = Opt.some(peerKey), addrs = peerAddr)
# Waku message
export waku_core.DefaultPubsubTopic, waku_core.DefaultContentTopic
proc fakeWakuMessage*(
payload: string | seq[byte] = "TEST-PAYLOAD",
contentTopic = DefaultContentTopic,
meta: string | seq[byte] = newSeq[byte](),
ts = now(),
ephemeral = false,
proof = newSeq[byte](),
): WakuMessage =
var payloadBytes: seq[byte]
var metaBytes: seq[byte]
when payload is string:
payloadBytes = toBytes(payload)
else:
payloadBytes = payload
when meta is string:
metaBytes = toBytes(meta)
else:
metaBytes = meta
WakuMessage(
payload: payloadBytes,
contentTopic: contentTopic,
meta: metaBytes,
version: 2,
timestamp: ts,
ephemeral: ephemeral,
proof: proof,
)