Initializable库用于开发可升级合约或代理合约背后的逻辑合约。由于代理合约无法执行constructor
函数,通常是将该函数逻辑放到一个external函数中(称为initialize函数)。本库可确保initializer函数全局只能被调用一次。
[openzeppelin]:v4.8.3,[forge-std]:v1.5.6
Initializable库用于开发可升级合约或代理合约背后的逻辑合约。由于代理合约无法执行constructor
函数,通常是将该函数逻辑放到一个external函数中(称为initialize函数)。本库可确保initializer函数全局只能被调用一次。initializer函数具有version且每个version只能使用一次。该机制可防止重新执行原初始化步骤但又允许在合约升级时添加新的初始化步骤。
注:合约部署时要避免暴露一个未初始化的代理合约,建议在部署代理合约时直接通过constructor
(见ERC1967Proxy)的_data
参数同步进行初始化。
ERC1967Proxy库详解参见:https://learnblockchain.cn/article/8594
继承Initializable合约:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "openzeppelin-contracts/contracts/proxy/utils/Initializable.sol";
interface IMockInitializable {
enum ConstructorLogic{
NO_LOGIC,
DO_WITH_SINGLE_INITIALIZER,
DO_WITH_NESTED_INITIALIZER,
DO_WITH_INITIALIZERS_IN_SEQUENCE
}
event DoWithInitializer(uint counter);
event DoInNestedInitializer(uint counter);
event DoWithReinitializer(uint counter);
event DoWithOnlyInitializing(uint8 version, bool isInitializing, uint counter);
}
contract MockInitializable is Initializable, IMockInitializable {
uint public counter;
constructor(ConstructorLogic flag){
if (flag == ConstructorLogic.NO_LOGIC) {
return;
} else if (flag == ConstructorLogic.DO_WITH_SINGLE_INITIALIZER) {
doWithInitializer();
}
else if (flag == ConstructorLogic.DO_WITH_NESTED_INITIALIZER) {
doWithNestedInitializer();
} else if (flag == ConstructorLogic.DO_WITH_INITIALIZERS_IN_SEQUENCE) {
doWithInitializer();
doWithInitializer();
} else {
revert("unsupported flag");
}
}
function getInitializedVersion() external view returns (uint8){
return _getInitializedVersion();
}
function isInitializing() external view returns (bool){
return _isInitializing();
}
function doWithInitializer() public initializer {
uint currentCounter = counter;
counter += 1;
emit DoWithInitializer(currentCounter);
}
function doWithNestedInitializer() public initializer {
doWithInitializer();
uint currentCounter = counter;
counter += 1;
emit DoInNestedInitializer(currentCounter);
}
function doWithReinitializer(uint8 version) public reinitializer(version) {
counter = version;
emit DoWithReinitializer(version);
}
function doWithReinitializersInSequence(uint8 version) external {
doWithReinitializer(version);
doWithReinitializer(version + 1);
}
// revert
function doWithNestedReinitializer(uint8 version) external reinitializer(version) {
doWithReinitializer(version);
counter = version + 1;
}
// revert
function doWithOnlyInitializing() public onlyInitializing {
uint currentCounter = counter;
counter += 1;
emit DoWithOnlyInitializing(
_getInitializedVersion(),
_isInitializing(),
currentCounter
);
}
function doWithNestedOnlyInitializing() public onlyInitializing {
doWithOnlyInitializing();
uint currentCounter = counter;
counter += 1;
emit DoWithOnlyInitializing(
_getInitializedVersion(),
_isInitializing(),
currentCounter
);
}
function doWithOnlyInitializingInInitializer() external initializer {
doWithOnlyInitializing();
uint currentCounter = counter;
counter += 1;
emit DoWithInitializer(currentCounter);
}
function doWithOnlyInitializingInSequenceInInitializer() external initializer {
doWithOnlyInitializing();
doWithOnlyInitializing();
uint currentCounter = counter;
counter += 1;
emit DoWithInitializer(currentCounter);
}
function doWithNestedOnlyInitializingInInitializer() external initializer {
doWithNestedOnlyInitializing();
uint currentCounter = counter;
counter += 1;
emit DoWithInitializer(currentCounter);
}
function doWithOnlyInitializingInReinitializer(uint8 version) external reinitializer(version) {
doWithOnlyInitializing();
uint currentCounter = counter;
counter += 1;
emit DoWithReinitializer(currentCounter);
}
function doWithOnlyInitializingInSequenceInReinitializer(uint8 version) external reinitializer(version) {
doWithOnlyInitializing();
doWithOnlyInitializing();
uint currentCounter = counter;
counter += 1;
emit DoWithReinitializer(currentCounter);
}
function doWithNestedOnlyInitializingInReinitializer(uint8 version) external reinitializer(version) {
doWithNestedOnlyInitializing();
uint currentCounter = counter;
counter += 1;
emit DoWithReinitializer(currentCounter);
}
function disableInitializers() external {
_disableInitializers();
}
// revert
function disableInitializersInInitializer() external initializer {
_disableInitializers();
}
// revert
function disableInitializersInReinitializer(uint8 version) external reinitializer(version) {
_disableInitializers();
}
}
全部foundry测试合约:
测试使用的物料合约:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "openzeppelin-contracts/contracts/proxy/utils/Initializable.sol";
// the implementation before upgrade
interface IImplementation {
event InitializeStorageUint(uint);
}
contract Implementation is Initializable, IImplementation {
// storage
uint public i;
// initializer
function __Implementation_initialize(uint i_) external initializer {
i = i_;
emit InitializeStorageUint(i_);
}
function setI(uint newI) external {
i = newI;
}
}
// supplement in the new implementation
interface IModuleAdded {
event InitializeStorageAddress(address);
}
contract ModuleAdded is Initializable, IModuleAdded {
// supplement storage
address public addr;
// initializer modified with onlyInitializing
function __ModuleAdded_initialize(address addr_) internal onlyInitializing {
addr = addr_;
emit InitializeStorageAddress(addr_);
}
function setAddr(address newAddr) external {
addr = newAddr;
}
}
// new implementation to upgrade to
contract ImplementationToUpgrade is Implementation, ModuleAdded {
function reinitialize(uint newI, address newAddr, uint8 version) external reinitializer(version) {
__ModuleAdded_initialize(newAddr);
this.setI(newI);
}
function disableInitializers() external {
_disableInitializers();
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Proxy.sol";
contract MockERC1967Proxy is ERC1967Proxy {
constructor(address logic, bytes memory data)
ERC1967Proxy(logic, data)
{}
function getImplementation() external view returns (address){
return _getImplementation();
}
function upgradeToAndCall(address newImplementation, bytes memory data) external {
_upgradeToAndCall(newImplementation, data, false);
}
}
_getInitializedVersion() internal
:返回合约已完成初始化的最高version;_isInitializing() internal
:返回合约是否正在处于初始化过程。 // 合约已经完成初始化的最高version,可用于判断合约是否已经完成初始化
uint8 private _initialized;
// 用于表明合约正在处于初始化过程的标志位
bool private _initializing;
function _getInitializedVersion() internal view returns (uint8) {
// 返回标志位_initialized
return _initialized;
}
function _isInitializing() internal view returns (bool) {
// 返回标志位_initializing
return _initializing;
}
用于修饰逻辑合约中用来做代理合约初始化的external函数,可以确保该函数最多只可被调用一次。在被该修饰器修饰的函数中,可用onlyInitializing
修饰器修饰逻辑合约继承的父类合约的初始化函数。
该修饰器和reinitializer(1)
都会将合约已经完成初始化的最高version设为1,不同的是该被修饰器修饰的函数可以在constructor
函数中嵌套调用。
// 当合约被初始化和重初始化时抛出的事件
event Initialized(uint8 version);
modifier initializer() {
// 用于标识本次调用是否是首次进入被initializer修饰的函数
bool isTopLevelCall = !_initializing;
// 进行检查,除了以下两种情况外都会revert:
// 1. 本次调用是首次进入被initializer修饰的函数 && 本次调用之前没有执行过被initializer修饰的函数;
// 2. 本次调用非首次进入被initializer修饰的函数 && 本次调用是处于constructor函数中。
require(
(isTopLevelCall && _initialized < 1) || (!Address.isContract(address(this)) && _initialized == 1),
"Initializable: contract is already initialized"
);
// 合约已经完成初始化的最高version置为1
_initialized = 1;
if (isTopLevelCall) {
// 如果本次调用是首次进入被initializer修饰的函数,将合约正在处于初始化过程的标志位置为true
_initializing = true;
}
// 执行被修饰函数逻辑
_;
if (isTopLevelCall) {
// 如果本次调用是首次进入被initializer修饰的函数,将合约正在处于初始化过程的标志位置为false
_initializing = false;
// 抛出事件
emit Initialized(1);
}
}
foundry代码验证:
contract InitializableTest is Test, IMockInitializable {
event Initialized(uint8 version);
function test_Initializer_SingleInitializerInConstructor() external {
vm.expectEmit();
emit IMockInitializable.DoWithInitializer(0);
vm.expectEmit();
emit Initialized(1);
MockInitializable testingSingleInitializerInConstructor = new MockInitializable(ConstructorLogic.DO_WITH_SINGLE_INITIALIZER);
assertEq(testingSingleInitializerInConstructor.counter(), 1);
assertEq(testingSingleInitializerInConstructor.getInitializedVersion(), 1);
assertFalse(testingSingleInitializerInConstructor.isInitializing());
// revert if call the initializer after constructor
vm.expectRevert("Initializable: contract is already initialized");
testingSingleInitializerInConstructor.doWithInitializer();
}
function test_Initializer_NestedInitializerInConstructor() external {
vm.expectEmit();
emit IMockInitializable.DoWithInitializer(0);
vm.expectEmit();
emit IMockInitializable.DoInNestedInitializer(1);
vm.expectEmit();
emit Initialized(1);
MockInitializable testingNestedInitializerInConstructor = new MockInitializable(ConstructorLogic.DO_WITH_NESTED_INITIALIZER);
assertEq(testingNestedInitializerInConstructor.counter(), 2);
assertEq(testingNestedInitializerInConstructor.getInitializedVersion(), 1);
assertFalse(testingNestedInitializerInConstructor.isInitializing());
// revert if call the initializer after constructor
vm.expectRevert("Initializable: contract is already initialized");
testingNestedInitializerInConstructor.doWithInitializer();
}
function test_Initializer_InitializersInSequenceInConstructor() external {
vm.expectEmit();
emit IMockInitializable.DoWithInitializer(0);
vm.expectEmit();
emit Initialized(1);
vm.expectEmit();
emit IMockInitializable.DoWithInitializer(1);
vm.expectEmit();
emit Initialized(1);
MockInitializable testingInitializersInSequenceInConstructor = new MockInitializable(ConstructorLogic.DO_WITH_INITIALIZERS_IN_SEQUENCE);
assertEq(testingInitializersInSequenceInConstructor.counter(), 2);
assertEq(testingInitializersInSequenceInConstructor.getInitializedVersion(), 1);
assertFalse(testingInitializersInSequenceInConstructor.isInitializing());
// revert if call the initializer after constructor
vm.expectRevert("Initializable: contract is already initialized");
testingInitializersInSequenceInConstructor.doWithInitializer();
}
function test_Initializer_OutOfConstructor() external {
MockInitializable testingEmptyConstructor = new MockInitializable(ConstructorLogic.NO_LOGIC);
assertEq(testingEmptyConstructor.getInitializedVersion(), 0);
assertFalse(testingEmptyConstructor.isInitializing());
vm.expectEmit(address(testingEmptyConstructor));
emit IMockInitializable.DoWithInitializer(0);
vm.expectEmit(address(testingEmptyConstructor));
emit Initialized(1);
testingEmptyConstructor.doWithInitializer();
assertEq(testingEmptyConstructor.counter(), 1);
assertEq(testingEmptyConstructor.getInitializedVersion(), 1);
assertFalse(testingEmptyConstructor.isInitializing());
// revert if call the initializer again
vm.expectRevert("Initializable: contract is already initialized");
testingEmptyConstructor.doWithInitializer();
}
}
用于修饰逻辑合约中用来做代理合约重初始化的external函数,可以确保该函数最多只可被调用一次。但是有个前提:代理合约重初始化之前的version必须小于该修饰器传入的version。应在代理合约完成初始化后使用reinitializer
,这对通过合约升级添加的模块和需要初始化的模块至关重要。
当version为1时,reinitializer(1)
等同于initializer
,但是reinitializer
修饰的函数中不可以调用被reinitializer
修饰的函数。
关于version的递增问题:version可以以大于1的增量递增。如果多个被reinitializer
修饰函数共同处于一个逻辑合约中,开发人员需要手动确保这些函数以version递增的顺序被执行。
注:version最大可以到255,之后无法再进行重初始化。
modifier reinitializer(uint8 version) {
// 要求本次调用是首次进入被reinitializer修饰的函数 且 传入的version大于本合约已经完成初始化的最高version,否则revert
require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
// 将合约已经完成初始化的最高version置为传入的version
_initialized = version;
// 将合约正在处于初始化过程的标志位置为true
_initializing = true;
// 执行被修饰函数逻辑
_;
// 将合约正在处于初始化过程的标志位置为false
_initializing = false;
// 抛出事件
emit Initialized(version);
}
foundry代码验证:
contract InitializableTest is Test, IMockInitializable {
event Initialized(uint8 version);
function test_Reinitializer() external {
// case 1: test call single reinitializer
MockInitializable testing = new MockInitializable(ConstructorLogic.DO_WITH_SINGLE_INITIALIZER);
assertEq(testing.counter(), 1);
assertEq(testing.getInitializedVersion(), 1);
uint8 version = uint8(testing.getInitializedVersion()) + 1;
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithReinitializer(version);
vm.expectEmit(address(testing));
emit Initialized(version);
testing.doWithReinitializer(version);
assertEq(testing.counter(), version);
assertEq(testing.getInitializedVersion(), version);
assertFalse(testing.isInitializing());
// revert if call single reinitializer again with the same or lesser version
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithReinitializer(version);
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithReinitializer(version - 1);
// revert if call initializer again
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithInitializer();
// case 2: test call initializers in sequence
version = testing.getInitializedVersion() + 10;
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithReinitializer(version);
vm.expectEmit(address(testing));
emit Initialized(version);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithReinitializer(version + 1);
vm.expectEmit(address(testing));
emit Initialized(version + 1);
testing.doWithReinitializersInSequence(version);
assertEq(testing.counter(), version + 1);
assertEq(testing.getInitializedVersion(), version + 1);
assertFalse(testing.isInitializing());
// revert if call single reinitializer again with the same or lesser version
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithReinitializer(version + 1);
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithReinitializer(version + 1 - 1);
// revert if call initializer again
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithInitializer();
// case 3: test call nested reinitializers
// revert
version = testing.getInitializedVersion() + 1;
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithNestedReinitializer(version);
}
}
用于修饰只可以被initializer
或reinitializer
直接或间接调用的函数。
modifier onlyInitializing() {
// 要求合约此时处于初始化过程,否则revert
require(_initializing, "Initializable: contract is not initializing");
// 执行被修饰函数逻辑
_;
}
foundry代码验证:
contract InitializableTest is Test, IMockInitializable {
event Initialized(uint8 version);
function test_OnlyInitializing() external {
// case 1: revert when call onlyInitializing directly out of initializer or reinitializer
MockInitializable testing = new MockInitializable(ConstructorLogic.NO_LOGIC);
vm.expectRevert("Initializable: contract is not initializing");
testing.doWithOnlyInitializing();
// case 2: revert when call nested onlyInitializing directly out of initializer or reinitializer
vm.expectRevert("Initializable: contract is not initializing");
testing.doWithNestedOnlyInitializing();
// case 3: call onlyInitializing in initializer
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(1, true, 0);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithInitializer(1);
vm.expectEmit(address(testing));
emit Initialized(1);
testing.doWithOnlyInitializingInInitializer();
assertEq(testing.counter(), 0 + 2);
assertEq(testing.getInitializedVersion(), 1);
assertFalse(testing.isInitializing());
// case 4: call onlyInitializing in sequence in initializer
testing = new MockInitializable(ConstructorLogic.NO_LOGIC);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(1, true, 0);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(1, true, 1);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithInitializer(2);
vm.expectEmit(address(testing));
emit Initialized(1);
testing.doWithOnlyInitializingInSequenceInInitializer();
assertEq(testing.counter(), 0 + 3);
assertEq(testing.getInitializedVersion(), 1);
assertFalse(testing.isInitializing());
// case 5: call nested onlyInitializing in initializer
testing = new MockInitializable(ConstructorLogic.NO_LOGIC);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(1, true, 0);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(1, true, 1);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithInitializer(2);
vm.expectEmit(address(testing));
emit Initialized(1);
testing.doWithNestedOnlyInitializingInInitializer();
assertEq(testing.counter(), 0 + 3);
assertEq(testing.getInitializedVersion(), 1);
assertFalse(testing.isInitializing());
// case 6: call onlyInitializing in reinitializer
uint8 version = testing.getInitializedVersion() + 10;
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(version, true, 3);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithReinitializer(4);
vm.expectEmit(address(testing));
emit Initialized(version);
testing.doWithOnlyInitializingInReinitializer(version);
assertEq(testing.counter(), 3 + 2);
assertEq(testing.getInitializedVersion(), version);
assertFalse(testing.isInitializing());
// case 7: call onlyInitializing in sequence in reinitializer
version = testing.getInitializedVersion() + 10;
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(version, true, 5);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(version, true, 6);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithReinitializer(7);
vm.expectEmit(address(testing));
emit Initialized(version);
testing.doWithOnlyInitializingInSequenceInReinitializer(version);
assertEq(testing.counter(), 5 + 3);
assertEq(testing.getInitializedVersion(), version);
assertFalse(testing.isInitializing());
// case 8: call nested onlyInitializing in reinitializer
version = testing.getInitializedVersion() + 10;
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(version, true, 8);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithOnlyInitializing(version, true, 9);
vm.expectEmit(address(testing));
emit IMockInitializable.DoWithReinitializer(10);
vm.expectEmit(address(testing));
emit Initialized(version);
testing.doWithNestedOnlyInitializingInReinitializer(version);
assertEq(testing.counter(), 8 + 3);
assertEq(testing.getInitializedVersion(), version);
assertFalse(testing.isInitializing());
}
}
锁住合约使其无法进行更高version的重初始化。该函数不可在被initializer
或reinitializer
修饰函数中被调用,一般在constructor
函数中调用此函数可以使得本合约无法再被初始化和重初始化。如果想禁止自己的代理合约可升级,可以在其背后的逻辑合约中暴露此函数。
function _disableInitializers() internal virtual {
// 要求合约此时未处于初始化过程
require(!_initializing, "Initializable: contract is initializing");
if (_initialized < type(uint8).max) {
// 如果已初始化的最高version小于uint8的最大值,则将其置为uint8最大值
_initialized = type(uint8).max;
// 抛出事件
emit Initialized(type(uint8).max);
}
}
foundry代码验证:
contract InitializableTest is Test, IMockInitializable {
event Initialized(uint8 version);
function test_DisableInitializers() external {
// case 1: disable initializers
// case 1.1: disable before initializer
MockInitializable testing = new MockInitializable(ConstructorLogic.NO_LOGIC);
vm.expectEmit(address(testing));
emit Initialized(type(uint8).max);
testing.disableInitializers();
assertEq(testing.getInitializedVersion(), type(uint8).max);
assertFalse(testing.isInitializing());
// can't initialize or reinitialize again
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithInitializer();
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithReinitializer(type(uint8).max);
// case 1.2: disable after initializer
testing = new MockInitializable(ConstructorLogic.DO_WITH_SINGLE_INITIALIZER);
vm.expectEmit(address(testing));
emit Initialized(type(uint8).max);
testing.disableInitializers();
assertEq(testing.getInitializedVersion(), type(uint8).max);
assertFalse(testing.isInitializing());
// can't reinitialize again
vm.expectRevert("Initializable: contract is already initialized");
testing.doWithReinitializer(type(uint8).max);
// case 2: revert if call _disableInitializers in initializer
testing = new MockInitializable(ConstructorLogic.NO_LOGIC);
vm.expectRevert("Initializable: contract is initializing");
testing.disableInitializersInInitializer();
// case 3: revert if call _disableInitializers in reinitializer
testing = new MockInitializable(ConstructorLogic.DO_WITH_SINGLE_INITIALIZER);
vm.expectRevert("Initializable: contract is initializing");
testing.disableInitializersInReinitializer(1 + 1);
}
}
foundry代码验证:
contract InitializableTest is Test, IMockInitializable, IImplementation, IModuleAdded {
event Initialized(uint8 version);
function test_AllModifiersInProxyAndImplementationPattern() external {
// deploy Implementation first
Implementation currentImplementation = new Implementation();
// deploy proxy and execute the initializer
uint i = 1024;
vm.expectEmit();
emit IImplementation.InitializeStorageUint(i);
vm.expectEmit();
emit Initialized(1);
address payable proxyAddress = payable(address(new MockERC1967Proxy(
address(currentImplementation),
abi.encodeCall(
currentImplementation.__Implementation_initialize,
(i)
)
)));
// check the initialized result of proxy
assertEq(MockERC1967Proxy(proxyAddress).getImplementation(), address(currentImplementation));
assertEq(Implementation(proxyAddress).i(), i);
// call proxy
i = 2048;
Implementation(proxyAddress).setI(i);
assertEq(Implementation(proxyAddress).i(), i);
// revert if initialize again
vm.expectRevert("Initializable: contract is already initialized");
Implementation(proxyAddress).__Implementation_initialize(i);
// deploy the implementation to upgrade
ImplementationToUpgrade newImplementation = new ImplementationToUpgrade();
// upgrade and execute the reinitialize
i = 4096;
address addr = address(1024);
uint8 version = 1 + 1;
vm.expectEmit(proxyAddress);
emit IModuleAdded.InitializeStorageAddress(addr);
vm.expectEmit(proxyAddress);
emit Initialized(version);
MockERC1967Proxy(proxyAddress).upgradeToAndCall(
address(newImplementation),
abi.encodeCall(
newImplementation.reinitialize,
(i, addr, version)
)
);
// check the reinitialized result of proxy
assertEq(MockERC1967Proxy(proxyAddress).getImplementation(), address(newImplementation));
assertEq(ImplementationToUpgrade(proxyAddress).i(), i);
assertEq(ImplementationToUpgrade(proxyAddress).addr(), addr);
// call proxy with old and new functions
i = 8192;
ImplementationToUpgrade(proxyAddress).setI(i);
assertEq(ImplementationToUpgrade(proxyAddress).i(), i);
addr = address(2048);
ImplementationToUpgrade(proxyAddress).setAddr(addr);
assertEq(ImplementationToUpgrade(proxyAddress).addr(), addr);
// reinitialize again with larger version
i = 16384;
addr = address(4096);
vm.expectEmit(proxyAddress);
emit IModuleAdded.InitializeStorageAddress(addr);
vm.expectEmit(proxyAddress);
emit Initialized(version + 1);
ImplementationToUpgrade(proxyAddress).reinitialize(i, addr, version + 1);
// check the reinitialized result of proxy
assertEq(ImplementationToUpgrade(proxyAddress).i(), i);
assertEq(ImplementationToUpgrade(proxyAddress).addr(), addr);
// disable the proxy from reinitializers
vm.expectEmit(proxyAddress);
emit Initialized(type(uint8).max);
ImplementationToUpgrade(proxyAddress).disableInitializers();
// revert if reinitialize again
vm.expectRevert("Initializable: contract is already initialized");
ImplementationToUpgrade(proxyAddress).reinitialize(i, addr, type(uint8).max);
}
}
ps: 本人热爱图灵,热爱中本聪,热爱V神。 以下是我个人的公众号,如果有技术问题可以关注我的公众号来跟我交流。 同时我也会在这个公众号上每周更新我的原创文章,喜欢的小伙伴或者老伙计可以支持一下! 如果需要转发,麻烦注明作者。十分感谢!
公众号名称:后现代泼痞浪漫主义奠基人
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!