You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.2 KiB
Solidity
52 lines
1.2 KiB
Solidity
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
|
|
||
|
pragma solidity ^0.8.0;
|
||
|
|
||
|
import "../security/ReentrancyGuard.sol";
|
||
|
import "./ReentrancyAttack.sol";
|
||
|
|
||
|
contract ReentrancyMock is ReentrancyGuard {
|
||
|
uint256 public counter;
|
||
|
|
||
|
constructor() {
|
||
|
counter = 0;
|
||
|
}
|
||
|
|
||
|
function callback() external nonReentrant {
|
||
|
_count();
|
||
|
}
|
||
|
|
||
|
function countLocalRecursive(uint256 n) public nonReentrant {
|
||
|
if (n > 0) {
|
||
|
_count();
|
||
|
countLocalRecursive(n - 1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function countThisRecursive(uint256 n) public nonReentrant {
|
||
|
if (n > 0) {
|
||
|
_count();
|
||
|
(bool success, ) = address(this).call(abi.encodeWithSignature("countThisRecursive(uint256)", n - 1));
|
||
|
require(success, "ReentrancyMock: failed call");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function countAndCall(ReentrancyAttack attacker) public nonReentrant {
|
||
|
_count();
|
||
|
bytes4 func = bytes4(keccak256("callback()"));
|
||
|
attacker.callSender(func);
|
||
|
}
|
||
|
|
||
|
function _count() private {
|
||
|
counter += 1;
|
||
|
}
|
||
|
|
||
|
function guardedCheckEntered() public nonReentrant {
|
||
|
require(_reentrancyGuardEntered());
|
||
|
}
|
||
|
|
||
|
function unguardedCheckNotEntered() public view {
|
||
|
require(!_reentrancyGuardEntered());
|
||
|
}
|
||
|
}
|