Skip to content

Commit

Permalink
simple middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
scolear committed Sep 25, 2024
1 parent 4ceecd2 commit e9e0ebd
Show file tree
Hide file tree
Showing 22 changed files with 1,055 additions and 242 deletions.
Binary file removed .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ docs/

# Dotenv file
.env

.DS_Store
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"solidity.compileUsingRemoteVersion": "v0.8.25+commit.b61c2a91",
"solidity.packageDefaultDependenciesContractsDirectory": "src",
"solidity.packageDefaultDependenciesDirectory": "lib",
"editor.formatOnSave": true,
"[solidity]": {
"editor.defaultFormatter": "JuanBlanco.solidity"
},
"solidity.formatter": "forge",
"solidity.remappings": [
"@openzeppelin/=project/contracts/lib/openzeppelin-contracts/",
"@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
"@symbiotic/=lib/core/src/"
]
}
10 changes: 6 additions & 4 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[profile.default]
solc = "0.8.25"
via_ir = true
src = "src"
out = "out"
libs = ["lib"]
ffi = true
ast = true
build_info = true
extra_output = ["storageLayout"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
remappings = [
'@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/',
'@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/',
]
2 changes: 1 addition & 1 deletion lib/core
Submodule core updated 63 files
+2 −2 README.md
+1 −1 package.json
+14 −0 src/contracts/common/Entity.sol
+29 −0 src/contracts/common/Factory.sol
+28 −6 src/contracts/common/MigratableEntity.sol
+30 −4 src/contracts/common/MigratablesFactory.sol
+30 −10 src/contracts/delegator/BaseDelegator.sol
+7 −4 src/contracts/delegator/FullRestakeDelegator.sol
+7 −4 src/contracts/delegator/NetworkRestakeDelegator.sol
+2 −2 src/contracts/libraries/Checkpoints.sol
+2 −2 src/contracts/libraries/ERC4626Math.sol
+21 −0 src/contracts/libraries/LICENSE
+2 −2 src/contracts/libraries/Subnetwork.sol
+15 −3 src/contracts/slasher/BaseSlasher.sol
+2 −6 src/contracts/slasher/Slasher.sol
+13 −10 src/contracts/slasher/VetoSlasher.sol
+67 −30 src/contracts/vault/Vault.sol
+2 −2 src/interfaces/IDelegatorFactory.sol
+2 −2 src/interfaces/INetworkRegistry.sol
+2 −2 src/interfaces/IOperatorRegistry.sol
+2 −2 src/interfaces/ISlasherFactory.sol
+2 −2 src/interfaces/IVaultConfigurator.sol
+2 −2 src/interfaces/IVaultFactory.sol
+21 −0 src/interfaces/LICENSE
+0 −99 src/interfaces/collateral/ICollateral.sol
+10 −2 src/interfaces/common/IEntity.sol
+33 −2 src/interfaces/common/IFactory.sol
+9 −2 src/interfaces/common/IMigratableEntity.sol
+3 −5 src/interfaces/common/IMigratableEntityProxy.sol
+32 −2 src/interfaces/common/IMigratablesFactory.sol
+2 −2 src/interfaces/common/IRegistry.sol
+18 −2 src/interfaces/delegator/IBaseDelegator.sol
+2 −1 src/interfaces/delegator/IDelegatorHook.sol
+2 −1 src/interfaces/delegator/IFullRestakeDelegator.sol
+2 −1 src/interfaces/delegator/INetworkRestakeDelegator.sol
+2 −2 src/interfaces/service/IMetadataService.sol
+2 −2 src/interfaces/service/INetworkMiddlewareService.sol
+2 −2 src/interfaces/service/IOptInService.sol
+5 −3 src/interfaces/slasher/IBaseSlasher.sol
+2 −2 src/interfaces/slasher/ISlasher.sol
+4 −4 src/interfaces/slasher/IVetoSlasher.sol
+28 −4 src/interfaces/vault/IVault.sol
+2 −2 src/interfaces/vault/IVaultStorage.sol
+3 −6 test/DelegatorFactory.t.sol
+27 −0 test/POC.t.sol
+636 −0 test/POCBase.t.sol
+3 −6 test/SlasherFactory.t.sol
+3 −6 test/VaultConfigurator.t.sol
+3 −6 test/VaultFactory.t.sol
+19 −1 test/common/Entity.t.sol
+22 −2 test/common/Factory.t.sol
+7 −2 test/common/MigratableEntity.t.sol
+2 −2 test/common/MigratableEntityProxy.t.sol
+23 −2 test/common/MigratablesFactory.t.sol
+1 −1 test/common/Registry.t.sol
+121 −8 test/delegator/FullRestakeDelegator.t.sol
+123 −8 test/delegator/NetworkRestakeDelegator.t.sol
+0 −99 test/mocks/SimpleCollateral.sol
+7 −3 test/mocks/SimpleFullRestakeDelegatorHook.sol
+7 −3 test/mocks/SimpleNetworkRestakeDelegatorHook.sol
+12 −10 test/slasher/Slasher.t.sol
+236 −11 test/slasher/VetoSlasher.t.sol
+165 −17 test/vault/Vault.t.sol
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts-upgradeable
3 changes: 3 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/
@symbiotic/=lib/core/src/
32 changes: 32 additions & 0 deletions script/NetworkSetup.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.25;

import {Script} from "forge-std/Script.sol";
import {SimpleMiddleware} from "src/SimpleMiddleware.sol";
import {IRegistry} from "@symbiotic/interfaces/common/IRegistry.sol";
import {INetworkRegistry} from "@symbiotic/interfaces/INetworkRegistry.sol";
import {IOperatorRegistry} from "@symbiotic/interfaces/IOperatorRegistry.sol";
import {IOptInService} from "@symbiotic/interfaces/service/IOptInService.sol";
import {IVault} from "@symbiotic/interfaces/vault/IVault.sol";
import {IBaseDelegator} from "@symbiotic/interfaces/delegator/IBaseDelegator.sol";

contract NetworkSetup is Script {
function run(
address networkRegistry,
address[] memory vaults,
uint256 subnetworksCnt,
uint256[][] calldata networkLimits
) external {
require(vaults.length == networkLimits.length, "inconsistent length");
vm.startBroadcast();
INetworkRegistry(networkRegistry).registerNetwork();
for (uint256 i = 0; i < vaults.length; ++i) {
require(subnetworksCnt == networkLimits[i].length, "inconsistent length");
address delegator = IVault(vaults[i]).delegator();
for (uint96 j = 0; j < subnetworksCnt; ++j) {
IBaseDelegator(delegator).setMaxNetworkLimit(j, networkLimits[i][j]);
}
}
vm.stopBroadcast();
}
}
38 changes: 38 additions & 0 deletions script/Setup.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.25;

import {Script} from "forge-std/Script.sol";
import {SimpleMiddleware} from "src/SimpleMiddleware.sol";

contract Setup is Script {
function run(
address network,
address owner,
uint48 epochDuration,
address[] memory vaults,
address[] memory operators,
bytes32[] memory keys,
address operatorRegistry,
address vaultRegistry,
address operatorNetworkOptIn
) external {
require(operators.length == keys.length, "inconsistent length");
vm.startBroadcast();

uint48 minSlashingWindow = epochDuration; // we dont use this

SimpleMiddleware middleware = new SimpleMiddleware(
network, operatorRegistry, vaultRegistry, operatorNetworkOptIn, owner, epochDuration, minSlashingWindow
);

for (uint256 i = 0; i < vaults.length; ++i) {
middleware.registerVault(vaults[i]);
}

for (uint256 i = 0; i < operators.length; ++i) {
middleware.registerOperator(operators[i], keys[i]);
}

vm.stopBroadcast();
}
}
2 changes: 1 addition & 1 deletion src/DLCBTC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// / /_// /__/ /____/ /__| | | | | <
// /___,'\____|____(_)____/_|_| |_|_|\_\

pragma solidity 0.8.20;
pragma solidity 0.8.25;

import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20PermitUpgradeable.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/DLCLinkLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// / /_// /__/ /____/ /__| | | | | <
// /___,'\____|____(_)____/_|_| |_|_|\_\

pragma solidity 0.8.20;
pragma solidity 0.8.25;

library DLCLink {
enum DLCStatus {
Expand Down
2 changes: 1 addition & 1 deletion src/DLCManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// / /_// /__/ /____/ /__| | | | | <
// /___,'\____|____(_)____/_|_| |_|_|\_\

pragma solidity 0.8.20;
pragma solidity 0.8.25;

import "@openzeppelin/contracts-upgradeable/access/extensions/AccessControlDefaultAdminRulesUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
Expand Down
78 changes: 78 additions & 0 deletions src/MapWithTimeData.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

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

library MapWithTimeData {
using EnumerableMap for EnumerableMap.AddressToUintMap;

error AlreadyAdded();
error NotEnabled();
error AlreadyEnabled();

uint256 private constant ENABLED_TIME_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFF;
uint256 private constant DISABLED_TIME_MASK =
0xFFFFFFFFFFFFFFFFFFFFFFFF << 48;

function add(
EnumerableMap.AddressToUintMap storage self,
address addr
) internal {
if (!self.set(addr, uint256(0))) {
revert AlreadyAdded();
}
}

function disable(
EnumerableMap.AddressToUintMap storage self,
address addr
) internal {
uint256 value = self.get(addr);

if (uint48(value) == 0 || uint48(value >> 48) != 0) {
revert NotEnabled();
}

value |= uint256(Time.timestamp()) << 48;
self.set(addr, value);
}

function enable(
EnumerableMap.AddressToUintMap storage self,
address addr
) internal {
uint256 value = self.get(addr);

if (uint48(value) != 0 && uint48(value >> 48) == 0) {
revert AlreadyEnabled();
}

value = uint256(Time.timestamp());
self.set(addr, value);
}

function atWithTimes(
EnumerableMap.AddressToUintMap storage self,
uint256 idx
)
internal
view
returns (address key, uint48 enabledTime, uint48 disabledTime)
{
uint256 value;
(key, value) = self.at(idx);
enabledTime = uint48(value);
disabledTime = uint48(value >> 48);
}

function getTimes(
EnumerableMap.AddressToUintMap storage self,
address addr
) internal view returns (uint48 enabledTime, uint48 disabledTime) {
uint256 value = self.get(addr);
enabledTime = uint48(value);
disabledTime = uint48(value >> 48);
}
}
60 changes: 60 additions & 0 deletions src/SimpleKeyRegistry32.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

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

import {IVault} from "@symbiotic/interfaces/vault/IVault.sol";

abstract contract SimpleKeyRegistry32 {
using Checkpoints for Checkpoints.Trace208;

error DuplicateKey();

mapping(address => Checkpoints.Trace208) private operatorToIdx;
mapping(bytes32 => address) private keyToOperator;
mapping(uint208 => bytes32) private idxToKey;
uint208 private totalKeys;

uint208 internal constant EMPTY_KEY_IDX = 0;

function getOperatorByKey(bytes32 key) public view returns (address) {
return keyToOperator[key];
}

function getCurrentOperatorKey(
address operator
) public view returns (bytes32) {
uint208 keyIdx = operatorToIdx[operator].latest();

if (keyIdx == EMPTY_KEY_IDX) {
return bytes32(0);
}

return idxToKey[keyIdx];
}

function getOperatorKeyAt(
address operator,
uint48 timestamp
) public view returns (bytes32) {
uint208 keyIdx = operatorToIdx[operator].upperLookup(timestamp);

if (keyIdx == EMPTY_KEY_IDX) {
return bytes32(0);
}

return idxToKey[keyIdx];
}

function updateKey(address operator, bytes32 key) internal {
if (keyToOperator[key] != address(0)) {
revert DuplicateKey();
}

uint208 newIdx = ++totalKeys;
idxToKey[newIdx] = key;
operatorToIdx[operator].push(Time.timestamp(), newIdx);
keyToOperator[key] = operator;
}
}
Loading

0 comments on commit e9e0ebd

Please sign in to comment.