1. 引言
前序博客有:
合约升级示例代码可参看:
其中,主要由三部分组成:
- 1)
UpgradeBeacon.sol
:存储了implementation合约的地址,支持controller对该implementation地址进行升级操作。 - 2)
UpgradeBeaconController.sol
:设置为UpgradeBeacon合约的controller,可改变其存储的implementation地址。 - 3)
UpgradeBeaconProxy.sol
:为Proxy合约,可delegate all logic, including initialization, to an implementation合约。
2. UpgradeBeacon.sol
UpgradeBeacon合约中存储了implementation合约的地址,支持controller对该implementation地址进行升级操作。
- 1)在0age’s implementation中无function selectors的gas saving方案:https://github.com/dharma-eng/dharma-smart-wallet/blob/master/contracts/proxies/smart-wallet/UpgradeBeaconProxyV1.sol
- 2)在OpenZeppelin的implementation基础上,增加了对每个implementation应为合约的安全检查,以及每次implementation改变时都释放an Upgrade event:https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/proxy/beacon/BeaconProxy.sol
3. UpgradeBeaconController.sol
UpgradeBeaconController.sol可设置UpgradeBeacon合约的controller,可改变其存储的implementation地址。
- 受0age的https://github.com/dharma-eng/dharma-smart-wallet/blob/master/contracts/upgradeability/DharmaUpgradeBeaconController.sol 启发,实现了a minimal version。
4. UpgradeBeaconProxy.sol
UpgradeBeaconProxy.sol为Proxy合约,可delegate all logic, including initialization, to an implementation合约。
该implementation合约存储在一个UpgradeBeacon合约中。
通过对UpgradeBeacon合约进行升级,可改变该implementation和玉。
UpgradeBeaconProxy.sol合约的UpgradeBeacon合约在部署时即指定了,后续不可修改。
- 1)0age的实现中,将UpgradeBeacon地址维护在contract storage之外,可节约gas费:https://github.com/dharma-eng/dharma-smart-wallet/blob/master/contracts/proxies/smart-wallet/UpgradeBeaconProxyV1.sol
- 2)OpenZeppelin的实现中,在部署时增加了对UpgradeBeacon合约和implementation合约 确实是有效合约 的安全检查:https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/proxy/beacon/BeaconProxy.sol