github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/legacy/ResolvedDelegateProxy.t.sol (about) 1 // SPDX-License-Identifier: MIT 2 pragma solidity 0.8.15; 3 4 // Testing utilities 5 import { Test } from "forge-std/Test.sol"; 6 7 // Target contract dependencies 8 import { AddressManager } from "src/legacy/AddressManager.sol"; 9 10 // Target contract 11 import { ResolvedDelegateProxy } from "src/legacy/ResolvedDelegateProxy.sol"; 12 13 contract ResolvedDelegateProxy_Test is Test { 14 AddressManager internal addressManager; 15 SimpleImplementation internal impl; 16 SimpleImplementation internal proxy; 17 18 /// @dev Sets up the test suite. 19 function setUp() public { 20 // Set up the address manager. 21 addressManager = new AddressManager(); 22 impl = new SimpleImplementation(); 23 addressManager.setAddress("SimpleImplementation", address(impl)); 24 25 // Set up the proxy. 26 proxy = SimpleImplementation(address(new ResolvedDelegateProxy(addressManager, "SimpleImplementation"))); 27 } 28 29 /// @dev Tests that the proxy properly bubbles up returndata when the delegatecall succeeds. 30 function testFuzz_fallback_delegateCallFoo_succeeds(uint256 x) public { 31 vm.expectCall(address(impl), abi.encodeWithSelector(impl.foo.selector, x)); 32 assertEq(proxy.foo(x), x); 33 } 34 35 /// @dev Tests that the proxy properly bubbles up returndata when the delegatecall reverts. 36 function test_fallback_delegateCallBar_reverts() public { 37 vm.expectRevert("SimpleImplementation: revert"); 38 vm.expectCall(address(impl), abi.encodeWithSelector(impl.bar.selector)); 39 proxy.bar(); 40 } 41 42 /// @dev Tests that the proxy fallback reverts as expected if the implementation within the 43 /// address manager is not set. 44 function test_fallback_addressManagerNotSet_reverts() public { 45 AddressManager am = new AddressManager(); 46 SimpleImplementation p = SimpleImplementation(address(new ResolvedDelegateProxy(am, "SimpleImplementation"))); 47 48 vm.expectRevert("ResolvedDelegateProxy: target address must be initialized"); 49 p.foo(0); 50 } 51 } 52 53 contract SimpleImplementation { 54 function foo(uint256 _x) public pure returns (uint256) { 55 return _x; 56 } 57 58 function bar() public pure { 59 revert("SimpleImplementation: revert"); 60 } 61 }