Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ownable reinitialization check #101

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/attribute/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract contract Ownable is IOwnable, Initializable {
* @notice Initializes the contract setting `msg.sender` as the initial owner
*/
function __Ownable__initialize() internal onlyInitializer {
if (owner() != address(0)) revert OwnableAlreadyInitializedError();
_updateOwner(_sender());
}

Expand Down
1 change: 1 addition & 0 deletions contracts/attribute/interfaces/IOwnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface IOwnable is IInitializable {

error OwnableNotOwnerError(address sender);
error OwnableNotPendingOwnerError(address sender);
error OwnableAlreadyInitializedError();

function owner() external view returns (address);
function pendingOwner() external view returns (address);
Expand Down
4 changes: 4 additions & 0 deletions contracts/mocks/MockOwnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ contract MockOwnable is Ownable {
super.__Ownable__initialize();
}

function __initializeV(uint256 version) external initializer(version) {
super.__Ownable__initialize();
}

function _beforeAcceptOwner() internal override {
beforeCalled = true;
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/attribute/Ownable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ describe('Ownable', () => {

expect(await ownable.owner()).to.equal(owner.address)
})

it('reverts if owner already set (reinitializing)', async () => {
expect(await ownable.owner()).to.equal(ethers.constants.AddressZero)

// mock the owner being set in a previous implementation version
await ownable.connect(owner).__initializeV(1)

await expect(ownable.connect(owner).__initializeV(2)).to.revertedWith('OwnableAlreadyInitializedError')

expect(await ownable.owner()).to.equal(owner.address)
})
})

describe('#setPendingOwner', async () => {
Expand Down
Loading