github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/L2/CrossDomainOwnable.t.sol (about) 1 // SPDX-License-Identifier: MIT 2 pragma solidity 0.8.15; 3 4 // Testing utilities 5 import { VmSafe } from "forge-std/Vm.sol"; 6 import { Test } from "forge-std/Test.sol"; 7 import { CommonTest } from "test/setup/CommonTest.sol"; 8 9 // Libraries 10 import { Bytes32AddressLib } from "@rari-capital/solmate/src/utils/Bytes32AddressLib.sol"; 11 12 // Target contract dependencies 13 import { AddressAliasHelper } from "src/vendor/AddressAliasHelper.sol"; 14 15 // Target contract 16 import { CrossDomainOwnable } from "src/L2/CrossDomainOwnable.sol"; 17 18 contract XDomainSetter is CrossDomainOwnable { 19 uint256 public value; 20 21 function set(uint256 _value) external onlyOwner { 22 value = _value; 23 } 24 } 25 26 contract CrossDomainOwnable_Test is Test { 27 XDomainSetter setter; 28 29 function setUp() public { 30 setter = new XDomainSetter(); 31 } 32 33 /// @dev Tests that the `onlyOwner` modifier reverts with the correct message. 34 function test_onlyOwner_notOwner_reverts() external { 35 vm.expectRevert("CrossDomainOwnable: caller is not the owner"); 36 setter.set(1); 37 } 38 39 /// @dev Tests that the `onlyOwner` modifier succeeds when called by the owner. 40 function test_onlyOwner_succeeds() external { 41 assertEq(setter.value(), 0); 42 43 vm.prank(AddressAliasHelper.applyL1ToL2Alias(setter.owner())); 44 setter.set(1); 45 assertEq(setter.value(), 1); 46 } 47 } 48 49 contract CrossDomainOwnableThroughPortal_Test is CommonTest { 50 XDomainSetter setter; 51 52 /// @dev Sets up the test suite. 53 function setUp() public override { 54 super.setUp(); 55 56 vm.prank(alice); 57 setter = new XDomainSetter(); 58 } 59 60 /// @dev Tests that `depositTransaction` succeeds when calling the `set` function on the 61 /// `XDomainSetter` contract. 62 function test_depositTransaction_crossDomainOwner_succeeds() external { 63 vm.recordLogs(); 64 65 vm.prank(alice); 66 optimismPortal.depositTransaction({ 67 _to: address(setter), 68 _value: 0, 69 _gasLimit: 30_000, 70 _isCreation: false, 71 _data: abi.encodeWithSelector(XDomainSetter.set.selector, 1) 72 }); 73 74 // Simulate the operation of the `op-node` by parsing data 75 // from logs 76 VmSafe.Log[] memory logs = vm.getRecordedLogs(); 77 // Only 1 log emitted 78 assertEq(logs.length, 1); 79 80 VmSafe.Log memory log = logs[0]; 81 82 // It is the expected topic 83 bytes32 topic = log.topics[0]; 84 assertEq(topic, keccak256("TransactionDeposited(address,address,uint256,bytes)")); 85 86 // from is indexed and the first argument to the event. 87 bytes32 _from = log.topics[1]; 88 address from = Bytes32AddressLib.fromLast20Bytes(_from); 89 90 assertEq(AddressAliasHelper.undoL1ToL2Alias(from), alice); 91 92 // Make a call from the "from" value received from the log. 93 // In theory the opaque data could be parsed from the log 94 // and passed to a low level call to "to", but calling set 95 // directly on the setter is good enough. 96 vm.prank(from); 97 setter.set(1); 98 assertEq(setter.value(), 1); 99 } 100 }