-
Notifications
You must be signed in to change notification settings - Fork 183
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 Reentrancy-Guard modifier to external MP methods #1334
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
12ae7bf
replace transfer with requestTransfer/cancelTransfer/transferPassedRe…
nicholaspai f1cf668
add requestTransferPassTimestamp to PositionData struct
nicholaspai ae729fa
enforce check-effects-interaction pattern throughout NFCT
nicholaspai 56b4ca8
merge master
nicholaspai 141d88a
fix tests
nicholaspai 5adc740
Merge branch 'master' into npai/nfct-reentrancy-guard
nicholaspai ff42ea5
add Reentrancy guard to FeePayer, currently out of bytecode size
nicholaspai 68598cd
Fixed gas limit
chrismaree 9a3344d
fixed branch
chrismaree aa24554
add ReentrancyGuard to all NFCT contracts
nicholaspai c7d2100
increase gas in dapp-test ci
nicholaspai b76a9ff
extend ReentrancyGuard and add viewLock method
nicholaspai 8347a75
rename local ReentryGuard, add TODO's
nicholaspai 32f60e8
remove nonReentrant from internal method
nicholaspai 2f3f5f4
make EMPCreator Lockable
nicholaspai c478dd1
merge master
nicholaspai 50143fa
add nonReentrant to all state-modifying public methods but no constru…
nicholaspai a7567e2
add view-only lock to all public view methods
nicholaspai e4532f5
revert pfc() in Liquidatable
nicholaspai 8b34b5b
update comment
nicholaspai 00321a6
fix comments
nicholaspai 8509bcc
add comment about pfc
nicholaspai 796001d
Merge branch 'master' into npai/nfct-reentrancy-guard
nicholaspai b168e94
add unit tests for Lockable.sol
nicholaspai 207e9cb
add credits
nicholaspai 856dd44
add address(this).callViewFunction tests
nicholaspai e992961
slither ignore
nicholaspai 61bef03
fix typo in Lockable, move nonReentrant to EMP from FeePayer
nicholaspai 9ad2009
add nonReentrantView to public pfc() methods
nicholaspai acf1b3b
fix local variable shadowing
nicholaspai 6c635d8
add nonReentrancy to all constructors
nicholaspai 00dafe1
add require messages
nicholaspai 9026624
add require messages after merge with master
nicholaspai 5552bf1
merge master
nicholaspai 126fc0d
make sure nonReentrant modifier is last
nicholaspai 696f4a0
merge master
nicholaspai c8e84ef
Merge branch 'master' into npai/nfct-reentrancy-guard
nicholaspai 996e773
move pfc() public method to feepayer
nicholaspai 55bdb2e
rename pfc to collateralPool to avoid local variable shadowing
nicholaspai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
pragma solidity ^0.6.0; | ||
|
||
|
||
/** | ||
* @title A contract that provides modifiers to prevent reentrancy to state-changing and view-only methods. This contract | ||
* is inspired by https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/ReentrancyGuard.sol | ||
* and https://github.com/balancer-labs/balancer-core/blob/master/contracts/BPool.sol. | ||
*/ | ||
contract Lockable { | ||
bool private _notEntered; | ||
|
||
constructor() internal { | ||
// Storing an initial non-zero value makes deployment a bit more | ||
// expensive, but in exchange the refund on every call to nonReentrant | ||
// will be lower in amount. Since refunds are capped to a percetange of | ||
// the total transaction's gas, it is best to keep them low in cases | ||
// like this one, to increase the likelihood of the full refund coming | ||
// into effect. | ||
_notEntered = true; | ||
} | ||
|
||
/** | ||
* @dev Prevents a contract from calling itself, directly or indirectly. | ||
* Calling a `nonReentrant` function from another `nonReentrant` | ||
* function is not supported. It is possible to prevent this from happening | ||
* by making the `nonReentrant` function external, and make it call a | ||
* `private` function that does the actual work. | ||
*/ | ||
modifier nonReentrant() { | ||
_preEntranceCheck(); | ||
_preEntranceSet(); | ||
_; | ||
_postEntranceReset(); | ||
} | ||
|
||
/** | ||
* @dev Designed to prevent a view-only method from being re-entered during a call to a `nonReentrant()` state-changing method. | ||
*/ | ||
modifier nonReentrantView() { | ||
_preEntranceCheck(); | ||
_; | ||
} | ||
|
||
// Internal methods are used to avoid copying the require statement's bytecode to every `nonReentrant()` method. | ||
// On entry into a function, `_preEntranceCheck()` should always be called to check if the function is being re-entered. | ||
// Then, if the function modifies state, it should call `_postEntranceSet()`, perform its logic, and then call `_postEntranceReset()`. | ||
// View-only methods can simply call `_preEntranceCheck()` to make sure that it is not being re-entered. | ||
function _preEntranceCheck() internal view { | ||
// On the first call to nonReentrant, _notEntered will be true | ||
require(_notEntered, "ReentrancyGuard: reentrant call"); | ||
} | ||
|
||
function _preEntranceSet() internal { | ||
// Any calls to nonReentrant after this point will fail | ||
_notEntered = false; | ||
} | ||
|
||
function _postEntranceReset() internal { | ||
// By storing the original value once again, a refund is triggered (see | ||
// https://eips.ethereum.org/EIPS/eip-2200) | ||
_notEntered = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pragma solidity ^0.6.0; | ||
|
||
|
||
// Tests reentrancy guards defined in Lockable.sol. | ||
// Copied from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.1/contracts/mocks/ReentrancyAttack.sol. | ||
contract ReentrancyAttack { | ||
function callSender(bytes4 data) public { | ||
// solhint-disable-next-line avoid-low-level-calls | ||
(bool success, ) = msg.sender.call(abi.encodeWithSelector(data)); | ||
require(success, "ReentrancyAttack: failed call"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
pragma solidity ^0.6.0; | ||
|
||
import "../implementation/Lockable.sol"; | ||
import "./ReentrancyAttack.sol"; | ||
|
||
|
||
// Tests reentrancy guards defined in Lockable.sol. | ||
// Extends https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.1/contracts/mocks/ReentrancyMock.sol. | ||
contract ReentrancyMock is Lockable { | ||
uint256 public counter; | ||
|
||
constructor() public { | ||
counter = 0; | ||
} | ||
|
||
function callback() external nonReentrant { | ||
_count(); | ||
} | ||
|
||
function countAndSend(ReentrancyAttack attacker) external nonReentrant { | ||
_count(); | ||
bytes4 func = bytes4(keccak256("callback()")); | ||
attacker.callSender(func); | ||
} | ||
|
||
function countAndCall(ReentrancyAttack attacker) external nonReentrant { | ||
_count(); | ||
bytes4 func = bytes4(keccak256("getCount()")); | ||
attacker.callSender(func); | ||
} | ||
|
||
function countLocalRecursive(uint256 n) public nonReentrant { | ||
if (n > 0) { | ||
_count(); | ||
countLocalRecursive(n - 1); | ||
} | ||
} | ||
|
||
function countThisRecursive(uint256 n) public nonReentrant { | ||
if (n > 0) { | ||
_count(); | ||
// solhint-disable-next-line avoid-low-level-calls | ||
(bool success, ) = address(this).call(abi.encodeWithSignature("countThisRecursive(uint256)", n - 1)); | ||
require(success, "ReentrancyMock: failed call"); | ||
} | ||
} | ||
|
||
function countLocalCall() public nonReentrant { | ||
getCount(); | ||
} | ||
|
||
function countThisCall() public nonReentrant { | ||
// solhint-disable-next-line avoid-low-level-calls | ||
(bool success, ) = address(this).call(abi.encodeWithSignature("getCount()")); | ||
require(success, "ReentrancyMock: failed call"); | ||
} | ||
|
||
function getCount() public view nonReentrantView returns (uint256) { | ||
return counter; | ||
} | ||
|
||
function _count() private { | ||
counter += 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
nonReentrant()
go into effect before or after the Liquidatable constructor gets called? Is it possible to put this modifier before the Liquidatable constructor is called? Or if it's not, maybe it makes sense to addnonReentrant()
to all the constructors (since they shouldn't interfere with one another)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constructors do not appear to interfere with each other so I just went ahead and put it on all of them.