@@ -12,26 +12,26 @@ import
1212
1313import
1414 waku/ [waku_core/ topics, waku_enr, discovery/ waku_discv5, common/ enr],
15- ../ testlib/ [wakucore, testasync, assertions, futures],
15+ ../ testlib/ [wakucore, testasync, assertions, futures, wakunode ],
1616 ../ waku_enr/ utils,
1717 ./ utils
1818
1919import eth/ p2p/ discoveryv5/ enr as ethEnr
2020
21- procSuite " Waku Discovery v5" :
21+ include waku/ factory/ waku
22+
23+ suite " Waku Discovery v5" :
24+ const validEnr =
25+ " enr:-K64QGAvsATunmvMT5c3LFjKS0tG39zlQ1195Z2pWu6RoB5fWP3EXz9QPlRXN" &
26+ " wOtDoRLgm4bATUB53AC8uml-ZtUE_kBgmlkgnY0gmlwhApkZgOKbXVsdGlhZGRyc4" &
27+ " CCcnOTAAAIAAAAAQACAAMABAAFAAYAB4lzZWNwMjU2azGhAwG-CMmXpAPj84f6dCt" &
28+ " MZ6xVYOa6bdmgAiKYG6LKGQlbg3RjcILqYIV3YWt1MgE"
29+
2230 let
2331 rng = eth_keys.newRng ()
2432 pk1 = eth_keys.PrivateKey .random (rng[])
2533 pk2 = eth_keys.PrivateKey .random (rng[])
2634
27- enrNoCapabilities =
28- initRecord (1 , pk1, {" rs" : @ [0 .byte , 0 .byte , 1 .byte , 0 .byte , 0 .byte ]}).value ()
29- enrRelay = initRecord (
30- 1 , pk2, {" waku2" : @ [1 .byte ], " rs" : @ [0 .byte , 1 .byte , 1 .byte , 0 .byte , 1 .byte ]}
31- )
32- .value ()
33- enrNoShardingInfo = initRecord (1 , pk1, {" waku2" : @ [1 .byte ]}).value ()
34-
3535 suite " shardingPredicate" :
3636 var
3737 recordCluster21 {.threadvar .}: Record
@@ -120,6 +120,14 @@ procSuite "Waku Discovery v5":
120120
121121 asyncTest " filter peer per bootnode" :
122122 let
123+ enrRelay = initRecord (
124+ 1 ,
125+ pk2,
126+ {" waku2" : @ [1 .byte ], " rs" : @ [0 .byte , 1 .byte , 1 .byte , 0 .byte , 1 .byte ]},
127+ )
128+ .value ()
129+ enrNoCapabilities =
130+ initRecord (1 , pk1, {" rs" : @ [0 .byte , 0 .byte , 1 .byte , 0 .byte , 0 .byte ]}).value ()
123131 predicateNoCapabilities =
124132 shardingPredicate (enrNoCapabilities, @ [enrNoCapabilities]).get ()
125133 predicateNoCapabilitiesWithBoth =
@@ -151,8 +159,10 @@ procSuite "Waku Discovery v5":
151159 predicateRecord.isNone ()
152160
153161 asyncTest " no relay sharding info" :
154- let predicateNoShardingInfo =
155- shardingPredicate (enrNoShardingInfo, @ [enrNoShardingInfo])
162+ let
163+ enrNoShardingInfo = initRecord (1 , pk1, {" waku2" : @ [1 .byte ]}).value ()
164+ predicateNoShardingInfo =
165+ shardingPredicate (enrNoShardingInfo, @ [enrNoShardingInfo])
156166
157167 check:
158168 predicateNoShardingInfo.isNone ()
@@ -166,7 +176,7 @@ procSuite "Waku Discovery v5":
166176 indices: seq [uint64 ] = @ [],
167177 recordFlags: Option [CapabilitiesBitfield ] = none (CapabilitiesBitfield ),
168178 bootstrapRecords: seq [waku_enr.Record ] = @ [],
169- ): (WakuDiscoveryV5 , Record ) =
179+ ): (WakuDiscoveryV5 , Record ) {. raises : [ ValueError , LPError ].} =
170180 let
171181 privKey = generateSecp256k1Key ()
172182 record = newTestEnrRecord (
@@ -188,17 +198,6 @@ procSuite "Waku Discovery v5":
188198
189199 (node, record)
190200
191- let filterForStore: WakuDiscv5Predicate = proc (record: waku_enr.Record ): bool =
192- let typedRecord = record.toTyped ()
193- if typedRecord.isErr ():
194- return false
195-
196- let capabilities = typedRecord.value.waku2
197- if capabilities.isNone ():
198- return false
199-
200- return capabilities.get ().supportsCapability (Capabilities .Store )
201-
202201 asyncTest " find random peers without predicate" :
203202 # Given 3 nodes
204203 let
@@ -234,6 +233,17 @@ procSuite "Waku Discovery v5":
234233 await allFutures (node1.stop (), node2.stop (), node3.stop ())
235234
236235 asyncTest " find random peers with parameter predicate" :
236+ let filterForStore: WakuDiscv5Predicate = proc (record: waku_enr.Record ): bool =
237+ let typedRecord = record.toTyped ()
238+ if typedRecord.isErr ():
239+ return false
240+
241+ let capabilities = typedRecord.value.waku2
242+ if capabilities.isNone ():
243+ return false
244+
245+ return capabilities.get ().supportsCapability (Capabilities .Store )
246+
237247 # Given 4 nodes
238248 let
239249 (node3, record3) = buildNode (
@@ -346,11 +356,6 @@ procSuite "Waku Discovery v5":
346356 await allFutures (node1.stop (), node2.stop (), node3.stop (), node4.stop ())
347357
348358 suite " addBoostrapNode" :
349- let validEnr =
350- " enr:-I-4QG3mX250ArniAs2DLpW-QHOLKSD5x_Ibp8AYcQZbz1HhHFJtl2dNDGcha" &
351- " U5ugLbDKRgtTDZH8NsxXlTXDpYAgzgBgmlkgnY0gnJzjwAVBgABAAIABQAHAAkAC4" &
352- " lzZWNwMjU2azGhA4_KwN0NRRmmfQ-B9B2h2PZjoJvBnaIOi6sR_b2UTQBBhXdha3U" & " yAQ"
353-
354359 asyncTest " address is valid" :
355360 # Given an empty list of enrs
356361 var enrs: seq [Record ] = @ []
@@ -400,3 +405,42 @@ procSuite "Waku Discovery v5":
400405 # Then the enr is not added to the list
401406 check:
402407 enrs.len == 0
408+
409+ suite " waku discv5 initialization" :
410+ asyncTest " Discv5 bootstrap nodes should be added to the peer store" :
411+ var conf = defaultTestWakuNodeConf ()
412+
413+ conf.discv5BootstrapNodes = @ [validEnr]
414+
415+ let waku = Waku .init (conf).valueOr:
416+ raiseAssert error
417+
418+ discard setupDiscoveryV5 (
419+ waku.node.enr, waku.node.peerManager, waku.node.topicSubscriptionQueue,
420+ waku.conf, waku.dynamicBootstrapNodes, waku.rng, waku.key,
421+ )
422+
423+ check:
424+ waku.node.peerManager.wakuPeerStore.peers ().anyIt (
425+ it.enr.isSome () and it.enr.get ().toUri () == validEnr
426+ )
427+
428+ asyncTest " Invalid discv5 bootstrap node ENRs are ignored" :
429+ var conf = defaultTestWakuNodeConf ()
430+
431+ let invalidEnr = " invalid-enr"
432+
433+ conf.discv5BootstrapNodes = @ [invalidEnr]
434+
435+ let waku = Waku .init (conf).valueOr:
436+ raiseAssert error
437+
438+ discard setupDiscoveryV5 (
439+ waku.node.enr, waku.node.peerManager, waku.node.topicSubscriptionQueue,
440+ waku.conf, waku.dynamicBootstrapNodes, waku.rng, waku.key,
441+ )
442+
443+ check:
444+ not waku.node.peerManager.wakuPeerStore.peers ().anyIt (
445+ it.enr.isSome () and it.enr.get ().toUri () == invalidEnr
446+ )
0 commit comments