|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import "../interfaces/IECDSATableCalculator.sol"; |
| 5 | +import "../interfaces/IKeyRegistrar.sol"; |
| 6 | +import "../libraries/Merkle.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @title ECDSATableCalculatorBase |
| 10 | + * @notice Abstract contract that provides base functionality for calculating ECDSA operator tables |
| 11 | + * @dev This contract contains all the core logic for operator table calculations, |
| 12 | + * with weight calculation left to be implemented by derived contracts |
| 13 | + */ |
| 14 | +abstract contract ECDSATableCalculatorBase is IECDSATableCalculator { |
| 15 | + using Merkle for bytes32[]; |
| 16 | + |
| 17 | + // Immutables |
| 18 | + /// @notice KeyRegistrar contract for managing operator keys |
| 19 | + IKeyRegistrar public immutable keyRegistrar; |
| 20 | + |
| 21 | + constructor( |
| 22 | + IKeyRegistrar _keyRegistrar |
| 23 | + ) { |
| 24 | + keyRegistrar = _keyRegistrar; |
| 25 | + } |
| 26 | + |
| 27 | + /// @inheritdoc IECDSATableCalculator |
| 28 | + function calculateOperatorTable( |
| 29 | + OperatorSet calldata operatorSet |
| 30 | + ) external view virtual returns (ECDSAOperatorInfo[] memory operatorInfos) { |
| 31 | + return _calculateOperatorTable(operatorSet); |
| 32 | + } |
| 33 | + |
| 34 | + /// @inheritdoc IOperatorTableCalculator |
| 35 | + function calculateOperatorTableBytes( |
| 36 | + OperatorSet calldata operatorSet |
| 37 | + ) external view virtual returns (bytes memory operatorTableBytes) { |
| 38 | + return abi.encode(_calculateOperatorTable(operatorSet)); |
| 39 | + } |
| 40 | + |
| 41 | + /// @inheritdoc IOperatorTableCalculator |
| 42 | + function getOperatorWeights( |
| 43 | + OperatorSet calldata operatorSet |
| 44 | + ) external view virtual returns (address[] memory operators, uint256[][] memory weights) { |
| 45 | + return _getOperatorWeights(operatorSet); |
| 46 | + } |
| 47 | + |
| 48 | + /// @inheritdoc IOperatorTableCalculator |
| 49 | + function getOperatorWeight( |
| 50 | + OperatorSet calldata operatorSet, |
| 51 | + address operator |
| 52 | + ) external view virtual returns (uint256 weight) { |
| 53 | + (address[] memory operators, uint256[][] memory weights) = _getOperatorWeights(operatorSet); |
| 54 | + |
| 55 | + // Find the index of the operator in the operators array |
| 56 | + for (uint256 i = 0; i < operators.length; i++) { |
| 57 | + if (operators[i] == operator) { |
| 58 | + return weights[i][0]; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @notice Abstract function to get the operator weights for a given operatorSet |
| 67 | + * @param operatorSet The operatorSet to get the weights for |
| 68 | + * @return operators The addresses of the operators in the operatorSet |
| 69 | + * @return weights The weights for each operator in the operatorSet, this is a 2D array where the first index is the operator |
| 70 | + * and the second index is the type of weight |
| 71 | + * @dev Must be implemented by derived contracts to define specific weight calculation logic |
| 72 | + */ |
| 73 | + function _getOperatorWeights( |
| 74 | + OperatorSet calldata operatorSet |
| 75 | + ) internal view virtual returns (address[] memory operators, uint256[][] memory weights); |
| 76 | + |
| 77 | + /** |
| 78 | + * @notice Calculates the operator table for a given operatorSet |
| 79 | + * @param operatorSet The operatorSet to calculate the operator table for |
| 80 | + * @return operatorInfos The operator table for the given operatorSet |
| 81 | + * @dev This function: |
| 82 | + * 1. Gets operator weights from the weight calculator |
| 83 | + * 2. Creates ECDSAOperatorInfo structs for each operator with registered ECDSA keys |
| 84 | + */ |
| 85 | + function _calculateOperatorTable( |
| 86 | + OperatorSet calldata operatorSet |
| 87 | + ) internal view returns (ECDSAOperatorInfo[] memory operatorInfos) { |
| 88 | + // Get the weights for all operators in the operatorSet |
| 89 | + (address[] memory operators, uint256[][] memory weights) = _getOperatorWeights(operatorSet); |
| 90 | + |
| 91 | + // If there are no weights, return an empty array |
| 92 | + if (weights.length == 0) { |
| 93 | + return new ECDSAOperatorInfo[](0); |
| 94 | + } |
| 95 | + |
| 96 | + // Create the operator infos array with maximum possible size |
| 97 | + operatorInfos = new ECDSAOperatorInfo[](operators.length); |
| 98 | + uint256 operatorCount = 0; |
| 99 | + |
| 100 | + for (uint256 i = 0; i < operators.length; i++) { |
| 101 | + // Skip if the operator has not registered their ECDSA key |
| 102 | + if (!keyRegistrar.isRegistered(operatorSet, operators[i])) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + // Get the ECDSA address (public key) for the operator |
| 107 | + address ecdsaAddress = keyRegistrar.getECDSAAddress(operatorSet, operators[i]); |
| 108 | + |
| 109 | + // Create the ECDSAOperatorInfo struct |
| 110 | + operatorInfos[operatorCount] = ECDSAOperatorInfo({pubkey: ecdsaAddress, weights: weights[i]}); |
| 111 | + |
| 112 | + operatorCount++; |
| 113 | + } |
| 114 | + |
| 115 | + // If no operators have registered keys, return empty array |
| 116 | + if (operatorCount == 0) { |
| 117 | + return new ECDSAOperatorInfo[](0); |
| 118 | + } |
| 119 | + |
| 120 | + // Resize the array to the actual number of operators with registered keys |
| 121 | + assembly { |
| 122 | + mstore(operatorInfos, operatorCount) |
| 123 | + } |
| 124 | + |
| 125 | + return operatorInfos; |
| 126 | + } |
| 127 | +} |
0 commit comments