Michael.W基于Foundry精读Openzeppelin第66期——ProxyAdmin.sol

  • Michael.W
  • 更新于 2024-07-21 14:59
  • 阅读 921

ProxyAdmin库是指定用于做透明代理TransparentUpgradeableProxy库admin的管理员合约。

0. 版本

[openzeppelin]:v4.8.3,[forge-std]:v1.5.6

0.1 ProxyAdmin.sol

Github: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.8.3/contracts/proxy/transparent/ProxyAdmin.sol

ProxyAdmin库是指定用于做透明代理TransparentUpgradeableProxy库admin的管理员合约。

注:用一个额外合约来做透明代理合约的admin的原因及透明代理合约详解参见:https://learnblockchain.cn/article/8770

1. 目标合约

ProxyAdmin合约可直接部署。

全部foundry测试合约:

Github: https://github.com/RevelationOfTuring/foundry-openzeppelin-contracts/blob/master/test/proxy/transparent/ProxyAdmin/ProxyAdmin.t.sol

测试使用的物料合约:

Github: https://github.com/RevelationOfTuring/foundry-openzeppelin-contracts/blob/master/test/proxy/transparent/ProxyAdmin/Implementation.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

interface IImplementation {
    event ChangeStorageUint(uint, uint);
}

contract Implementation is IImplementation {
    // storage
    uint public i;

    function __Implementation_init(uint i_) external {
        i = i_;
    }
}

contract ImplementationNew is Implementation {
    // add a function
    function addI(uint i_) external payable {
        i += i_;
        emit ChangeStorageUint(i, msg.value);
    }
}

2. 代码精读

注:以下所有方法只有当本ProxyAdmin合约为透明代理合约proxy的admin时才能调用成功。

2.1 getProxyImplementation(ITransparentUpgradeableProxy proxy)

查询透明代理合约proxy背后的逻辑合约地址。

    function getProxyImplementation(ITransparentUpgradeableProxy proxy) public view virtual returns (address) {
        // staticcall到透明代理合约proxy(calldata为0x5c60da1b),如同去调用proxy合约的implementation()方法
        (bool success, bytes memory returndata) = address(proxy).staticcall(hex"5c60da1b");
        // 要求调用必须成功
        require(success);
        // 将staticcall返回的bytes解码成address类型并返回
        return abi.decode(returndata, (address));
    }

foundry代码验证:

contract ProxyAdminTest is Test, IERC1967, IImplementation {
    ProxyAdmin private _testing = new ProxyAdmin();
    Implementation private _implementation1 = new Implementation();
    Implementation private _implementation2 = new Implementation();
    TransparentUpgradeableProxy private _transparentUpgradeableProxy1 = new TransparentUpgradeableProxy(
        address(_implementation1),
        address(_testing),
        abi.encodeCall(
            _implementation1.__Implementation_init,
            (1024)
        )
    );
    TransparentUpgradeableProxy private _transparentUpgradeableProxy2 = new TransparentUpgradeableProxy(
        address(_implementation2),
        address(_testing),
        abi.encodeCall(
            _implementation2.__Implementation_init,
            (2048)
        )
    );

    function test_GetProxyImplementation() external {
        assertEq(
            _testing.getProxyImplementation(
                ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1))
            ),
            address(_implementation1)
        );
        assertEq(
            _testing.getProxyImplementation(
                ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy2))
            ),
            address(_implementation2)
        );
    }
}

2.2 getProxyAdmin(ITransparentUpgradeableProxy proxy) && changeProxyAdmin(ITransparentUpgradeableProxy proxy, address newAdmin)

  • getProxyAdmin(ITransparentUpgradeableProxy proxy):查询透明代理合约proxy的admin;
  • changeProxyAdmin(ITransparentUpgradeableProxy proxy, address newAdmin):本合约的owner修改透明代理合约proxy的admin为newAdmin。
    function getProxyAdmin(ITransparentUpgradeableProxy proxy) public view virtual returns (address) {
        // staticcall到透明代理合约proxy(calldata为f851a440),如同去调用proxy合约的admin()方法
        (bool success, bytes memory returndata) = address(proxy).staticcall(hex"f851a440");
        // 要求调用必须成功
        require(success);
        // 将staticcall返回的bytes解码成address类型并返回
        return abi.decode(returndata, (address));
    }

    function changeProxyAdmin(ITransparentUpgradeableProxy proxy, address newAdmin) public virtual onlyOwner {
        // 如同调用proxy合约的changeAdmin(address)方法
        proxy.changeAdmin(newAdmin);
    }

foundry代码验证:

contract ProxyAdminTest is Test, IERC1967, IImplementation {
    ProxyAdmin private _testing = new ProxyAdmin();
    Implementation private _implementation1 = new Implementation();
    Implementation private _implementation2 = new Implementation();
    TransparentUpgradeableProxy private _transparentUpgradeableProxy1 = new TransparentUpgradeableProxy(
        address(_implementation1),
        address(_testing),
        abi.encodeCall(
            _implementation1.__Implementation_init,
            (1024)
        )
    );
    TransparentUpgradeableProxy private _transparentUpgradeableProxy2 = new TransparentUpgradeableProxy(
        address(_implementation2),
        address(_testing),
        abi.encodeCall(
            _implementation2.__Implementation_init,
            (2048)
        )
    );

    function test_GetProxyAdminAndChangeProxyAdmin() external {
        assertEq(
            _testing.getProxyAdmin(
                ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1))
            ),
            address(_testing)
        );
        assertEq(
            _testing.getProxyAdmin(
                ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy2))
            ),
            address(_testing)
        );

        // deploy another ProxyAdmin
        ProxyAdmin newProxyAdmin = new ProxyAdmin();
        // test changeProxyAdmin(ITransparentUpgradeableProxy,address)
        vm.expectEmit(address(_transparentUpgradeableProxy1));
        emit IERC1967.AdminChanged(address(_testing), address(newProxyAdmin));

        _testing.changeProxyAdmin(ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1)), address(newProxyAdmin));

        vm.expectEmit(address(_transparentUpgradeableProxy2));
        emit IERC1967.AdminChanged(address(_testing), address(newProxyAdmin));

        _testing.changeProxyAdmin(ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy2)), address(newProxyAdmin));

        assertEq(
            newProxyAdmin.getProxyAdmin(
                ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1))
            ),
            address(newProxyAdmin)
        );
        assertEq(
            newProxyAdmin.getProxyAdmin(
                ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy2))
            ),
            address(newProxyAdmin)
        );

        // revert if not owner calls
        assertEq(newProxyAdmin.owner(), address(this));
        vm.prank(address(1));
        vm.expectRevert("Ownable: caller is not the owner");
        newProxyAdmin.changeProxyAdmin(
            ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1)),
            address(1)
        );
    }
}

2.3 upgrade(ITransparentUpgradeableProxy proxy, address implementation)

本合约的owner升级透明代理合约proxy的逻辑合约地址为implementation。

    function upgrade(ITransparentUpgradeableProxy proxy, address implementation) public virtual onlyOwner {
        // 如同调用proxy合约的upgradeTo(address)方法
        proxy.upgradeTo(implementation);
    }

foundry代码验证:

contract ProxyAdminTest is Test, IERC1967, IImplementation {
    ProxyAdmin private _testing = new ProxyAdmin();
    Implementation private _implementation1 = new Implementation();
    Implementation private _implementation2 = new Implementation();
    TransparentUpgradeableProxy private _transparentUpgradeableProxy1 = new TransparentUpgradeableProxy(
        address(_implementation1),
        address(_testing),
        abi.encodeCall(
            _implementation1.__Implementation_init,
            (1024)
        )
    );
    TransparentUpgradeableProxy private _transparentUpgradeableProxy2 = new TransparentUpgradeableProxy(
        address(_implementation2),
        address(_testing),
        abi.encodeCall(
            _implementation2.__Implementation_init,
            (2048)
        )
    );
    ImplementationNew private _implementationNew = new ImplementationNew();

    function test_Upgrade() external {
        // upgrade one transparent upgradeable proxy
        vm.expectEmit(address(_transparentUpgradeableProxy1));
        emit IERC1967.Upgraded(address(_implementationNew));

        _testing.upgrade(
            ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1)),
            address(_implementationNew)
        );

        // check the result of upgrade
        assertEq(
            _testing.getProxyImplementation(ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1))),
            address(_implementationNew)
        );

        ImplementationNew transparentUpgradeableProxy1AsNew = ImplementationNew(address(_transparentUpgradeableProxy1));
        assertEq(transparentUpgradeableProxy1AsNew.i(), 1024);
        vm.expectEmit(address(transparentUpgradeableProxy1AsNew));
        emit IImplementation.ChangeStorageUint(1024 + 1, 0);

        transparentUpgradeableProxy1AsNew.addI(1);
        assertEq(transparentUpgradeableProxy1AsNew.i(), 1024 + 1);

        // upgrade another transparent upgradeable proxy
        vm.expectEmit(address(_transparentUpgradeableProxy2));
        emit IERC1967.Upgraded(address(_implementationNew));

        _testing.upgrade(
            ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy2)),
            address(_implementationNew)
        );

        // check the result of upgrade
        assertEq(
            _testing.getProxyImplementation(ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy2))),
            address(_implementationNew)
        );

        ImplementationNew transparentUpgradeableProxy2AsNew = ImplementationNew(address(_transparentUpgradeableProxy2));
        assertEq(transparentUpgradeableProxy2AsNew.i(), 2048);
        vm.expectEmit(address(transparentUpgradeableProxy2AsNew));
        emit IImplementation.ChangeStorageUint(2048 + 2, 0);

        transparentUpgradeableProxy2AsNew.addI(2);
        assertEq(transparentUpgradeableProxy2AsNew.i(), 2048 + 2);

        // revert if not owner calls
        assertEq(_testing.owner(), address(this));
        vm.prank(address(1));
        vm.expectRevert("Ownable: caller is not the owner");
        _testing.upgrade(
            ITransparentUpgradeableProxy(address(transparentUpgradeableProxy1AsNew)),
            address(_implementationNew)
        );
    }
}

2.4 upgradeAndCall(ITransparentUpgradeableProxy proxy, address implementation, bytes memory data)

本合约的owner升级透明代理合约proxy的逻辑合约地址为implementation并随后以data为calldata执行一次delegatecall。

    function upgradeAndCall(
        ITransparentUpgradeableProxy proxy,
        address implementation,
        bytes memory data
    ) public payable virtual onlyOwner {
        // 如同携带本次call的全部msg.value去调用proxy合约的upgradeToAndCall(address,bytes)方法
        proxy.upgradeToAndCall{value: msg.value}(implementation, data);
    }

foundry代码验证:

contract ProxyAdminTest is Test, IERC1967, IImplementation {
    ProxyAdmin private _testing = new ProxyAdmin();
    Implementation private _implementation1 = new Implementation();
    Implementation private _implementation2 = new Implementation();
    TransparentUpgradeableProxy private _transparentUpgradeableProxy1 = new TransparentUpgradeableProxy(
        address(_implementation1),
        address(_testing),
        abi.encodeCall(
            _implementation1.__Implementation_init,
            (1024)
        )
    );
    TransparentUpgradeableProxy private _transparentUpgradeableProxy2 = new TransparentUpgradeableProxy(
        address(_implementation2),
        address(_testing),
        abi.encodeCall(
            _implementation2.__Implementation_init,
            (2048)
        )
    );
    ImplementationNew private _implementationNew = new ImplementationNew();

    function test_UpgradeAndCall() external {
        // upgrade one transparent upgradeable proxy and delegatecall the added function in new implementation
        uint ethValue = 1024;
        vm.expectEmit(address(_transparentUpgradeableProxy1));
        emit IERC1967.Upgraded(address(_implementationNew));
        vm.expectEmit(address(_transparentUpgradeableProxy1));
        emit IImplementation.ChangeStorageUint(1024 + 1, ethValue);

        _testing.upgradeAndCall{value: ethValue}(
            ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1)),
            address(_implementationNew),
            abi.encodeCall(
                _implementationNew.addI,
                (1)
            )
        );

        // check the result of upgrade
        assertEq(
            _testing.getProxyImplementation(ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1))),
            address(_implementationNew)
        );

        assertEq(ImplementationNew(address(_transparentUpgradeableProxy1)).i(), 1024 + 1);
        assertEq(address(_transparentUpgradeableProxy1).balance, ethValue);

        // upgrade another transparent upgradeable proxy and delegatecall the added function in new implementation
        vm.expectEmit(address(_transparentUpgradeableProxy2));
        emit IERC1967.Upgraded(address(_implementationNew));
        vm.expectEmit(address(_transparentUpgradeableProxy2));
        emit IImplementation.ChangeStorageUint(2048 + 2, ethValue);

        _testing.upgradeAndCall{value: ethValue}(
            ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy2)),
            address(_implementationNew),
            abi.encodeCall(
                _implementationNew.addI,
                (2)
            )
        );

        // check the result of upgrade
        assertEq(
            _testing.getProxyImplementation(ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy2))),
            address(_implementationNew)
        );

        assertEq(ImplementationNew(address(_transparentUpgradeableProxy2)).i(), 2048 + 2);
        assertEq(address(_transparentUpgradeableProxy2).balance, ethValue);

        // revert if not owner calls
        assertEq(_testing.owner(), address(this));
        address nonOwner = address(1);
        vm.deal(nonOwner, ethValue);
        vm.prank(nonOwner);
        vm.expectRevert("Ownable: caller is not the owner");

        _testing.upgradeAndCall{value: ethValue}(
            ITransparentUpgradeableProxy(address(_transparentUpgradeableProxy1)),
            address(_implementationNew),
            abi.encodeCall(
                _implementationNew.addI,
                (1)
            )
        );
    }
}

ps: 本人热爱图灵,热爱中本聪,热爱V神。 以下是我个人的公众号,如果有技术问题可以关注我的公众号来跟我交流。 同时我也会在这个公众号上每周更新我的原创文章,喜欢的小伙伴或者老伙计可以支持一下! 如果需要转发,麻烦注明作者。十分感谢!

1.jpeg

公众号名称:后现代泼痞浪漫主义奠基人

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
Michael.W
Michael.W
0x93E7...0000
狂热的区块链爱好者