Skip to content

Commit

Permalink
test(LibInitializeGuard): test cases for LibInitializeGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
tringuyenskymavis committed Aug 26, 2024
1 parent 324ed7f commit 1850606
Show file tree
Hide file tree
Showing 12 changed files with 583 additions and 3 deletions.
4 changes: 2 additions & 2 deletions script/sample/contracts/SampleProxyDeploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract SampleProxyDeploy is SampleMigration {
instance = SampleProxy(_deployProxy(Contract.SampleProxy.key()));
assertEq(instance.getMessage(), ISharedArgument(address(vme)).sharedArguments().proxyMessage);

vm.prank(sender());
instance.initializeV4();
// vm.prank(sender());
// instance.initializeV4();
}
}
4 changes: 3 additions & 1 deletion script/sample/utils/Contract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ enum Contract {
tWETH,
Sample,
SampleClone,
SampleProxy
SampleProxy,
SampleProxyForTestingPurpose
}

using { key, name } for Contract global;
Expand All @@ -31,5 +32,6 @@ function name(Contract contractEnum) pure returns (string memory) {
if (contractEnum == Contract.tWRON) return "tWRON";
if (contractEnum == Contract.SampleClone) return "SampleClone";
if (contractEnum == Contract.SampleProxy) return "SampleProxy";
if (contractEnum == Contract.SampleProxyForTestingPurpose) return "SampleProxyForTestingPurpose";
revert("Contract: Unknown contract");
}
115 changes: 115 additions & 0 deletions src/mocks/ForTesting/InitializableTesting.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol";
import "@openzeppelin-4.9.3/contracts/utils/Address.sol";

abstract contract InitializableTesting {
/**
* @dev Indicates that the contract has been initialized.
* @custom:oz-retyped-from bool
*/
uint8 internal _initialized;

/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool internal _initializing;

/**
* @dev Triggered when the contract has been initialized or reinitialized.
*/
event Initialized(uint8 version);

/**
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
* `onlyInitializing` functions can be used to initialize parent contracts.
*
* Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a
* constructor.
*
* Emits an {Initialized} event.
*/
modifier initializer() {
bool isTopLevelCall = !_initializing;
require(
(isTopLevelCall && _initialized < 1) || (!Address.isContract(address(this)) && _initialized == 1),
"Initializable: contract is already initialized"
);
_initialized = 1;
if (isTopLevelCall) {
_initializing = true;
}
_;
if (isTopLevelCall) {
_initializing = false;
emit Initialized(1);
}
}

/**
* @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
* contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
* used to initialize parent contracts.
*
* A reinitializer may be used after the original initialization step. This is essential to configure modules that
* are added through upgrades and that require initialization.
*
* When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
* cannot be nested. If one is invoked in the context of another, execution will revert.
*
* Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
* a contract, executing them in the right order is up to the developer or operator.
*
* WARNING: setting the version to 255 will prevent any future reinitialization.
*
* Emits an {Initialized} event.
*/
modifier reinitializer(uint8 version) {
require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
_initialized = version;
_initializing = true;
_;
_initializing = false;
emit Initialized(version);
}

/**
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
* {initializer} and {reinitializer} modifiers, directly or indirectly.
*/
modifier onlyInitializing() {
require(_initializing, "Initializable: contract is not initializing");
_;
}

/**
* @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
* Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
* to any version. It is recommended to use this to lock implementation contracts that are designed to be called
* through proxies.
*
* Emits an {Initialized} event the first time it is successfully executed.
*/
function _disableInitializers() internal virtual {
require(!_initializing, "Initializable: contract is initializing");
if (_initialized != type(uint8).max) {
_initialized = type(uint8).max;
emit Initialized(type(uint8).max);
}
}

/**
* @dev Returns the highest version that has been initialized. See {reinitializer}.
*/
function _getInitializedVersion() internal view returns (uint8) {
return _initialized;
}

/**
* @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
*/
function _isInitializing() internal view returns (bool) {
return _initializing;
}
}
30 changes: 30 additions & 0 deletions src/mocks/ForTesting/SampleProxyForTestingPurpose.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol";
import { Initializable as InitializableV5 } from
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol";
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol";

contract SampleProxyForTestingPurpose is Ownable, InitializableV5 {
uint256[50] private __gap;

string internal _message;
address internal _addr;

constructor() {
// _disableInitializers();
}

function initialize(string calldata message) external initializer {
_message = message;
}

function setMessage(string memory message) public {
_message = message;
}

function getMessage() public view returns (string memory) {
return _message;
}
}
30 changes: 30 additions & 0 deletions src/mocks/ForTesting/SampleProxyForTestingPurpose2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol";
import "./InitializableTesting.sol";

contract SampleProxyForTestingPurpose2 is Ownable, InitializableTesting {
uint256[50] private __gap;

string internal _message;
address internal _addr;

constructor() {
_disableInitializers();
}

function initialize(string calldata message) public {
_message = message;
}

function abcXYZ(string calldata message) public { }

function setMessage(string memory message) public {
_message = message;
}

function getMessage() public view returns (string memory) {
return _message;
}
}
29 changes: 29 additions & 0 deletions src/mocks/ForTesting/SampleProxyForTestingPurpose3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol";
import "./InitializableTesting.sol";

contract SampleProxyForTestingPurpose3 is Ownable, InitializableTesting {
uint256[50] private __gap;

string internal _message;
address internal _addr;

constructor() {
_disableInitializers();
}

function initialize(string calldata message) public initializer {
_message = message;
_initialized = type(uint8).max - 10;
}

function setMessage(string memory message) public {
_message = message;
}

function getMessage() public view returns (string memory) {
return _message;
}
}
30 changes: 30 additions & 0 deletions src/mocks/ForTesting/SampleProxyForTestingPurpose4.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol";
import { Initializable as InitializableV5 } from
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol";
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol";

contract SampleProxyForTestingPurpose4 is Ownable, Initializable {
uint256[50] private __gap;

string internal _message;
address internal _addr;

constructor() {
_disableInitializers();
}

function initialize(string calldata message) external initializer {
_message = message;
}

function setMessage(string memory message) public {
_message = message;
}

function getMessage() public view returns (string memory) {
return _message;
}
}
35 changes: 35 additions & 0 deletions src/mocks/ForTesting/SampleProxyForTestingPurpose5.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol";
import { Initializable as InitializableV5 } from
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol";
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol";

contract SampleProxyForTestingPurpose5 is Ownable, Initializable {
uint256[50] private __gap;

string internal _message;
address internal _addr;
uint256 internal _newVariable;

constructor() {
_disableInitializers();
}

function initialize(string calldata message) external initializer {
_message = message;
}

function initializeV2(uint256 newValues) external reinitializer(2) {
_newVariable = newValues;
}

function setMessage(string memory message) public {
_message = message;
}

function getMessage() public view returns (string memory) {
return _message;
}
}
40 changes: 40 additions & 0 deletions src/mocks/ForTesting/SampleProxyForTestingPurpose6.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol";
import { Initializable as InitializableV5 } from
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol";
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol";

contract SampleProxyForTestingPurpose6 is Ownable, Initializable {
uint256[50] private __gap;

string internal _message;
address internal _addr;
uint256 internal _newVariable;
uint256 internal _newVariable2;

constructor() {
_disableInitializers();
}

function initialize(string calldata message) external initializer {
_message = message;
}

function initializeV2(uint256 newValues) external reinitializer(2) {
_newVariable = newValues;
}

function initializeV3(uint256 newValues) external reinitializer(3) {
_newVariable2 = newValues;
}

function setMessage(string memory message) public {
_message = message;
}

function getMessage() public view returns (string memory) {
return _message;
}
}
48 changes: 48 additions & 0 deletions src/mocks/ForTesting/SampleProxyForTestingPurpose7.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol";
import { Initializable as InitializableV5 } from
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol";
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol";

contract SampleProxyForTestingPurpose7 is Ownable, Initializable {
uint256[50] private __gap;

string internal _message;
address internal _addr;
uint256 internal _newVariable;
uint256 internal _newVariable2;

constructor() {
_disableInitializers();
}

function initialize(string calldata message) external initializer {
_message = message;
}

function initializeV2(uint256 newValues) external reinitializer(2) {
_newVariable = newValues;
}

function initializeV3(uint256 newValues) external reinitializer(3) {
_newVariable2 = newValues;
}

function initializeV4(uint256 newValues) external reinitializer(3) {
_newVariable2 = newValues;
}

function initializeV5(uint256 newValues) external reinitializer(5) {
_newVariable2 = newValues;
}

function setMessage(string memory message) public {
_message = message;
}

function getMessage() public view returns (string memory) {
return _message;
}
}
Loading

0 comments on commit 1850606

Please sign in to comment.