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

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity ^0.8.15;
     3  
     4  import { IBigStepper, IPreimageOracle } from "src/dispute/interfaces/IBigStepper.sol";
     5  import { PreimageOracle, PreimageKeyLib } from "src/cannon/PreimageOracle.sol";
     6  import "src/libraries/DisputeTypes.sol";
     7  
     8  /// @title AlphabetVM
     9  /// @dev A mock VM for the purpose of testing the dispute game infrastructure. Note that this only works
    10  ///      for games with an execution trace subgame max depth of 3 (8 instructions per subgame).
    11  contract AlphabetVM is IBigStepper {
    12      Claim internal immutable ABSOLUTE_PRESTATE;
    13      IPreimageOracle public oracle;
    14  
    15      constructor(Claim _absolutePrestate, PreimageOracle _oracle) {
    16          ABSOLUTE_PRESTATE = _absolutePrestate;
    17          oracle = _oracle;
    18      }
    19  
    20      /// @inheritdoc IBigStepper
    21      function step(
    22          bytes calldata _stateData,
    23          bytes calldata,
    24          bytes32 _localContext
    25      )
    26          external
    27          view
    28          returns (bytes32 postState_)
    29      {
    30          uint256 traceIndex;
    31          uint256 claim;
    32          if ((keccak256(_stateData) << 8) == (Claim.unwrap(ABSOLUTE_PRESTATE) << 8)) {
    33              // If the state data is empty, then the absolute prestate is the claim.
    34              (bytes32 dat,) = oracle.readPreimage(
    35                  PreimageKeyLib.localizeIdent(LocalPreimageKey.DISPUTED_L2_BLOCK_NUMBER, _localContext), 0
    36              );
    37              uint256 startingL2BlockNumber = ((uint256(dat) >> 128) & 0xFFFFFFFF) - 1;
    38              traceIndex = startingL2BlockNumber << 4;
    39              (uint256 absolutePrestateClaim) = abi.decode(_stateData, (uint256));
    40              claim = absolutePrestateClaim + traceIndex;
    41          } else {
    42              // Otherwise, decode the state data.
    43              (traceIndex, claim) = abi.decode(_stateData, (uint256, uint256));
    44              traceIndex++;
    45              claim++;
    46          }
    47  
    48          // STF: n -> n + 1
    49          postState_ = keccak256(abi.encode(traceIndex, claim));
    50          assembly {
    51              postState_ := or(and(postState_, not(shl(248, 0xFF))), shl(248, 1))
    52          }
    53      }
    54  }