From 210c71b54f037d691d93134a7858c84d92f8ac10 Mon Sep 17 00:00:00 2001 From: Eric Lau Date: Tue, 10 Dec 2024 16:59:57 -0500 Subject: [PATCH] Test initializer visibility --- .../contracts/test/ValidationsInitializer.sol | 28 ++++++++++++++++--- .../core/src/validate-initializers.test.ts | 4 +++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/core/contracts/test/ValidationsInitializer.sol b/packages/core/contracts/test/ValidationsInitializer.sol index 97a18b714..b4a7c41f0 100644 --- a/packages/core/contracts/test/ValidationsInitializer.sol +++ b/packages/core/contracts/test/ValidationsInitializer.sol @@ -24,19 +24,19 @@ contract Parent__OnlyInitializingModifier is Initializable { } contract Parent_InitializeName { - function initialize() public virtual {} + function initialize() internal virtual {} } contract Parent_InitializerName { - function initializer() public {} + function initializer() internal {} } contract Parent_ReinitializeName { - function reinitialize(uint64 version) public {} + function reinitialize(uint64 version) internal {} } contract Parent_ReinitializerName { - function reinitializer(uint64 version) public {} + function reinitializer(uint64 version) internal {} } // ==== Child contracts ==== @@ -220,4 +220,24 @@ contract InitializationOrder_Duplicate_UnsafeAllow_Call is A, B, C, Parent_NoIni __B_init(); __C_init(); } +} + +// ==== Initializer visibility ==== + +contract Parent_PrivateInitializer { + function initialize() private {} // not considered an initializer because it's private +} + +contract Parent_PublicInitializer { + function initialize() public {} // does not strictly need to be called by child +} + +contract Child_Of_ParentPrivateInitializer_Ok is Parent_PrivateInitializer { // no initializer required since parent initializer is private +} + +contract Child_Of_ParentPublicInitializer_Ok is Parent_PublicInitializer { // no initializer required since parent initializer is public +} + +contract Child_Has_PrivateInitializer_Bad is Parent__OnlyInitializingModifier { // parent has internal initializer, but child has private + function initialize() private {} } \ No newline at end of file diff --git a/packages/core/src/validate-initializers.test.ts b/packages/core/src/validate-initializers.test.ts index a5f565962..4399e10ee 100644 --- a/packages/core/src/validate-initializers.test.ts +++ b/packages/core/src/validate-initializers.test.ts @@ -134,3 +134,7 @@ testValid('InitializationOrder_Duplicate_UnsafeAllow_Contract', 'transparent', t testValid('InitializationOrder_Duplicate_UnsafeAllow_Function', 'transparent', true); testValid('InitializationOrder_Duplicate_UnsafeAllow_Call', 'transparent', true); testOverride('InitializationOrder_Duplicate_Bad', 'transparent', { unsafeAllow: ['duplicate-initializer-call'] }, true); + +testValid('Child_Of_ParentPrivateInitializer_Ok', 'transparent', true); +testValid('Child_Of_ParentPublicInitializer_Ok', 'transparent', true); +testValid('Child_Has_PrivateInitializer_Bad', 'transparent', false, 'Contract is missing an initializer');