Skip to content

Commit

Permalink
feat: make vault onSlash() return real slashed amount
Browse files Browse the repository at this point in the history
  • Loading branch information
1kresh committed Oct 14, 2024
1 parent 8dda1d3 commit def15e0
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/contracts/delegator/BaseDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,18 @@ contract BaseDelegator is
function onSlash(
bytes32 subnetwork,
address operator,
uint256 slashedAmount,
uint256 amount,
uint48 captureTimestamp,
bytes memory data
) external nonReentrant {
if (IVault(vault).slasher() != msg.sender) {
if (msg.sender != IVault(vault).slasher()) {
revert NotSlasher();
}

address hook_ = hook;
if (hook_ != address(0)) {
bytes memory calldata_ = abi.encodeWithSelector(
IDelegatorHook.onSlash.selector, subnetwork, operator, slashedAmount, captureTimestamp, data
IDelegatorHook.onSlash.selector, subnetwork, operator, amount, captureTimestamp, data
);

if (gasleft() < HOOK_RESERVE + HOOK_GAS_LIMIT * 64 / 63) {
Expand All @@ -198,7 +198,7 @@ contract BaseDelegator is
}
}

emit OnSlash(subnetwork, operator, slashedAmount);
emit OnSlash(subnetwork, operator, amount, captureTimestamp);
}

function _initialize(
Expand Down
8 changes: 4 additions & 4 deletions src/contracts/vault/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, IVau
/**
* @inheritdoc IVault
*/
function onSlash(uint256 slashedAmount, uint48 captureTimestamp) external nonReentrant {
function onSlash(uint256 amount, uint48 captureTimestamp) external nonReentrant returns (uint256 slashedAmount) {
if (msg.sender != slasher) {
revert NotSlasher();
}
Expand All @@ -233,7 +233,7 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, IVau
uint256 nextWithdrawals = withdrawals[currentEpoch_ + 1];
if (captureEpoch == currentEpoch_) {
uint256 slashableStake = activeStake_ + nextWithdrawals;
slashedAmount = Math.min(slashedAmount, slashableStake);
slashedAmount = Math.min(amount, slashableStake);
if (slashedAmount > 0) {
uint256 activeSlashed = slashedAmount.mulDiv(activeStake_, slashableStake);
uint256 nextWithdrawalsSlashed = slashedAmount - activeSlashed;
Expand All @@ -244,7 +244,7 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, IVau
} else {
uint256 withdrawals_ = withdrawals[currentEpoch_];
uint256 slashableStake = activeStake_ + withdrawals_ + nextWithdrawals;
slashedAmount = Math.min(slashedAmount, slashableStake);
slashedAmount = Math.min(amount, slashableStake);
if (slashedAmount > 0) {
uint256 activeSlashed = slashedAmount.mulDiv(activeStake_, slashableStake);
uint256 nextWithdrawalsSlashed = slashedAmount.mulDiv(nextWithdrawals, slashableStake);
Expand All @@ -265,7 +265,7 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, IVau
IERC20(collateral).safeTransfer(burner, slashedAmount);
}

emit OnSlash(msg.sender, slashedAmount);
emit OnSlash(amount, captureTimestamp, slashedAmount);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/interfaces/delegator/IBaseDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ interface IBaseDelegator is IEntity {
event SetMaxNetworkLimit(bytes32 indexed subnetwork, uint256 amount);

/**
* @notice Emitted when a slash happened.
* @notice Emitted when a slash happens.
* @param subnetwork full identifier of the subnetwork (address of the network concatenated with the uint96 identifier)
* @param operator address of the operator
* @param slashedAmount amount of the collateral slashed
* @param amount amount of the collateral to be slashed
* @param captureTimestamp time point when the stake was captured
*/
event OnSlash(bytes32 indexed subnetwork, address indexed operator, uint256 slashedAmount);
event OnSlash(bytes32 indexed subnetwork, address indexed operator, uint256 amount, uint48 captureTimestamp);

/**
* @notice Emitted when a hook is set.
Expand Down Expand Up @@ -174,15 +175,15 @@ interface IBaseDelegator is IEntity {
* @notice Called when a slash happens.
* @param subnetwork full identifier of the subnetwork (address of the network concatenated with the uint96 identifier)
* @param operator address of the operator
* @param slashedAmount amount of the collateral slashed
* @param amount amount of the collateral slashed
* @param captureTimestamp time point when the stake was captured
* @param data some additional data
* @dev Only the vault's slasher can call this function.
*/
function onSlash(
bytes32 subnetwork,
address operator,
uint256 slashedAmount,
uint256 amount,
uint48 captureTimestamp,
bytes calldata data
) external;
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/delegator/IDelegatorHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ interface IDelegatorHook {
* @notice Called when a slash happens.
* @param subnetwork full identifier of the subnetwork (address of the network concatenated with the uint96 identifier)
* @param operator address of the operator
* @param slashedAmount amount of the collateral slashed
* @param amount amount of the collateral to be slashed
* @param captureTimestamp time point when the stake was captured
* @param data some additional data
*/
function onSlash(
bytes32 subnetwork,
address operator,
uint256 slashedAmount,
uint256 amount,
uint48 captureTimestamp,
bytes calldata data
) external;
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/slasher/ISlasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ISlasher {
* @notice Emitted when a slash is performed.
* @param subnetwork subnetwork that requested the slash
* @param operator operator that is slashed
* @param slashedAmount amount of the collateral slashed
* @param slashedAmount virtual amount of the collateral slashed
* @param captureTimestamp time point when the stake was captured
*/
event Slash(bytes32 indexed subnetwork, address indexed operator, uint256 slashedAmount, uint48 captureTimestamp);
Expand All @@ -29,7 +29,7 @@ interface ISlasher {
* @param amount maximum amount of the collateral to be slashed
* @param captureTimestamp time point when the stake was captured
* @param hints hints for checkpoints' indexes
* @return slashedAmount amount of the collateral slashed
* @return slashedAmount virtual amount of the collateral slashed
* @dev Only a network middleware can call this function.
*/
function slash(
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/slasher/IVetoSlasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ interface IVetoSlasher {
/**
* @notice Emitted when a slash request is executed.
* @param slashIndex index of the slash request
* @param slashedAmount amount of the collateral slashed
* @param slashedAmount virtual amount of the collateral slashed
*/
event ExecuteSlash(uint256 indexed slashIndex, uint256 slashedAmount);

Expand Down Expand Up @@ -207,7 +207,7 @@ interface IVetoSlasher {
* @notice Execute a slash with a given slash index using hints.
* @param slashIndex index of the slash request
* @param hints hints for checkpoints' indexes
* @return slashedAmount amount of the collateral slashed
* @return slashedAmount virtual amount of the collateral slashed
* @dev Only a network middleware can call this function.
*/
function executeSlash(uint256 slashIndex, bytes calldata hints) external returns (uint256 slashedAmount);
Expand Down
16 changes: 9 additions & 7 deletions src/interfaces/vault/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ interface IVault is IMigratableEntity, IVaultStorage {
event ClaimBatch(address indexed claimer, address indexed recipient, uint256[] epochs, uint256 amount);

/**
* @notice Emitted when a slash happened.
* @param slasher address of the slasher
* @param slashedAmount amount of the collateral slashed
* @notice Emitted when a slash happens.
* @param amount amount of the collateral to slash
* @param captureTimestamp time point when the stake was captured
* @param slashedAmount real amount of the collateral slashed
*/
event OnSlash(address indexed slasher, uint256 slashedAmount);
event OnSlash(uint256 amount, uint48 captureTimestamp, uint256 slashedAmount);

/**
* @notice Emitted when a deposit whitelist status is enabled/disabled.
Expand Down Expand Up @@ -223,7 +224,7 @@ interface IVault is IMigratableEntity, IVaultStorage {
* @notice Deposit collateral into the vault.
* @param onBehalfOf account the deposit is made on behalf of
* @param amount amount of the collateral to deposit
* @return depositedAmount amount of the collateral deposited
* @return depositedAmount real amount of the collateral deposited
* @return mintedShares amount of the active shares minted
*/
function deposit(
Expand Down Expand Up @@ -267,11 +268,12 @@ interface IVault is IMigratableEntity, IVaultStorage {

/**
* @notice Slash callback for burning collateral.
* @param slashedAmount amount to slash
* @param amount amount to slash
* @param captureTimestamp time point when the stake was captured
* @return slashedAmount real amount of the collateral slashed
* @dev Only the slasher can call this function.
*/
function onSlash(uint256 slashedAmount, uint48 captureTimestamp) external;
function onSlash(uint256 amount, uint48 captureTimestamp) external returns (uint256 slashedAmount);

/**
* @notice Enable/disable deposit whitelist.
Expand Down

0 comments on commit def15e0

Please sign in to comment.