-
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 5 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++; | ||
| } | ||
| } | ||
| } | ||
163 changes: 163 additions & 0 deletions
163
src/middlewareV2/tableCalculator/BN254TableCalculatorWithCaps.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,163 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.27; | ||
|
|
||
| import {OperatorSet} from "eigenlayer-contracts/src/contracts/libraries/OperatorSetLib.sol"; | ||
eigenmikem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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"; | ||
| import {WeightCapUtils} from "../../libraries/WeightCapUtils.sol"; | ||
|
|
||
| /** | ||
| * @title BN254TableCalculatorWithCaps | ||
| * @notice BN254 table calculator with configurable weight caps | ||
| * @dev Extends the basic table calculator to cap operator weights | ||
| */ | ||
| contract BN254TableCalculatorWithCaps is BN254TableCalculatorBase, PermissionControllerMixin { | ||
| // 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 key to weight caps per stake type (0 = no cap) | ||
| /// @dev Index 0 represents the total weight cap for backwards compatibility | ||
| mapping(bytes32 => uint256[]) public weightCaps; | ||
|
|
||
| // Events | ||
| /// @notice Emitted when weight caps are set for an operator set | ||
| event WeightCapsSet(OperatorSet indexed operatorSet, uint256[] maxWeights); | ||
|
|
||
| constructor( | ||
| IKeyRegistrar _keyRegistrar, | ||
| IAllocationManager _allocationManager, | ||
| IPermissionController _permissionController, | ||
| uint256 _LOOKAHEAD_BLOCKS | ||
| ) BN254TableCalculatorBase(_keyRegistrar) PermissionControllerMixin(_permissionController) { | ||
| allocationManager = _allocationManager; | ||
| LOOKAHEAD_BLOCKS = _LOOKAHEAD_BLOCKS; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Set weight caps for a given operator set | ||
| * @param operatorSet The operator set to set caps for | ||
| * @param maxWeights Array of maximum allowed weights per stake type (0 = no cap) | ||
| * Index 0 is the total weight cap for backwards compatibility | ||
| * @dev Only the AVS can set caps for their operator sets | ||
| */ | ||
| function setWeightCaps( | ||
| OperatorSet calldata operatorSet, | ||
| uint256[] calldata maxWeights | ||
| ) external checkCanCall(operatorSet.avs) { | ||
| require(maxWeights.length > 0, "BN254TableCalculatorWithCaps: empty weight caps array"); | ||
|
|
||
| bytes32 operatorSetKey = operatorSet.key(); | ||
| weightCaps[operatorSetKey] = maxWeights; | ||
|
|
||
| emit WeightCapsSet(operatorSet, maxWeights); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Set the total weight cap for a given operator set (backwards compatibility) | ||
| * @param operatorSet The operator set to set the cap for | ||
| * @param maxWeight Maximum allowed total weight per operator (0 = no cap) | ||
| * @dev Only the AVS can set caps for their operator sets | ||
| */ | ||
| function setWeightCap( | ||
| OperatorSet calldata operatorSet, | ||
eigenmikem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| uint256 maxWeight | ||
| ) external checkCanCall(operatorSet.avs) { | ||
| bytes32 operatorSetKey = operatorSet.key(); | ||
| uint256[] memory caps = new uint256[](1); | ||
| caps[0] = maxWeight; | ||
| weightCaps[operatorSetKey] = caps; | ||
|
|
||
| emit WeightCapsSet(operatorSet, caps); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Get weight caps for a given operator set | ||
| * @param operatorSet The operator set to get caps for | ||
| * @return maxWeights Array of maximum weight caps per stake type (0 = no cap) | ||
| */ | ||
| function getWeightCaps( | ||
| OperatorSet calldata operatorSet | ||
| ) external view returns (uint256[] memory maxWeights) { | ||
| bytes32 operatorSetKey = operatorSet.key(); | ||
| return weightCaps[operatorSetKey]; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Get the total weight cap for a given operator set (backwards compatibility) | ||
| * @param operatorSet The operator set to get the cap for | ||
| * @return maxWeight The maximum weight cap (0 = no cap) | ||
| */ | ||
| function getWeightCap( | ||
| OperatorSet calldata operatorSet | ||
| ) external view returns (uint256 maxWeight) { | ||
| bytes32 operatorSetKey = operatorSet.key(); | ||
| uint256[] storage caps = weightCaps[operatorSetKey]; | ||
| return caps.length > 0 ? caps[0] : 0; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Get operator weights with caps applied | ||
| * @param operatorSet The operator set to calculate weights for | ||
| * @return operators Array of operator addresses | ||
| * @return weights Array of weights per operator | ||
| */ | ||
| 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) | ||
| }); | ||
|
|
||
| operators = new address[](registeredOperators.length); | ||
| weights = new uint256[][](registeredOperators.length); | ||
| uint256 operatorCount = 0; | ||
| for (uint256 i = 0; i < registeredOperators.length; ++i) { | ||
| uint256 totalWeight; | ||
| for (uint256 stratIndex = 0; stratIndex < strategies.length; ++stratIndex) { | ||
| totalWeight += minSlashableStake[i][stratIndex]; | ||
| } | ||
|
|
||
| if (totalWeight > 0) { | ||
| weights[operatorCount] = new uint256[](1); | ||
| weights[operatorCount][0] = totalWeight; | ||
| operators[operatorCount] = registeredOperators[i]; | ||
| operatorCount++; | ||
| } | ||
| } | ||
|
|
||
| assembly { | ||
| mstore(operators, operatorCount) | ||
| mstore(weights, operatorCount) | ||
| } | ||
|
|
||
| // Apply weight caps if configured | ||
| bytes32 operatorSetKey = operatorSet.key(); | ||
| uint256[] storage maxWeights = weightCaps[operatorSetKey]; | ||
|
|
||
| if (maxWeights.length > 0) { | ||
| (operators, weights) = WeightCapUtils.applyWeightCaps(operators, weights, maxWeights); | ||
| } | ||
|
|
||
| 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.