From b8090cc1b3b1165503be65704e900f04c3248939 Mon Sep 17 00:00:00 2001 From: ctrlc03 <93448202+ctrlc03@users.noreply.github.com> Date: Sun, 14 Jan 2024 15:49:38 +0000 Subject: [PATCH] feat(contracts): return contract addresses when deploying the poll contract Ensure we return tally, mp and subsidy (if enabled) addresses when deploying a new poll, alongside the poll address --- circuits/scripts/build_intel.sh | 2 +- cli/tests/e2e/e2e.subsidy.test.ts | 2 +- cli/ts/commands/deployPoll.ts | 37 ++++++++++++++---------- contracts/contracts/MACI.sol | 24 ++++++++++----- contracts/tests/MessageProcessor.test.ts | 11 +++++-- contracts/tests/Subsidy.test.ts | 22 +++++++------- contracts/tests/Tally.test.ts | 13 +++++++-- contracts/ts/genMaciState.ts | 12 ++++++-- 8 files changed, 79 insertions(+), 44 deletions(-) diff --git a/circuits/scripts/build_intel.sh b/circuits/scripts/build_intel.sh index a6fb9b79ce..d94ff3b421 100644 --- a/circuits/scripts/build_intel.sh +++ b/circuits/scripts/build_intel.sh @@ -18,4 +18,4 @@ do make cd "$CWD" -done \ No newline at end of file +done diff --git a/cli/tests/e2e/e2e.subsidy.test.ts b/cli/tests/e2e/e2e.subsidy.test.ts index f5e35fd937..f13d230024 100644 --- a/cli/tests/e2e/e2e.subsidy.test.ts +++ b/cli/tests/e2e/e2e.subsidy.test.ts @@ -71,7 +71,7 @@ describe("e2e with Subsidy tests", function test() { ); }); - describe("4 signups, 6 messages", () => { + describe.only("4 signups, 6 messages", () => { after(() => { cleanSubsidy(); }); diff --git a/cli/ts/commands/deployPoll.ts b/cli/ts/commands/deployPoll.ts index 8b60ccbdf7..6a3f2e0aac 100644 --- a/cli/ts/commands/deployPoll.ts +++ b/cli/ts/commands/deployPoll.ts @@ -138,30 +138,35 @@ export const deployPoll = async ( const iface = maciContract.interface; const receiptLog = receipt!.logs[receipt!.logs.length - 1]; - const log = iface.parseLog(receiptLog as unknown as { topics: string[]; data: string }); + + // parse DeployPoll log + const log = iface.parseLog(receiptLog as unknown as { topics: string[]; data: string }) as unknown as { + args: { + _pollId: number; + pollAddr: { + poll: string; + messageProcessor: string; + tally: string; + subsidy: string; + }; + }; + name: string; + }; + // we are trying to get the poll id from the event logs // if we do not find this log then we throw - if (log?.name !== "DeployPoll") { + if (log.name !== "DeployPoll") { logError("Invalid event log"); } // eslint-disable-next-line no-underscore-dangle - const pollId = log!.args._pollId as number; - // eslint-disable-next-line no-underscore-dangle - pollAddr = log!.args._pollAddr as string; - // eslint-disable-next-line no-underscore-dangle - messageProcessorContractAddress = log!.args._mpAddr as string; - // eslint-disable-next-line no-underscore-dangle - tallyContractAddress = log!.args._tallyAddr as string; + const pollId = log.args._pollId; + pollAddr = log.args.pollAddr.poll; + messageProcessorContractAddress = log.args.pollAddr.messageProcessor; + tallyContractAddress = log.args.pollAddr.tally; if (subsidyEnabled) { - const receiptLogSubsidy = receipt!.logs[receipt!.logs.length - 2]; - const logSubsidy = iface.parseLog(receiptLogSubsidy as unknown as { topics: string[]; data: string }); - if (logSubsidy?.name !== "DeploySubsidy") { - logError("Invalid event log"); - } - // eslint-disable-next-line no-underscore-dangle - subsidyContractAddress = logSubsidy!.args._subsidyAddr as string; + subsidyContractAddress = log.args.pollAddr.subsidy; } logGreen(quiet, info(`Poll ID: ${pollId.toString()}`)); diff --git a/contracts/contracts/MACI.sol b/contracts/contracts/MACI.sol index e923eff37e..f49593a759 100644 --- a/contracts/contracts/MACI.sol +++ b/contracts/contracts/MACI.sol @@ -67,14 +67,21 @@ contract MACI is IMACI, Params, Utilities, Ownable { /// user may sign up to vote SignUpGatekeeper public immutable signUpGatekeeper; - /// @notice The contract which provides the values of the initial voice credit + /// @notice The contract which provides the values of the initial voice credit /// balance per user InitialVoiceCreditProxy public immutable initialVoiceCreditProxy; + /// @notice A struct holding the addresses of poll, mp and tally + struct PollContracts { + address poll; + address messageProcessor; + address tally; + address subsidy; + } + // Events event SignUp(uint256 _stateIndex, PubKey _userPubKey, uint256 _voiceCreditBalance, uint256 _timestamp); - event DeployPoll(uint256 _pollId, address _pollAddr, PubKey _pubKey, address _mpAddr, address _tallyAddr); - event DeploySubsidy(address _subsidyAddr); + event DeployPoll(uint256 _pollId, PubKey _pubKey, PollContracts pollAddr); /// @notice Only allow a Poll contract to call the modified function. modifier onlyPoll(uint256 _pollId) { @@ -199,7 +206,7 @@ contract MACI is IMACI, Params, Utilities, Ownable { address _verifier, address _vkRegistry, bool useSubsidy - ) public onlyOwner returns (address pollAddr) { + ) public onlyOwner returns (PollContracts memory pollAddr) { // cache the poll to a local variable so we can increment it uint256 pollId = nextPollId; @@ -236,16 +243,17 @@ contract MACI is IMACI, Params, Utilities, Ownable { address mp = messageProcessorFactory.deploy(_verifier, _vkRegistry, p, _owner); address tally = tallyFactory.deploy(_verifier, _vkRegistry, p, mp, _owner); + address subsidy; if (useSubsidy) { - address subsidy = subsidyFactory.deploy(_verifier, _vkRegistry, p, mp, _owner); - emit DeploySubsidy(subsidy); + subsidy = subsidyFactory.deploy(_verifier, _vkRegistry, p, mp, _owner); } polls[pollId] = p; - pollAddr = address(p); + // store the addresses in a struct so they can be returned + pollAddr = PollContracts({ poll: p, messageProcessor: mp, tally: tally, subsidy: subsidy }); - emit DeployPoll(pollId, pollAddr, _coordinatorPubKey, mp, tally); + emit DeployPoll(pollId, _coordinatorPubKey, pollAddr); } /// @inheritdoc IMACI diff --git a/contracts/tests/MessageProcessor.test.ts b/contracts/tests/MessageProcessor.test.ts index f5b700f7f6..0b4e8ac77f 100644 --- a/contracts/tests/MessageProcessor.test.ts +++ b/contracts/tests/MessageProcessor.test.ts @@ -75,14 +75,21 @@ describe("MessageProcessor", () => { const iface = maciContract.interface; const logs = receipt!.logs[receipt!.logs.length - 1]; const event = iface.parseLog(logs as unknown as { topics: string[]; data: string }) as unknown as { - args: { _pollId: number; _mpAddr: string }; + args: { + _pollId: number; + pollAddr: { + poll: string; + messageProcessor: string; + tally: string; + }; + }; }; pollId = event.args._pollId; const pollContractAddress = await maciContract.getPoll(pollId); pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract; - mpContract = new BaseContract(event.args._mpAddr, mpAbi, signer) as MessageProcessor; + mpContract = new BaseContract(event.args.pollAddr.messageProcessor, mpAbi, signer) as MessageProcessor; const block = await signer.provider!.getBlock(receipt!.blockHash); const deployTime = block!.timestamp; diff --git a/contracts/tests/Subsidy.test.ts b/contracts/tests/Subsidy.test.ts index f61e505270..44dcd5d428 100644 --- a/contracts/tests/Subsidy.test.ts +++ b/contracts/tests/Subsidy.test.ts @@ -73,18 +73,18 @@ describe("Subsidy", () => { expect(receipt?.status).to.eq(1); const iface = maciContract.interface; - // parse DeploySubsidy log - const logSubsidy = receipt!.logs[receipt!.logs.length - 2]; - const subsidyEvent = iface.parseLog(logSubsidy as unknown as { topics: string[]; data: string }) as unknown as { - args: { _subsidyAddr: string }; - name: string; - }; - expect(subsidyEvent.name).to.eq("DeploySubsidy"); - // parse DeployPoll log const logMPTally = receipt!.logs[receipt!.logs.length - 1]; const MPTallyEvent = iface.parseLog(logMPTally as unknown as { topics: string[]; data: string }) as unknown as { - args: { _pollId: number; _mpAddr: string; _tallyAddr: string }; + args: { + _pollId: number; + pollAddr: { + poll: string; + messageProcessor: string; + tally: string; + subsidy: string; + }; + }; name: string; }; expect(MPTallyEvent.name).to.eq("DeployPoll"); @@ -93,9 +93,9 @@ describe("Subsidy", () => { const pollContractAddress = await maciContract.getPoll(pollId); pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract; - const mpContractAddress = MPTallyEvent.args._mpAddr; + const mpContractAddress = MPTallyEvent.args.pollAddr.messageProcessor; mpContract = new BaseContract(mpContractAddress, mpAbi, signer) as MessageProcessor; - const subsidyContractAddress = subsidyEvent.args._subsidyAddr; + const subsidyContractAddress = MPTallyEvent.args.pollAddr.subsidy; subsidyContract = new BaseContract(subsidyContractAddress, subsidyAbi, signer) as Subsidy; // deploy local poll diff --git a/contracts/tests/Tally.test.ts b/contracts/tests/Tally.test.ts index 3081632d8b..ab5266a0f1 100644 --- a/contracts/tests/Tally.test.ts +++ b/contracts/tests/Tally.test.ts @@ -82,7 +82,14 @@ describe("TallyVotes", () => { const iface = maciContract.interface; const logs = receipt!.logs[receipt!.logs.length - 1]; const event = iface.parseLog(logs as unknown as { topics: string[]; data: string }) as unknown as { - args: { _pollId: number; _mpAddr: string; _tallyAddr: string }; + args: { + _pollId: number; + pollAddr: { + poll: string; + messageProcessor: string; + tally: string; + }; + }; name: string; }; expect(event.name).to.eq("DeployPoll"); @@ -91,8 +98,8 @@ describe("TallyVotes", () => { const pollContractAddress = await maciContract.getPoll(pollId); pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract; - mpContract = new BaseContract(event.args._mpAddr, mpAbi, signer) as MessageProcessor; - tallyContract = new BaseContract(event.args._tallyAddr, tallyAbi, signer) as Tally; + mpContract = new BaseContract(event.args.pollAddr.messageProcessor, mpAbi, signer) as MessageProcessor; + tallyContract = new BaseContract(event.args.pollAddr.tally, tallyAbi, signer) as Tally; // deploy local poll const p = maciState.deployPoll(BigInt(deployTime + duration), maxValues, treeDepths, messageBatchSize, coordinator); diff --git a/contracts/ts/genMaciState.ts b/contracts/ts/genMaciState.ts index 21e32615b5..4e75b81daf 100644 --- a/contracts/ts/genMaciState.ts +++ b/contracts/ts/genMaciState.ts @@ -108,7 +108,15 @@ export const genMaciStateFromContract = async ( assert(!!log); const mutableLogs = { ...log, topics: [...log.topics] }; const event = maciIface.parseLog(mutableLogs) as unknown as { - args: { _pubKey: string[]; _pollAddr: string; _pollId: number }; + args: { + _pubKey: string[]; + _pollId: number; + pollAddr: { + poll: string; + messageProcessor: string; + tally: string; + }; + }; }; const pubKey = new PubKey(event.args._pubKey.map((x) => BigInt(x.toString())) as [bigint, bigint]); @@ -116,7 +124,7 @@ export const genMaciStateFromContract = async ( const p = Number(event.args._pollId); assert(p === index); - const pollAddr = event.args._pollAddr; + const pollAddr = event.args.pollAddr.poll; actions.push({ type: "DeployPoll", blockNumber: log.blockNumber,