Skip to content

Commit

Permalink
remove tally and message processor factories from funding round and u…
Browse files Browse the repository at this point in the history
…se the copies in MACI
  • Loading branch information
yuetloo committed Jan 23, 2024
1 parent 698680c commit 6d70868
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 30 deletions.
18 changes: 5 additions & 13 deletions contracts/contracts/FundingRound.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ contract FundingRound is
error InvalidUserRegistry();
error InvalidRecipientRegistry();
error InvalidCoordinator();
error InvalidTallyFactory();
error InvalidMessageProcessorFactory();
error UnexpectedPollAddress(address expected, address actual);


Expand Down Expand Up @@ -118,8 +116,6 @@ contract FundingRound is
TopupToken public topupToken;
IUserRegistry public userRegistry;
IRecipientRegistry public recipientRegistry;
ITallySubsidyFactory public tallyFactory;
IMessageProcessorFactory public messageProcessorFactory;
string public tallyHash;

// The alpha used in quadratic funding formula
Expand Down Expand Up @@ -223,6 +219,10 @@ contract FundingRound is

address verifier = address(tally.verifier());
address vkRegistry = address(tally.vkRegistry());

IMessageProcessorFactory messageProcessorFactory = maci.messageProcessorFactory();
ITallySubsidyFactory tallyFactory = maci.tallyFactory();

address mp = messageProcessorFactory.deploy(verifier, vkRegistry, address(poll), coordinator);
address newTally = tallyFactory.deploy(verifier, vkRegistry, address(poll), mp, coordinator);
_setTally(newTally);
Expand All @@ -233,8 +233,7 @@ contract FundingRound is
*/
function setMaci(
MACI _maci,
MACI.PollContracts memory _pollContracts,
Factories memory _factories
MACI.PollContracts memory _pollContracts
)
external
onlyOwner
Expand All @@ -244,8 +243,6 @@ contract FundingRound is
if (isAddressZero(address(_maci))) revert InvalidMaci();
if (isAddressZero(_pollContracts.poll)) revert InvalidPoll();
if (isAddressZero(_pollContracts.messageProcessor)) revert InvalidMessageProcessor();
if (isAddressZero(_factories.tallyFactory)) revert InvalidTallyFactory();
if (isAddressZero(_factories.messageProcessorFactory)) revert InvalidMessageProcessorFactory();

// we only create 1 poll per maci, make sure MACI use pollId = 0
// as the first poll index
Expand All @@ -259,11 +256,6 @@ contract FundingRound is
maci = _maci;
poll = Poll(_pollContracts.poll);
_setTally(_pollContracts.tally);

// save a copy of the factory in case we need to reset the tally result by recreating the
// the tally and message processor contracts
tallyFactory = ITallySubsidyFactory(_factories.tallyFactory);
messageProcessorFactory = IMessageProcessorFactory(_factories.messageProcessorFactory);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/FundingRoundFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ contract FundingRoundFactory is MACICommon {
);

// link funding round with maci related contracts
newRound.setMaci(maci, pollContracts, maciFactory.factories());
newRound.setMaci(maci, pollContracts);
newRound.transferOwnership(_clrfund);
maci.transferOwnership(address(newRound));
return address(newRound);
Expand Down
98 changes: 94 additions & 4 deletions contracts/e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,22 @@ describe('End-to-end Tests', function () {
return contributions
}

async function finalizeRound(): Promise<any> {
/**
* Get the tally and message processor contract address from the funding round
* @returns tally and message processor contract addresses
*/
async function getTallyAndMessageProcessor(): Promise<{
tallyAddress: string
messageProcessorAddress: string
}> {
const tallyAddress = await fundingRound.tally()
const tallyContact = await ethers.getContractAt('Tally', tallyAddress)
const messageProcessorAddress = await tallyContact.mp()

return { tallyAddress, messageProcessorAddress }
}

async function finalizeRound(testResetTally = false): Promise<any> {
debugLog('Finalizing round')
// Process messages and tally votes
const maciAddress = await maci.getAddress()
Expand Down Expand Up @@ -350,9 +365,8 @@ describe('End-to-end Tests', function () {
await genProofs(genProofArgs)
debugLog('Generated proof')

const tallyAddress = await fundingRound.tally()
const tallyContact = await ethers.getContractAt('Tally', tallyAddress)
const messageProcessorAddress = await tallyContact.mp()
const { tallyAddress, messageProcessorAddress } =
await getTallyAndMessageProcessor()

debugLog('Proving on chain')
// Submit proofs to MACI contract
Expand All @@ -365,6 +379,26 @@ describe('End-to-end Tests', function () {
tallyAddress,
quiet,
})

if (testResetTally) {
console.log('resetting tally and message processor contracts')
const resetTx = await fundingRound.resetTally()
await resetTx.wait()

const { tallyAddress, messageProcessorAddress } =
await getTallyAndMessageProcessor()
console.log('redoing proveOnChain with new tallyAddress', tallyAddress)

await proveOnChain({
pollId,
proofDir: genProofArgs.outputDir,
subsidyEnabled: false,
maciAddress,
messageProcessorAddress,
tallyAddress,
quiet,
})
}
console.log('finished proveOnChain')

const tally = JSON.parse(readFileSync(genProofArgs.tallyFile).toString())
Expand Down Expand Up @@ -922,4 +956,60 @@ describe('End-to-end Tests', function () {
])
expect(claims[2]).to.equal(expectedClaims[0])
})

it('should allow reset and re-run tally', async () => {
const contributions = await makeContributions([
(UNIT * BigInt(50)) / BigInt(10),
(UNIT * BigInt(50)) / BigInt(10),
])
// Submit messages
for (const contribution of contributions) {
const contributor = contribution.signer
const messages: Message[] = []
const encPubKeys: PubKey[] = []
let nonce = 1

// Spend voice credits on both recipients
for (const recipientIndex of [1, 2]) {
const voiceCredits = BigInt(contribution.voiceCredits) / BigInt(2)
const [message, encPubKey] = createMessage(
contribution.stateIndex,
contribution.keypair,
null,
coordinatorKeypair.pubKey,
recipientIndex,
voiceCredits,
nonce,
pollId
)
messages.push(message)
encPubKeys.push(encPubKey)
nonce += 1
}

await (fundingRound.connect(contributor) as Contract).submitMessageBatch(
messages.reverse().map((msg) => msg.asContractParam()),
encPubKeys.reverse().map((key) => key.asContractParam())
)
}

await time.increase(roundDuration)
const testResetTally = true
const { tally, claims } = await finalizeRound(testResetTally)
const expectedTotalVoiceCredits = sumVoiceCredits(
contributions.map((x) => x.voiceCredits)
)
expect(tally.totalSpentVoiceCredits.spent).to.equal(
expectedTotalVoiceCredits
)
const expectedClaims = await calculateClaims(
fundingRound,
new Array(2).fill(
contributions.map((x) => BigInt(x.voiceCredits) / BigInt(2))
)
)
console.log('expected claim', claims[1], expectedClaims[0])
expect(BigInt(claims[1])).to.equal(expectedClaims[0])
expect(BigInt(claims[2])).to.equal(expectedClaims[1])
})
})
13 changes: 1 addition & 12 deletions contracts/utils/testutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,7 @@ export async function deployTestFundingRound(
subsidy: pollAddr.subsidy,
}

const factoryContracts = {
pollFactory: factories.pollFactory,
messageProcessorFactory: factories.messageProcessorFactory,
tallyFactory: factories.tallyFactory,
subsidyFactory: factories.subsidyFactory,
}

await fundingRound.setMaci(
maciInstance.target,
pollContracts,
factoryContracts
)
await fundingRound.setMaci(maciInstance.target, pollContracts)

return {
token,
Expand Down

0 comments on commit 6d70868

Please sign in to comment.