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

refactor: remove redundant init from poll contract #1976

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
49 changes: 19 additions & 30 deletions packages/contracts/contracts/Poll.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions packages/contracts/contracts/PollFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ contract PollFactory is Params, DomainObjs, IPollFactory {
_pollId
);

// init Poll
poll.init();

pollAddr = address(poll);
}
}
4 changes: 0 additions & 4 deletions packages/contracts/tests/Poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,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());
Expand Down
Loading