-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: decouple AVS<>Operator mapping from DelegationManager (#403)
* refactor: initial draft * fix: revert require chages * fix: small nits
- Loading branch information
Showing
8 changed files
with
393 additions
and
281 deletions.
There are no files selected for viewing
This file contains 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,174 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity =0.8.12; | ||
|
||
import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol"; | ||
import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol"; | ||
import "@openzeppelin-upgrades/contracts/security/ReentrancyGuardUpgradeable.sol"; | ||
import "../permissions/Pausable.sol"; | ||
import "../libraries/EIP1271SignatureUtils.sol"; | ||
import "./AVSDirectoryStorage.sol"; | ||
|
||
contract AVSDirectory is | ||
Initializable, | ||
OwnableUpgradeable, | ||
Pausable, | ||
AVSDirectoryStorage, | ||
ReentrancyGuardUpgradeable | ||
{ | ||
// @dev Index for flag that pauses operator register/deregister to avs when set. | ||
uint8 internal constant PAUSED_OPERATOR_REGISTER_DEREGISTER_TO_AVS = 0; | ||
|
||
// @dev Chain ID at the time of contract deployment | ||
uint256 internal immutable ORIGINAL_CHAIN_ID; | ||
|
||
/******************************************************************************* | ||
INITIALIZING FUNCTIONS | ||
*******************************************************************************/ | ||
|
||
/** | ||
* @dev Initializes the immutable addresses of the strategy mananger, delegationManager, slasher, | ||
* and eigenpodManager contracts | ||
*/ | ||
constructor(IDelegationManager _delegation) AVSDirectoryStorage(_delegation) { | ||
_disableInitializers(); | ||
ORIGINAL_CHAIN_ID = block.chainid; | ||
} | ||
|
||
/** | ||
* @dev Initializes the addresses of the initial owner, pauser registry, and paused status. | ||
* minWithdrawalDelayBlocks is set only once here | ||
*/ | ||
function initialize( | ||
address initialOwner, | ||
IPauserRegistry _pauserRegistry, | ||
uint256 initialPausedStatus | ||
) external initializer { | ||
_initializePauser(_pauserRegistry, initialPausedStatus); | ||
_DOMAIN_SEPARATOR = _calculateDomainSeparator(); | ||
_transferOwnership(initialOwner); | ||
} | ||
|
||
/******************************************************************************* | ||
EXTERNAL FUNCTIONS | ||
*******************************************************************************/ | ||
|
||
|
||
/** | ||
* @notice Called by the AVS's service manager contract to register an operator with the avs. | ||
* @param operator The address of the operator to register. | ||
* @param operatorSignature The signature, salt, and expiry of the operator's signature. | ||
*/ | ||
function registerOperatorToAVS( | ||
address operator, | ||
ISignatureUtils.SignatureWithSaltAndExpiry memory operatorSignature | ||
) external onlyWhenNotPaused(PAUSED_OPERATOR_REGISTER_DEREGISTER_TO_AVS) { | ||
|
||
require( | ||
operatorSignature.expiry >= block.timestamp, | ||
"AVSDirectory.registerOperatorToAVS: operator signature expired" | ||
); | ||
require( | ||
avsOperatorStatus[msg.sender][operator] != OperatorAVSRegistrationStatus.REGISTERED, | ||
"AVSDirectory.registerOperatorToAVS: operator already registered" | ||
); | ||
require( | ||
!operatorSaltIsSpent[operator][operatorSignature.salt], | ||
"AVSDirectory.registerOperatorToAVS: salt already spent" | ||
); | ||
require( | ||
delegation.isOperator(operator), | ||
"AVSDirectory.registerOperatorToAVS: operator not registered to EigenLayer yet"); | ||
|
||
// Calculate the digest hash | ||
bytes32 operatorRegistrationDigestHash = calculateOperatorAVSRegistrationDigestHash({ | ||
operator: operator, | ||
avs: msg.sender, | ||
salt: operatorSignature.salt, | ||
expiry: operatorSignature.expiry | ||
}); | ||
|
||
// Check that the signature is valid | ||
EIP1271SignatureUtils.checkSignature_EIP1271( | ||
operator, | ||
operatorRegistrationDigestHash, | ||
operatorSignature.signature | ||
); | ||
|
||
// Set the operator as registered | ||
avsOperatorStatus[msg.sender][operator] = OperatorAVSRegistrationStatus.REGISTERED; | ||
|
||
// Mark the salt as spent | ||
operatorSaltIsSpent[operator][operatorSignature.salt] = true; | ||
|
||
emit OperatorAVSRegistrationStatusUpdated(operator, msg.sender, OperatorAVSRegistrationStatus.REGISTERED); | ||
} | ||
|
||
/** | ||
* @notice Called by an avs to deregister an operator with the avs. | ||
* @param operator The address of the operator to deregister. | ||
*/ | ||
function deregisterOperatorFromAVS(address operator) external onlyWhenNotPaused(PAUSED_OPERATOR_REGISTER_DEREGISTER_TO_AVS) { | ||
require( | ||
avsOperatorStatus[msg.sender][operator] == OperatorAVSRegistrationStatus.REGISTERED, | ||
"AVSDirectory.deregisterOperatorFromAVS: operator not registered" | ||
); | ||
|
||
// Set the operator as deregistered | ||
avsOperatorStatus[msg.sender][operator] = OperatorAVSRegistrationStatus.UNREGISTERED; | ||
|
||
emit OperatorAVSRegistrationStatusUpdated(operator, msg.sender, OperatorAVSRegistrationStatus.UNREGISTERED); | ||
} | ||
|
||
/** | ||
* @notice Called by an avs to emit an `AVSMetadataURIUpdated` event indicating the information has updated. | ||
* @param metadataURI The URI for metadata associated with an avs | ||
*/ | ||
function updateAVSMetadataURI(string calldata metadataURI) external { | ||
emit AVSMetadataURIUpdated(msg.sender, metadataURI); | ||
} | ||
|
||
/******************************************************************************* | ||
VIEW FUNCTIONS | ||
*******************************************************************************/ | ||
|
||
/** | ||
* @notice Calculates the digest hash to be signed by an operator to register with an AVS | ||
* @param operator The account registering as an operator | ||
* @param avs The address of the service manager contract for the AVS that the operator is registering to | ||
* @param salt A unique and single use value associated with the approver signature. | ||
* @param expiry Time after which the approver's signature becomes invalid | ||
*/ | ||
function calculateOperatorAVSRegistrationDigestHash( | ||
address operator, | ||
address avs, | ||
bytes32 salt, | ||
uint256 expiry | ||
) public view returns (bytes32) { | ||
// calculate the struct hash | ||
bytes32 structHash = keccak256( | ||
abi.encode(OPERATOR_AVS_REGISTRATION_TYPEHASH, operator, avs, salt, expiry) | ||
); | ||
// calculate the digest hash | ||
bytes32 digestHash = keccak256( | ||
abi.encodePacked("\x19\x01", domainSeparator(), structHash) | ||
); | ||
return digestHash; | ||
} | ||
|
||
/** | ||
* @notice Getter function for the current EIP-712 domain separator for this contract. | ||
* @dev The domain separator will change in the event of a fork that changes the ChainID. | ||
*/ | ||
function domainSeparator() public view returns (bytes32) { | ||
if (block.chainid == ORIGINAL_CHAIN_ID) { | ||
return _DOMAIN_SEPARATOR; | ||
} else { | ||
return _calculateDomainSeparator(); | ||
} | ||
} | ||
|
||
// @notice Internal function for calculating the current domain separator of this contract | ||
function _calculateDomainSeparator() internal view returns (bytes32) { | ||
return keccak256(abi.encode(DOMAIN_TYPEHASH, keccak256(bytes("EigenLayer")), block.chainid, address(this))); | ||
} | ||
} |
This file contains 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,46 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity =0.8.12; | ||
|
||
import "../interfaces/IAVSDirectory.sol"; | ||
import "../interfaces/IStrategyManager.sol"; | ||
import "../interfaces/IDelegationManager.sol"; | ||
import "../interfaces/ISlasher.sol"; | ||
import "../interfaces/IEigenPodManager.sol"; | ||
|
||
abstract contract AVSDirectoryStorage is IAVSDirectory { | ||
/// @notice The EIP-712 typehash for the contract's domain | ||
bytes32 public constant DOMAIN_TYPEHASH = | ||
keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)"); | ||
|
||
/// @notice The EIP-712 typehash for the `Registration` struct used by the contract | ||
bytes32 public constant OPERATOR_AVS_REGISTRATION_TYPEHASH = | ||
keccak256("OperatorAVSRegistration(address operator,address avs,bytes32 salt,uint256 expiry)"); | ||
|
||
/// @notice The DelegationManager contract for EigenLayer | ||
IDelegationManager public immutable delegation; | ||
|
||
/** | ||
* @notice Original EIP-712 Domain separator for this contract. | ||
* @dev The domain separator may change in the event of a fork that modifies the ChainID. | ||
* Use the getter function `domainSeparator` to get the current domain separator for this contract. | ||
*/ | ||
bytes32 internal _DOMAIN_SEPARATOR; | ||
|
||
/// @notice Mapping: AVS => operator => enum of operator status to the AVS | ||
mapping(address => mapping(address => OperatorAVSRegistrationStatus)) public avsOperatorStatus; | ||
|
||
/// @notice Mapping: operator => 32-byte salt => whether or not the salt has already been used by the operator. | ||
/// @dev Salt is used in the `registerOperatorToAVS` function. | ||
mapping(address => mapping(bytes32 => bool)) public operatorSaltIsSpent; | ||
|
||
constructor(IDelegationManager _delegation) { | ||
delegation = _delegation; | ||
} | ||
|
||
/** | ||
* @dev This empty reserved space is put in place to allow future versions to add new | ||
* variables without shifting down storage in the inheritance chain. | ||
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps | ||
*/ | ||
uint256[47] private __gap; | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.