Skip to content

Commit 7d1c6ab

Browse files
authored
chore: do not mount lightpush without relay (fixes #2808) (#3540)
* chore: do not mount lightpush without relay (fixes #2808) - Change mountLightPush signature to return Result[void, string] - Return error when relay is not mounted - Update all call sites to handle Result return type - Add test verifying mounting fails without relay - Only advertise lightpush capability when relay is enabled * chore: don't mount legacy lightpush without relay
1 parent 868d431 commit 7d1c6ab

File tree

9 files changed

+88
-44
lines changed

9 files changed

+88
-44
lines changed

apps/chat2/chat2.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} =
480480
if conf.lightpushnode != "":
481481
let peerInfo = parsePeerInfo(conf.lightpushnode)
482482
if peerInfo.isOk():
483-
await mountLegacyLightPush(node)
483+
(await node.mountLegacyLightPush()).isOkOr:
484+
error "failed to mount legacy lightpush", error = error
485+
quit(QuitFailure)
484486
node.mountLegacyLightPushClient()
485487
node.peerManager.addServicePeer(peerInfo.value, WakuLightpushCodec)
486488
else:

tests/node/test_wakunode_legacy_lightpush.nim

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import
1313
node/peer_manager,
1414
node/waku_node,
1515
node/kernel_api,
16+
node/kernel_api/lightpush,
1617
waku_lightpush_legacy,
1718
waku_lightpush_legacy/common,
1819
waku_lightpush_legacy/protocol_metrics,
@@ -56,7 +57,7 @@ suite "Waku Legacy Lightpush - End To End":
5657
(await server.mountRelay()).isOkOr:
5758
assert false, "Failed to mount relay"
5859

59-
await server.mountLegacyLightpush() # without rln-relay
60+
check (await server.mountLegacyLightpush()).isOk() # without rln-relay
6061
client.mountLegacyLightpushClient()
6162

6263
serverRemotePeerInfo = server.peerInfo.toRemotePeerInfo()
@@ -147,7 +148,7 @@ suite "RLN Proofs as a Lightpush Service":
147148
(await server.mountRelay()).isOkOr:
148149
assert false, "Failed to mount relay"
149150
await server.mountRlnRelay(wakuRlnConfig)
150-
await server.mountLegacyLightPush()
151+
check (await server.mountLegacyLightPush()).isOk()
151152
client.mountLegacyLightPushClient()
152153

153154
let manager1 = cast[OnchainGroupManager](server.wakuRlnRelay.groupManager)
@@ -213,7 +214,7 @@ suite "Waku Legacy Lightpush message delivery":
213214
assert false, "Failed to mount relay"
214215
(await bridgeNode.mountRelay()).isOkOr:
215216
assert false, "Failed to mount relay"
216-
await bridgeNode.mountLegacyLightPush()
217+
check (await bridgeNode.mountLegacyLightPush()).isOk()
217218
lightNode.mountLegacyLightPushClient()
218219

219220
discard await lightNode.peerManager.dialPeer(
@@ -249,3 +250,19 @@ suite "Waku Legacy Lightpush message delivery":
249250

250251
## Cleanup
251252
await allFutures(lightNode.stop(), bridgeNode.stop(), destNode.stop())
253+
254+
suite "Waku Legacy Lightpush mounting behavior":
255+
asyncTest "fails to mount when relay is not mounted":
256+
## Given a node without Relay mounted
257+
let
258+
key = generateSecp256k1Key()
259+
node = newTestWakuNode(key, parseIpAddress("0.0.0.0"), Port(0))
260+
261+
# Do not mount Relay on purpose
262+
check node.wakuRelay.isNil()
263+
264+
## Then mounting Legacy Lightpush must fail
265+
let res = await node.mountLegacyLightPush()
266+
check:
267+
res.isErr()
268+
res.error == MountWithoutRelayError

tests/node/test_wakunode_lightpush.nim

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import
1313
node/peer_manager,
1414
node/waku_node,
1515
node/kernel_api,
16+
node/kernel_api/lightpush,
1617
waku_lightpush,
1718
waku_rln_relay,
1819
],
@@ -55,7 +56,7 @@ suite "Waku Lightpush - End To End":
5556

5657
(await server.mountRelay()).isOkOr:
5758
assert false, "Failed to mount relay"
58-
await server.mountLightpush() # without rln-relay
59+
check (await server.mountLightpush()).isOk() # without rln-relay
5960
client.mountLightpushClient()
6061

6162
serverRemotePeerInfo = server.peerInfo.toRemotePeerInfo()
@@ -147,7 +148,7 @@ suite "RLN Proofs as a Lightpush Service":
147148
(await server.mountRelay()).isOkOr:
148149
assert false, "Failed to mount relay"
149150
await server.mountRlnRelay(wakuRlnConfig)
150-
await server.mountLightPush()
151+
check (await server.mountLightPush()).isOk()
151152
client.mountLightPushClient()
152153

153154
let manager1 = cast[OnchainGroupManager](server.wakuRlnRelay.groupManager)
@@ -213,7 +214,7 @@ suite "Waku Lightpush message delivery":
213214
assert false, "Failed to mount relay"
214215
(await bridgeNode.mountRelay()).isOkOr:
215216
assert false, "Failed to mount relay"
216-
await bridgeNode.mountLightPush()
217+
check (await bridgeNode.mountLightPush()).isOk()
217218
lightNode.mountLightPushClient()
218219

219220
discard await lightNode.peerManager.dialPeer(
@@ -251,3 +252,19 @@ suite "Waku Lightpush message delivery":
251252

252253
## Cleanup
253254
await allFutures(lightNode.stop(), bridgeNode.stop(), destNode.stop())
255+
256+
suite "Waku Lightpush mounting behavior":
257+
asyncTest "fails to mount when relay is not mounted":
258+
## Given a node without Relay mounted
259+
let
260+
key = generateSecp256k1Key()
261+
node = newTestWakuNode(key, parseIpAddress("0.0.0.0"), Port(0))
262+
263+
# Do not mount Relay on purpose
264+
check node.wakuRelay.isNil()
265+
266+
## Then mounting Lightpush must fail
267+
let res = await node.mountLightPush()
268+
check:
269+
res.isErr()
270+
res.error == MountWithoutRelayError

tests/node/test_wakunode_sharding.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ suite "Sharding":
282282
asyncTest "lightpush":
283283
# Given a connected server and client subscribed to the same pubsub topic
284284
client.mountLegacyLightPushClient()
285-
await server.mountLightpush()
285+
check (await server.mountLightpush()).isOk()
286286

287287
let
288288
topic = "/waku/2/rs/0/1"
@@ -405,7 +405,7 @@ suite "Sharding":
405405
asyncTest "lightpush (automatic sharding filtering)":
406406
# Given a connected server and client using the same content topic (with two different formats)
407407
client.mountLegacyLightPushClient()
408-
await server.mountLightpush()
408+
check (await server.mountLightpush()).isOk()
409409

410410
let
411411
contentTopicShort = "/toychat/2/huilong/proto"
@@ -563,7 +563,7 @@ suite "Sharding":
563563
asyncTest "lightpush - exclusion (automatic sharding filtering)":
564564
# Given a connected server and client using different content topics
565565
client.mountLegacyLightPushClient()
566-
await server.mountLightpush()
566+
check (await server.mountLightpush()).isOk()
567567

568568
let
569569
contentTopic1 = "/toychat/2/huilong/proto"
@@ -874,7 +874,7 @@ suite "Sharding":
874874
asyncTest "Waku LightPush Sharding (Static Sharding)":
875875
# Given a connected server and client using two different pubsub topics
876876
client.mountLegacyLightPushClient()
877-
await server.mountLightpush()
877+
check (await server.mountLightpush()).isOk()
878878

879879
# Given a connected server and client subscribed to multiple pubsub topics
880880
let

tests/wakunode_rest/test_rest_lightpush.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ proc init(
6161
assert false, "Failed to mount relay: " & $error
6262
(await testSetup.serviceNode.mountRelay()).isOkOr:
6363
assert false, "Failed to mount relay: " & $error
64-
await testSetup.serviceNode.mountLightPush(rateLimit)
64+
check (await testSetup.serviceNode.mountLightPush(rateLimit)).isOk()
6565
testSetup.pushNode.mountLightPushClient()
6666

6767
testSetup.serviceNode.peerManager.addServicePeer(

tests/wakunode_rest/test_rest_lightpush_legacy.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ proc init(
6161
assert false, "Failed to mount relay"
6262
(await testSetup.serviceNode.mountRelay()).isOkOr:
6363
assert false, "Failed to mount relay"
64-
await testSetup.serviceNode.mountLegacyLightPush(rateLimit)
64+
check (await testSetup.serviceNode.mountLegacyLightPush(rateLimit)).isOk()
6565
testSetup.pushNode.mountLegacyLightPushClient()
6666

6767
testSetup.serviceNode.peerManager.addServicePeer(

waku/factory/conf_builder/waku_conf_builder.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ proc build*(
606606
let relayShardedPeerManagement = builder.relayShardedPeerManagement.get(false)
607607

608608
let wakuFlags = CapabilitiesBitfield.init(
609-
lightpush = lightPush,
609+
lightpush = lightPush and relay,
610610
filter = filterServiceConf.isSome,
611611
store = storeServiceConf.isSome,
612612
relay = relay,

waku/factory/node_factory.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,11 @@ proc setupProtocols(
368368
# NOTE Must be mounted after relay
369369
if conf.lightPush:
370370
try:
371-
await mountLightPush(node, node.rateLimitSettings.getSetting(LIGHTPUSH))
372-
await mountLegacyLightPush(node, node.rateLimitSettings.getSetting(LIGHTPUSH))
371+
(await mountLightPush(node, node.rateLimitSettings.getSetting(LIGHTPUSH))).isOkOr:
372+
return err("failed to mount waku lightpush protocol: " & $error)
373+
374+
(await mountLegacyLightPush(node, node.rateLimitSettings.getSetting(LIGHTPUSH))).isOkOr:
375+
return err("failed to mount waku legacy lightpush protocol: " & $error)
373376
except CatchableError:
374377
return err("failed to mount waku lightpush protocol: " & getCurrentExceptionMsg())
375378

waku/node/kernel_api/lightpush.nim

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,27 @@ import
3434
logScope:
3535
topics = "waku node lightpush api"
3636

37+
const MountWithoutRelayError* = "cannot mount lightpush because relay is not mounted"
38+
3739
## Waku lightpush
3840
proc mountLegacyLightPush*(
3941
node: WakuNode, rateLimit: RateLimitSetting = DefaultGlobalNonRelayRateLimit
40-
) {.async.} =
42+
): Future[Result[void, string]] {.async.} =
4143
info "mounting legacy light push"
4244

43-
let pushHandler =
44-
if node.wakuRelay.isNil:
45-
info "mounting legacy lightpush without relay (nil)"
46-
legacy_lightpush_protocol.getNilPushHandler()
45+
if node.wakuRelay.isNil():
46+
return err(MountWithoutRelayError)
47+
48+
info "mounting legacy lightpush with relay"
49+
let rlnPeer =
50+
if node.wakuRlnRelay.isNil():
51+
info "mounting legacy lightpush without rln-relay"
52+
none(WakuRLNRelay)
4753
else:
48-
info "mounting legacy lightpush with relay"
49-
let rlnPeer =
50-
if isNil(node.wakuRlnRelay):
51-
info "mounting legacy lightpush without rln-relay"
52-
none(WakuRLNRelay)
53-
else:
54-
info "mounting legacy lightpush with rln-relay"
55-
some(node.wakuRlnRelay)
56-
legacy_lightpush_protocol.getRelayPushHandler(node.wakuRelay, rlnPeer)
54+
info "mounting legacy lightpush with rln-relay"
55+
some(node.wakuRlnRelay)
56+
let pushHandler =
57+
legacy_lightpush_protocol.getRelayPushHandler(node.wakuRelay, rlnPeer)
5758

5859
node.wakuLegacyLightPush =
5960
WakuLegacyLightPush.new(node.peerManager, node.rng, pushHandler, some(rateLimit))
@@ -64,6 +65,9 @@ proc mountLegacyLightPush*(
6465

6566
node.switch.mount(node.wakuLegacyLightPush, protocolMatcher(WakuLegacyLightPushCodec))
6667

68+
info "legacy lightpush mounted successfully"
69+
return ok()
70+
6771
proc mountLegacyLightPushClient*(node: WakuNode) =
6872
info "mounting legacy light push client"
6973

@@ -146,23 +150,21 @@ proc legacyLightpushPublish*(
146150

147151
proc mountLightPush*(
148152
node: WakuNode, rateLimit: RateLimitSetting = DefaultGlobalNonRelayRateLimit
149-
) {.async.} =
153+
): Future[Result[void, string]] {.async.} =
150154
info "mounting light push"
151155

152-
let pushHandler =
153-
if node.wakuRelay.isNil():
154-
info "mounting lightpush v2 without relay (nil)"
155-
lightpush_protocol.getNilPushHandler()
156+
if node.wakuRelay.isNil():
157+
return err(MountWithoutRelayError)
158+
159+
info "mounting lightpush with relay"
160+
let rlnPeer =
161+
if node.wakuRlnRelay.isNil():
162+
info "mounting lightpush without rln-relay"
163+
none(WakuRLNRelay)
156164
else:
157-
info "mounting lightpush with relay"
158-
let rlnPeer =
159-
if isNil(node.wakuRlnRelay):
160-
info "mounting lightpush without rln-relay"
161-
none(WakuRLNRelay)
162-
else:
163-
info "mounting lightpush with rln-relay"
164-
some(node.wakuRlnRelay)
165-
lightpush_protocol.getRelayPushHandler(node.wakuRelay, rlnPeer)
165+
info "mounting lightpush with rln-relay"
166+
some(node.wakuRlnRelay)
167+
let pushHandler = lightpush_protocol.getRelayPushHandler(node.wakuRelay, rlnPeer)
166168

167169
node.wakuLightPush = WakuLightPush.new(
168170
node.peerManager, node.rng, pushHandler, node.wakuAutoSharding, some(rateLimit)
@@ -174,6 +176,9 @@ proc mountLightPush*(
174176

175177
node.switch.mount(node.wakuLightPush, protocolMatcher(WakuLightPushCodec))
176178

179+
info "lightpush mounted successfully"
180+
return ok()
181+
177182
proc mountLightPushClient*(node: WakuNode) =
178183
info "mounting light push client"
179184

0 commit comments

Comments
 (0)