Skip to content

Commit

Permalink
remove the unnecessary var totalSlashedStakes
Browse files Browse the repository at this point in the history
  • Loading branch information
spengrah committed May 13, 2023
1 parent d2646f5 commit b0fbbc7
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 33 deletions.
14 changes: 2 additions & 12 deletions src/StakingEligibility.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,6 @@ contract StakingEligibility is HatsEligibilityModule {
/// @notice Current unstaking cooldown periods
mapping(address staker => Cooldown cooldown) public cooldowns;

/// @notice The sum of all valid stakes
uint248 public totalValidStakes;

/// @notice The sum of all slashed stakes that have not yet been withdrawn to a wearer of the `recipientHat`
uint248 public totalSlashedStakes;

Expand Down Expand Up @@ -227,9 +224,8 @@ contract StakingEligibility is HatsEligibilityModule {
// staker must have not been slashed
if (stk.slashed) revert StakingEligibility_AlreadySlashed();

// increment the staker's stake and total valid stakes
// increment the staker's stake
stk.amount += _amount;
totalValidStakes += _amount;

// execute the stake and log it, reverting if the transfer fails
bool success = TOKEN().transferFrom(msg.sender, address(this), uint256(_amount));
Expand Down Expand Up @@ -266,8 +262,6 @@ contract StakingEligibility is HatsEligibilityModule {
unchecked {
// should not underflow given the InsufficientStake check above
stk.amount -= _amount;
// should not underflow since totalValidStakes is always >= stk.amount
totalValidStakes -= _amount;
}

// log the unstake initiation
Expand Down Expand Up @@ -343,11 +337,7 @@ contract StakingEligibility is HatsEligibilityModule {
stk.amount = 0;
cooldown.amount = 0;
cooldown.endsAt = 0;
// decrement the total valid stakes by the staked amount (the cooldown amount is already subtracted)
unchecked {
// should not underflow since totalValidStakes is always >= stk.amount
totalValidStakes -= stakedAmount;
}

// increment the total slashed stakes by the total amount to slash
totalSlashedStakes += toSlash;

Expand Down
21 changes: 0 additions & 21 deletions test/StakingEligibility.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,9 @@ contract WithInstanceTest is StakingEligibilityTest {
// change the stakerHat's eligibility to instance
vm.prank(dao);
hats.changeHatEligibility(stakerHat, address(instance));

// mint some tokens to the stakers

deal(token, staker2, 100_000);
}

function recordPrelimValues(address _staker) public {
totalStaked = instance.totalValidStakes();
totalSlashed = instance.totalSlashedStakes();
(stakerStake, stakerSlashed) = instance.stakes(_staker);
stakerBalance = IERC20(token).balanceOf(_staker);
Expand All @@ -169,7 +164,6 @@ contract WithInstanceTest is StakingEligibilityTest {

function stateAssertions(address _staker) public {
// global state assertions
assertEq(instance.totalValidStakes(), expTotalStaked, "totalStaked");
assertEq(instance.totalSlashedStakes(), expTotalSlashed, "totalSlashedStakes");

// staker stake assertions
Expand Down Expand Up @@ -280,7 +274,6 @@ contract SetUp is WithInstanceTest {
}

function test_otherStateVars_areEmpty() public {
assertEq(instance.totalValidStakes(), 0, "totalStaked");
assertEq(instance.totalSlashedStakes(), 0, "totalSlashedStakes");
}

Expand Down Expand Up @@ -384,15 +377,13 @@ contract Staking is WithInstanceTest {
// set expected post values
if (stakerSlashed || !_approved) {
// expect no change
expTotalStaked = totalStaked;
expTotalSlashed = totalSlashed;
expStakerStake = stakerStake;
expStakerSlashed = stakerSlashed;
expStakerBalance = uint256(stakerBalance);
expInstanceBalance = uint256(instanceBalance);
} else {
// expect stake vars to change
expTotalStaked = totalStaked + _amount;
expTotalSlashed = totalSlashed;
expStakerStake = stakerStake + _amount;
expStakerSlashed = stakerSlashed;
Expand Down Expand Up @@ -546,7 +537,6 @@ contract Unstaking is WithInstanceTest {
// set expected post values
if (!_sufficient) {
// expect no change
expTotalStaked = totalStaked;
expTotalSlashed = totalSlashed;
expStakerStake = stakerStake;
expStakerSlashed = stakerSlashed;
Expand All @@ -556,7 +546,6 @@ contract Unstaking is WithInstanceTest {
expStakerCooldownEnd = 0;
} else {
// expect stake vars to change
expTotalStaked = totalStaked - _amount;
expTotalSlashed = totalSlashed;
expStakerStake = stakerStake - _amount;
expStakerSlashed = stakerSlashed;
Expand Down Expand Up @@ -592,7 +581,6 @@ contract Unstaking is WithInstanceTest {
// set expected post values
if (stakerSlashed || !unstaking || !cooldownEnded) {
// expect no change
expTotalStaked = totalStaked;
expTotalSlashed = totalSlashed;
expStakerStake = stakerStake;
expStakerSlashed = stakerSlashed;
Expand All @@ -602,7 +590,6 @@ contract Unstaking is WithInstanceTest {
expStakerCooldownEnd = stakerCooldownEnd;
} else {
// expect only cooldown and balance vars to change
expTotalStaked = totalStaked;
expTotalSlashed = totalSlashed;
expStakerStake = stakerStake;
expStakerSlashed = stakerSlashed;
Expand Down Expand Up @@ -780,7 +767,6 @@ contract Slashing is WithInstanceTest {
// set expected post values
if (!_judge) {
// expect no change
expTotalStaked = totalStaked;
expTotalSlashed = totalSlashed;
expStakerStake = stakerStake;
expStakerSlashed = stakerSlashed;
Expand All @@ -790,7 +776,6 @@ contract Slashing is WithInstanceTest {
expStakerCooldownEnd = stakerCooldownEnd;
} else {
// expect stake, cooldown, and slashed vars to change
expTotalStaked = totalStaked - stakerStake;
expTotalSlashed = totalSlashed + stakerStake + stakerUnstakeAmount;
expStakerStake = stakerStake - stakerStake;
expStakerSlashed = true;
Expand Down Expand Up @@ -886,15 +871,13 @@ contract Forgiving is WithInstanceTest {
// set expected post values
if (!_judge) {
// expect no change
expTotalStaked = totalStaked;
expTotalSlashed = totalSlashed;
expStakerStake = stakerStake;
expStakerSlashed = stakerSlashed;
expStakerBalance = stakerBalance;
expInstanceBalance = instanceBalance;
} else {
// expect stake and slashed vars to change
expTotalStaked = totalStaked;
expTotalSlashed = totalSlashed;
expStakerStake = stakerStake;
expStakerSlashed = false;
Expand Down Expand Up @@ -953,7 +936,6 @@ contract Forgiving is WithInstanceTest {

contract Withdrawing is WithInstanceTest {
function withdrawTest(address _to, bool _recipient, bool _somethingToWithdraw) public {
totalStaked = instance.totalValidStakes();
totalSlashed = instance.totalSlashedStakes();
instanceBalance = IERC20(token).balanceOf(address(instance));

Expand All @@ -972,17 +954,14 @@ contract Withdrawing is WithInstanceTest {
// set expected post values
if (!_recipient) {
// expect no change
expTotalStaked = totalStaked;
expTotalSlashed = totalSlashed;
expInstanceBalance = instanceBalance;
} else {
// expect slashed vars to change
expTotalStaked = totalStaked;
expTotalSlashed = 0;
expInstanceBalance = instanceBalance - totalSlashed;
}

assertEq(instance.totalValidStakes(), expTotalStaked, "totalStaked");
assertEq(instance.totalSlashedStakes(), expTotalSlashed, "totalSlashedStakes");
assertEq(IERC20(token).balanceOf(address(instance)), expInstanceBalance, "instanceBalance");
}
Expand Down

0 comments on commit b0fbbc7

Please sign in to comment.