Skip to content

Commit

Permalink
feat(contracts): return contract addresses when deploying the poll co…
Browse files Browse the repository at this point in the history
…ntract

Ensure we return tally, mp and subsidy (if enabled) addresses when deploying a new poll, alongside
the poll address
  • Loading branch information
ctrlc03 committed Jan 14, 2024
1 parent 1f72f6c commit e94998c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 43 deletions.
2 changes: 1 addition & 1 deletion circuits/scripts/build_intel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ do
make

cd "$CWD"
done
done
37 changes: 21 additions & 16 deletions cli/ts/commands/deployPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`));
Expand Down
24 changes: 16 additions & 8 deletions contracts/contracts/MACI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,21 @@ contract MACI is IMACI, Params, Utilities, Ownable {
/// user may sign up to vote
SignUpGatekeeper public immutable signUpGatekeeper;

Check warning on line 68 in contracts/contracts/MACI.sol

View workflow job for this annotation

GitHub Actions / check (lint:sol)

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @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;

Check warning on line 72 in contracts/contracts/MACI.sol

View workflow job for this annotation

GitHub Actions / check (lint:sol)

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions contracts/tests/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions contracts/tests/Subsidy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down
13 changes: 10 additions & 3 deletions contracts/tests/Tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down
12 changes: 10 additions & 2 deletions contracts/ts/genMaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,23 @@ 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]);

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,
Expand Down

0 comments on commit e94998c

Please sign in to comment.