-
Couldn't load subscription status.
- Fork 114
feat: tablecalc modules #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8c128b1
feat: tablecalc modules
eigenmikem cafb9d1
chore: fmt
eigenmikem 0bb3394
Merge branch 'dev' into mike/tablecalc-modules
eigenmikem 634d37b
refactor: weight cap per asset
eigenmikem f179957
chore: fmt
eigenmikem fa8e0f6
refactor: move file
eigenmikem f65c755
Merge branch 'dev' into mike/tablecalc-modules
eigenmikem f54d0b0
chore: revert bad edit
eigenmikem 7d32666
chore: move files
eigenmikem 3fddd10
Merge branch 'dev' into mike/tablecalc-modules
eigenmikem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.27; | ||
|
|
||
| /** | ||
| * @title WeightCapUtils | ||
| * @notice Utility library for applying weight caps to operator weights | ||
| */ | ||
| library WeightCapUtils { | ||
| /** | ||
| * @notice Apply single weight cap to operator weights (backwards compatibility) | ||
| * @param operators Array of operator addresses | ||
| * @param weights 2D array of weights for each operator | ||
| * @param maxWeight Maximum allowed total weight per operator (0 = no cap) | ||
| * @return cappedOperators Array of operators after applying caps | ||
| * @return cappedWeights Array of weights after applying caps | ||
| */ | ||
| function applyWeightCap( | ||
| address[] memory operators, | ||
| uint256[][] memory weights, | ||
| uint256 maxWeight | ||
eigenmikem marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) internal pure returns (address[] memory cappedOperators, uint256[][] memory cappedWeights) { | ||
| uint256[] memory maxWeights = new uint256[](1); | ||
| maxWeights[0] = maxWeight; | ||
| return applyWeightCaps(operators, weights, maxWeights); | ||
| } | ||
| /** | ||
| * @notice Apply weight caps to operator weights | ||
| * @param operators Array of operator addresses | ||
| * @param weights 2D array of weights for each operator | ||
| * @param maxWeights Array of maximum allowed weights per stake type (0 = no cap) | ||
| * Index 0 is treated as total weight cap if array length is 1 | ||
| * @return cappedOperators Array of operators after filtering | ||
| * @return cappedWeights Array of weights after applying caps | ||
| * @dev For single cap: truncates to cap (first weight = cap, rest = 0) and filters zero-weight operators. For multi-cap: applies per-stake-type caps. | ||
| */ | ||
|
|
||
| function applyWeightCaps( | ||
| address[] memory operators, | ||
| uint256[][] memory weights, | ||
| uint256[] memory maxWeights | ||
| ) internal pure returns (address[] memory cappedOperators, uint256[][] memory cappedWeights) { | ||
| require( | ||
| operators.length == weights.length, "WeightCapUtils: operators/weights length mismatch" | ||
| ); | ||
|
|
||
| if (maxWeights.length == 0 || operators.length == 0) { | ||
| return (operators, weights); | ||
| } | ||
|
|
||
| if (maxWeights.length == 1 && maxWeights[0] == 0) { | ||
| return (operators, weights); | ||
| } | ||
|
|
||
| // Count operators with non-zero weights for filtering | ||
| uint256 validOperatorCount = 0; | ||
| bool[] memory isValid = new bool[](operators.length); | ||
|
|
||
| for (uint256 i = 0; i < operators.length; i++) { | ||
| uint256 totalWeight = 0; | ||
| for (uint256 j = 0; j < weights[i].length; j++) { | ||
| totalWeight += weights[i][j]; | ||
| } | ||
|
|
||
| if (totalWeight > 0) { | ||
| isValid[i] = true; | ||
| validOperatorCount++; | ||
| } | ||
| } | ||
|
|
||
| // Initialize result arrays with only valid operators | ||
| cappedOperators = new address[](validOperatorCount); | ||
| cappedWeights = new uint256[][](validOperatorCount); | ||
|
|
||
| uint256 resultIndex = 0; | ||
| for (uint256 i = 0; i < operators.length; i++) { | ||
| if (!isValid[i]) continue; | ||
|
|
||
| cappedOperators[resultIndex] = operators[i]; | ||
| cappedWeights[resultIndex] = new uint256[](weights[i].length); | ||
|
|
||
| if (maxWeights.length == 1 && maxWeights[0] > 0) { | ||
| // Legacy mode: single total weight cap with truncation behavior | ||
| uint256 totalWeight = 0; | ||
| for (uint256 j = 0; j < weights[i].length; j++) { | ||
| totalWeight += weights[i][j]; | ||
| } | ||
|
|
||
| if (totalWeight <= maxWeights[0]) { | ||
| // No cap needed | ||
| for (uint256 j = 0; j < weights[i].length; j++) { | ||
| cappedWeights[resultIndex][j] = weights[i][j]; | ||
| } | ||
| } else { | ||
| // Cap with truncation: set first weight to cap, zero out rest | ||
| cappedWeights[resultIndex][0] = maxWeights[0]; | ||
| for (uint256 j = 1; j < weights[i].length; j++) { | ||
| cappedWeights[resultIndex][j] = 0; | ||
| } | ||
| } | ||
| } else { | ||
| // Per-stake-type caps | ||
| for (uint256 j = 0; j < weights[i].length; j++) { | ||
| if (j < maxWeights.length && maxWeights[j] > 0) { | ||
| cappedWeights[resultIndex][j] = | ||
| weights[i][j] > maxWeights[j] ? maxWeights[j] : weights[i][j]; | ||
| } else { | ||
| cappedWeights[resultIndex][j] = weights[i][j]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resultIndex++; | ||
| } | ||
| } | ||
| } | ||
167 changes: 167 additions & 0 deletions
167
src/middlewareV2/tableCalculator/BN254WeightedTableCalculator.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.27; | ||
|
|
||
| import {OperatorSet} from "eigenlayer-contracts/src/contracts/libraries/OperatorSetLib.sol"; | ||
| import {IAllocationManager} from | ||
| "eigenlayer-contracts/src/contracts/interfaces/IAllocationManager.sol"; | ||
| import {IStrategy} from "eigenlayer-contracts/src/contracts/interfaces/IStrategy.sol"; | ||
| import {IKeyRegistrar} from "eigenlayer-contracts/src/contracts/interfaces/IKeyRegistrar.sol"; | ||
| import {IPermissionController} from | ||
| "eigenlayer-contracts/src/contracts/interfaces/IPermissionController.sol"; | ||
| import {PermissionControllerMixin} from | ||
| "eigenlayer-contracts/src/contracts/mixins/PermissionControllerMixin.sol"; | ||
|
|
||
| import "./BN254TableCalculatorBase.sol"; | ||
|
|
||
| /** | ||
| * @title BN254WeightedTableCalculator | ||
| * @notice Implementation that calculates BN254 operator tables using custom multipliers for different strategies | ||
| * @dev This contract allows AVSs to set custom multipliers for each strategy instead of weighting all strategies equally. | ||
| */ | ||
| contract BN254WeightedTableCalculator is BN254TableCalculatorBase, PermissionControllerMixin { | ||
eigenmikem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Constants | ||
| /// @notice Default multiplier in basis points (10000 = 1x) | ||
| uint256 public constant DEFAULT_STRATEGY_MULTIPLIER = 10000; | ||
|
|
||
| // Immutables | ||
| /// @notice AllocationManager contract for managing operator allocations | ||
| IAllocationManager public immutable allocationManager; | ||
| /// @notice The default lookahead blocks for the slashable stake lookup | ||
| uint256 public immutable LOOKAHEAD_BLOCKS; | ||
|
|
||
| // Storage | ||
| /// @notice Mapping from operatorSet hash to strategy to multiplier (in basis points, 10000 = 1x) | ||
| mapping(bytes32 => mapping(IStrategy => uint256)) public strategyMultipliers; | ||
|
|
||
| // Events | ||
| /// @notice Emitted when strategy multipliers are updated for an operator set | ||
| event StrategyMultipliersUpdated( | ||
| OperatorSet indexed operatorSet, IStrategy[] strategies, uint256[] multipliers | ||
| ); | ||
|
|
||
| // Errors | ||
| error ArrayLengthMismatch(); | ||
|
|
||
| constructor( | ||
| IKeyRegistrar _keyRegistrar, | ||
| IAllocationManager _allocationManager, | ||
| IPermissionController _permissionController, | ||
| uint256 _LOOKAHEAD_BLOCKS | ||
| ) BN254TableCalculatorBase(_keyRegistrar) PermissionControllerMixin(_permissionController) { | ||
| allocationManager = _allocationManager; | ||
| LOOKAHEAD_BLOCKS = _LOOKAHEAD_BLOCKS; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Set strategy multipliers for a given operator set | ||
| * @param operatorSet The operator set to set multipliers for | ||
| * @param strategies Array of strategies to set multipliers for | ||
| * @param multipliers Array of multipliers in basis points (10000 = 1x) | ||
| * @dev Only the AVS can set multipliers for their operator sets | ||
| */ | ||
| function setStrategyMultipliers( | ||
| OperatorSet calldata operatorSet, | ||
| IStrategy[] calldata strategies, | ||
| uint256[] calldata multipliers | ||
| ) external checkCanCall(operatorSet.avs) { | ||
| // Validate input arrays | ||
| require(strategies.length == multipliers.length, ArrayLengthMismatch()); | ||
|
|
||
| bytes32 operatorSetKey = operatorSet.key(); | ||
|
|
||
| // Set multipliers for each strategy | ||
| for (uint256 i = 0; i < strategies.length; i++) { | ||
| strategyMultipliers[operatorSetKey][strategies[i]] = multipliers[i]; | ||
| strategyMultipliersSet[operatorSetKey][strategies[i]] = true; | ||
| } | ||
|
|
||
| emit StrategyMultipliersUpdated(operatorSet, strategies, multipliers); | ||
| } | ||
|
|
||
| // Storage to track which strategies have been explicitly set | ||
| mapping(bytes32 => mapping(IStrategy => bool)) public strategyMultipliersSet; | ||
|
|
||
| /** | ||
| * @notice Get the strategy multiplier for a given operator set and strategy | ||
| * @param operatorSet The operator set | ||
| * @param strategy The strategy | ||
| * @return multiplier The multiplier in basis points (returns 10000 if not set) | ||
| */ | ||
| function getStrategyMultiplier( | ||
| OperatorSet calldata operatorSet, | ||
| IStrategy strategy | ||
| ) external view returns (uint256 multiplier) { | ||
| bytes32 operatorSetKey = operatorSet.key(); | ||
| if (strategyMultipliersSet[operatorSetKey][strategy]) { | ||
| multiplier = strategyMultipliers[operatorSetKey][strategy]; | ||
| } else { | ||
| multiplier = DEFAULT_STRATEGY_MULTIPLIER; // Default 1x multiplier | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @notice Get the operator weights for a given operatorSet based on weighted slashable stake. | ||
| * @param operatorSet The operatorSet to get the weights for | ||
| * @return operators The addresses of the operators in the operatorSet | ||
| * @return weights The weights for each operator in the operatorSet, this is a 2D array where the first index is the operator | ||
| * and the second index is the type of weight. In this case its of length 1 and returns the weighted slashable stake for the operatorSet. | ||
| */ | ||
| function _getOperatorWeights( | ||
| OperatorSet calldata operatorSet | ||
| ) internal view override returns (address[] memory operators, uint256[][] memory weights) { | ||
| // Get all operators & strategies in the operatorSet | ||
| address[] memory registeredOperators = allocationManager.getMembers(operatorSet); | ||
| IStrategy[] memory strategies = allocationManager.getStrategiesInOperatorSet(operatorSet); | ||
|
|
||
| // Get the minimum slashable stake for each operator | ||
| uint256[][] memory minSlashableStake = allocationManager.getMinimumSlashableStake({ | ||
| operatorSet: operatorSet, | ||
| operators: registeredOperators, | ||
| strategies: strategies, | ||
| futureBlock: uint32(block.number + LOOKAHEAD_BLOCKS) | ||
| }); | ||
|
|
||
| bytes32 operatorSetKey = operatorSet.key(); | ||
|
|
||
| operators = new address[](registeredOperators.length); | ||
| weights = new uint256[][](registeredOperators.length); | ||
| uint256 operatorCount = 0; | ||
| for (uint256 i = 0; i < registeredOperators.length; ++i) { | ||
| // For the given operator, loop through the strategies and apply multipliers before summing | ||
| uint256 totalWeight; | ||
| for (uint256 stratIndex = 0; stratIndex < strategies.length; ++stratIndex) { | ||
| uint256 stakeAmount = minSlashableStake[i][stratIndex]; | ||
|
|
||
| // Get the multiplier for this strategy (default to DEFAULT_STRATEGY_MULTIPLIER if not set) | ||
| uint256 multiplier; | ||
| if (strategyMultipliersSet[operatorSetKey][strategies[stratIndex]]) { | ||
| multiplier = strategyMultipliers[operatorSetKey][strategies[stratIndex]]; | ||
| } else { | ||
| multiplier = DEFAULT_STRATEGY_MULTIPLIER; // Default 1x multiplier | ||
| } | ||
|
|
||
| // Apply multiplier (divide by DEFAULT_STRATEGY_MULTIPLIER to convert from basis points) | ||
| totalWeight += (stakeAmount * multiplier) / DEFAULT_STRATEGY_MULTIPLIER; | ||
| } | ||
|
|
||
| // If the operator has nonzero weighted stake, add them to the operators array | ||
| if (totalWeight > 0) { | ||
| // Initialize operator weights array of length 1 just for weighted slashable stake | ||
| weights[operatorCount] = new uint256[](1); | ||
| weights[operatorCount][0] = totalWeight; | ||
|
|
||
| // Add the operator to the operators array | ||
| operators[operatorCount] = registeredOperators[i]; | ||
| operatorCount++; | ||
| } | ||
| } | ||
|
|
||
| // Resize arrays to be the size of the number of operators with nonzero weighted stake | ||
| assembly { | ||
| mstore(operators, operatorCount) | ||
| mstore(weights, operatorCount) | ||
| } | ||
|
|
||
| return (operators, weights); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.