github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/mocks/Callers.sol (about)

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity ^0.8.0;
     3  
     4  contract CallRecorder {
     5      struct CallInfo {
     6          address sender;
     7          bytes data;
     8          uint256 gas;
     9          uint256 value;
    10      }
    11  
    12      CallInfo public lastCall;
    13  
    14      function record() public payable {
    15          lastCall.sender = msg.sender;
    16          lastCall.data = msg.data;
    17          lastCall.gas = gasleft();
    18          lastCall.value = msg.value;
    19      }
    20  }
    21  
    22  /// @dev Useful for testing reentrancy guards
    23  contract CallerCaller {
    24      event WhatHappened(bool success, bytes returndata);
    25  
    26      fallback() external {
    27          (bool success, bytes memory returndata) = msg.sender.call(msg.data);
    28          emit WhatHappened(success, returndata);
    29          assembly {
    30              switch success
    31              case 0 { revert(add(returndata, 0x20), mload(returndata)) }
    32              default { return(add(returndata, 0x20), mload(returndata)) }
    33          }
    34      }
    35  }
    36  
    37  /// @dev Used for testing the `CrossDomainMessenger`'s per-message reentrancy guard.
    38  contract ConfigurableCaller {
    39      bool doRevert = true;
    40      address target;
    41      bytes payload;
    42  
    43      event WhatHappened(bool success, bytes returndata);
    44  
    45      /// @notice Call the configured target with the configured payload OR revert.
    46      function call() external {
    47          if (doRevert) {
    48              revert("ConfigurableCaller: revert");
    49          } else {
    50              (bool success, bytes memory returndata) = address(target).call(payload);
    51              emit WhatHappened(success, returndata);
    52              assembly {
    53                  switch success
    54                  case 0 { revert(add(returndata, 0x20), mload(returndata)) }
    55                  default { return(add(returndata, 0x20), mload(returndata)) }
    56              }
    57          }
    58      }
    59  
    60      /// @notice Set whether or not to have `call` revert.
    61      function setDoRevert(bool _doRevert) external {
    62          doRevert = _doRevert;
    63      }
    64  
    65      /// @notice Set the target for the call made in `call`.
    66      function setTarget(address _target) external {
    67          target = _target;
    68      }
    69  
    70      /// @notice Set the payload for the call made in `call`.
    71      function setPayload(bytes calldata _payload) external {
    72          payload = _payload;
    73      }
    74  
    75      /// @notice Fallback function that reverts if `doRevert` is true.
    76      ///        Otherwise, it does nothing.
    77      fallback() external {
    78          if (doRevert) {
    79              revert("ConfigurableCaller: revert");
    80          }
    81      }
    82  }
    83  
    84  /// @dev Any call will revert
    85  contract Reverter {
    86      function doRevert() public pure {
    87          revert("Reverter reverted");
    88      }
    89  
    90      fallback() external {
    91          revert();
    92      }
    93  }