logos-delivery/tests/common/test_requestratelimiter.nim

99 lines
4.3 KiB
Nim
Raw Normal View History

# Chronos Test Suite
# (c) Copyright 2022-Present
# Status Research & Development GmbH
#
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
{.used.}
import testutils/unittests
fix(tests): libp2p v2.0.0 API migrations across test suite Three small classes of fix across 12 test files (uncovered by a local `nim c` sweep of tests/all_tests_common.nim + tests/all_tests_waku.nim): 1. `rng` no longer auto-calls when used as an argument: libp2p v2.0.0 exports its own `rng` symbol (SwitchBuilder field), so Nim's resolver sees the testlib `common.rng` template ambiguously and stops auto- calling it. Add explicit `()`: - tests/test_helpers.nim - tests/waku_filter_v2/waku_filter_utils.nim - tests/waku_lightpush/lightpush_utils.nim - tests/waku_lightpush_legacy/lightpush_utils.nim - tests/node/test_wakunode_filter.nim - tests/waku_store/store_utils.nim 2. `PeerId.random()` zero-arg form removed in v2.0.0; new signature takes `rng: Rng`. Add `newRng()` + `libp2p/crypto/crypto` import: - tests/common/test_requestratelimiter.nim - tests/common/test_ratelimit_setting.nim 3. `some()` ambiguity vs `Opt.some` — files that don't directly `import std/options` get `Opt.some` template only, breaking calls intended for `Option[T]`. Add `std/options` to imports: - tests/waku_store/test_wakunode_store.nim - tests/waku_relay/test_wakunode_relay.nim 4. Two more API removals: - tests/testlib/wakunode.nim — `builders.MaxConnections` was removed; use `DefaultMaxConnections` from `libp2p/connmanager`. - tests/waku_rln_relay/utils_onchain.nim — `keys.PrivateKey.random (rng[])` `rng` ambiguous between testlib/common and eth/keys exports; qualify as `common.rng()[]`. There are still more test-compile errors past this point — this is incremental progress, not a complete v2.0.0 test-suite migration.
2026-06-04 18:55:26 +05:30
import chronos, libp2p/stream/connection, libp2p/crypto/crypto
import std/options
import ../../waku/common/rate_limit/request_limiter
import ../../waku/common/rate_limit/timed_map
let proto = "ProtocolDescriptor"
fix(tests): libp2p v2.0.0 API migrations across test suite Three small classes of fix across 12 test files (uncovered by a local `nim c` sweep of tests/all_tests_common.nim + tests/all_tests_waku.nim): 1. `rng` no longer auto-calls when used as an argument: libp2p v2.0.0 exports its own `rng` symbol (SwitchBuilder field), so Nim's resolver sees the testlib `common.rng` template ambiguously and stops auto- calling it. Add explicit `()`: - tests/test_helpers.nim - tests/waku_filter_v2/waku_filter_utils.nim - tests/waku_lightpush/lightpush_utils.nim - tests/waku_lightpush_legacy/lightpush_utils.nim - tests/node/test_wakunode_filter.nim - tests/waku_store/store_utils.nim 2. `PeerId.random()` zero-arg form removed in v2.0.0; new signature takes `rng: Rng`. Add `newRng()` + `libp2p/crypto/crypto` import: - tests/common/test_requestratelimiter.nim - tests/common/test_ratelimit_setting.nim 3. `some()` ambiguity vs `Opt.some` — files that don't directly `import std/options` get `Opt.some` template only, breaking calls intended for `Option[T]`. Add `std/options` to imports: - tests/waku_store/test_wakunode_store.nim - tests/waku_relay/test_wakunode_relay.nim 4. Two more API removals: - tests/testlib/wakunode.nim — `builders.MaxConnections` was removed; use `DefaultMaxConnections` from `libp2p/connmanager`. - tests/waku_rln_relay/utils_onchain.nim — `keys.PrivateKey.random (rng[])` `rng` ambiguous between testlib/common and eth/keys exports; qualify as `common.rng()[]`. There are still more test-compile errors past this point — this is incremental progress, not a complete v2.0.0 test-suite migration.
2026-06-04 18:55:26 +05:30
let conn1 = Connection(peerId: PeerId.random(newRng()).tryGet())
let conn2 = Connection(peerId: PeerId.random(newRng()).tryGet())
let conn3 = Connection(peerId: PeerId.random(newRng()).tryGet())
suite "RequestRateLimiter":
test "RequestRateLimiter Allow up to main bucket":
# keep limits low for easier calculation of ratios
let rateLimit: RateLimitSetting = (4, 2.minutes)
var limiter = newRequestRateLimiter(some(rateLimit))
# per peer tokens will be 6 / 4min
# as ratio is 2 in this case but max tokens are main tokens*ratio . 0.75
# notice meanwhile we have 8 global tokens over 2 period (4 mins) in sum
# See: waku/common/rate_limit/request_limiter.nim #func calcPeriodRatio
let now = Moment.now()
# with first use we register the peer also and start its timer
check limiter.checkUsage(proto, conn2, now) == true
for i in 0 ..< 3:
check limiter.checkUsage(proto, conn1, now) == true
check limiter.checkUsage(proto, conn2, now + 3.minutes) == true
for i in 0 ..< 3:
check limiter.checkUsage(proto, conn1, now + 3.minutes) == true
# conn1 reached the 75% of the main bucket over 2 periods of time
check limiter.checkUsage(proto, conn1, now + 3.minutes) == false
# conn2 has not used its tokens while we have 1 more tokens left in the main bucket
check limiter.checkUsage(proto, conn2, now + 3.minutes) == true
test "RequestRateLimiter Restrict overusing peer":
# keep limits low for easier calculation of ratios
let rateLimit: RateLimitSetting = (10, 2.minutes)
var limiter = newRequestRateLimiter(some(rateLimit))
# per peer tokens will be 15 / 4min
# as ratio is 2 in this case but max tokens are main tokens*ratio . 0.75
# notice meanwhile we have 20 tokens over 2 period (4 mins) in sum
# See: waku/common/rate_limit/request_limiter.nim #func calcPeriodRatio
let now = Moment.now()
# with first use we register the peer also and start its timer
for i in 0 ..< 10:
check limiter.checkUsage(proto, conn1, now) == true
# run out of main tokens but still used one more token from the peer's bucket
check limiter.checkUsage(proto, conn1, now) == false
for i in 0 ..< 4:
check limiter.checkUsage(proto, conn1, now + 3.minutes) == true
# conn1 reached the 75% of the main bucket over 2 periods of time
check limiter.checkUsage(proto, conn1, now + 3.minutes) == false
check limiter.checkUsage(proto, conn2, now + 3.minutes) == true
check limiter.checkUsage(proto, conn2, now + 3.minutes) == true
check limiter.checkUsage(proto, conn3, now + 3.minutes) == true
check limiter.checkUsage(proto, conn2, now + 3.minutes) == true
check limiter.checkUsage(proto, conn3, now + 3.minutes) == true
# conn1 gets replenished as the ratio was 2 giving twice as long replenish period than the main bucket
# see waku/common/rate_limit/request_limiter.nim #func calcPeriodRatio and calcPeerTokenSetting
check limiter.checkUsage(proto, conn1, now + 4.minutes) == true
# requests of other peers can also go
check limiter.checkUsage(proto, conn2, now + 4100.milliseconds) == true
check limiter.checkUsage(proto, conn3, now + 5.minutes) == true
test "RequestRateLimiter lowest possible volume":
# keep limits low for easier calculation of ratios
let rateLimit: RateLimitSetting = (1, 1.seconds)
var limiter = newRequestRateLimiter(some(rateLimit))
let now = Moment.now()
# with first use we register the peer also and start its timer
check limiter.checkUsage(proto, conn1, now + 500.milliseconds) == true
# run out of main tokens but still used one more token from the peer's bucket
check limiter.checkUsage(proto, conn1, now + 800.milliseconds) == false
check limiter.checkUsage(proto, conn1, now + 1499.milliseconds) == false
check limiter.checkUsage(proto, conn1, now + 1501.milliseconds) == true