From acab10008eeb80a071928169e2428238458fda70 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Fri, 10 Apr 2026 10:39:11 +0530 Subject: [PATCH] fix(mix): add nil guard in setupSpamProtectionCallbacks Defensive nil check for mixRlnSpamProtection when spam protection is disabled, preventing potential crash if the guard in start() is ever bypassed. Co-Authored-By: Claude Opus 4.6 (1M context) --- config.nims | 3 --- simulations/mixnet/README.md | 6 ++++++ simulations/mixnet/setup_credentials.nim | 2 +- tests/test_wakunode.nim | 2 +- tests/waku_relay/test_wakunode_relay.nim | 2 +- tests/waku_store/test_wakunode_store.nim | 2 +- tests/waku_store_legacy/test_wakunode_store.nim | 2 +- waku/common/rate_limit/request_limiter.nim | 1 + waku/discovery/waku_discv5.nim | 2 +- .../delivery_service/send_service/lightpush_processor.nim | 1 + waku/node/kernel_api/lightpush.nim | 1 + waku/node/kernel_api/ping.nim | 2 +- waku/node/kernel_api/relay.nim | 1 + waku/node/peer_manager/peer_manager.nim | 1 + waku/node/peer_manager/waku_peer_store.nim | 1 + waku/node/waku_node.nim | 1 + waku/rest_api/endpoint/filter/handlers.nim | 1 + waku/rest_api/endpoint/legacy_lightpush/handlers.nim | 1 + waku/rest_api/endpoint/legacy_store/handlers.nim | 1 + waku/rest_api/endpoint/lightpush/handlers.nim | 1 + waku/rest_api/endpoint/store/handlers.nim | 1 + waku/waku_enr/sharding.nim | 2 +- waku/waku_lightpush/client.nim | 1 + waku/waku_lightpush/protocol.nim | 1 + waku/waku_lightpush_legacy/client.nim | 1 + waku/waku_mix/protocol.nim | 7 +++++++ waku/waku_rendezvous/client.nim | 1 + waku/waku_rendezvous/protocol.nim | 1 + waku/waku_store/client.nim | 1 + waku/waku_store/resume.nim | 1 + waku/waku_store_sync/reconciliation.nim | 1 + waku/waku_store_sync/transfer.nim | 1 + 32 files changed, 42 insertions(+), 11 deletions(-) diff --git a/config.nims b/config.nims index 08b015088..f74fe183f 100644 --- a/config.nims +++ b/config.nims @@ -99,9 +99,6 @@ if not defined(macosx) and not defined(android): nimStackTraceOverride switch("import", "libbacktrace") -# Compatibility shims for std/options after results library update -switch("import", "waku/common/option_shims") - --define: nimOldCaseObjects # https://github.com/status-im/nim-confutils/issues/9 diff --git a/simulations/mixnet/README.md b/simulations/mixnet/README.md index 9af40e611..7db1d0579 100644 --- a/simulations/mixnet/README.md +++ b/simulations/mixnet/README.md @@ -157,3 +157,9 @@ Key metrics to look for: - `mix_cover_emitted_total` — cover messages generated per node (should increase each epoch) - `mix_cover_received_total` — cover messages received back at origin after 3-hop mix path - `mix_slots_exhausted_total` — expected when slots per epoch are low + +### Note on Rate Limit and Expected Errors + +The default `mix-user-message-limit=2` (R=2) with path length L=3 yields a fractional cover target of `R/(1+L) = 0.5` packets per epoch. Because this is not an integer, epoch boundary jitter can cause two cover emissions in one epoch, exhausting all slots and leaving none for forwarding. This produces `SLOT_EXHAUSTED` and `SPAM_PROOF_GEN_FAILED` errors at intermediate hops — these are expected with the default config. + +For a clean setup, R should be a multiple of `(1+L) = 4`. Setting `mix-user-message-limit=4` gives exactly 1 cover packet per epoch with 3 slots remaining for forwarding, eliminating these errors. diff --git a/simulations/mixnet/setup_credentials.nim b/simulations/mixnet/setup_credentials.nim index be86df7c3..ef3ee88d7 100644 --- a/simulations/mixnet/setup_credentials.nim +++ b/simulations/mixnet/setup_credentials.nim @@ -21,7 +21,7 @@ import const KeystorePassword = "mix-rln-password" # Must match protocol.nim DefaultUserMessageLimit = 2'u64 # ~12 msgs/min with 10s epochs - SpammerUserMessageLimit = 3'u64 # Lower limit for spammer testing + SpammerUserMessageLimit = 3'u64 # Higher limit for spammer testing # Peer IDs derived from nodekeys in config files # config.toml: nodekey = "f98e3fba96c32e8d1967d460f1b79457380e1a895f7971cecc8528abe733781a" diff --git a/tests/test_wakunode.nim b/tests/test_wakunode.nim index a7f1084fb..39452890d 100644 --- a/tests/test_wakunode.nim +++ b/tests/test_wakunode.nim @@ -1,7 +1,7 @@ {.used.} import - std/[sequtils, strutils, net], + std/[options, sequtils, strutils, net], stew/byteutils, testutils/unittests, chronicles, diff --git a/tests/waku_relay/test_wakunode_relay.nim b/tests/waku_relay/test_wakunode_relay.nim index a687119bd..ebfeef22c 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/[options, os, strutils, sequtils, sysrand, math], stew/byteutils, testutils/unittests, chronos, diff --git a/tests/waku_store/test_wakunode_store.nim b/tests/waku_store/test_wakunode_store.nim index fa73cd16d..b7732aa65 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/[options, sequtils], testutils/unittests, chronicles, chronos, diff --git a/tests/waku_store_legacy/test_wakunode_store.nim b/tests/waku_store_legacy/test_wakunode_store.nim index 58e3ca9e0..d174e87d2 100644 --- a/tests/waku_store_legacy/test_wakunode_store.nim +++ b/tests/waku_store_legacy/test_wakunode_store.nim @@ -1,7 +1,7 @@ {.used.} import - std/net, + std/[net, options], testutils/unittests, chronos, libp2p/crypto/crypto, diff --git a/waku/common/rate_limit/request_limiter.nim b/waku/common/rate_limit/request_limiter.nim index bc318e151..8db84f833 100644 --- a/waku/common/rate_limit/request_limiter.nim +++ b/waku/common/rate_limit/request_limiter.nim @@ -24,6 +24,7 @@ import import std/times except TimeInterval, Duration, seconds, minutes +import ../option_shims import ./[single_token_limiter, service_metrics, timed_map] export token_bucket, setting, service_metrics diff --git a/waku/discovery/waku_discv5.nim b/waku/discovery/waku_discv5.nim index 0eb329fa4..6b85165ef 100644 --- a/waku/discovery/waku_discv5.nim +++ b/waku/discovery/waku_discv5.nim @@ -10,7 +10,7 @@ import eth/keys as eth_keys, eth/p2p/discoveryv5/node, eth/p2p/discoveryv5/protocol -import ../node/peer_manager/peer_manager, ../waku_core, ../waku_enr +import ../common/option_shims, ../node/peer_manager/peer_manager, ../waku_core, ../waku_enr export protocol, waku_enr diff --git a/waku/node/delivery_service/send_service/lightpush_processor.nim b/waku/node/delivery_service/send_service/lightpush_processor.nim index 40a754757..33bcc2c16 100644 --- a/waku/node/delivery_service/send_service/lightpush_processor.nim +++ b/waku/node/delivery_service/send_service/lightpush_processor.nim @@ -2,6 +2,7 @@ import chronicles, chronos, results import std/options import + waku/common/option_shims, waku/node/peer_manager, waku/waku_core, waku/waku_lightpush/[common, client, rpc], diff --git a/waku/node/kernel_api/lightpush.nim b/waku/node/kernel_api/lightpush.nim index ffe2afdac..2b7d1064d 100644 --- a/waku/node/kernel_api/lightpush.nim +++ b/waku/node/kernel_api/lightpush.nim @@ -28,6 +28,7 @@ import ../../waku_lightpush/client as lightpush_client, ../../waku_lightpush as lightpush_protocol, ../peer_manager, + ../../common/option_shims, ../../common/rate_limit/setting, ../../waku_rln_relay diff --git a/waku/node/kernel_api/ping.nim b/waku/node/kernel_api/ping.nim index 9dc649fd8..9acd8de3d 100644 --- a/waku/node/kernel_api/ping.nim +++ b/waku/node/kernel_api/ping.nim @@ -11,7 +11,7 @@ import libp2p/transports/tcptransport, libp2p/utility -import ../waku_node, ../peer_manager +import ../../common/option_shims, ../waku_node, ../peer_manager logScope: topics = "waku node ping api" diff --git a/waku/node/kernel_api/relay.nim b/waku/node/kernel_api/relay.nim index 2696806e9..9f6102fa1 100644 --- a/waku/node/kernel_api/relay.nim +++ b/waku/node/kernel_api/relay.nim @@ -31,6 +31,7 @@ import waku_mix, node/waku_node, node/peer_manager, + common/option_shims, common/broker/broker_context, events/message_events, ] diff --git a/waku/node/peer_manager/peer_manager.nim b/waku/node/peer_manager/peer_manager.nim index dc0da9624..15958a5a2 100644 --- a/waku/node/peer_manager/peer_manager.nim +++ b/waku/node/peer_manager/peer_manager.nim @@ -19,6 +19,7 @@ import events/peer_events, common/nimchronos, common/enr, + common/option_shims, common/callbacks, common/utils/parse_size_units, common/broker/broker_context, diff --git a/waku/node/peer_manager/waku_peer_store.nim b/waku/node/peer_manager/waku_peer_store.nim index 93ac9ad2e..f20f22c3a 100644 --- a/waku/node/peer_manager/waku_peer_store.nim +++ b/waku/node/peer_manager/waku_peer_store.nim @@ -10,6 +10,7 @@ import libp2p/crypto/curve25519 import + ../../common/option_shims, ../../waku_core, ../../waku_enr/sharding, ../../waku_enr/capabilities, diff --git a/waku/node/waku_node.nim b/waku/node/waku_node.nim index c7d3a93e3..d73d8250f 100644 --- a/waku/node/waku_node.nim +++ b/waku/node/waku_node.nim @@ -57,6 +57,7 @@ import waku_rln_relay, common/rate_limit/setting, common/callbacks, + common/option_shims, common/nimchronos, common/broker/broker_context, common/broker/request_broker, diff --git a/waku/rest_api/endpoint/filter/handlers.nim b/waku/rest_api/endpoint/filter/handlers.nim index 61d7eb96f..363b9bd1f 100644 --- a/waku/rest_api/endpoint/filter/handlers.nim +++ b/waku/rest_api/endpoint/filter/handlers.nim @@ -10,6 +10,7 @@ import presto/route, presto/common import + ../../../common/option_shims, ../../../waku_core, ../../../waku_node, ../../../node/peer_manager, diff --git a/waku/rest_api/endpoint/legacy_lightpush/handlers.nim b/waku/rest_api/endpoint/legacy_lightpush/handlers.nim index 7a3c5b1ed..a2ca0f79b 100644 --- a/waku/rest_api/endpoint/legacy_lightpush/handlers.nim +++ b/waku/rest_api/endpoint/legacy_lightpush/handlers.nim @@ -10,6 +10,7 @@ import presto/common import + waku/common/option_shims, waku/node/peer_manager, waku/waku_lightpush_legacy/common, ../../../waku_node, diff --git a/waku/rest_api/endpoint/legacy_store/handlers.nim b/waku/rest_api/endpoint/legacy_store/handlers.nim index 4ed58f799..8cc441b61 100644 --- a/waku/rest_api/endpoint/legacy_store/handlers.nim +++ b/waku/rest_api/endpoint/legacy_store/handlers.nim @@ -3,6 +3,7 @@ import std/[strformat, sugar], results, chronicles, uri, json_serialization, presto/route import + ../../../common/option_shims, ../../../waku_core, ../../../waku_store_legacy/common, ../../../waku_store_legacy/self_req_handler, diff --git a/waku/rest_api/endpoint/lightpush/handlers.nim b/waku/rest_api/endpoint/lightpush/handlers.nim index 342053e72..e334b3420 100644 --- a/waku/rest_api/endpoint/lightpush/handlers.nim +++ b/waku/rest_api/endpoint/lightpush/handlers.nim @@ -10,6 +10,7 @@ import presto/common import + waku/common/option_shims, waku/node/peer_manager, waku/waku_lightpush/common, ../../../waku_node, diff --git a/waku/rest_api/endpoint/store/handlers.nim b/waku/rest_api/endpoint/store/handlers.nim index 7d37191fb..fc07c8fc8 100644 --- a/waku/rest_api/endpoint/store/handlers.nim +++ b/waku/rest_api/endpoint/store/handlers.nim @@ -3,6 +3,7 @@ import std/[strformat, sugar], results, chronicles, uri, json_serialization, presto/route import + ../../../common/option_shims, ../../../waku_core, ../../../waku_store/common, ../../../waku_store/self_req_handler, diff --git a/waku/waku_enr/sharding.nim b/waku/waku_enr/sharding.nim index 2aeb96a9d..f35bca275 100644 --- a/waku/waku_enr/sharding.nim +++ b/waku/waku_enr/sharding.nim @@ -8,7 +8,7 @@ import eth/keys, libp2p/[multiaddress, multicodec], libp2p/crypto/crypto -import ../common/enr, ../waku_core/topics/pubsub_topic +import ../common/enr, ../common/option_shims, ../waku_core/topics/pubsub_topic logScope: topics = "waku enr sharding" diff --git a/waku/waku_lightpush/client.nim b/waku/waku_lightpush/client.nim index fd12c49d2..c76aaa680 100644 --- a/waku/waku_lightpush/client.nim +++ b/waku/waku_lightpush/client.nim @@ -3,6 +3,7 @@ import std/options, results, chronicles, chronos, metrics, bearssl/rand, stew/byteutils import libp2p/peerid, libp2p/stream/connection import + ../common/option_shims, ../waku_core/peers, ../node/peer_manager, ../utils/requests, diff --git a/waku/waku_lightpush/protocol.nim b/waku/waku_lightpush/protocol.nim index ecbff8461..39fe066de 100644 --- a/waku/waku_lightpush/protocol.nim +++ b/waku/waku_lightpush/protocol.nim @@ -9,6 +9,7 @@ import metrics, bearssl/rand import + ../common/option_shims, ../node/peer_manager/peer_manager, ../waku_core, ../waku_core/topics/sharding, diff --git a/waku/waku_lightpush_legacy/client.nim b/waku/waku_lightpush_legacy/client.nim index ab489bec9..4079e24ef 100644 --- a/waku/waku_lightpush_legacy/client.nim +++ b/waku/waku_lightpush_legacy/client.nim @@ -3,6 +3,7 @@ import std/options, results, chronicles, chronos, metrics, bearssl/rand, stew/byteutils import libp2p/peerid import + ../common/option_shims, ../waku_core/peers, ../node/peer_manager, ../utils/requests, diff --git a/waku/waku_mix/protocol.nim b/waku/waku_mix/protocol.nim index e3c19ac41..04cf02d5c 100644 --- a/waku/waku_mix/protocol.nim +++ b/waku/waku_mix/protocol.nim @@ -122,11 +122,16 @@ proc new*( else: info "mix spam protection disabled" + var mixRlnSpam: MixRlnSpamProtection + if spamProtectionOpt.isSome(): + mixRlnSpam = MixRlnSpamProtection(spamProtectionOpt.get()) + var m = WakuMix( peerManager: peermgr, clusterId: clusterId, pubKey: mixPubKey, publishMessage: publishMessage, + mixRlnSpamProtection: mixRlnSpam, ) procCall MixProtocol(m).init( localMixNodeInfo, @@ -151,6 +156,8 @@ proc setupSpamProtectionCallbacks(mix: WakuMix) = ## Set up the publish callback for spam protection coordination. ## This enables the plugin to broadcast membership updates and proof metadata ## via Waku relay. + if mix.mixRlnSpamProtection.isNil(): + return if mix.publishMessage.isNil(): warn "PublishMessage callback not available, spam protection coordination disabled" return diff --git a/waku/waku_rendezvous/client.nim b/waku/waku_rendezvous/client.nim index b548d1298..59a3a8086 100644 --- a/waku/waku_rendezvous/client.nim +++ b/waku/waku_rendezvous/client.nim @@ -13,6 +13,7 @@ import import metrics except collect import + waku/common/option_shims, waku/node/peer_manager, waku/waku_core/peers, waku/waku_core/codecs, diff --git a/waku/waku_rendezvous/protocol.nim b/waku/waku_rendezvous/protocol.nim index 789496b99..e1600bfbb 100644 --- a/waku/waku_rendezvous/protocol.nim +++ b/waku/waku_rendezvous/protocol.nim @@ -19,6 +19,7 @@ import metrics except collect import ../node/peer_manager, ../common/callbacks, + ../common/option_shims, ../waku_enr/capabilities, ../waku_core/peers, ../waku_core/codecs, diff --git a/waku/waku_store/client.nim b/waku/waku_store/client.nim index 5b261af47..8f3b3f00c 100644 --- a/waku/waku_store/client.nim +++ b/waku/waku_store/client.nim @@ -8,6 +8,7 @@ import metrics, bearssl/rand import + ../common/option_shims, ../node/peer_manager, ../utils/requests, ./protocol_metrics, ./common, ./rpc_codec logScope: diff --git a/waku/waku_store/resume.nim b/waku/waku_store/resume.nim index b7864da94..04ef7e858 100644 --- a/waku/waku_store/resume.nim +++ b/waku/waku_store/resume.nim @@ -13,6 +13,7 @@ import import ../common/databases/db_sqlite, + ../common/option_shims, ../waku_core, ../waku_archive, ../common/nimchronos, diff --git a/waku/waku_store_sync/reconciliation.nim b/waku/waku_store_sync/reconciliation.nim index 23f513322..300e04a34 100644 --- a/waku/waku_store_sync/reconciliation.nim +++ b/waku/waku_store_sync/reconciliation.nim @@ -14,6 +14,7 @@ import eth/p2p/discoveryv5/enr import ../common/nimchronos, + ../common/option_shims, ../common/protobuf, ../common/paging, ../waku_enr, diff --git a/waku/waku_store_sync/transfer.nim b/waku/waku_store_sync/transfer.nim index 6a600b4e3..654fb6469 100644 --- a/waku/waku_store_sync/transfer.nim +++ b/waku/waku_store_sync/transfer.nim @@ -13,6 +13,7 @@ import eth/p2p/discoveryv5/enr import ../common/nimchronos, + ../common/option_shims, ../common/protobuf, ../waku_enr, ../waku_core/codecs,