Skip to content

Commit

Permalink
credit horsefacts
Browse files Browse the repository at this point in the history
  • Loading branch information
wildmolasses committed Feb 22, 2024
1 parent 9ace232 commit 28f31ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
3 changes: 3 additions & 0 deletions test/helpers/AddressSet.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.23;

// AddressSet.sol comes from
// https://github.com/horsefacts/weth-invariant-testing/blob/973156bc9b6684f0cf62de19e9bb4c5c27a41bb2/test/helpers/AddressSet.sol

struct AddressSet {
address[] addrs;
mapping(address => bool) saved;
Expand Down
38 changes: 19 additions & 19 deletions test/helpers/UniStaker.handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract UniStakerHandler is CommonBase, StdCheats, StdUtils {
address public admin;

// actors, deposit state
address internal currentActor;
address internal _currentActor;
AddressSet internal _depositors;
AddressSet internal _delegates;
AddressSet internal _beneficiaries;
Expand Down Expand Up @@ -80,11 +80,11 @@ contract UniStakerHandler is CommonBase, StdCheats, StdUtils {
doCheckpoints
{
_useActor(_rewardNotifiers, _actorSeed);
vm.assume(currentActor != address(0));
vm.assume(_currentActor != address(0));
_amount = bound(_amount, 0, 100_000_000e18);
ghost_prevRewardPerTokenAccumulatedCheckpoint = uniStaker.rewardPerTokenAccumulatedCheckpoint();
_mintRewardToken(currentActor, _amount);
vm.startPrank(currentActor);
_mintRewardToken(_currentActor, _amount);
vm.startPrank(_currentActor);
rewardToken.transfer(address(uniStaker), _amount);
uniStaker.notifyRewardAmount(_amount);
vm.stopPrank();
Expand All @@ -105,15 +105,15 @@ contract UniStakerHandler is CommonBase, StdCheats, StdUtils {
_amount = bound(_amount, 0, 100_000_000e18);

// assume user has stake amount
_mintStakeToken(currentActor, _amount);
_mintStakeToken(_currentActor, _amount);

vm.startPrank(currentActor);
vm.startPrank(_currentActor);
stakeToken.approve(address(uniStaker), _amount);
uniStaker.stake(_amount, _delegatee, _beneficiary);
vm.stopPrank();

// update handler state
_depositIds[currentActor].push(ghost_depositCount);
_depositIds[_currentActor].push(ghost_depositCount);
ghost_depositCount++;
_surrogates.add(address(uniStaker.surrogates(_delegatee)));
ghost_stakeSum += _amount;
Expand All @@ -125,13 +125,13 @@ contract UniStakerHandler is CommonBase, StdCheats, StdUtils {
doCheckpoints
{
_useActor(_depositors, _actorSeed);
vm.assume(currentActor != address(0));
vm.assume(_depositIds[currentActor].length > 0);
vm.assume(_currentActor != address(0));
vm.assume(_depositIds[_currentActor].length > 0);
UniStaker.DepositIdentifier _depositId =
UniStaker.DepositIdentifier.wrap(_getActorRandDepositId(_actorDepositSeed));
(uint256 _balance,,,) = uniStaker.deposits(_depositId);
_amount = bound(_amount, 0, _balance);
vm.startPrank(currentActor);
vm.startPrank(_currentActor);
stakeToken.approve(address(uniStaker), _amount);
uniStaker.stakeMore(_depositId, _amount);
vm.stopPrank();
Expand All @@ -145,22 +145,22 @@ contract UniStakerHandler is CommonBase, StdCheats, StdUtils {
doCheckpoints
{
_useActor(_depositors, _actorSeed);
vm.assume(currentActor != address(0));
vm.assume(_depositIds[currentActor].length > 0);
vm.assume(_currentActor != address(0));
vm.assume(_depositIds[_currentActor].length > 0);
UniStaker.DepositIdentifier _depositId =
UniStaker.DepositIdentifier.wrap(_getActorRandDepositId(_actorDepositSeed));
(uint256 _balance,,,) = uniStaker.deposits(_depositId);
_amount = bound(_amount, 0, _balance);
vm.startPrank(currentActor);
vm.startPrank(_currentActor);
uniStaker.withdraw(_depositId, _amount);
vm.stopPrank();
ghost_stakeWithdrawn += _amount;
}

function claimReward(uint256 _actorSeed) public countCall("claimReward") doCheckpoints {
_useActor(_beneficiaries, _actorSeed);
vm.startPrank(currentActor);
uint256 rewardsClaimed = uniStaker.unclaimedRewardCheckpoint(currentActor);
vm.startPrank(_currentActor);
uint256 rewardsClaimed = uniStaker.unclaimedRewardCheckpoint(_currentActor);
uniStaker.claimReward();
vm.stopPrank();
ghost_rewardsClaimed += rewardsClaimed;
Expand All @@ -172,18 +172,18 @@ contract UniStakerHandler is CommonBase, StdCheats, StdUtils {
}

function _getActorRandDepositId(uint256 _randomDepositSeed) internal view returns (uint256) {
return _depositIds[currentActor][_randomDepositSeed % _depositIds[currentActor].length];
return _depositIds[_currentActor][_randomDepositSeed % _depositIds[_currentActor].length];
}

function _createDepositor() internal {
currentActor = msg.sender;
_currentActor = msg.sender;
// Surrogates can't stake. We won't include them as potential depositors.
vm.assume(!_surrogates.contains(currentActor));
vm.assume(!_surrogates.contains(_currentActor));
_depositors.add(msg.sender);
}

function _useActor(AddressSet storage _set, uint256 _randomActorSeed) internal {
currentActor = _set.rand(_randomActorSeed);
_currentActor = _set.rand(_randomActorSeed);
}

function reduceDepositors(uint256 acc, function(uint256,address) external returns (uint256) func)
Expand Down

0 comments on commit 28f31ea

Please sign in to comment.