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 to increase test coverage #118

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

garyghayrat
Copy link
Collaborator

@garyghayrat garyghayrat commented Feb 11, 2025

closes #117

Before:
Pasted image 20250211112035

After:
Pasted image 20250211121723

The uncovered lines and statements in StakerPermitAndStake.sol seems to be due to the try/catch block in

  try IERC20Permit(address(STAKE_TOKEN)).permit(
      msg.sender, address(this), _amount, _deadline, _v, _r, _s
    ) {} catch {}

which we already test with valid and invalid signatures.

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.

Hey @garyghayrat, have not done a full review, but just wanted to address your comment about the remaining untested blocks:

The uncovered lines and statements in StakerPermitAndStake.sol seems to be due to the try/catch block in which we already test with valid and invalid signatures.

This is not quite right! The point of the Try/Catch is to prevent a griefing attack where someone pulls out the signature and executes the permit directly ahead of the actual permitAnd function call. This would cause the original transaction to fail, even though the allowance had been created, which is why we include the try/catch blocks—with them included, the transaction would proceed despite the fact the permit call was frontrun.

To test this, you should create a test that simulates this, i.e. generate the signature, the submit it directly on the token contract first before calling the permitAnd function. The test actually wouldn't need an assert, since we're just ensure the call does not revert.

Copy link

Coverage after merging chore/increase-coverage into main will be

99.54%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
src
   DelegationSurrogate.sol100%100%100%100%
   DelegationSurrogateVotes.sol100%100%100%100%
   Staker.sol100%100%100%100%
src/calculators
   BinaryEligibilityOracleEarningPowerCalculator.sol100%100%100%100%
   IdentityEarningPowerCalculator.sol100%100%100%100%
src/extensions
   StakerCapDeposits.sol100%100%100%100%
   StakerDelegateSurrogateVotes.sol100%100%100%100%
   StakerOnBehalf.sol100%100%100%100%
   StakerPermitAndStake.sol88.24%100%100%84.62%50, 78
src/notifiers
   MintRewardNotifier.sol100%100%100%100%
   RewardTokenNotifierBase.sol100%100%100%100%
   TransferFromRewardNotifier.sol100%100%100%100%
   TransferRewardNotifier.sol100%100%100%100%

@garyghayrat
Copy link
Collaborator Author

Hey @garyghayrat, have not done a full review, but just wanted to address your comment about the remaining untested blocks:

The uncovered lines and statements in StakerPermitAndStake.sol seems to be due to the try/catch block in which we already test with valid and invalid signatures.

This is not quite right! The point of the Try/Catch is to prevent a griefing attack where someone pulls out the signature and executes the permit directly ahead of the actual permitAnd function call. This would cause the original transaction to fail, even though the allowance had been created, which is why we include the try/catch blocks—with them included, the transaction would proceed despite the fact the permit call was frontrun.

To test this, you should create a test that simulates this, i.e. generate the signature, the submit it directly on the token contract first before calling the permitAnd function. The test actually wouldn't need an assert, since we're just ensure the call does not revert.

That's good to know, and I've added the test cases you mentioned in 2eaa2b5. But I meant the coverage report summary shows that there are 4 statements (I assume the 2 try catch blocks) are not covered in StakerPermitAndStake.sol. And I've tried using a token contract that doesn't have the permit function so permitAndStake reverts and the catch block is run, but the test coverage report is still showing the try/catch lines as uncovered.

@garyghayrat garyghayrat requested a review from apbendi February 14, 2025 20:15
@apbendi
Copy link
Collaborator

apbendi commented Feb 19, 2025

Hey @garyghayrat we have these same try/catch blocks in UniStaker and the lines are covered there. I'd suggest testing out the test suite there to see how it's done.

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.

This looks good @garyghayrat. Given we have to wait for Foundry to fix foundry-rs/foundry#9921 before we can get up to 100, let's create an issue to track that work so we remember to come back to it. Otherwise, this is good to merge IMO!

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/update Unit Tests to get Test Coverage Back to 100%
2 participants