Skip to content

Commit

Permalink
Merge pull request #8 from symbioticfi/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
1kresh authored Jul 4, 2024
2 parents 39abd66 + 3987070 commit 87bb342
Show file tree
Hide file tree
Showing 18 changed files with 2,026 additions and 89 deletions.
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ quote_style = "double"
tab_width = 4

[fuzz]
runs = 2048
runs = 4096
max_test_rejects = 262144

[etherscan]
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/common/MigratableEntity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract contract MigratableEntity is Initializable, OwnableUpgradeable, IMigrat
address private immutable SELF;

modifier uninitialized() {
if (_getInitializedVersion() != 0) {
if (_getInitializedVersion() > 0) {
revert AlreadyInitialized();
}

Expand Down
6 changes: 5 additions & 1 deletion src/contracts/delegator/BaseDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ contract BaseDelegator is Entity, AccessControlUpgradeable, IBaseDelegator {
return 0;
}

minOperatorNetworkStakeDuring_ = operatorNetworkStake(network, operator);
minOperatorNetworkStakeDuring_ = Math.min(IVault(vault).activeSupply(), operatorNetworkStake(network, operator));

uint48 epochDuration = IVault(vault).epochDuration();
uint48 nextEpochStart = IVault(vault).currentEpochStart() + epochDuration;
Expand Down Expand Up @@ -148,6 +148,10 @@ contract BaseDelegator is Entity, AccessControlUpgradeable, IBaseDelegator {
revert NotSlasher();
}

if (slashedAmount > operatorNetworkStake(network, operator)) {
revert TooMuchSlash();
}

_onSlash(network, operator, slashedAmount);

emit OnSlash(network, operator, slashedAmount);
Expand Down
12 changes: 8 additions & 4 deletions src/contracts/delegator/FullRestakeDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,17 @@ contract FullRestakeDelegator is BaseDelegator, IFullRestakeDelegator {
function _onSlash(address network, address operator, uint256 slashedAmount) internal override {
uint256 networkLimit_ = networkLimit(network);
if (networkLimit_ != type(uint256).max) {
_networkLimit[network].push(Time.timestamp(), networkLimit_ - slashedAmount);
_insertCheckpoint(_networkLimit[network], Time.timestamp(), networkLimit_ - slashedAmount);
}

_totalOperatorNetworkLimit[network].push(Time.timestamp(), totalOperatorNetworkLimit(network) - slashedAmount);
_insertCheckpoint(
_totalOperatorNetworkLimit[network], Time.timestamp(), totalOperatorNetworkLimit(network) - slashedAmount
);

_operatorNetworkLimit[network][operator].push(
Time.timestamp(), operatorNetworkLimit(network, operator) - slashedAmount
_insertCheckpoint(
_operatorNetworkLimit[network][operator],
Time.timestamp(),
operatorNetworkLimit(network, operator) - slashedAmount
);
}

Expand Down
18 changes: 11 additions & 7 deletions src/contracts/delegator/NetworkRestakeDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,23 @@ contract NetworkRestakeDelegator is BaseDelegator, INetworkRestakeDelegator {

function _onSlash(address network, address operator, uint256 slashedAmount) internal override {
uint256 networkLimit_ = networkLimit(network);
if (networkLimit_ != type(uint256).max) {
_networkLimit[network].push(Time.timestamp(), networkLimit_ - slashedAmount);
}

uint256 operatorNetworkShares_ = operatorNetworkShares(network, operator);
uint256 operatorSlashedShares =
slashedAmount.mulDiv(operatorNetworkShares_, operatorNetworkStake(network, operator), Math.Rounding.Ceil);

_totalOperatorNetworkShares[network].push(
Time.timestamp(), totalOperatorNetworkShares(network) - operatorSlashedShares
if (networkLimit_ != type(uint256).max) {
_insertCheckpoint(_networkLimit[network], Time.timestamp(), networkLimit_ - slashedAmount);
}

_insertCheckpoint(
_totalOperatorNetworkShares[network],
Time.timestamp(),
totalOperatorNetworkShares(network) - operatorSlashedShares
);

_operatorNetworkShares[network][operator].push(Time.timestamp(), operatorNetworkShares_ - operatorSlashedShares);
_insertCheckpoint(
_operatorNetworkShares[network][operator], Time.timestamp(), operatorNetworkShares_ - operatorSlashedShares
);
}

function _initializeInternal(
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/libraries/Checkpoints.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ library Checkpoints {
*/
function upperLookupRecent(Trace256 storage self, uint48 key) internal view returns (uint256) {
uint208 idx = self._trace.upperLookupRecent(key);
return idx != 0 ? self._values[idx] : 0;
return idx > 0 ? self._values[idx] : 0;
}

/**
Expand Down Expand Up @@ -207,7 +207,7 @@ library Checkpoints {
*/
function latest(Trace256 storage self) internal view returns (uint256) {
uint208 idx = self._trace.latest();
return idx != 0 ? self._values[idx] : 0;
return idx > 0 ? self._values[idx] : 0;
}

function latestCheckpoint(Trace256 storage self) internal view returns (bool exists, uint48 _key, uint256 _value) {
Expand Down
5 changes: 2 additions & 3 deletions src/contracts/slasher/BaseSlasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ abstract contract BaseSlasher is Entity, IBaseSlasher {

function _checkOptIns(address network, address operator) internal view {
address vault_ = vault;
uint48 timestamp = IVault(vault_).currentEpoch() != 0
? IVault(vault_).previousEpochStart()
: IVault(vault_).currentEpochStart();
uint48 timestamp =
IVault(vault_).currentEpoch() > 0 ? IVault(vault_).previousEpochStart() : IVault(vault_).currentEpochStart();

if (!IOptInService(NETWORK_VAULT_OPT_IN_SERVICE).wasOptedInAfter(network, vault_, timestamp)) {
revert NetworkNotOptedInVault();
Expand Down
7 changes: 5 additions & 2 deletions src/contracts/slasher/Slasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ contract Slasher is BaseSlasher, ISlasher {
address operator,
uint256 amount
) external onlyNetworkMiddleware(network) returns (uint256) {
if (amount == 0) {
uint256 operateNetworkStake = IBaseDelegator(IVault(vault).delegator()).operatorNetworkStake(network, operator);
if (amount == 0 || operateNetworkStake == 0) {
revert InsufficientSlash();
}

amount = Math.min(amount, IBaseDelegator(IVault(vault).delegator()).operatorNetworkStake(network, operator));
if (amount > operateNetworkStake) {
amount = operateNetworkStake;
}

_checkOptIns(network, operator);

Expand Down
15 changes: 13 additions & 2 deletions src/contracts/slasher/VetoSlasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ contract VetoSlasher is BaseSlasher, AccessControlUpgradeable, IVetoSlasher {
*/
uint256 public resolverSetEpochsDelay;

/**
* @inheritdoc IVetoSlasher
*/
mapping(address resolver => mapping(uint256 slashIndex => bool value)) public hasVetoed;

mapping(address network => mapping(address resolver => Checkpoints.Trace256 shares)) private _resolverShares;

constructor(
Expand Down Expand Up @@ -158,7 +163,7 @@ contract VetoSlasher is BaseSlasher, AccessControlUpgradeable, IVetoSlasher {

slashedAmount -= slashedAmount.mulDiv(request.vetoedShares, SHARES_BASE, Math.Rounding.Ceil);

if (slashedAmount != 0) {
if (slashedAmount > 0) {
_callOnSlash(request.network, request.operator, slashedAmount);
}

Expand Down Expand Up @@ -189,6 +194,12 @@ contract VetoSlasher is BaseSlasher, AccessControlUpgradeable, IVetoSlasher {
revert SlashRequestCompleted();
}

if (hasVetoed[msg.sender][slashIndex]) {
revert AlreadyVetoed();
}

hasVetoed[msg.sender][slashIndex] = true;

uint256 vetoedShares_ = Math.min(request.vetoedShares + resolverShares_, SHARES_BASE);

request.vetoedShares = vetoedShares_;
Expand Down Expand Up @@ -231,7 +242,7 @@ contract VetoSlasher is BaseSlasher, AccessControlUpgradeable, IVetoSlasher {
}

uint48 epochDuration = IVault(vault_).epochDuration();
if (epochDuration != 0 && params.vetoDuration + params.executeDuration > epochDuration) {
if (epochDuration > 0 && params.vetoDuration + params.executeDuration > epochDuration) {
revert InvalidSlashDuration();
}

Expand Down
45 changes: 21 additions & 24 deletions src/contracts/vault/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -201,36 +201,33 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, IVau
revert NotSlasher();
}

if (slashedAmount == 0) {
revert InsufficientSlash();
}
if (slashedAmount > 0) {
uint256 totalSupply_ = totalSupply();
if (slashedAmount > totalSupply_) {
revert TooMuchSlash();
}

uint256 epoch = currentEpoch();
uint256 totalSupply_ = totalSupply();
uint256 epoch = currentEpoch();
uint256 activeSupply_ = activeSupply();
uint256 withdrawals_ = withdrawals[epoch];
uint256 nextWithdrawals = withdrawals[epoch + 1];

if (slashedAmount > totalSupply_) {
revert TooMuchSlash();
}
uint256 nextWithdrawalsSlashed = slashedAmount.mulDiv(nextWithdrawals, totalSupply_);
uint256 withdrawalsSlashed = slashedAmount.mulDiv(withdrawals_, totalSupply_);
uint256 activeSlashed = slashedAmount - nextWithdrawalsSlashed - withdrawalsSlashed;

uint256 activeSupply_ = activeSupply();
uint256 withdrawals_ = withdrawals[epoch];
uint256 nextWithdrawals = withdrawals[epoch + 1];
if (activeSupply_ < activeSlashed) {
withdrawalsSlashed += activeSlashed - activeSupply_;
activeSlashed = activeSupply_;
}

uint256 nextWithdrawalsSlashed = slashedAmount.mulDiv(nextWithdrawals, totalSupply_);
uint256 withdrawalsSlashed = slashedAmount.mulDiv(withdrawals_, totalSupply_);
uint256 activeSlashed = slashedAmount - nextWithdrawalsSlashed - withdrawalsSlashed;
_activeSupplies.push(Time.timestamp(), activeSupply_ - activeSlashed);
withdrawals[epoch] = withdrawals_ - withdrawalsSlashed;
withdrawals[epoch + 1] = nextWithdrawals - nextWithdrawalsSlashed;

if (activeSupply_ < activeSlashed) {
withdrawalsSlashed += activeSlashed - activeSupply_;
activeSlashed = activeSupply_;
ICollateral(collateral).issueDebt(burner, slashedAmount);
}

_activeSupplies.push(Time.timestamp(), activeSupply_ - activeSlashed);
withdrawals[epoch] = withdrawals_ - withdrawalsSlashed;
withdrawals[epoch + 1] = nextWithdrawals - nextWithdrawalsSlashed;

ICollateral(collateral).issueDebt(burner, slashedAmount);

emit OnSlash(msg.sender, slashedAmount);
}

Expand All @@ -242,7 +239,7 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, IVau
revert NotSlasher();
}

if (_nextSlasher.timestamp != 0 && _nextSlasher.timestamp <= Time.timestamp()) {
if (_nextSlasher.timestamp > 0 && _nextSlasher.timestamp <= Time.timestamp()) {
_slasher.address_ = _nextSlasher.address_;
_nextSlasher.timestamp = 0;
_nextSlasher.address_ = address(0);
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/delegator/IBaseDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface IBaseDelegator {
error NotSlasher();
error NotNetwork();
error NotVault();
error TooMuchSlash();

/**
* @notice Base parameters needed for delegators' deployment.
Expand Down
9 changes: 9 additions & 0 deletions src/interfaces/slasher/IVetoSlasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface IVetoSlasher {
error InvalidResolversLength();
error InvalidResolverSetEpochsDelay();
error ResolverAlreadySet();
error AlreadyVetoed();

/**
* @notice Initial parameters needed for a slasher deployment.
Expand Down Expand Up @@ -167,6 +168,14 @@ interface IVetoSlasher {
*/
function resolverShares(address network, address resolver) external view returns (uint256);

/**
* @notice Get if a resolver has vetoed a particular slash request.
* @param resolver address of the resolver
* @param slashIndex index of the slash request
* @return if the resolver has vetoed the slash request
*/
function hasVetoed(address resolver, uint256 slashIndex) external view returns (bool);

/**
* @notice Request a slash using a network and a resolver for a particular operator by a given amount.
* @param network address of the network
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/vault/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ interface IVault is IVaultStorage {
error TooMuchWithdraw();
error InvalidSlasherSetEpochsDelay();
error NotDelegator();
error InsufficientSlash();
error TooMuchSlash();

/**
Expand Down
Loading

0 comments on commit 87bb342

Please sign in to comment.