ReentrancyGuard.sol 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. pragma solidity ^0.4.24;
  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. * If you mark a function `nonReentrant`, you should also
  19. * mark it `external`. Calling one `nonReentrant` function from
  20. * another is not supported. Instead, you can implement a
  21. * `private` function doing the actual work, and an `external`
  22. * wrapper marked as `nonReentrant`.
  23. */
  24. modifier nonReentrant() {
  25. _guardCounter += 1;
  26. uint256 localCounter = _guardCounter;
  27. _;
  28. require(localCounter == _guardCounter);
  29. }
  30. }