Skip to content

Commit

Permalink
Merge pull request #37 from symbioticfi/polish
Browse files Browse the repository at this point in the history
Cosmetics
  • Loading branch information
1kresh authored Sep 10, 2024
2 parents 8333f3e + 1d7ead8 commit 8b1177d
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 21 deletions.
13 changes: 11 additions & 2 deletions src/contracts/slasher/BaseSlasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ abstract contract BaseSlasher is Entity, StaticDelegateCallable, ReentrancyGuard
slashableStakeHints = abi.decode(hints, (SlashableStakeHints));
}

if (captureTimestamp < Time.timestamp() - IVault(vault).epochDuration() || captureTimestamp >= Time.timestamp())
{
if (
captureTimestamp < Time.timestamp() - IVault(vault).epochDuration() || captureTimestamp >= Time.timestamp()
|| captureTimestamp < latestSlashedCaptureTimestamp[subnetwork]
) {
return 0;
}

uint256 stakeAmount = IBaseDelegator(IVault(vault).delegator()).stakeAt(
subnetwork, operator, captureTimestamp, slashableStakeHints.stakeHints
);
Expand All @@ -123,6 +126,12 @@ abstract contract BaseSlasher is Entity, StaticDelegateCallable, ReentrancyGuard
}
}

function _updateLatestSlashedCaptureTimestamp(bytes32 subnetwork, uint48 captureTimestamp) internal {
if (latestSlashedCaptureTimestamp[subnetwork] < captureTimestamp) {
latestSlashedCaptureTimestamp[subnetwork] = captureTimestamp;
}
}

function _updateCumulativeSlash(bytes32 subnetwork, address operator, uint256 amount) internal {
_cumulativeSlash[subnetwork][operator].push(Time.timestamp(), cumulativeSlash(subnetwork, operator) + amount);
}
Expand Down
6 changes: 1 addition & 5 deletions src/contracts/slasher/Slasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@ contract Slasher is BaseSlasher, ISlasher {
revert InvalidCaptureTimestamp();
}

_checkLatestSlashedCaptureTimestamp(subnetwork, captureTimestamp);

slashedAmount =
Math.min(amount, slashableStake(subnetwork, operator, captureTimestamp, slashHints.slashableStakeHints));
if (slashedAmount == 0) {
revert InsufficientSlash();
}

if (latestSlashedCaptureTimestamp[subnetwork] < captureTimestamp) {
latestSlashedCaptureTimestamp[subnetwork] = captureTimestamp;
}
_updateLatestSlashedCaptureTimestamp(subnetwork, captureTimestamp);

_updateCumulativeSlash(subnetwork, operator, slashedAmount);

Expand Down
6 changes: 1 addition & 5 deletions src/contracts/slasher/VetoSlasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ contract VetoSlasher is BaseSlasher, IVetoSlasher {
revert InvalidCaptureTimestamp();
}

_checkLatestSlashedCaptureTimestamp(subnetwork, captureTimestamp);

amount = Math.min(
amount, slashableStake(subnetwork, operator, captureTimestamp, requestSlashHints.slashableStakeHints)
);
Expand Down Expand Up @@ -164,9 +162,7 @@ contract VetoSlasher is BaseSlasher, IVetoSlasher {

request.completed = true;

if (latestSlashedCaptureTimestamp[request.subnetwork] < request.captureTimestamp) {
latestSlashedCaptureTimestamp[request.subnetwork] = request.captureTimestamp;
}
_updateLatestSlashedCaptureTimestamp(request.subnetwork, request.captureTimestamp);

slashedAmount = Math.min(
request.amount,
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 @@ -194,7 +194,7 @@ interface IVetoSlasher {
* @param captureTimestamp time point when the stake was captured
* @param hints hints for checkpoints' indexes
* @return slashIndex index of the slash request
* @dev Only network middleware can call this function.
* @dev Only a network middleware can call this function.
*/
function requestSlash(
bytes32 subnetwork,
Expand All @@ -209,7 +209,7 @@ interface IVetoSlasher {
* @param slashIndex index of the slash request
* @param hints hints for checkpoints' indexes
* @return slashedAmount amount of the collateral slashed
* @dev Anyone can call this function.
* @dev Only a network middleware can call this function.
*/
function executeSlash(uint256 slashIndex, bytes calldata hints) external returns (uint256 slashedAmount);

Expand Down
11 changes: 8 additions & 3 deletions test/slasher/Slasher.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ contract SlasherTest is Test {
slasher.slashableStake(network.subnetwork(0), alice, uint48(blockTimestamp - 1), ""),
delegator.stakeAt(network.subnetwork(0), alice, uint48(blockTimestamp - 1), "")
);
assertEq(slasher.latestSlashedCaptureTimestamp(network.subnetwork(0)), 0);

assertEq(
Math.min(slashAmount1, delegator.stakeAt(network.subnetwork(0), alice, uint48(blockTimestamp - 1), "")),
_slash(alice, network, alice, slashAmount1, uint48(blockTimestamp - 1), "")
);

assertEq(slasher.latestSlashedCaptureTimestamp(network.subnetwork(0)), uint48(blockTimestamp - 1));
assertEq(slasher.cumulativeSlashAt(alice.subnetwork(0), alice, uint48(blockTimestamp - 1), ""), 0);
assertEq(
slasher.cumulativeSlashAt(alice.subnetwork(0), alice, uint48(blockTimestamp), ""),
Expand All @@ -245,6 +247,7 @@ contract SlasherTest is Test {
slasher.cumulativeSlash(alice.subnetwork(0), alice),
Math.min(slashAmount1, delegator.stakeAt(network.subnetwork(0), alice, uint48(blockTimestamp - 1), ""))
);
assertEq(slasher.slashableStake(network.subnetwork(0), alice, uint48(blockTimestamp - 2), ""), 0);
assertEq(
slasher.slashableStake(network.subnetwork(0), alice, uint48(blockTimestamp - 1), ""),
delegator.stakeAt(network.subnetwork(0), alice, uint48(blockTimestamp - 1), "")
Expand All @@ -263,6 +266,7 @@ contract SlasherTest is Test {
_slash(alice, network, bob, slashAmount2, uint48(blockTimestamp - 1), "")
);

assertEq(slasher.latestSlashedCaptureTimestamp(network.subnetwork(0)), uint48(blockTimestamp - 1));
assertEq(slasher.cumulativeSlashAt(alice.subnetwork(0), bob, uint48(blockTimestamp - 1), ""), 0);
assertEq(
slasher.cumulativeSlashAt(alice.subnetwork(0), bob, uint48(blockTimestamp), ""),
Expand All @@ -289,6 +293,7 @@ contract SlasherTest is Test {
vm.assume(slashAmountReal3 > 0);
assertEq(slashAmountReal3, _slash(alice, network, alice, slashAmount3, uint48(blockTimestamp - 2), ""));

assertEq(slasher.latestSlashedCaptureTimestamp(network.subnetwork(0)), uint48(blockTimestamp - 2));
assertEq(slasher.cumulativeSlashAt(alice.subnetwork(0), alice, uint48(blockTimestamp - 2), ""), 0);
assertEq(
slasher.cumulativeSlashAt(alice.subnetwork(0), alice, uint48(blockTimestamp - 1), ""),
Expand Down Expand Up @@ -600,7 +605,7 @@ contract SlasherTest is Test {
);
}

function test_SlashRevertOutdatedCaptureTimestamp(
function test_SlashRevertInsufficientSlash1(
uint48 epochDuration,
uint256 depositAmount,
uint256 networkLimit,
Expand Down Expand Up @@ -650,7 +655,7 @@ contract SlasherTest is Test {

_slash(alice, network, alice, slashAmount1, uint48(blockTimestamp - 1), "");

vm.expectRevert(IBaseSlasher.OutdatedCaptureTimestamp.selector);
vm.expectRevert(ISlasher.InsufficientSlash.selector);
_slash(alice, network, bob, slashAmount2, uint48(blockTimestamp - 2), "");
}

Expand Down Expand Up @@ -701,7 +706,7 @@ contract SlasherTest is Test {
_slash(bob, network, alice, slashAmount1, uint48(blockTimestamp - 1), "");
}

function test_SlashRevertInsufficientSlash(
function test_SlashRevertInsufficientSlash2(
uint48 epochDuration,
uint256 depositAmount,
uint256 networkLimit,
Expand Down
Loading

0 comments on commit 8b1177d

Please sign in to comment.