Skip to content

Commit

Permalink
feat: introduce wouldVaultBeValidWith function, along with documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz committed Nov 7, 2024
1 parent f9a200c commit 4223760
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
24 changes: 24 additions & 0 deletions contracts-abi/abi/MevCommitMiddleware.abi
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,30 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "wouldVaultBeValidWith",
"inputs": [
{
"name": "vault",
"type": "address",
"internalType": "address"
},
{
"name": "potentialSLashPeriodSeconds",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "bool",
"internalType": "bool"
}
],
"stateMutability": "view"
},
{
"type": "event",
"name": "Initialized",
Expand Down
33 changes: 32 additions & 1 deletion contracts-abi/clients/MevCommitMiddleware/MevCommitMiddleware.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,24 @@ contract MevCommitMiddleware is IMevCommitMiddleware, MevCommitMiddlewareStorage
}

/// @dev Sets the slash period in seconds, restricted to contract owner.
///
/// @dev WARNING: Before the owner calls this function, ALL registered vaults must be validated
/// or deregistered, to conform to the new slashPeriodSeconds.
///
/// @dev The owner will need to use VaultRegistered, VaultDeregistrationRequested, and VaultDeregistered events
/// to determine the registered vault set, call wouldVaultBeValidWith for each vault, and deregister any
/// vaults that are not valid. Only then can this function be called.
function setSlashPeriodSeconds(uint256 slashPeriodSeconds_) external onlyOwner {
_setSlashPeriodSeconds(slashPeriodSeconds_);
}

/// @dev Checks if a vault would be valid with a given slashPeriodSeconds.
/// @return True if the vault would be valid, reverts otherwise.
function wouldVaultBeValidWith(address vault, uint256 potentialSLashPeriodSeconds) external view returns (bool) {
_validateVaultParams(vault, potentialSLashPeriodSeconds);
return true;
}

/// @dev Sets the slash oracle, restricted to contract owner.
function setSlashOracle(address slashOracle_) external onlyOwner {
_setSlashOracle(slashOracle_);
Expand Down Expand Up @@ -457,14 +471,18 @@ contract MevCommitMiddleware is IMevCommitMiddleware, MevCommitMiddlewareStorage
require(!vaultRecords[vault].exists, VaultAlreadyRegistered(vault));
require(vaultFactory.isEntity(vault), VaultNotEntity(vault));
require(slashAmount != 0, SlashAmountMustBeNonZero(vault));
_validateVaultParams(vault, slashPeriodSeconds);
_setVaultRecord(vault, slashAmount);
emit VaultRegistered(vault, slashAmount);
}

function _validateVaultParams(address vault, uint256 slashPeriodSeconds) internal view {
IEntity delegator = IEntity(IVault(vault).delegator());
if (delegator.TYPE() == _FULL_RESTAKE_DELEGATOR_TYPE) {
revert FullRestakeDelegatorNotSupported(vault);
} else if (delegator.TYPE() != _NETWORK_RESTAKE_DELEGATOR_TYPE) {
revert UnknownDelegatorType(vault, delegator.TYPE());
}

IVaultStorage vaultContract = IVaultStorage(vault);
uint256 vaultEpochDurationSeconds = vaultContract.epochDuration();

Expand All @@ -483,12 +501,8 @@ contract MevCommitMiddleware is IMevCommitMiddleware, MevCommitMiddlewareStorage
} else if (slasherType != _INSTANT_SLASHER_TYPE) {
revert UnknownSlasherType(vault, slasherType);
}

require(vaultEpochDurationSeconds > slashPeriodSeconds,
InvalidVaultEpochDuration(vault, vaultEpochDurationSeconds, slashPeriodSeconds));

_setVaultRecord(vault, slashAmount);
emit VaultRegistered(vault, slashAmount);
}

function _updateSlashAmount(address vault, uint160 slashAmount) internal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,18 @@ contract MevCommitMiddlewareTest is Test {
);
mevCommitMiddleware.registerVaults(vaults, slashAmounts);

vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.UnknownSlasherType.selector, vault2, 88)
);
mevCommitMiddleware.wouldVaultBeValidWith(address(vault2), 150 hours);

uint64 instantSlasherType = 0;

mockSlasher2.setType(instantSlasherType);

assertTrue(mevCommitMiddleware.wouldVaultBeValidWith(address(vault1), 150 hours));
assertTrue(mevCommitMiddleware.wouldVaultBeValidWith(address(vault2), 150 hours));

vm.prank(owner);
vm.expectEmit(true, true, true, true);
emit VaultRegistered(address(vault1), 15);
Expand Down Expand Up @@ -687,6 +695,11 @@ contract MevCommitMiddlewareTest is Test {
abi.encodeWithSelector(IMevCommitMiddleware.VaultAlreadyRegistered.selector, vault2)
);
mevCommitMiddleware.registerVaults(vaults, slashAmounts);

vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.InvalidVaultEpochDuration.selector, vault1, 152 hours, 24 days)
);
mevCommitMiddleware.wouldVaultBeValidWith(address(vault1), 24 days);
}

function test_updateSlashAmount() public {
Expand Down

0 comments on commit 4223760

Please sign in to comment.