Skip to content

Commit

Permalink
Add checkpointing in missing methods (#88)
Browse files Browse the repository at this point in the history
* Add checkpointing in alterDelegatee and alterClaimer
  • Loading branch information
alexkeating authored Dec 13, 2024
1 parent 3a9ad65 commit 334b9fb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/GovernanceStaker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,9 @@ abstract contract GovernanceStaker is INotifiableRewardReceiver, Multicall {
address _newDelegatee
) internal virtual {
_revertIfAddressZero(_newDelegatee);
_checkpointGlobalReward();
_checkpointReward(deposit);

DelegationSurrogate _oldSurrogate = surrogates(deposit.delegatee);
uint256 _newEarningPower =
earningPowerCalculator.getEarningPower(deposit.balance, deposit.owner, _newDelegatee);
Expand All @@ -653,6 +656,8 @@ abstract contract GovernanceStaker is INotifiableRewardReceiver, Multicall {
virtual
{
_revertIfAddressZero(_newClaimer);
_checkpointGlobalReward();
_checkpointReward(deposit);

// Updating the earning power here is not strictly necessary, but if the user is touching their
// deposit anyway, it seems reasonable to make sure their earning power is up to date.
Expand Down
50 changes: 50 additions & 0 deletions test/GovernanceStaker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,56 @@ contract NotifyRewardAmount is GovernanceStakerRewardsTest {
assertLe(govStaker.scaledRewardRate(), _expectedRewardRate);
}

function test_SharesArentManipulatedIfRewardsCheckpointed() public {
earningPowerCalculator.__setEarningPowerForDelegatee(address(0x1), 0);
earningPowerCalculator.__setEarningPowerForDelegatee(address(0x2), 500e18);

address _doe = makeAddr("doe");
_mintGovToken(_doe, 500e18);
_stake(_doe, 500e18, address(0x2));

address _fox = makeAddr("fox");

// fox deposits with full earning power
_mintGovToken(_fox, 500e18);
_stake(_fox, 500e18, address(0x2));

// some rewards are sent
rewardToken.mint(rewardNotifier, 1_000_000e18);
// The contract is notified of a reward
vm.startPrank(rewardNotifier);
rewardToken.transfer(address(govStaker), 1_000_000e18);
govStaker.notifyRewardAmount(1_000_000e18);
vm.stopPrank();

// some time passes and fox becomes eligible
_jumpAheadByPercentOfRewardDuration(101);

/*
* Begin manipulation attempt
*/

// fox alters delegatee
vm.prank(_fox);
govStaker.alterDelegatee(GovernanceStaker.DepositIdentifier.wrap(1), address(0x2));

// fox checkpoints global rewards
_mintGovToken(_fox, 0);
_stake(_fox, 0, address(0x1));

// fox alters back to valid delegatee
vm.prank(_fox);
govStaker.alterDelegatee(GovernanceStaker.DepositIdentifier.wrap(1), address(0x2));

// fox claims double the rewards
vm.prank(_fox);
govStaker.claimReward(GovernanceStaker.DepositIdentifier.wrap(1));

vm.prank(_doe);
govStaker.claimReward(GovernanceStaker.DepositIdentifier.wrap(0));
assertEq(rewardToken.balanceOf(_doe), rewardToken.balanceOf(_fox));
}

function testFuzz_EmitsAnEventWhenRewardsAreNotified(uint256 _amount) public {
_amount = _boundToRealisticReward(_amount);
rewardToken.mint(rewardNotifier, _amount);
Expand Down
28 changes: 14 additions & 14 deletions test/gas-reports/gov-staker-gas-report.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
{
"generatedAt": 1730917215,
"generatedAt": 1734023383,
"reportName:": "gov-stakerGasReport",
"results": [
{
"scenarioName": "First stake to a new delegatee",
"gasUsed": 326097
"gasUsed": 330405
},
{
"scenarioName": "Second stake to a existing delegatee",
"gasUsed": 159815
"gasUsed": 164123
},
{
"scenarioName": "Second stake to a new delegatee",
"gasUsed": 291897
"gasUsed": 296205
},
{
"scenarioName": "Stake more after initial stake",
"gasUsed": 97307
"gasUsed": 101615
},
{
"scenarioName": "Alter delegatee with new delegatee",
"gasUsed": 198482
"gasUsed": 218503
},
{
"scenarioName": "Alter delegatee with existing delegatee",
"gasUsed": 66376
"gasUsed": 86397
},
{
"scenarioName": "Alter claimer to a new address",
"gasUsed": 49455
"gasUsed": 69468
},
{
"scenarioName": "Withdraw full stake",
"gasUsed": 106298
"gasUsed": 110594
},
{
"scenarioName": "Withdraw partial stake",
"gasUsed": 120698
"gasUsed": 124994
},
{
"scenarioName": "Claim reward when no reward",
"gasUsed": 46021
},
{
"scenarioName": "Claim reward when reward",
"gasUsed": 160600
"gasUsed": 164908
},
{
"scenarioName": "Notify reward",
Expand All @@ -56,15 +56,15 @@
},
{
"scenarioName": "Bump earning power up no reward",
"gasUsed": 80175
"gasUsed": 80178
},
{
"scenarioName": "Bump earning power down with reward",
"gasUsed": 108919
"gasUsed": 108922
},
{
"scenarioName": "Bump earning power up with reward",
"gasUsed": 108919
"gasUsed": 108922
}
]
}

0 comments on commit 334b9fb

Please sign in to comment.