Skip to content

Commit

Permalink
Disable initializers in contract constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanProgrammer committed Jan 22, 2024
1 parent 7a39e25 commit 53a6630
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 15 deletions.
5 changes: 5 additions & 0 deletions contracts/Distribution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ contract Distribution is IDistribution, OwnableUpgradeable, UUPSUpgradeable {
/**********************************************************************************************/
/*** Init ***/
/**********************************************************************************************/

constructor() {
_disableInitializers();
}

function Distribution_init(

Check warning on line 54 in contracts/Distribution.sol

View workflow job for this annotation

GitHub Actions / test

Function name must be in mixedCase
address depositToken_,
address l1Sender_,
Expand Down
4 changes: 4 additions & 0 deletions contracts/L1Sender.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ contract L1Sender is IL1Sender, ERC165, OwnableUpgradeable, UUPSUpgradeable {
_;
}

constructor() {
_disableInitializers();
}

function L1Sender__init(
address distribution_,
RewardTokenConfig calldata rewardTokenConfig_,
Expand Down
4 changes: 4 additions & 0 deletions contracts/L2MessageReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ contract L2MessageReceiver is IL2MessageReceiver, OwnableUpgradeable, UUPSUpgrad

mapping(uint16 => mapping(bytes => mapping(uint64 => bytes32))) public failedMessages;

constructor() {
_disableInitializers();
}

function L2MessageReceiver__init() external initializer {
__Ownable_init();
__UUPSUpgradeable_init();
Expand Down
4 changes: 4 additions & 0 deletions contracts/L2TokenReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ contract L2TokenReceiver is IL2TokenReceiver, OwnableUpgradeable, UUPSUpgradeabl

SwapParams public params;

constructor() {
_disableInitializers();
}

function L2TokenReceiver__init(
address router_,
address nonfungiblePositionManager_,
Expand Down
17 changes: 16 additions & 1 deletion test/Distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ describe('Distribution', () => {
afterEach(reverter.revert);

describe('UUPS proxy functionality', () => {
describe('#constructor', () => {
it('should disable initialize function', async () => {
const reason = 'Initializable: contract is already initialized';

const distribution = await distributionFactory.deploy();

await expect(distribution.Distribution_init(depositToken, l1Sender, [])).to.be.revertedWith(reason);
});
});

describe('#Distribution_init', () => {
it('should set correct data after creation', async () => {
const depositToken_ = await distribution.depositToken();
Expand All @@ -204,7 +214,12 @@ describe('Distribution', () => {
decreaseInterval: oneDay * 2,
};

const distribution = await distributionFactory.deploy();
const distributionProxy = await (
await ethers.getContractFactory('ERC1967Proxy')
).deploy(await distributionFactory.deploy(), '0x');

const distribution = distributionFactory.attach(await distributionProxy.getAddress()) as Distribution;

await distribution.Distribution_init(depositToken, l1Sender, [pool1, pool2]);

const pool1Data: IDistribution.PoolStruct = await distribution.pools(0);
Expand Down
39 changes: 27 additions & 12 deletions test/L1Sender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,35 @@ describe('L1Sender', () => {
});

describe('UUPS proxy functionality', () => {
describe('#Distribution_init', () => {
it('should revert if try to call init function twice', async () => {
let rewardTokenConfig: IL1Sender.RewardTokenConfigStruct;
let depositTokenConfig: IL1Sender.DepositTokenConfigStruct;

before(async () => {
rewardTokenConfig = {
gateway: lZEndpointMockL1,
receiver: l2MessageReceiver,
receiverChainId: receiverChainId,
};
depositTokenConfig = {
token: depositToken,
gateway: gatewayRouter,
receiver: SECOND,
};
});

describe('#constructor', () => {
it('should disable initialize function', async () => {
const reason = 'Initializable: contract is already initialized';

const rewardTokenConfig: IL1Sender.RewardTokenConfigStruct = {
gateway: lZEndpointMockL1,
receiver: l2MessageReceiver,
receiverChainId: receiverChainId,
};
const depositTokenConfig: IL1Sender.DepositTokenConfigStruct = {
token: depositToken,
gateway: gatewayRouter,
receiver: SECOND,
};
const l1Sender = await (await ethers.getContractFactory('L1Sender')).deploy();

await expect(l1Sender.L1Sender__init(OWNER, rewardTokenConfig, depositTokenConfig)).to.be.rejectedWith(reason);
});
});

describe('#L1Sender__init', () => {
it('should revert if try to call init function twice', async () => {
const reason = 'Initializable: contract is already initialized';

await expect(l1Sender.L1Sender__init(OWNER, rewardTokenConfig, depositTokenConfig)).to.be.rejectedWith(reason);
});
Expand Down
12 changes: 11 additions & 1 deletion test/L2MessageReceiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ describe('L2MessageReceiver', () => {
});

describe('UUPS proxy functionality', () => {
describe('#Distribution_init', () => {
describe('#constructor', () => {
it('should disable initialize function', async () => {
const reason = 'Initializable: contract is already initialized';

const l2MessageReceiver = await (await ethers.getContractFactory('L2MessageReceiver')).deploy();

await expect(l2MessageReceiver.L2MessageReceiver__init()).to.be.rejectedWith(reason);
});
});

describe('#L2MessageReceiver__init', () => {
it('should revert if try to call init function twice', async () => {
const reason = 'Initializable: contract is already initialized';

Expand Down
17 changes: 16 additions & 1 deletion test/L2TokenReceiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,22 @@ describe('L2TokenReceiver', () => {
});

describe('UUPS proxy functionality', () => {
describe('#Distribution_init', () => {
it('should disable initialize function', async () => {
const reason = 'Initializable: contract is already initialized';

const l2TokenReceiver = await (await ethers.getContractFactory('L2TokenReceiver')).deploy();

await expect(
l2TokenReceiver.L2TokenReceiver__init(swapRouter, nonfungiblePositionManager, {
tokenIn: inputToken,
tokenOut: outputToken,
fee: 500,
sqrtPriceLimitX96: 0,
}),
).to.be.rejectedWith(reason);
});

describe('#L2TokenReceiver__init', () => {
it('should revert if try to call init function twice', async () => {
const reason = 'Initializable: contract is already initialized';

Expand Down

0 comments on commit 53a6630

Please sign in to comment.