ReentrancyGuard.sol 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. pragma solidity ^0.5.2;
  2. /**
  3. * @title Helps contracts guard against reentrancy attacks.
  4. * @author Remco Bloemen <remco@2π.com>, Eenae <alexey@mixbytes.io>
  5. * @dev If you mark a function `nonReentrant`, you should also
  6. * mark it `external`.
  7. */
  8. contract ReentrancyGuard {
  9. /// @dev counter to allow mutex lock with only one SSTORE operation
  10. uint256 private _guardCounter;
  11. constructor () internal {
  12. // The counter starts at one to prevent changing it from zero to a non-zero
  13. // value, which is a more expensive operation.
  14. _guardCounter = 1;
  15. }
  16. /**
  17. * @dev Prevents a contract from calling itself, directly or indirectly.
  18. * Calling a `nonReentrant` function from another `nonReentrant`
  19. * function is not supported. It is possible to prevent this from happening
  20. * by making the `nonReentrant` function external, and make it call a
  21. * `private` function that does the actual work.
  22. */
  23. modifier nonReentrant() {
  24. _guardCounter += 1;
  25. uint256 localCounter = _guardCounter;
  26. _;
  27. require(localCounter == _guardCounter);
  28. }
  29. }