Skip to content

Commit 4c48cd4

Browse files
committed
chore: put test_all and utils back
1 parent b7fbc8e commit 4c48cd4

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

tests/discovery/test_all.nim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Nim-LibP2P
2+
# Copyright (c) 2023-2025 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0 ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
10+
{.used.}
11+
12+
import testrendezvous, testrendezvouserrors, testrendezvousprotobuf

tests/discovery/utils.nim

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Nim-LibP2P
2+
# Copyright (c) 2023-2025 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0 ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
10+
import chronos, sequtils
11+
import
12+
../../libp2p/[
13+
builders,
14+
crypto/crypto,
15+
peerid,
16+
protobuf/minprotobuf,
17+
protocols/rendezvous,
18+
protocols/rendezvous/protobuf,
19+
routing_record,
20+
switch,
21+
]
22+
import ../tools/[crypto]
23+
24+
proc createSwitch*(): Switch =
25+
SwitchBuilder
26+
.new()
27+
.withRng(newRng())
28+
.withAddresses(@[MultiAddress.init(MemoryAutoAddress).tryGet()])
29+
.withMemoryTransport()
30+
.withMplex()
31+
.withNoise()
32+
.build()
33+
34+
proc createSwitch*(rdv: RendezVous): Switch =
35+
var lrdv = rdv
36+
if rdv.isNil():
37+
lrdv = RendezVous.new()
38+
39+
SwitchBuilder
40+
.new()
41+
.withRng(newRng())
42+
.withAddresses(@[MultiAddress.init(MemoryAutoAddress).tryGet()])
43+
.withMemoryTransport()
44+
.withMplex()
45+
.withNoise()
46+
.withRendezVous(lrdv)
47+
.build()
48+
49+
proc setupNodes*(count: int): seq[RendezVous] =
50+
doAssert(count > 0, "Count must be greater than 0")
51+
52+
var rdvs: seq[RendezVous] = @[]
53+
54+
for x in 0 ..< count:
55+
var rdv: RendezVous = RendezVous.new()
56+
let node = createSwitch(rdv)
57+
rdvs.add(rdv)
58+
59+
return rdvs
60+
61+
proc setupRendezvousNodeWithPeerNodes*(count: int): (RendezVous, seq[RendezVous]) =
62+
let
63+
rdvs = setupNodes(count + 1)
64+
rendezvousRdv = rdvs[0]
65+
peerRdvs = rdvs[1 ..^ 1]
66+
67+
return (rendezvousRdv, peerRdvs)
68+
69+
template startAndDeferStop*(nodes: seq[RendezVous]) =
70+
await allFutures(nodes.mapIt(it.switch.start()))
71+
defer:
72+
await allFutures(nodes.mapIt(it.switch.stop()))
73+
74+
proc connectNodes*(dialer: RendezVous, target: RendezVous) {.async.} =
75+
await dialer.switch.connect(
76+
target.switch.peerInfo.peerId, target.switch.peerInfo.addrs
77+
)
78+
79+
proc connectNodesToRendezvousNode*(
80+
nodes: seq[RendezVous], rendezvousNode: RendezVous
81+
) {.async.} =
82+
for node in nodes:
83+
await connectNodes(node, rendezvousNode)
84+
85+
proc buildProtobufCookie*(offset: uint64, namespace: string): seq[byte] =
86+
var pb = initProtoBuffer()
87+
pb.write(1, offset)
88+
pb.write(2, namespace)
89+
pb.finish()
90+
pb.buffer
91+
92+
proc injectCookieForPeer*(
93+
rdv: RendezVous, peerId: PeerId, namespace: string, cookie: seq[byte]
94+
) =
95+
discard rdv.cookiesSaved.hasKeyOrPut(peerId, {namespace: cookie}.toTable())
96+
97+
proc populatePeerRegistrations*(
98+
peerRdv: RendezVous, targetRdv: RendezVous, namespace: string, count: int
99+
) {.async.} =
100+
# Test helper: quickly populate many registrations for a peer.
101+
# We first create a single real registration, then clone that record
102+
# directly into the rendezvous registry to reach the desired count fast.
103+
#
104+
# Notes:
105+
# - Calling advertise() concurrently results in bufferstream defect.
106+
# - Calling advertise() sequentially is too slow for large counts.
107+
await peerRdv.advertise(namespace)
108+
109+
let record = targetRdv.registered.s[0]
110+
for i in 0 ..< count - 1:
111+
targetRdv.registered.s.add(record)
112+
113+
proc createCorruptedSignedPeerRecord*(peerId: PeerId): SignedPeerRecord =
114+
let wrongPrivKey = PrivateKey.random(rng[]).tryGet()
115+
let record = PeerRecord.init(peerId, @[])
116+
SignedPeerRecord.init(wrongPrivKey, record).tryGet()
117+
118+
proc sendRdvMessage*(
119+
node: RendezVous, target: RendezVous, buffer: seq[byte]
120+
): Future[seq[byte]] {.async.} =
121+
let conn = await node.switch.dial(target.switch.peerInfo.peerId, RendezVousCodec)
122+
defer:
123+
await conn.close()
124+
125+
await conn.writeLp(buffer)
126+
127+
let response = await conn.readLp(4096)
128+
response
129+
130+
proc prepareRegisterMessage*(
131+
namespace: string, spr: seq[byte], ttl: Duration
132+
): Message =
133+
Message(
134+
msgType: MessageType.Register,
135+
register: Opt.some(
136+
Register(ns: namespace, signedPeerRecord: spr, ttl: Opt.some(ttl.seconds.uint64))
137+
),
138+
)
139+
140+
proc prepareDiscoverMessage*(
141+
ns: Opt[string] = Opt.none(string),
142+
limit: Opt[uint64] = Opt.none(uint64),
143+
cookie: Opt[seq[byte]] = Opt.none(seq[byte]),
144+
): Message =
145+
Message(
146+
msgType: MessageType.Discover,
147+
discover: Opt.some(Discover(ns: ns, limit: limit, cookie: cookie)),
148+
)

0 commit comments

Comments
 (0)