From d9908029825b701caa31f9d764ab682af9e2b09c Mon Sep 17 00:00:00 2001 From: ctrlc03 <93448202+ctrlc03@users.noreply.github.com> Date: Mon, 23 Dec 2024 11:18:50 +0100 Subject: [PATCH] refactor: remove redundant init from poll contract --- packages/contracts/contracts/Poll.sol | 49 ++++++++------------ packages/contracts/contracts/PollFactory.sol | 3 -- packages/contracts/tests/Poll.test.ts | 4 -- 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/packages/contracts/contracts/Poll.sol b/packages/contracts/contracts/Poll.sol index e90d025b1..4e0695cbf 100644 --- a/packages/contracts/contracts/Poll.sol +++ b/packages/contracts/contracts/Poll.sol @@ -16,9 +16,6 @@ import { CurveBabyJubJub } from "./crypto/BabyJubJub.sol"; /// @dev Do not deploy this directly. Use PollFactory.deploy() which performs some /// checks on the Poll constructor arguments. contract Poll is Params, Utilities, SnarkCommon, IPoll { - /// @notice Whether the Poll has been initialized - bool internal isInit; - /// @notice The coordinator's public key PubKey public coordinatorPubKey; @@ -163,6 +160,25 @@ contract Poll is Params, Utilities, SnarkCommon, IPoll { emptyBallotRoot = _emptyBallotRoot; // store the poll id pollId = _pollId; + + unchecked { + numMessages++; + } + + // init chainHash here by inserting placeholderLeaf + uint256[2] memory dat; + dat[0] = NOTHING_UP_MY_SLEEVE; + dat[1] = 0; + + (Message memory _message, PubKey memory _padKey, uint256 placeholderLeaf) = padAndHashMessage(dat); + chainHash = NOTHING_UP_MY_SLEEVE; + batchHashes.push(NOTHING_UP_MY_SLEEVE); + updateChainHash(placeholderLeaf); + + InternalLazyIMT._init(pollStateTree, extContracts.maci.stateTreeDepth()); + InternalLazyIMT._insert(pollStateTree, BLANK_STATE_LEAF_HASH); + + emit PublishMessage(_message, _padKey); } /// @notice A modifier that causes the function to revert if the voting period is @@ -186,33 +202,6 @@ contract Poll is Params, Utilities, SnarkCommon, IPoll { _; } - /// @notice The initialization function. - /// @dev Should be called immediately after Poll creation - function init() public virtual { - if (isInit) revert PollAlreadyInit(); - // set to true so it cannot be called again - isInit = true; - - unchecked { - numMessages++; - } - - // init chainHash here by inserting placeholderLeaf - uint256[2] memory dat; - dat[0] = NOTHING_UP_MY_SLEEVE; - dat[1] = 0; - - (Message memory _message, PubKey memory _padKey, uint256 placeholderLeaf) = padAndHashMessage(dat); - chainHash = NOTHING_UP_MY_SLEEVE; - batchHashes.push(NOTHING_UP_MY_SLEEVE); - updateChainHash(placeholderLeaf); - - InternalLazyIMT._init(pollStateTree, extContracts.maci.stateTreeDepth()); - InternalLazyIMT._insert(pollStateTree, BLANK_STATE_LEAF_HASH); - - emit PublishMessage(_message, _padKey); - } - // get all batch hash array elements function getBatchHashes() external view returns (uint256[] memory) { return batchHashes; diff --git a/packages/contracts/contracts/PollFactory.sol b/packages/contracts/contracts/PollFactory.sol index e2ce4d27a..b68c0a8bb 100644 --- a/packages/contracts/contracts/PollFactory.sol +++ b/packages/contracts/contracts/PollFactory.sol @@ -35,9 +35,6 @@ contract PollFactory is Params, DomainObjs, IPollFactory { _pollId ); - // init Poll - poll.init(); - pollAddr = address(poll); } } diff --git a/packages/contracts/tests/Poll.test.ts b/packages/contracts/tests/Poll.test.ts index 5b156e313..df04312a0 100644 --- a/packages/contracts/tests/Poll.test.ts +++ b/packages/contracts/tests/Poll.test.ts @@ -131,10 +131,6 @@ describe("Poll", () => { ); }); - it("should not be possible to init the Poll contract twice", async () => { - await expect(pollContract.init()).to.be.revertedWithCustomError(pollContract, "PollAlreadyInit"); - }); - it("should have the correct coordinator public key set", async () => { const coordinatorPubKey = await pollContract.coordinatorPubKey(); expect(coordinatorPubKey[0].toString()).to.eq(coordinator.pubKey.rawPubKey[0].toString());