github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/L2/L1Block.t.sol (about)

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity 0.8.15;
     3  
     4  // Testing utilities
     5  import { CommonTest } from "test/setup/CommonTest.sol";
     6  
     7  // Libraries
     8  import { Encoding } from "src/libraries/Encoding.sol";
     9  
    10  // Target contract
    11  import { L1Block } from "src/L2/L1Block.sol";
    12  
    13  contract L1BlockTest is CommonTest {
    14      address depositor;
    15  
    16      /// @dev Sets up the test suite.
    17      function setUp() public virtual override {
    18          super.setUp();
    19          depositor = l1Block.DEPOSITOR_ACCOUNT();
    20      }
    21  }
    22  
    23  contract L1BlockBedrock_Test is L1BlockTest {
    24      // @dev Tests that `setL1BlockValues` updates the values correctly.
    25      function testFuzz_updatesValues_succeeds(
    26          uint64 n,
    27          uint64 t,
    28          uint256 b,
    29          bytes32 h,
    30          uint64 s,
    31          bytes32 bt,
    32          uint256 fo,
    33          uint256 fs
    34      )
    35          external
    36      {
    37          vm.prank(depositor);
    38          l1Block.setL1BlockValues(n, t, b, h, s, bt, fo, fs);
    39          assertEq(l1Block.number(), n);
    40          assertEq(l1Block.timestamp(), t);
    41          assertEq(l1Block.basefee(), b);
    42          assertEq(l1Block.hash(), h);
    43          assertEq(l1Block.sequenceNumber(), s);
    44          assertEq(l1Block.batcherHash(), bt);
    45          assertEq(l1Block.l1FeeOverhead(), fo);
    46          assertEq(l1Block.l1FeeScalar(), fs);
    47      }
    48  
    49      /// @dev Tests that `setL1BlockValues` can set max values.
    50      function test_updateValues_succeeds() external {
    51          vm.prank(depositor);
    52          l1Block.setL1BlockValues({
    53              _number: type(uint64).max,
    54              _timestamp: type(uint64).max,
    55              _basefee: type(uint256).max,
    56              _hash: keccak256(abi.encode(1)),
    57              _sequenceNumber: type(uint64).max,
    58              _batcherHash: bytes32(type(uint256).max),
    59              _l1FeeOverhead: type(uint256).max,
    60              _l1FeeScalar: type(uint256).max
    61          });
    62      }
    63  }
    64  
    65  contract L1BlockEcotone_Test is L1BlockTest {
    66      /// @dev Tests that setL1BlockValuesEcotone updates the values appropriately.
    67      function testFuzz_setL1BlockValuesEcotone_succeeds(
    68          uint32 baseFeeScalar,
    69          uint32 blobBaseFeeScalar,
    70          uint64 sequenceNumber,
    71          uint64 timestamp,
    72          uint64 number,
    73          uint256 baseFee,
    74          uint256 blobBaseFee,
    75          bytes32 hash,
    76          bytes32 batcherHash
    77      )
    78          external
    79      {
    80          bytes memory functionCallDataPacked = Encoding.encodeSetL1BlockValuesEcotone(
    81              baseFeeScalar, blobBaseFeeScalar, sequenceNumber, timestamp, number, baseFee, blobBaseFee, hash, batcherHash
    82          );
    83  
    84          vm.prank(depositor);
    85          (bool success,) = address(l1Block).call(functionCallDataPacked);
    86          assertTrue(success, "Function call failed");
    87  
    88          assertEq(l1Block.baseFeeScalar(), baseFeeScalar);
    89          assertEq(l1Block.blobBaseFeeScalar(), blobBaseFeeScalar);
    90          assertEq(l1Block.sequenceNumber(), sequenceNumber);
    91          assertEq(l1Block.timestamp(), timestamp);
    92          assertEq(l1Block.number(), number);
    93          assertEq(l1Block.basefee(), baseFee);
    94          assertEq(l1Block.blobBaseFee(), blobBaseFee);
    95          assertEq(l1Block.hash(), hash);
    96          assertEq(l1Block.batcherHash(), batcherHash);
    97  
    98          // ensure we didn't accidentally pollute the 128 bits of the sequencenum+scalars slot that
    99          // should be empty
   100          bytes32 scalarsSlot = vm.load(address(l1Block), bytes32(uint256(3)));
   101          bytes32 mask128 = hex"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000";
   102  
   103          assertEq(0, scalarsSlot & mask128);
   104  
   105          // ensure we didn't accidentally pollute the 128 bits of the number & timestamp slot that
   106          // should be empty
   107          bytes32 numberTimestampSlot = vm.load(address(l1Block), bytes32(uint256(0)));
   108          assertEq(0, numberTimestampSlot & mask128);
   109      }
   110  
   111      /// @dev Tests that `setL1BlockValuesEcotone` succeeds if sender address is the depositor
   112      function test_setL1BlockValuesEcotone_isDepositor_succeeds() external {
   113          bytes memory functionCallDataPacked = Encoding.encodeSetL1BlockValuesEcotone(
   114              type(uint32).max,
   115              type(uint32).max,
   116              type(uint64).max,
   117              type(uint64).max,
   118              type(uint64).max,
   119              type(uint256).max,
   120              type(uint256).max,
   121              bytes32(type(uint256).max),
   122              bytes32(type(uint256).max)
   123          );
   124  
   125          vm.prank(depositor);
   126          (bool success,) = address(l1Block).call(functionCallDataPacked);
   127          assertTrue(success, "function call failed");
   128      }
   129  
   130      /// @dev Tests that `setL1BlockValuesEcotone` fails if sender address is not the depositor
   131      function test_setL1BlockValuesEcotone_notDepositor_fails() external {
   132          bytes memory functionCallDataPacked = Encoding.encodeSetL1BlockValuesEcotone(
   133              type(uint32).max,
   134              type(uint32).max,
   135              type(uint64).max,
   136              type(uint64).max,
   137              type(uint64).max,
   138              type(uint256).max,
   139              type(uint256).max,
   140              bytes32(type(uint256).max),
   141              bytes32(type(uint256).max)
   142          );
   143  
   144          (bool success, bytes memory data) = address(l1Block).call(functionCallDataPacked);
   145          assertTrue(!success, "function call should have failed");
   146          // make sure return value is the expected function selector for "NotDepositor()"
   147          bytes memory expReturn = hex"3cc50b45";
   148          assertEq(data, expReturn);
   149      }
   150  }