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 ReentrancyGuard status getter #3714

Merged
merged 15 commits into from
Oct 17, 2022

Conversation

zhiqiangxu
Copy link
Contributor

This PR adds a entered modifier to ReentrancyGuard contract, it's useful for callbacks.

@Amxx
Copy link
Collaborator

Amxx commented Sep 22, 2022

Hello @zhiqiangxu

I'm not sure about this one. If multiple functions in the contract are nonReentrant, you don't know which one was you are entering from. You are also not sure what path you are entering trough, and if you are not entering twice (either one after the other or one within the other).

Can you give us more details about your usecase?

@zhiqiangxu
Copy link
Contributor Author

Hello @zhiqiangxu

I'm not sure about this one. If multiple functions in the contract are nonReentrant, you don't know which one was you are entering from. You are also not sure what path you are entering trough, and if you are not entering twice (either one after the other or one within the other).

Can you give us more details about your usecase?

Your concern makes sense, I now prefer just exposing the guard status and let users decide his own logic?

@frangio
Copy link
Contributor

frangio commented Sep 23, 2022

I'm ok with exposing the guard status but we shouldn't make this function virtual.

@zhiqiangxu
Copy link
Contributor Author

I'm ok with exposing the guard status but we shouldn't make this function virtual.

Yeah internal is enough.

@frangio
Copy link
Contributor

frangio commented Sep 24, 2022

Coverage wants you to add a test for the new getter.

/**
* expose _status as internal function, the caller decides what to do with it
*/
function _guardStatus() internal view returns (uint256) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's call this _reentrancyGuardStatus?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@frangio frangio changed the title add entered modifier Add _reentrancyGuardStatus getter Sep 24, 2022
@frangio frangio changed the title Add _reentrancyGuardStatus getter Add ReentrancyGuard status getter Sep 24, 2022
@zhiqiangxu
Copy link
Contributor Author

Coverage wants you to add a test for the new getter.

Added a check in an existing nonReentrant function, let's see if Coverage will be happy this time.

frangio
frangio previously approved these changes Sep 27, 2022
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

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

Looks good to me! Awaiting review by @Amxx.

@frangio frangio requested a review from Amxx September 27, 2022 14:55
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

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

Realized tests still need work. The coverage report is clearly showing that the getter is not tested.

@@ -35,6 +35,8 @@ contract ReentrancyMock is ReentrancyGuard {
_count();
bytes4 func = bytes4(keccak256("callback()"));
attacker.callSender(func);

require(_reentrancyGuardStatus() == 2, "ReentrancyMock: _reentrancyGuardStatus failed");
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is getting executed, the attacker line above probably reverts.

Please add a separate test specifically for the guard status, and for the various values it can have.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 for specific testing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Sorry for late response, busy doing a bunch of other things lol:)

@frangio frangio dismissed their stale review September 27, 2022 15:02

Pending tests

Copy link
Collaborator

@Amxx Amxx left a comment

Choose a reason for hiding this comment

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

Looks mostly good. Need better testing.

I'd do an function that emit the value of the guard ... and this function would be called through 2 public function (one with the modifier, one without)

@frangio
Copy link
Contributor

frangio commented Sep 29, 2022

Sorry for the back and forth. I realized the getter was leaking the exact values we use for entered and not entered, and these are values we might want to change in the future, given that they were chosen to optimize gas usage and the optimal values may be different once again in the future.

So in order to avoid leaking the exact values I've changed the function to return a boolean and renamed the function accordingly.

@zhiqiangxu @Amxx Let me know what you think.

@zhiqiangxu
Copy link
Contributor Author

Sorry for the back and forth. I realized the getter was leaking the exact values we use for entered and not entered, and these are values we might want to change in the future, given that they were chosen to optimize gas usage and the optimal values may be different once again in the future.

So in order to avoid leaking the exact values I've changed the function to return a boolean and renamed the function accordingly.

@zhiqiangxu @Amxx Let me know what you think.

Cool, I think that's better !

@frangio frangio enabled auto-merge (squash) October 6, 2022 19:50
@zhiqiangxu
Copy link
Contributor Author

Any update?

@frangio frangio merged commit eb03304 into OpenZeppelin:master Oct 17, 2022
@gitpoap-bot
Copy link

gitpoap-bot bot commented Oct 17, 2022

Congrats, your important contribution to this open-source project has earned you a GitPOAP!

GitPOAP: 2022 OpenZeppelin Contracts Contributor:

GitPOAP: 2022 OpenZeppelin Contracts Contributor GitPOAP Badge

Head to gitpoap.io & connect your GitHub account to mint!

Learn more about GitPOAPs here.

JulissaDantes pushed a commit to JulissaDantes/openzeppelin-contracts that referenced this pull request Nov 3, 2022
Co-authored-by: Francisco Giordano <[email protected]>
Co-authored-by: Hadrien Croubois <[email protected]>
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.

3 participants