Skip to content
This repository was archived by the owner on Apr 25, 2026. It is now read-only.

fix: update credits pallet params#1039

Merged
1xstj merged 11 commits into
mainfrom
1xstj/fix-wasm
Jul 31, 2025
Merged

fix: update credits pallet params#1039
1xstj merged 11 commits into
mainfrom
1xstj/fix-wasm

Conversation

@1xstj
Copy link
Copy Markdown
Contributor

@1xstj 1xstj commented Jul 30, 2025

Summary of changes
Changes introduced in this pull request:

Reference issue to close (if applicable)

Closes

@1xstj 1xstj changed the title chore: fix max rate per block fix: update credits pallet params Jul 30, 2025
@1xstj 1xstj mentioned this pull request Jul 30, 2025
@1xstj 1xstj marked this pull request as ready for review July 30, 2025 22:54
@1xstj 1xstj requested review from drewstone and shekohex as code owners July 30, 2025 22:54
@claude
Copy link
Copy Markdown

claude Bot commented Jul 30, 2025

Claude finished @1xstj's task —— View job


PR Review: fix: update credits pallet params

Review Progress:

  • Read and analyze pallets/credits/src/lib.rs changes
  • Read and analyze pallets/credits/src/mock.rs changes
  • Read and analyze pallets/credits/src/tests.rs changes
  • Read and analyze precompiles/credits/src/mock.rs changes
  • Read and analyze precompiles/credits/src/tests.rs changes
  • Read and analyze primitives/src/lib.rs changes
  • Read and analyze runtime/mainnet/src/lib.rs changes
  • Read and analyze runtime/testnet/src/lib.rs changes
  • Provide comprehensive review feedback
  • Check for documentation needs

Summary of Changes

This PR introduces a maximum rate per block validation system for the credits pallet stake tiers. The main changes include:

  • Added MaxRatePerBlock configuration parameter to limit stake tier rates
  • Enhanced set_stake_tiers and set_asset_stake_tiers with rate validation
  • Added MAX_RATE_PER_BLOCK constant (1 TNT per block = 14,400 TNT/day)
  • Comprehensive test coverage including production stake tier validation
  • Updated all runtime configurations and mock setups

Code Quality & Best Practices ✅

Strengths:

  • Consistent API Design: The new MaxRatePerBlock parameter follows existing pallet configuration patterns
  • Comprehensive Validation: Both global and asset-specific stake tiers are validated consistently
  • Error Handling: Proper error types (RateTooHigh) with descriptive names
  • Code Reuse: Common validation logic is applied to both tier-setting functions

Areas for Improvement:

  • Consider extracting the rate validation logic into a helper function to reduce code duplication in pallets/credits/src/lib.rs:418-420 and pallets/credits/src/lib.rs:471-473

Security Analysis 🔒

Strong Security Measures:

  • DoS Prevention: The MaxRatePerBlock limit prevents malicious governance proposals from setting extremely high rates that could cause overflow attacks
  • Overflow Protection: Uses saturated_from() and saturating_mul() to prevent arithmetic overflows
  • Access Control: Both set_stake_tiers and set_asset_stake_tiers require ForceOrigin (root) access
  • Atomic Validation: All tiers in a batch must pass validation or the entire operation fails

Security Validation - Excellent:

  • Rate limits prevent economic attacks through excessive credit emission
  • Input validation prevents malformed tier configurations
  • No direct user input affects rate calculations (rates are governance-controlled)

Performance Considerations ⚡

Efficient Implementation:

  • O(n) Validation: Rate validation scales linearly with the number of tiers
  • Bounded Storage: MaxStakeTiers (20) limits the validation loop size
  • Early Exit: Validation fails fast on the first invalid tier
  • No Runtime Performance Impact: Validation only occurs during governance actions

Performance Assessment - Good:

  • Validation overhead is negligible during tier configuration
  • No impact on normal credit claiming or burning operations
  • Storage access patterns remain efficient

Potential Issues & Bugs 🐛

Minor Issues Found:

  1. Inconsistent Rate Type Conversion (pallets/credits/src/lib.rs:418):

    let max_rate = BalanceOf::<T>::saturated_from(T::MaxRatePerBlock::get());

    Since T::MaxRatePerBlock::get() returns BalanceOf<T>, the saturated_from conversion is unnecessary. Consider direct comparison:

    ensure!(tier.rate_per_block <= T::MaxRatePerBlock::get(), Error::<T>::RateTooHigh);
  2. Missing Edge Case Test: No test verifies behavior when MaxRatePerBlock is set to 0 (which would disable all credit accrual)

  3. Production Tier Values: The production stake tiers in tests use hardcoded values that should be verified against the actual production configuration

Test Coverage Analysis 🧪

Excellent Test Coverage:

  • Boundary Testing: Tests exactly at max rate and one above max rate
  • Mixed Scenarios: Tests valid and invalid rates in the same tier set
  • Asset-Specific Testing: Covers both global and asset-specific tiers
  • Production Validation: Comprehensive production stake tier verification with real TNT values
  • Error Conditions: Tests all new error cases (RateTooHigh)
  • Integration Testing: Tests interact correctly with existing functionality

Test Quality - Outstanding: The test suite at pallets/credits/src/tests.rs:1137-1434 is particularly well-designed with production stake tier validation.

Documentation Assessment 📚

Current Documentation:

  • ⚠️ Missing Documentation: The new MaxRatePerBlock configuration parameter lacks inline documentation beyond the basic "maximum rate per block" comment
  • ⚠️ Migration Guide: No documentation about the breaking change to the Config trait
  • Error Documentation: New error types are properly documented
  • Constant Documentation: MAX_RATE_PER_BLOCK has clear documentation in primitives

Recommendations:

  1. Add detailed documentation for MaxRatePerBlock explaining:

    • The purpose of the rate limit (DoS prevention)
    • The relationship to daily credit emission (1 TNT/block = 14,400 TNT/day)
    • Governance considerations for setting this value
  2. Consider adding a migration guide for runtime upgraders

Substrate-Specific Considerations 🔗

Well-Designed for Substrate:

  • Configuration Pattern: Follows standard Substrate pallet configuration patterns
  • Error Handling: Uses proper DispatchError and pallet-specific error types
  • Weight Considerations: Validation adds minimal weight to tier configuration calls
  • Runtime Integration: Properly integrated into both mainnet and testnet runtimes
  • Storage Efficiency: No additional storage overhead - uses existing configuration pattern

Final Assessment

Overall Rating: Excellent ⭐⭐⭐⭐⭐

This is a high-quality, security-focused enhancement that addresses a real vulnerability (potential DoS through excessive rates) while maintaining backward compatibility and following Substrate best practices. The extensive test coverage, including production environment validation, demonstrates thorough engineering.

Recommended Actions:

  1. Approve: The core implementation is solid and ready for deployment
  2. 📝 Minor Enhancement: Consider the type conversion optimization mentioned above
  3. 📚 Documentation: Add more detailed documentation for the new parameter

No blocking issues found. The PR successfully enhances security without introducing new vulnerabilities or performance regressions.


Copy link
Copy Markdown
Contributor

@shekohex shekohex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@1xstj 1xstj merged commit c58b1a0 into main Jul 31, 2025
7 checks passed
@1xstj 1xstj deleted the 1xstj/fix-wasm branch July 31, 2025 12:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants