ReentrancyMock.sol 904 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. pragma solidity ^0.4.18;
  2. import '../ReentrancyGuard.sol';
  3. import './ReentrancyAttack.sol';
  4. contract ReentrancyMock is ReentrancyGuard {
  5. uint256 public counter;
  6. function ReentrancyMock() public {
  7. counter = 0;
  8. }
  9. function count() private {
  10. counter += 1;
  11. }
  12. function countLocalRecursive(uint256 n) public nonReentrant {
  13. if(n > 0) {
  14. count();
  15. countLocalRecursive(n - 1);
  16. }
  17. }
  18. function countThisRecursive(uint256 n) public nonReentrant {
  19. bytes4 func = bytes4(keccak256("countThisRecursive(uint256)"));
  20. if(n > 0) {
  21. count();
  22. bool result = this.call(func, n - 1);
  23. require(result == true);
  24. }
  25. }
  26. function countAndCall(ReentrancyAttack attacker) public nonReentrant {
  27. count();
  28. bytes4 func = bytes4(keccak256("callback()"));
  29. attacker.callSender(func);
  30. }
  31. function callback() external nonReentrant {
  32. count();
  33. }
  34. }