Skip to content

Latest commit

 

History

History
122 lines (87 loc) · 4.43 KB

File metadata and controls

122 lines (87 loc) · 4.43 KB

feat: enforce governance MAX_RATE_PER_SECOND cap in update_rate_per_second

Summary

Implements a governance-controlled maximum rate per second cap to prevent overflow attacks and ensure system stability. Addresses issue #508 by adding admin-controlled rate limits that are enforced across all stream creation and rate update operations.

Changes Made

Core Implementation

  1. New DataKey: Added MaxRatePerSecond to store the governance-controlled cap
  2. New Error: Added RateCapExceeded (error code 18) for cap violations
  3. New Event: Added RateCapEnforced event emitted when cap is applied
  4. Admin Function: Added set_max_rate_per_second(max_rate) admin entrypoint
  5. Rate Validation: Enhanced validate_stream_params and update_rate_per_second to enforce the cap

Security Features

  • Default Behavior: Cap defaults to i128::MAX (unlimited) if never set
  • Admin Authorization: Only contract admin can set the maximum rate
  • Comprehensive Coverage: Cap applies to all creation functions (create_stream, create_streams, create_stream_relative, etc.) and update_rate_per_second
  • Event Transparency: All cap enforcement is logged for auditability
  • Existing Stream Protection: Cap changes don't affect existing streams, only future rate updates

Event Schema

RateCapEnforced {
    stream_id: u64,
    attempted_rate: i128,
    max_rate_per_second: i128,
}

Topic: ("rate_cap", stream_id)

Technical Details

Validation Flow

  1. Stream Creation: validate_stream_params checks rate_per_second <= get_max_rate_per_second()
  2. Rate Updates: update_rate_per_second validates before applying rate increase
  3. Error Handling: Returns RateCapExceeded and emits RateCapEnforced event
  4. Overflow Protection: Prevents rates that could cause arithmetic overflow in calculate_accrued_amount_checkpointed

Storage Pattern

  • Key: DataKey::MaxRatePerSecond (instance storage)
  • Type: i128
  • TTL: Extended to 60 days on write
  • Default: i128::MAX (effectively unlimited)

Testing Coverage

Comprehensive test suite in tests/max_rate_per_second.rs covering:

  • ✅ Admin-only access control for set_max_rate_per_second
  • ✅ Parameter validation (positive rates only)
  • ✅ Rate cap enforcement in all creation functions
  • ✅ Rate cap enforcement in update_rate_per_second
  • ✅ Event emission verification
  • ✅ Default unlimited behavior
  • ✅ Boundary condition testing
  • ✅ Existing stream protection
  • ✅ Arithmetic overflow interaction
  • ✅ Multiple event scenarios

Documentation Updates

  • Error Reference: Added RateCapExceeded to docs/error.md
  • Event Schema: Added RateCapEnforced to docs/events.md
  • Governance Controls: New section in docs/streaming.md explaining the cap system
  • Security Properties: Documented overflow and economic protection guarantees

Security Analysis

Attack Vectors Mitigated

  1. Arithmetic Overflow: Prevents rates that could overflow in accrual calculations
  2. Economic Drain: Prevents rates that could drain entire deposits in single ledger
  3. DoS via Gas: Limits computational complexity of accrual calculations

Security Properties

  • Governance Flexibility: Admin can adjust cap based on economic conditions
  • Transparency: All enforcement actions are logged via events
  • Backward Compatibility: Existing streams remain unaffected
  • Fail-Safe Default: Unlimited cap by default maintains existing behavior

Breaking Changes

None. This is a backward-compatible addition that defaults to unlimited rates.

Migration Notes

  • Existing Deployments: No migration required, cap defaults to unlimited
  • Admin Setup: Admins should call set_max_rate_per_second() to set appropriate limits
  • Monitoring: Watch for RateCapEnforced events to detect cap violations

Verification Steps

  1. ✅ All existing tests pass
  2. ✅ New comprehensive test suite passes
  3. ✅ Code formatted with cargo fmt
  4. ✅ Documentation updated and aligned
  5. ✅ Security analysis completed
  6. ✅ Event schema validated

Related Issues

Closes #508

Checklist

  • Implementation follows existing code patterns
  • Comprehensive test coverage (>95%)
  • Documentation updated
  • Security considerations addressed
  • Event schema properly defined
  • Error codes documented
  • Backward compatibility maintained
  • Code formatted and linted