Skip to content

Conversation

@sfffaaa
Copy link
Contributor

@sfffaaa sfffaaa commented Aug 11, 2025

No description provided.

jaypan added 2 commits August 11, 2025 12:22
This implementation allows querying delegator information from the EVM layer:

Features:
- Returns delegator's delegations to various collators with amounts
- Delegations are sorted by stake amount (descending order)
- Returns empty vector for non-existent delegators
- Zero address parameter reserved for future 'get all delegators' functionality

Interface:
- Function: getDelegatorState(bytes32 delegator)
- Selector: 0x72a09ed8
- Returns: CollatorDelegatorState[] memory

Test Coverage:
- Basic delegator state queries
- Multi-collator delegation scenarios
- Stake amount sorting verification
- Edge cases (zero address, non-existent delegators)
…tion

Features added:
- Paging support for getDelegatorState function with offset/limit parameters
- Zero address (0x0) functionality to retrieve all delegators' states
- Proper handling of edge cases (U256::MAX values, out-of-bounds offsets)
- Comprehensive test coverage with concrete data verification

Technical improvements:
- Made DelegatorState public in pallet for iteration support
- Fixed U256::MAX conversion issues in paging logic
- Enhanced Solidity interface with proper documentation
- Added robust test suite with 15 tests covering all scenarios

Test coverage:
- Basic delegator state queries with exact data verification
- Multi-delegator paging scenarios with predictable results
- Boundary testing (MAX values, empty results, non-existent delegators)
- Edge case handling and error conditions
- Gas accounting verification
@sfffaaa sfffaaa requested a review from Copilot August 11, 2025 14:59
@peaq-coolify
Copy link

peaq-coolify bot commented Aug 11, 2025

The preview deployment is ready. 🟢

Open Preview | Open Build Logs

Last updated at: 2025-08-17 07:01:59 CET

Apply cargo fmt to fix code formatting in the parachain-staking
precompile implementation and tests.

This comment was marked as outdated.

jaypan added 3 commits August 11, 2025 17:23
…lity

Major improvements:
- Extract AccountConverter helper to eliminate code duplication across methods
- Create GasCalculator utility with centralized constants and calculations
- Split complex get_delegator_state_paged method into focused helper functions
- Optimize get_all_delegators_paged with lazy evaluation using iterator chaining
- Consolidate collator list methods to remove duplicated iterator patterns

Performance gains:
- Memory: O(n) -> O(k) for bulk delegator queries (only process requested pages)
- CPU: Avoid processing unwanted data with skip().take() iterator chaining
- Gas accounting: Accurate costs based on actual processed entries

Code quality improvements:
- Reduced cyclomatic complexity from 8+ to 2-3 branches per method
- Eliminated ~40% code duplication between similar methods
- Centralized magic gas numbers (3789, 2580, 7200) with clear documentation
- Maintained full backward compatibility - all 15 tests pass

All existing functionality preserved with cleaner, more maintainable implementation.
Update gas cost assumptions to be more realistic:
- COLLATOR_POOL_READ: 7200 -> 3072 gas units (64 vs 150 collators)
- Updated documentation to reflect realistic 64 collator pool size
- Proportional calculation: 7200 * (64/150) = 3072

This provides more accurate gas accounting for actual network conditions
while maintaining the same calculation methodology.
Breaking changes and improvements:
- Remove single-parameter getDelegatorState(bytes32) - all calls now require offset/limit
- Add strict validation: forbid limit=0 and limit>512 for all query types
- Implement consistent MAX_DELEGATORS_PER_QUERY=512 limit across bulk and single queries
- Return clear error messages instead of silently capping or allowing dangerous operations

Validation rules:
- limit=0: "Invalid limit: must be greater than 0"
- limit>512: "Invalid limit: maximum allowed is 512"
- offset too large: "Invalid offset: value too large"

This forces explicit pagination and prevents resource exhaustion while providing
predictable behavior and clear error feedback to users.

Updated selector: getDelegatorState now only supports 0x657c7960 (3-parameter version)

All 16 tests pass with comprehensive validation coverage.
@sfffaaa sfffaaa requested a review from Copilot August 11, 2025 15:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new precompile function for querying delegator states in the parachain staking module. The feature provides comprehensive delegation information with pagination support and sorting capabilities.

  • Adds getDelegatorState function with pagination support to retrieve delegator delegation information
  • Implements efficient gas cost calculation and validation for bulk operations
  • Includes comprehensive test coverage for various scenarios including edge cases and pagination

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
precompiles/parachain-staking/src/lib.rs Core implementation of the getDelegatorState function with helper utilities and gas calculation
precompiles/parachain-staking/src/tests.rs Comprehensive test suite covering functionality, edge cases, and pagination scenarios
precompiles/parachain-staking/ParachainStaking.sol Solidity interface definitions for the new delegator state structures and functions
pallets/parachain-staking/src/lib.rs Changes DelegatorState storage visibility from private to public

jaypan and others added 14 commits August 12, 2025 08:06
Remove unnecessary borrows from format\! calls in error messages.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Update comments to clearly distinguish between:
- Delegator order when querying all: NOT sorted (unpredictable storage order)
- Individual delegator's delegations: ARE sorted by stake descending

This prevents confusion about what is and isn't sorted in the API responses.
Add required imports for no_std environment:
- extern crate alloc for format! macro
- vec macro from sp_std for vec![] usage
Adjust line breaks in sorting behavior documentation for consistency.
Always apply limit when paging, even when offset is 0.
Previously limit was only applied when offset > 0, causing tests to fail.
- Remove single-parameter getDelegatorState from Solidity interface
- Remove redundant limit_usize == usize::MAX checks (impossible with MAX_DELEGATORS_PER_QUERY=512)
- Remove unnecessary actual_limit variable
- Simplify code while maintaining all functional validations
- Accept Ethereum address as input parameter (address delegator)
- Convert Ethereum address to substrate account internally via AddressMapping
- Keep bytes32 output in response structs (shows substrate account)
- Update function selector from 0x657c7960 to 0xbeae0df4
- Update all test calls to use Address type for inputs
- Add H160 import and address conversion helpers
…g precompile

- Adds new view function to convert Ethereum addresses to substrate account hashes
- Provides transparency into how AddressMapping works internally
- Useful for debugging address mapping between Ethereum and Substrate
- Function selector: 0xb76f87bf
- Includes comprehensive tests and Solidity interface documentation
- Remove trailing whitespace in documentation comments
- Clean up formatting in test code
…g precompile

- Clarify that getDelegatorState accepts Ethereum addresses as input but returns substrate account hashes
- Add detailed INPUT/OUTPUT ADDRESS FORMAT section to both Rust and Solidity documentation
- Specify that input parameters use Ethereum addresses (20 bytes) for user convenience
- Explain that output structs contain substrate account hashes (32 bytes) for internal consistency
- Makes the address conversion behavior transparent to developers
@sfffaaa sfffaaa requested a review from talhadaar August 20, 2025 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants