From 4224a3de937d2839c46df689c200d4cdc5971e6e Mon Sep 17 00:00:00 2001 From: Zero Ekkusu <94782988+ZeroEkkusu@users.noreply.github.com> Date: Fri, 14 Jul 2023 20:30:12 +0200 Subject: [PATCH] test: update `NetworkParams` tests --- test/child/NetworkParams.test.ts | 332 +++++++++++++++++++++++++++---- 1 file changed, 289 insertions(+), 43 deletions(-) diff --git a/test/child/NetworkParams.test.ts b/test/child/NetworkParams.test.ts index d050ec18..8e8c0930 100644 --- a/test/child/NetworkParams.test.ts +++ b/test/child/NetworkParams.test.ts @@ -6,43 +6,74 @@ import { ethers } from "hardhat"; import { NetworkParams } from "../../typechain-types"; describe("NetworkParams", () => { - let networkParams: NetworkParams, - accounts: SignerWithAddress[], - blockGasLimit: number, - checkpointBlockInterval: number, - minStake: BigNumber, - maxValidatorSetSize: number; + let initParams: NetworkParams.InitParamsStruct; + let networkParams: NetworkParams, accounts: SignerWithAddress[]; before(async () => { accounts = await ethers.getSigners(); + + initParams = { + newOwner: ethers.constants.AddressZero, + newBlockGasLimit: 0, + newCheckpointBlockInterval: 0, + newEpochSize: 0, + newEpochReward: 0, + newMinValidatorSetSize: 0, + newMaxValidatorSetSize: 0, + newMinStake: 0, + newWithdrawalWaitPeriod: 0, + newBlockTime: 0, + newBlockTimeDrift: 0, + newVotingDelay: 0, + newVotingPeriod: 0, + newProposalThreshold: 0, + }; }); - it("fail deployment, invalid input", async () => { + it("fail initialization, invalid input", async () => { const networkParamsFactory = await ethers.getContractFactory("NetworkParams"); - await expect(networkParamsFactory.deploy(accounts[0].address, 0, 0, 0, 0)).to.be.revertedWith( - "NetworkParams: INVALID_INPUT" - ); + networkParams = (await networkParamsFactory.deploy()) as NetworkParams; + await networkParams.deployed(); + await expect(networkParams.initialize(initParams)).to.be.revertedWith("NetworkParams: INVALID_INPUT"); }); - it("deployment success", async () => { - const networkParamsFactory = await ethers.getContractFactory("NetworkParams"); - blockGasLimit = 10 ** Math.floor(Math.random() + 6); - checkpointBlockInterval = 2 ** Math.floor(Math.random() * 5 + 10); - minStake = ethers.utils.parseUnits(String(Math.floor(Math.random() * 20 + 1))); - maxValidatorSetSize = Math.floor(Math.random() * 20 + 5); - networkParams = (await networkParamsFactory.deploy( - accounts[0].address, - blockGasLimit, - checkpointBlockInterval, - minStake, - maxValidatorSetSize - )) as NetworkParams; + it("initialization success", async () => { + initParams.newOwner = accounts[0].address; + initParams.newBlockGasLimit = 10 ** Math.floor(Math.random() + 6); + initParams.newCheckpointBlockInterval = 2 ** Math.floor(Math.random() * 5 + 10); + initParams.newEpochSize = 2 ** Math.floor(Math.random() * 5 + 10); + initParams.newEpochReward = ethers.utils.parseUnits(String(Math.floor(Math.random() * 20 + 1))); + initParams.newMinValidatorSetSize = Math.floor(Math.random() * 20 + 5); + initParams.newMaxValidatorSetSize = Math.floor(Math.random() * 20 + 5); + initParams.newMinStake = ethers.utils.parseUnits(String(Math.floor(Math.random() * 20 + 1))); + initParams.newWithdrawalWaitPeriod = 2 ** Math.floor(Math.random() * 5 + 10); + initParams.newBlockTime = 2 ** Math.floor(Math.random() * 5 + 10); + initParams.newBlockTimeDrift = 2 ** Math.floor(Math.random() * 5 + 10); + initParams.newVotingDelay = 2 ** Math.floor(Math.random() * 5 + 10); + initParams.newVotingPeriod = 2 ** Math.floor(Math.random() * 5 + 10); + initParams.newProposalThreshold = Math.floor(Math.random() * 100 + 1); - await networkParams.deployed(); + await networkParams.initialize(initParams); - expect(await networkParams.blockGasLimit()).to.equal(blockGasLimit); - expect(await networkParams.checkpointBlockInterval()).to.equal(checkpointBlockInterval); - expect(await networkParams.minStake()).to.equal(minStake); - expect(await networkParams.maxValidatorSetSize()).to.equal(maxValidatorSetSize); + expect(await networkParams.owner()).to.equal(initParams.newOwner); + expect(await networkParams.blockGasLimit()).to.equal(initParams.newBlockGasLimit); + expect(await networkParams.checkpointBlockInterval()).to.equal(initParams.newCheckpointBlockInterval); + expect(await networkParams.epochSize()).to.equal(initParams.newEpochSize); + expect(await networkParams.epochReward()).to.equal(initParams.newEpochReward); + expect(await networkParams.minValidatorSetSize()).to.equal(initParams.newMinValidatorSetSize); + expect(await networkParams.maxValidatorSetSize()).to.equal(initParams.newMaxValidatorSetSize); + expect(await networkParams.minStake()).to.equal(initParams.newMinStake); + expect(await networkParams.withdrawalWaitPeriod()).to.equal(initParams.newWithdrawalWaitPeriod); + expect(await networkParams.blockTime()).to.equal(initParams.newBlockTime); + expect(await networkParams.blockTimeDrift()).to.equal(initParams.newBlockTimeDrift); + expect(await networkParams.votingDelay()).to.equal(initParams.newVotingDelay); + expect(await networkParams.votingPeriod()).to.equal(initParams.newVotingPeriod); + expect(await networkParams.proposalThreshold()).to.equal(initParams.newProposalThreshold); + }); + + it("should throw error on reinitialization", async () => { + await expect(networkParams.initialize(initParams)).to.be.revertedWith( + "Initializable: contract is already initialized" + ); }); it("set new block gas limit fail: only owner", async () => { @@ -59,10 +90,21 @@ describe("NetworkParams", () => { }); it("set new block gas limit success", async () => { - blockGasLimit = 10 ** Math.floor(Math.random() + 6); - await networkParams.setNewBlockGasLimit(blockGasLimit); + initParams.newBlockGasLimit = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewBlockGasLimit(initParams.newBlockGasLimit); - expect(await networkParams.blockGasLimit()).to.equal(blockGasLimit); + expect(await networkParams.blockGasLimit()).to.equal(initParams.newBlockGasLimit); + }); + + it("set new checkpoint block interval fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewCheckpointBlockInterval(1)).to.be.revertedWith( + "Ownable: caller is not the owner" + ); + await stopImpersonatingAccount(accounts[1].address); }); it("set new checkpoint block interval fail: invalid input", async () => { @@ -72,21 +114,81 @@ describe("NetworkParams", () => { }); it("set new checkpoint block interval success", async () => { - checkpointBlockInterval = 2 ** Math.floor(Math.random() * 5 + 10); - await networkParams.setNewCheckpointBlockInterval(checkpointBlockInterval); + initParams.newCheckpointBlockInterval = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewCheckpointBlockInterval(initParams.newCheckpointBlockInterval); - expect(await networkParams.checkpointBlockInterval()).to.equal(checkpointBlockInterval); + expect(await networkParams.checkpointBlockInterval()).to.equal(initParams.newCheckpointBlockInterval); }); - it("set new min stake fail: invalid input", async () => { - await expect(networkParams.setNewMinStake(0)).to.be.revertedWith("NetworkParams: INVALID_MIN_STAKE"); + it("set new epoch size fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewEpochSize(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); }); - it("set new min stake success", async () => { - minStake = ethers.utils.parseUnits(String(Math.floor(Math.random() * 20 + 1))); - await networkParams.setNewMinStake(minStake); + it("set new epoch size fail: invalid input", async () => { + await expect(networkParams.setNewEpochSize(0)).to.be.revertedWith("NetworkParams: INVALID_EPOCH_SIZE"); + }); + + it("set new epoch size success", async () => { + initParams.newEpochSize = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewEpochSize(initParams.newEpochSize); + + expect(await networkParams.epochSize()).to.equal(initParams.newEpochSize); + }); + + it("set new epoch reward fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewEpochReward(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); + }); + + it("set new epoch reward fail: invalid input", async () => { + await expect(networkParams.setNewEpochReward(0)).to.be.revertedWith("NetworkParams: INVALID_EPOCH_REWARD"); + }); - expect(await networkParams.minStake()).to.equal(minStake); + it("set new epoch reward success", async () => { + initParams.newEpochReward = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewEpochReward(initParams.newEpochReward); + + expect(await networkParams.epochReward()).to.equal(initParams.newEpochReward); + }); + + it("set new min validator set size fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewMinValidatorSetSize(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); + }); + + it("set new min validator set size fail: invalid input", async () => { + await expect(networkParams.setNewMinValidatorSetSize(0)).to.be.revertedWith( + "NetworkParams: INVALID_MIN_VALIDATOR_SET_SIZE" + ); + }); + + it("set new min validator set size success", async () => { + initParams.newMinValidatorSetSize = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewMinValidatorSetSize(initParams.newMinValidatorSetSize); + + expect(await networkParams.minValidatorSetSize()).to.equal(initParams.newMinValidatorSetSize); + }); + + it("set new max validator set size fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewMaxValidatorSetSize(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); }); it("set new max validator set size fail: invalid input", async () => { @@ -96,9 +198,153 @@ describe("NetworkParams", () => { }); it("set new max validator set size success", async () => { - maxValidatorSetSize = Math.floor(Math.random() * 20 + 5); - await networkParams.setNewMaxValidatorSetSize(maxValidatorSetSize); + initParams.newMaxValidatorSetSize = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewMaxValidatorSetSize(initParams.newMaxValidatorSetSize); + + expect(await networkParams.maxValidatorSetSize()).to.equal(initParams.newMaxValidatorSetSize); + }); + + it("set new min stake fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewMinStake(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); + }); + + it("set new min stake fail: invalid input", async () => { + await expect(networkParams.setNewMinStake(0)).to.be.revertedWith("NetworkParams: INVALID_MIN_STAKE"); + }); + + it("set new min stake success", async () => { + initParams.newMinStake = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewMinStake(initParams.newMinStake); + + expect(await networkParams.minStake()).to.equal(initParams.newMinStake); + }); + + it("set new withdrawal wait period fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewWithdrawalWaitPeriod(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); + }); + + it("set new withdrawal wait period fail: invalid input", async () => { + await expect(networkParams.setNewWithdrawalWaitPeriod(0)).to.be.revertedWith( + "NetworkParams: INVALID_WITHDRAWAL_WAIT_PERIOD" + ); + }); + + it("set new withdrawal wait period success", async () => { + initParams.newWithdrawalWaitPeriod = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewWithdrawalWaitPeriod(initParams.newWithdrawalWaitPeriod); + + expect(await networkParams.withdrawalWaitPeriod()).to.equal(initParams.newWithdrawalWaitPeriod); + }); + + it("set new block time fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewBlockTime(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); + }); + + it("set new block time fail: invalid input", async () => { + await expect(networkParams.setNewBlockTime(0)).to.be.revertedWith("NetworkParams: INVALID_BLOCK_TIME"); + }); + + it("set new block time success", async () => { + initParams.newBlockTime = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewBlockTime(initParams.newBlockTime); + + expect(await networkParams.blockTime()).to.equal(initParams.newBlockTime); + }); + + it("set new block time drift fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewBlockTimeDrift(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); + }); + + it("set new block time drift fail: invalid input", async () => { + await expect(networkParams.setNewBlockTimeDrift(0)).to.be.revertedWith("NetworkParams: INVALID_BLOCK_TIME_DRIFT"); + }); + + it("set new block time drift success", async () => { + initParams.newBlockTimeDrift = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewBlockTimeDrift(initParams.newBlockTimeDrift); + + expect(await networkParams.blockTimeDrift()).to.equal(initParams.newBlockTimeDrift); + }); + + it("set new voting delay fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewVotingDelay(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); + }); + + it("set new voting delay fail: invalid input", async () => { + await expect(networkParams.setNewVotingDelay(0)).to.be.revertedWith("NetworkParams: INVALID_VOTING_DELAY"); + }); + + it("set new voting delay success", async () => { + initParams.newVotingDelay = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewVotingDelay(initParams.newVotingDelay); + + expect(await networkParams.votingDelay()).to.equal(initParams.newVotingDelay); + }); + + it("set new voting period fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewVotingPeriod(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); + }); + + it("set new voting period fail: invalid input", async () => { + await expect(networkParams.setNewVotingPeriod(0)).to.be.revertedWith("NetworkParams: INVALID_VOTING_PERIOD"); + }); + + it("set new voting period success", async () => { + initParams.newVotingPeriod = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewVotingPeriod(initParams.newVotingPeriod); + + expect(await networkParams.votingPeriod()).to.equal(initParams.newVotingPeriod); + }); + + it("set new proposal threshold fail: only owner", async () => { + await impersonateAccount(accounts[1].address); + await setBalance(accounts[1].address, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + const newNetworkParams = networkParams.connect(accounts[1]); + + await expect(newNetworkParams.setNewProposalThreshold(1)).to.be.revertedWith("Ownable: caller is not the owner"); + await stopImpersonatingAccount(accounts[1].address); + }); + + it("set new proposal threshold fail: invalid input", async () => { + await expect(networkParams.setNewProposalThreshold(0)).to.be.revertedWith( + "NetworkParams: INVALID_PROPOSAL_THRESHOLD" + ); + }); + + it("set new proposal threshold success", async () => { + initParams.newProposalThreshold = 10 ** Math.floor(Math.random() + 6); + await networkParams.setNewProposalThreshold(initParams.newProposalThreshold); - expect(await networkParams.maxValidatorSetSize()).to.equal(maxValidatorSetSize); + expect(await networkParams.proposalThreshold()).to.equal(initParams.newProposalThreshold); }); });