Skip to content

Add RateLimiter Token-Bucket invariant tests with deterministic ledger clock #620

Description

@greatest0fallt1me

Description

rate_limiter.rs implements a per-user limiter, but the refill arithmetic is not invariant-tested under fast-forwarded ledger clocks. Add a property test that drives env.ledger().set and asserts tokens never exceed bucket capacity.

Requirements and Context

  • Fast-forward ledger sequence and timestamp deterministically
  • Assert tokens <= capacity after any sequence of acquire/refill operations
  • Detect potential underflow on refill
  • Must be secure, tested, and documented
  • Should be efficient and easy to review

Suggested Execution

  1. Fork the repo and create a branch
    git checkout -b test/rate-limiter-bucket-invariants
  2. Implement changes
    • contracts/predictify-hybrid/tests/rate_limiter_invariants.rs (new)
    • contracts/predictify-hybrid/src/rate_limiter.rs (only on failure)
  3. Test and commit
    • cargo test -p predictify-hybrid rate_limiter_invariants -- --nocapture
    • Cover edge cases: time goes backward (rejected), very large dt, zero refill rate
    • Include test output and notes in the PR

Example commit message

test: add token-bucket invariants for RateLimiter

Acceptance Criteria

  • Invariant holds under 1000+ random schedules
  • Backward-time scenarios rejected
  • Documented in rate_limiter.rs
  • No unwrap() added

Guidelines

  • Use env.ledger().set_timestamp only inside tests
  • Minimum 95% coverage
  • Clear documentation and inline comments
  • Timeframe: 96 hours

Metadata

Metadata

Labels

GRANTFOX OSSGrantFox open-source campaign taskMAYBE REWARDEDMay be rewarded under the GrantFox campaignOFFICIAL CAMPAIGNOfficial GrantFox campaign issueStellar WaveIssues in the Stellar wave programrustRust implementationsmart-contractSoroban smart-contract worksorobanSoroban SDK / StellartestingTests and coverage

Type

Fields

No fields configured for Task.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions