Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Tests for Staker-Calculator Interactions #75

Merged
merged 1 commit into from
Nov 27, 2024

Conversation

mikeghen
Copy link
Collaborator

@mikeghen mikeghen commented Nov 1, 2024

Resolves: #27

@mikeghen mikeghen marked this pull request as draft November 1, 2024 21:03
@@ -4164,6 +4166,142 @@ contract BumpEarningPower is GovernanceStakerRewardsTest {
}
}

contract EarningPowerCalculation is GovernanceStakerRewardsTest {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test contract names should match the specific method that is being called so scopelint spec can render things correctly

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, all of these stake tests should go under that test contract

contract MockConfigurableEarningPowerCalculator is IEarningPowerCalculator {
// Multiplier applied to all earning power calculations (in basis points)
// 10000 = 100% (no change), 5000 = 50%, 20000 = 200% etc
uint256 public multiplier = 10_000;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: might be good to name this something like multiplierBips or scaleBips i.e. for Basis Points, which should help make it clearer it's a 10K denominator scale.

@mikeghen mikeghen force-pushed the calculator-tests branch 3 times, most recently from 415b390 to 33a0a89 Compare November 12, 2024 16:26
@mikeghen mikeghen marked this pull request as ready for review November 12, 2024 16:33
Copy link
Collaborator

@apbendi apbendi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some thoughts @mikeghen

@@ -789,6 +795,95 @@ contract Stake is GovernanceStakerTest {
vm.expectRevert(GovernanceStaker.GovernanceStaker__InvalidAddress.selector);
govStaker.stake(_amount, _delegatee, address(0));
}

function testFuzz_StakeWithReducedEarningPower(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test names need to be improved to provide a better explanation of what part of the spec is being defined & tested here. See how these read compared to other Stake tests in the output of scopelint spec.

Screen Shot 2024-11-12 at 12 55 10 PM

So for this test, I would say something more like, "Sets the appropriate earning power when the calculator scales down the amount staked"

) public {
vm.assume(_depositor != address(0));
_stakeAmount = _boundMintAmount(_stakeAmount);
_multiplierBips = bound(_multiplierBips, 1, 20_000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name says "reduced earning power" but the multiplier bound could be > 100%, no? Similarly, we could include 0 here, couldn't we?

) public {
vm.assume(_depositor != address(0));
_stakeAmount = _boundMintAmount(_stakeAmount);
_multiplierBips = bound(_multiplierBips, 1, 20_000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, the test says "boosted" but the multiplier could be < 100%


import {IEarningPowerCalculator} from "src/GovernanceStaker.sol";

contract MockConfigurableEarningPowerCalculator is IEarningPowerCalculator {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we really needed a new mock here. I think we could add the multiplier bips override to the existing mock (and rename it). Curious if you agree @alexkeating

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted to add this into the existing mock in favor of having less code overall.

@@ -15,12 +15,15 @@ import {ERC20VotesMock, ERC20Permit} from "test/mocks/MockERC20Votes.sol";
import {IERC20Errors} from "openzeppelin/interfaces/draft-IERC6093.sol";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arbitrary comment location

I think there a lot more tests we could write for this, including ones that show the earning power is updated when the user takes various other actions, such as stakeMore, withdraw, alter*, claimReward, etc....

We don't necessarily have to do them in this PR, but let's make sure we don't close out the issue if we don't, and add some notes to the issue of additional tests we should handle.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apbendi, I went and added simple tests to the contracts that test those methods. I figured I could just do it so we could close this issue out. Lmk if there were other contracts to be added for the etc., it wasn't obvious to me where else to add these scaled earning calculator tests to.

@mikeghen mikeghen force-pushed the calculator-tests branch 2 times, most recently from eacf025 to 93473a2 Compare November 14, 2024 00:21
This commit introduces a mock calculator with adjustable earning power to expand test coverage, validating staker responses to varied earning scenarios such as scaled and boosted staking amounts.
Copy link

Coverage after merging calculator-tests into main will be

99.29%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
src
   BinaryEligibilityOracleEarningPowerCalculator.sol100%100%100%100%
   DelegationSurrogate.sol100%100%100%100%
   DelegationSurrogateVotes.sol100%100%100%100%
   GovernanceStaker.sol98.66%100%97.22%98.84%258–259
src/extensions
   GovernanceStakerDelegateSurrogateVotes.sol100%100%100%100%
   GovernanceStakerOnBehalf.sol100%100%100%100%
   GovernanceStakerPermitAndStake.sol100%100%100%100%

@alexkeating alexkeating merged commit a1d4835 into main Nov 27, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add much more extensive unit testing for how the staker contract interacts with the calculator
3 participants