Skip to content

Commit

Permalink
Merge pull request #5 from symbioticfi/adapt-core
Browse files Browse the repository at this point in the history
Add new hooks
  • Loading branch information
1kresh authored Oct 28, 2024
2 parents a55c004 + 84e3c54 commit a1237c2
Show file tree
Hide file tree
Showing 35 changed files with 2,443 additions and 601 deletions.
2 changes: 1 addition & 1 deletion lib/core
Submodule core updated 56 files
+18 −2 script/deploy/Core.s.sol
+2 −2 script/deploy/OptInService.s.sol
+60 −26 script/deploy/Vault.s.sol
+1 −1 src/contracts/common/Entity.sol
+4 −2 src/contracts/common/MigratableEntity.sol
+2 −3 src/contracts/common/MigratablesFactory.sol
+6 −1 src/contracts/common/StaticDelegateCallable.sol
+19 −17 src/contracts/delegator/BaseDelegator.sol
+10 −6 src/contracts/delegator/FullRestakeDelegator.sol
+12 −8 src/contracts/delegator/NetworkRestakeDelegator.sol
+155 −0 src/contracts/delegator/OperatorSpecificDelegator.sol
+75 −16 src/contracts/hints/DelegatorHints.sol
+2 −3 src/contracts/hints/Hints.sol
+2 −3 src/contracts/hints/OptInServiceHints.sol
+9 −45 src/contracts/hints/SlasherHints.sol
+3 −9 src/contracts/hints/VaultHints.sol
+14 −6 src/contracts/libraries/Checkpoints.sol
+106 −15 src/contracts/service/OptInService.sol
+77 −47 src/contracts/slasher/BaseSlasher.sol
+20 −9 src/contracts/slasher/Slasher.sol
+45 −29 src/contracts/slasher/VetoSlasher.sol
+8 −18 src/contracts/vault/Vault.sol
+1 −1 src/contracts/vault/VaultStorage.sol
+0 −1 src/contracts/vault/VaultTokenized.sol
+0 −2 src/interfaces/IVaultConfigurator.sol
+12 −0 src/interfaces/common/IStaticDelegateCallable.sol
+6 −5 src/interfaces/delegator/IBaseDelegator.sol
+2 −2 src/interfaces/delegator/IDelegatorHook.sol
+88 −0 src/interfaces/delegator/IOperatorSpecificDelegator.sol
+48 −4 src/interfaces/service/IOptInService.sol
+38 −19 src/interfaces/slasher/IBaseSlasher.sol
+13 −0 src/interfaces/slasher/IBurner.sol
+23 −3 src/interfaces/slasher/ISlasher.sol
+22 −6 src/interfaces/slasher/IVetoSlasher.sol
+11 −22 src/interfaces/vault/IVault.sol
+2 −2 src/interfaces/vault/IVaultStorage.sol
+21 −3 test/DelegatorFactory.t.sol
+2 −2 test/POC.t.sol
+35 −7 test/POCBase.t.sol
+38 −5 test/SlasherFactory.t.sol
+21 −3 test/VaultConfigurator.t.sol
+21 −3 test/VaultFactory.t.sol
+133 −21 test/delegator/FullRestakeDelegator.t.sol
+156 −19 test/delegator/NetworkRestakeDelegator.t.sol
+1,691 −0 test/delegator/OperatorSpecificDelegator.t.sol
+626 −0 test/libraries/Checkpoints.t.sol
+32 −0 test/mocks/SimpleBurner.sol
+35 −1 test/mocks/SimpleFullRestakeDelegatorHook.sol
+1 −1 test/mocks/SimpleMigratableEntity.sol
+35 −1 test/mocks/SimpleNetworkRestakeDelegatorHook.sol
+20 −0 test/mocks/SimpleOperatorSpecificDelegatorHook.sol
+309 −6 test/service/OptInService.t.sol
+433 −285 test/slasher/Slasher.t.sol
+124 −19 test/slasher/VetoSlasher.t.sol
+70 −54 test/vault/Vault.t.sol
+70 −54 test/vault/VaultTokenized.t.sol
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts
13 changes: 13 additions & 0 deletions specs/FullRestakeDelegatorHooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Hooks

### FullRestakeDecreaseHook

FullRestakeDecreaseHook supports `onSlash()` calls only from `FullRestakeDelegator`.

This hook decreases the network's limit by the slashed amount and decreases the operator's limit also by the slashed amount. It doesn't change stake amounts for other operators.

### FullRestakeResetHook

FullRestakeResetHook supports `onSlash()` calls only from `FullRestakeDelegator`.

This hook resets the slashed operator's limit to zero in case it was slashed a configured number of times during a configured period of time.
19 changes: 19 additions & 0 deletions specs/NetworkRestakeDelegatorHooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Hooks

### NetworkRestakeDecreaseHook

NetworkRestakeDecreaseHook supports `onSlash()` calls only from `NetworkRestakeDelegator`.

This hook decreases the network's limit by the slashed amount and decreases the slashed operator's shares in such a way as to decrease his stake (which depends on the network's limit) by the slashed amount. It doesn't change stake amounts for other operators.

### NetworkRestakeRedistributeHook

NetworkRestakeRedistributeHook supports `onSlash()` calls only from `NetworkRestakeDelegator`.

This hook decreases the slashed operator's shares by slashed percent from the given stake, redistributing the decreased stake to other operators.

### NetworkRestakeResetHook

NetworkRestakeResetHook supports `onSlash()` calls only from `NetworkRestakeDelegator`.

This hook resets the slashed operator's shares to zero in case it was slashed a configured number of times during a configured period of time.
13 changes: 13 additions & 0 deletions specs/OperatorSpecificDelegatorHooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Hooks

### OperatorSpecificDecreaseHook

OperatorSpecificDecreaseHook supports `onSlash()` calls only from `OperatorSpecificDelegator`.

This hook decreases the network's limit by the slashed amount.

### OperatorSpecificResetHook

OperatorSpecificResetHook supports `onSlash()` calls only from `OperatorSpecificDelegator`.

This hook resets the slashing network's limit to zero in case it was slashed a configured number of times during a configured period of time.
19 changes: 0 additions & 19 deletions specs/hooks.md

This file was deleted.

54 changes: 0 additions & 54 deletions src/contracts/NetworkRestakeFairHook.sol

This file was deleted.

59 changes: 0 additions & 59 deletions src/contracts/NetworkRestakeRedistributionHook.sol

This file was deleted.

47 changes: 47 additions & 0 deletions src/contracts/fullRestakeDelegator/FullRestakeDecreaseHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IFullRestakeDecreaseHook} from "../../interfaces/fullRestakeDelegator/IFullRestakeDecreaseHook.sol";

import {IDelegatorHook} from "@symbioticfi/core/src/interfaces/delegator/IDelegatorHook.sol";
import {IEntity} from "@symbioticfi/core/src/interfaces/common/IEntity.sol";
import {IFullRestakeDelegator} from "@symbioticfi/core/src/interfaces/delegator/IFullRestakeDelegator.sol";

import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";

contract FullRestakeDecreaseHook is IFullRestakeDecreaseHook {
using Math for uint256;

/**
* @inheritdoc IDelegatorHook
*/
function onSlash(
bytes32 subnetwork,
address operator,
uint256 slashedAmount,
uint48, /* captureTimestamp */
bytes calldata /* data */
) external {
if (IEntity(msg.sender).TYPE() != 1) {
revert NotFullRestakeDelegator();
}

if (slashedAmount == 0) {
return;
}

uint256 networkLimit = IFullRestakeDelegator(msg.sender).networkLimit(subnetwork);
if (networkLimit != 0) {
IFullRestakeDelegator(msg.sender).setNetworkLimit(
subnetwork, networkLimit - Math.min(slashedAmount, networkLimit)
);
}

uint256 operatorNetworkLimit = IFullRestakeDelegator(msg.sender).operatorNetworkLimit(subnetwork, operator);
if (operatorNetworkLimit != 0) {
IFullRestakeDelegator(msg.sender).setOperatorNetworkLimit(
subnetwork, operator, operatorNetworkLimit - Math.min(slashedAmount, operatorNetworkLimit)
);
}
}
}
77 changes: 77 additions & 0 deletions src/contracts/fullRestakeDelegator/FullRestakeResetHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IFullRestakeResetHook} from "../../interfaces/fullRestakeDelegator/IFullRestakeResetHook.sol";

import {IDelegatorHook} from "@symbioticfi/core/src/interfaces/delegator/IDelegatorHook.sol";
import {IEntity} from "@symbioticfi/core/src/interfaces/common/IEntity.sol";
import {IFullRestakeDelegator} from "@symbioticfi/core/src/interfaces/delegator/IFullRestakeDelegator.sol";
import {IVault} from "@symbioticfi/core/src/interfaces/vault/IVault.sol";

import {CircularBuffer} from "@openzeppelin/contracts/utils/structs/CircularBuffer.sol";
import {Time} from "@openzeppelin/contracts/utils/types/Time.sol";

contract FullRestakeResetHook is IFullRestakeResetHook {
using CircularBuffer for CircularBuffer.Bytes32CircularBuffer;

/**
* @inheritdoc IFullRestakeResetHook
*/
uint48 public immutable PERIOD;

/**
* @inheritdoc IFullRestakeResetHook
*/
uint256 public immutable SLASH_COUNT;

mapping(address vault => mapping(address operator => CircularBuffer.Bytes32CircularBuffer buffer)) private
_slashings;

constructor(uint48 period_, uint256 slashCount_) {
if (slashCount_ == 0) {
revert InvalidSlashCount();
}

PERIOD = period_;
SLASH_COUNT = slashCount_;
}

/**
* @inheritdoc IDelegatorHook
*/
function onSlash(
bytes32 subnetwork,
address operator,
uint256, /* slashedAmount */
uint48, /* captureTimestamp */
bytes calldata /* data */
) external {
if (IEntity(msg.sender).TYPE() != 1) {
revert NotFullRestakeDelegator();
}

address vault = IFullRestakeDelegator(msg.sender).vault();

if (IVault(vault).delegator() != msg.sender) {
revert NotVaultDelegator();
}

uint256 slashCount_ = SLASH_COUNT;
if (_slashings[vault][operator].count() == 0) {
_slashings[vault][operator].setup(slashCount_);
}

if (IFullRestakeDelegator(msg.sender).operatorNetworkLimit(subnetwork, operator) == 0) {
return;
}

_slashings[vault][operator].push(bytes32(uint256(Time.timestamp())));

if (
_slashings[vault][operator].count() == slashCount_
&& Time.timestamp() - uint256(_slashings[vault][operator].last(slashCount_ - 1)) <= PERIOD
) {
IFullRestakeDelegator(msg.sender).setOperatorNetworkLimit(subnetwork, operator, 0);
}
}
}
Loading

0 comments on commit a1237c2

Please sign in to comment.