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
- Fork the repo and create a branch
git checkout -b test/rate-limiter-bucket-invariants
- Implement changes
contracts/predictify-hybrid/tests/rate_limiter_invariants.rs (new)
contracts/predictify-hybrid/src/rate_limiter.rs (only on failure)
- 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
Guidelines
- Use
env.ledger().set_timestamp only inside tests
- Minimum 95% coverage
- Clear documentation and inline comments
- Timeframe: 96 hours
Description
rate_limiter.rsimplements a per-user limiter, but the refill arithmetic is not invariant-tested under fast-forwarded ledger clocks. Add a property test that drivesenv.ledger().setand asserts tokens never exceed bucket capacity.Requirements and Context
tokens <= capacityafter any sequence of acquire/refill operationsSuggested Execution
contracts/predictify-hybrid/tests/rate_limiter_invariants.rs(new)contracts/predictify-hybrid/src/rate_limiter.rs(only on failure)cargo test -p predictify-hybrid rate_limiter_invariants -- --nocaptureExample commit message
Acceptance Criteria
rate_limiter.rsunwrap()addedGuidelines
env.ledger().set_timestamponly inside tests