Skip to content

Commit

Permalink
Donation attack test
Browse files Browse the repository at this point in the history
  • Loading branch information
anajuliabit committed Aug 3, 2024
1 parent 4ece1c3 commit 895ea8e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 57 deletions.
114 changes: 59 additions & 55 deletions test/DelegateStaking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -651,59 +651,63 @@ contract Stake is DelegateStakingTest {
delegate.stake(_keyper, 0);
}

// function test_DonationAttackNoRewards(
// address keyper,
// address bob,
// address alice,
// uint256 bobAmount
// ) public {
// vm.assume(bob != alice);
// rewardsDistributor.removeRewardConfiguration(address(delegate));

// _setKeyper(keyper, true);

// bobAmount = _boundToRealisticStake(bobAmount);

// // alice deposits 1
// _mintGovToken(alice, 1);
// uint256 aliceStakeId = _stake(alice, keyper, 1);

// // simulate donation
// govToken.mint(address(delegate), bobAmount);

// // bob stake
// _mintGovToken(bob, bobAmount);
// uint256 bobStakeId = _stake(bob, keyper, bobAmount);

// _jumpAhead(vm.getBlockTimestamp() + LOCK_PERIOD);

// // alice withdraw rewards (bob stake) even when there is no rewards distributed
// vm.startPrank(alice);
// //delegate.unstake(aliceStakeId, 0);
// delegate.claimRewards(0);
// vm.stopPrank();

// uint256 aliceBalanceAfterAttack = govToken.balanceOf(alice);

// // attack should not be profitable for alice
// assertGtDecimal(
// bobAmount + 1, // amount alice has spend in total
// aliceBalanceAfterAttack,
// 1e18,
// "Alice receive more than expend for the attack"
// );

// vm.startPrank(bob);
// delegate.unstake(bobStakeId, 0);
// delegate.claimRewards(0);

// uint256 bobBalanceAfterAttack = govToken.balanceOf(bob);

// // at the end Alice still earn less than bob
// assertGt(
// bobBalanceAfterAttack,
// aliceBalanceAfterAttack,
// "Alice earn more than Bob after the attack"
// );
// }
function test_DonationAttackNoRewards(
address keyper,
address bob,
address alice,
uint256 bobAmount
) public {
vm.assume(bob != alice);
rewardsDistributor.removeRewardConfiguration(address(delegate));

_setKeyper(keyper, true);

bobAmount = _boundToRealisticStake(bobAmount);

// alice deposits 1
_mintGovToken(alice, 1);
_stake(alice, keyper, 1);

// simulate donation
govToken.mint(address(delegate), bobAmount);

// bob stake
_mintGovToken(bob, bobAmount);
uint256 bobStakeId = _stake(bob, keyper, bobAmount);

_jumpAhead(vm.getBlockTimestamp() + LOCK_PERIOD);

// alice withdraw rewards (bob stake) even when there is no rewards distributed
vm.startPrank(alice);
//delegate.unstake(aliceStakeId, 0);
uint256 aliceRewards = delegate.claimRewards(0);
vm.stopPrank();

uint256 aliceBalanceAfterAttack = govToken.balanceOf(alice);

// attack should not be profitable for alice
assertGtDecimal(
bobAmount + 1, // amount alice has spend in total
aliceBalanceAfterAttack,
1e18,
"Alice receive more than expend for the attack"
);

// as previewWithdraw rounds up, someone needs to stake again to have a dSHU total supply > 1
// so bob can unstake
_mintGovToken(bob, aliceRewards + 10e18);
_stake(bob, keyper, aliceRewards + 10e18);

vm.prank(bob);
delegate.unstake(bobStakeId, 0);

uint256 bobBalanceAfterAttack = govToken.balanceOf(bob);

// Alice earn less than bob
assertGt(
bobBalanceAfterAttack,
aliceBalanceAfterAttack,
"Alice earn more than Bob after the attack"
);
}
}
3 changes: 1 addition & 2 deletions test/Staking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ contract Unstake is StakingTest {
_jumpAhead(_jump);

uint256 unstakeAmount = _amount - MIN_STAKE;
uint256 shares = staking.previewWithdraw(unstakeAmount);
uint256 shares = staking.exposed_previewWithdraw(unstakeAmount);
vm.expectEmit();
emit Staking.Unstaked(_depositor, unstakeAmount, shares);

Expand Down Expand Up @@ -1589,7 +1589,6 @@ contract OwnableFunctions is StakingTest {
);

vm.expectEmit();
emit BaseStaking.NewRewardsDistributor(_newRewardsDistributor);
staking.setRewardsDistributor(_newRewardsDistributor);

assertEq(
Expand Down
6 changes: 6 additions & 0 deletions test/helpers/DelegateStakingHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ contract DelegateStakingHarness is DelegateStaking {
function exposed_nextStakeId() external view returns (uint256) {
return nextStakeId;
}

function exposed_previewWithdraw(
uint256 amount
) external view returns (uint256) {
return _previewWithdraw(amount);
}
}
6 changes: 6 additions & 0 deletions test/helpers/StakingHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ contract StakingHarness is Staking {
) external view virtual returns (uint256) {
return _maxWithdraw(keyper, unlockedAmount);
}

function exposed_previewWithdraw(
uint256 amount
) external view returns (uint256) {
return _previewWithdraw(amount);
}
}

0 comments on commit 895ea8e

Please sign in to comment.