Skip to content

Commit

Permalink
refactor: remove redundant init from poll contract
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 authored and 0xmad committed Jan 9, 2025
1 parent 65afbdc commit d990802
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
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 @@ -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());
Expand Down

0 comments on commit d990802

Please sign in to comment.