Michael.W基于Foundry精读Openzeppelin

2024年08月13日更新 117 人订阅
专栏简介 Michael.W基于Foundry精读Openzeppelin第8期——Context.sol Michael.W基于Foundry精读Openzeppelin第1期——Address.sol Michael.W基于Foundry精读Openzeppelin第2期——StorageSlot.sol Michael.W基于Foundry精读Openzeppelin第3期——Arrays.sol Michael.W基于Foundry精读Openzeppelin第4期——Base64.sol Michael.W基于Foundry精读Openzeppelin第5期——Counters.sol Michael.W基于Foundry精读Openzeppelin第6期——Strings.sol Michael.W基于Foundry精读Openzeppelin第7期——Timers.sol Michael.W基于Foundry精读Openzeppelin第9期——Multicall.sol Michael.W基于Foundry精读Openzeppelin第10期——Create2.sol Michael.W基于Foundry精读Openzeppelin第11期——Math.sol Michael.W基于Foundry精读Openzeppelin第12期——SafeCast.sol Michael.W基于Foundry精读Openzeppelin第13期——Checkpoints.sol Michael.W基于Foundry精读Openzeppelin第14期——SafeMath.sol Michael.W基于Foundry精读Openzeppelin第15期——SignedMath.sol Michael.W基于Foundry精读Openzeppelin第16期——SignedSafeMath.sol Michael.W基于Foundry精读Openzeppelin第17期——BitMaps.sol Michael.W基于Foundry精读Openzeppelin第18期——DoubleEndedQueue.sol Michael.W基于Foundry精读Openzeppelin第19期——EnumerableSet.sol Michael.W基于Foundry精读Openzeppelin第20期——EnumerableMap.sol Michael.W基于Foundry精读Openzeppelin第21期——ERC165.sol (番外篇)Michael.W基于Foundry精读Openzeppelin第22期——内联汇编staticcall Michael.W基于Foundry精读Openzeppelin第23期——ERC165Checker.sol Michael.W基于Foundry精读Openzeppelin第24期——ERC165Storage.sol Michael.W基于Foundry精读Openzeppelin第25期——IERC1820Registry.sol Michael.W基于Foundry精读Openzeppelin第26期——ERC1820Implementer.sol Michael.W基于Foundry精读Openzeppelin第27期——Escrow.sol Michael.W基于Foundry精读Openzeppelin第28期——ConditionalEscrow.sol Michael.W基于Foundry精读Openzeppelin第29期——RefundEscrow.sol Michael.W基于Foundry精读Openzeppelin第30期——ECDSA.sol Michael.W基于Foundry精读Openzeppelin第31期——IERC1271.sol Michael.W基于Foundry精读Openzeppelin第32期——SignatureChecker.sol Michael.W基于Foundry精读Openzeppelin第33期——EIP712.sol Michael.W基于Foundry精读Openzeppelin第34期——MerkleProof.sol Michael.W基于Foundry精读Openzeppelin第35期——Ownable.sol Michael.W基于Foundry精读Openzeppelin第36期——Ownable2Step.sol Michael.W基于Foundry精读Openzeppelin第37期——AccessControl.sol Michael.W基于Foundry精读Openzeppelin第38期——AccessControlEnumerable.sol Michael.W基于Foundry精读Openzeppelin第39期——ERC20.sol Michael.W基于Foundry精读Openzeppelin第40期——ERC20Burnable.sol Michael.W基于Foundry精读Openzeppelin第41期——ERC20Capped.sol Michael.W基于Foundry精读Openzeppelin第42期——draft-ERC20Permit.sol Michael.W基于Foundry精读Openzeppelin第43期——Pausable.sol Michael.W基于Foundry精读Openzeppelin第44期——ERC20Pausable.sol Michael.W基于Foundry精读Openzeppelin第45期——ERC20FlashMint.sol Michael.W基于Foundry精读Openzeppelin第46期——ERC20Snapshot.sol Michael.W基于Foundry精读Openzeppelin第47期——SafeERC20.sol Michael.W基于Foundry精读Openzeppelin第48期——TokenTimelock.sol Michael.W基于Foundry精读Openzeppelin第49期——ERC20Wrapper.sol Michael.W基于Foundry精读Openzeppelin第50期——ERC20Votes.sol Michael.W基于Foundry精读Openzeppelin第51期——ERC20VotesComp.sol Michael.W基于Foundry精读Openzeppelin第52期——ERC4626.sol Michael.W基于Foundry精读Openzeppelin第53期——ERC20PresetFixedSupply.sol Michael.W基于Foundry精读Openzeppelin第54期——ERC20PresetMinterPauser.sol Michael.W基于Foundry精读Openzeppelin第55期——PaymentSplitter.sol Michael.W基于Foundry精读Openzeppelin第56期——VestingWallet.sol Michael.W基于Foundry精读Openzeppelin第57期——ReentrancyGuard.sol Michael.W基于Foundry精读Openzeppelin第58期——PullPayment.sol Michael.W基于Foundry精读Openzeppelin第59期——Proxy.sol Michael.W基于Foundry精读Openzeppelin第60期——Clones.sol Michael.W基于Foundry精读Openzeppelin第61期——ERC1967Upgrade.sol Michael.W基于Foundry精读Openzeppelin第62期——ERC1967Proxy.sol Michael.W基于Foundry精读Openzeppelin第63期——Initializable.sol Michael.W基于Foundry精读Openzeppelin第64期——UUPSUpgradeable.sol Michael.W基于Foundry精读Openzeppelin第65期——TransparentUpgradeableProxy.sol Michael.W基于Foundry精读Openzeppelin第66期——ProxyAdmin.sol Michael.W基于Foundry精读Openzeppelin第67期——BeaconProxy.sol Michael.W基于Foundry精读Openzeppelin第68期——UpgradeableBeacon.sol

Michael.W基于Foundry精读Openzeppelin第68期——UpgradeableBeacon.sol

  • Michael.W
  • 发布于 2024-08-13 20:40
  • 阅读 1837

UpgradeableBeacon库是信标代理模式中的信标合约的实现,与一个或多个BeaconProxy库实例配合使用。所有到BeaconProxy的调用都会被委托到本库指向的逻辑合约上。本库的owner具有更换逻辑合约地址的权限,从而实现信标代理合约的升级功能。

0. 版本

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

0.1 UpgradeableBeacon.sol

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

UpgradeableBeacon库是信标代理模式中的信标合约的实现,与一个或多个BeaconProxy库实例配合使用。所有到BeaconProxy的调用都会被委托到本库指向的逻辑合约上。本库的owner具有更换逻辑合约地址的权限,从而实现信标代理合约的升级功能。

注:BeaconProxy库详解参见:https://learnblockchain.cn/article/8810

1. 目标合约

UpgradeableBeacon合约可直接部署。

全部foundry测试合约:

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

测试使用的物料合约:

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

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

interface IImplementation {
    event ChangeStorageUint(uint);
}

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

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

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

2. 代码精读

2.1 constructor(address implementation_)

设置了逻辑合约地址,并且只有UpgradeableBeacon的部署者才可升级该信标合约。

    // 逻辑合约地址
    address private _implementation;

    // 当信标合约指向的逻辑合约地址变化时被抛出。该事件符合ERC1967标准
    // 注:ERC1967标准详解参见:https://learnblockchain.cn/article/8581
    event Upgraded(address indexed implementation);

    constructor(address implementation_) {
        // 设置逻辑合约地址为implementation_
        _setImplementation(implementation_);
    }

    // 为信标合约设置逻辑合约地址为newImplementation
    function _setImplementation(address newImplementation) private {
        // 要求newImplementation地址为合约地址,否则revert
        require(Address.isContract(newImplementation), "UpgradeableBeacon: implementation is not a contract");
        // 将逻辑合约地址设置为newImplementation
        _implementation = newImplementation;
    }

foundry代码验证:

contract UpgradeableBeaconTest is Test {
    Implementation private _implementation = new Implementation();
    UpgradeableBeacon private _testing = new UpgradeableBeacon(address(_implementation));

    function test_Constructor() external {
        assertEq(_testing.owner(), address(this));

        // revert if the implementation address is an EOA
        vm.expectRevert("UpgradeableBeacon: implementation is not a contract");
        new UpgradeableBeacon(address(1024));
    }
}

2.2 implementation() && upgradeTo(address newImplementation)

  • implementation():返回当前信标合约的逻辑合约地址;
  • upgradeTo(address newImplementation):owner升级更新该信标合约的逻辑合约地址为newImplementation。
    function implementation() public view virtual override returns (address) {
        // 返回_implementation
        return _implementation;
    }

    function upgradeTo(address newImplementation) public virtual onlyOwner {
        // 为该信标合约设置逻辑合约地址为newImplementation
        _setImplementation(newImplementation);
        // 抛出事件
        emit Upgraded(newImplementation);
    }

foundry代码验证:

contract UpgradeableBeaconTest is Test, IERC1967, IImplementation {
    Implementation private _implementation = new Implementation();
    UpgradeableBeacon private _testing = new UpgradeableBeacon(address(_implementation));
    ImplementationNew private _implementationNew = new ImplementationNew();

    function test_UpgradeToAndImplementation() external {
        // test for implementation()
        assertEq(_testing.implementation(), address(_implementation));

        // deploy beacon proxies
        BeaconProxy beaconProxy1 = new BeaconProxy(
            address(_testing),
            abi.encodeCall(
                Implementation.__Implementation_init,
                (1024)
            )
        );

        BeaconProxy beaconProxy2 = new BeaconProxy(
            address(_testing),
            abi.encodeCall(
                Implementation.__Implementation_init,
                (2048)
            )
        );

        // check beacon proxies
        assertEq(Implementation(address(beaconProxy1)).i(), 1024);
        assertEq(Implementation(address(beaconProxy2)).i(), 2048);
        // no function addI()
        vm.expectRevert();
        ImplementationNew(address(beaconProxy1)).addI(1);
        vm.expectRevert();
        ImplementationNew(address(beaconProxy2)).addI(1);

        // test for upgradeTo()
        vm.expectEmit();
        emit IERC1967.Upgraded(address(_implementationNew));

        _testing.upgradeTo(address(_implementationNew));
        assertEq(_testing.implementation(), address(_implementationNew));

        // check the upgrade for beacon proxy
        uint i = 4096;
        // function addI() is available for beaconProxy1
        vm.expectEmit(address(beaconProxy1));
        emit IImplementation.ChangeStorageUint(i);

        ImplementationNew(address(beaconProxy1)).addI(i);
        assertEq(ImplementationNew(address(beaconProxy1)).i(), 1024 + i);
        // function addI() is available for beaconProxy2
        vm.expectEmit(address(beaconProxy2));
        emit IImplementation.ChangeStorageUint(i);

        ImplementationNew(address(beaconProxy2)).addI(i);
        assertEq(ImplementationNew(address(beaconProxy2)).i(), 2048 + i);

        // revert if the caller is not owner
        vm.prank(address(1024));
        vm.expectRevert("Ownable: caller is not the owner");
        _testing.upgradeTo(address(_implementationNew));

        // revert if the implementation address is an EOA
        vm.expectRevert("UpgradeableBeacon: implementation is not a contract");
        _testing.upgradeTo(address(1024));
    }
}

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

1.jpeg

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

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

0 条评论

请先 登录 后评论