Skip to content

Commit

Permalink
Add testcases for transitive initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Dec 19, 2024
1 parent 4e8c435 commit a8d06cf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
46 changes: 45 additions & 1 deletion packages/core/contracts/test/ValidationsInitializer.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {ERC20Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";

// These contracts are for testing only. They are not safe for use in production, and do not represent best practices.

Expand Down Expand Up @@ -316,3 +320,43 @@ contract Child_Has_PrivateInitializer_Bad is Parent__OnlyInitializingModifier {
__Parent_init();
}
}

// ==== Transitive initialization ====

contract Ownable_Ok is Initializable, ERC20Upgradeable, OwnableUpgradeable {
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

function initialize(address initialOwner) initializer public {
__ERC20_init("MyToken", "MTK");
__Ownable_init(initialOwner);
}
}

contract Ownable2Step_Ok is Initializable, ERC20Upgradeable, Ownable2StepUpgradeable {
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

function initialize(address initialOwner) initializer public {
__ERC20_init("MyToken", "MTK");
__Ownable_init(initialOwner); // Transitive dependency that needs to be initialized
__Ownable2Step_init();
}
}

contract Ownable2Step_Bad is Initializable, ERC20Upgradeable, Ownable2StepUpgradeable {
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

function initialize() initializer public {
__ERC20_init("MyToken", "MTK");
// Missing Ownable, which is a transitive dependency that needs to be initialized
__Ownable2Step_init();
}
}
8 changes: 8 additions & 0 deletions packages/core/src/validate-initializers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,11 @@ testRejects(
testAccepts('Child_Of_ParentPrivateInitializer_Ok', 'transparent');
testAccepts('Child_Of_ParentPublicInitializer_Ok', 'transparent');
testRejects('Child_Has_PrivateInitializer_Bad', 'transparent', 'Contract is missing an initializer');

testAccepts('Ownable_Ok', 'transparent');
testAccepts('Ownable2Step_Ok', 'transparent');
testRejects(
'Ownable2Step_Bad',
'transparent',
'Contract is missing initializer calls for one or more parent contracts: `OwnableUpgradeable`',
);

0 comments on commit a8d06cf

Please sign in to comment.