diff --git a/integrationTests/ts/__tests__/suites.json b/integrationTests/ts/__tests__/suites.json index 529ec47ff5..5670ca0e3e 100644 --- a/integrationTests/ts/__tests__/suites.json +++ b/integrationTests/ts/__tests__/suites.json @@ -14,9 +14,9 @@ "description": "Full tree, 4 full batches, no bribers", "numVotesPerUser": 1, "numUsers": 16, - "expectedTally": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], + "expectedTally": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], "expectedSpentVoiceCredits": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], - "expectedTotalSpentVoiceCredits": 17 + "expectedTotalSpentVoiceCredits": 16 }, { "name": "Happy path 2", @@ -33,9 +33,9 @@ "numUsers": 3, "numVotesPerUser": 2, "bribers": { "0": { "voteOptionIndices": [0,1] }, "1": { "voteOptionIndices": [0,1]}}, - "expectedTally": [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - "expectedSpentVoiceCredits": [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - "expectedTotalSpentVoiceCredits": 2 + "expectedTally": [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "expectedSpentVoiceCredits": [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "expectedTotalSpentVoiceCredits": 3 }, { "name": "Happy path for user key change", @@ -62,9 +62,9 @@ "1": { "voteOptionIndex": 1, "voteWeight": 5, "valid": true } } }, - "expectedTally": [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - "expectedSpentVoiceCredits": [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - "expectedTotalSpentVoiceCredits": 1 + "expectedTally": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "expectedSpentVoiceCredits": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "expectedTotalSpentVoiceCredits": 2 } ] } diff --git a/integrationTests/ts/__tests__/suites.test.ts b/integrationTests/ts/__tests__/suites.test.ts index e8c9eb30fc..65599f0936 100644 --- a/integrationTests/ts/__tests__/suites.test.ts +++ b/integrationTests/ts/__tests__/suites.test.ts @@ -7,7 +7,6 @@ import { describe('Test suites', () => { const data = loadData('suites.json') - for (const test of data.suites) { it(test.description, async () => { const result = await executeSuite(test, expect) diff --git a/integrationTests/ts/__tests__/suites.ts b/integrationTests/ts/__tests__/suites.ts index fc411a70a0..5bdabfcd87 100644 --- a/integrationTests/ts/__tests__/suites.ts +++ b/integrationTests/ts/__tests__/suites.ts @@ -1,4 +1,6 @@ const { ethers } = require('hardhat') +import * as path from 'path' +import * as fs from 'fs' import { PubKey, PrivKey, @@ -22,14 +24,13 @@ import { import { genPubKey } from 'maci-crypto' -import { exec, loadYaml, genTestUserCommands } from './utils' +import { exec, loadYaml, genTestUserCommands, expectTally } from './utils' const loadData = (name: string) => { return require('@maci-integrationTests/ts/__tests__/' + name) } const executeSuite = async (data: any, expect: any) => { - console.log(data) const config = loadYaml() const coordinatorKeypair = new Keypair() @@ -128,7 +129,6 @@ const executeSuite = async (data: any, expect: any) => { data.numVotesPerUser, data.bribers ) - console.log(users.length) // Sign up for (let i = 0; i < users.length; i++) { @@ -249,8 +249,6 @@ const executeSuite = async (data: any, expect: any) => { } console.log(e.stdout) - maciState.polls[pollId].processAllMessages() - const mergeSignupsCommand = `node build/index.js mergeSignups -x ${maciAddress} -o ${pollId}` e = exec(mergeSignupsCommand) @@ -284,6 +282,17 @@ const executeSuite = async (data: any, expect: any) => { } console.log(e.stdout) + const tally = JSON.parse(fs.readFileSync(path.join(__dirname, '../../../cli/tally.json')).toString()) + // Validate generated proof file + expect(JSON.stringify(tally.pollId)).toEqual(pollId) + expectTally( + config.constants.maci.maxMessages, + data.expectedTally, + data.expectedSpentVoiceCredits, + data.expectedTotalSpentVoiceCredits, + tally + ) + const proveOnChainCommand = `node build/index.js proveOnChain` + ` -x ${maciAddress}` + ` -o ${pollId}` + diff --git a/integrationTests/ts/__tests__/utils.ts b/integrationTests/ts/__tests__/utils.ts index 405286a526..4cbdbc4ca1 100644 --- a/integrationTests/ts/__tests__/utils.ts +++ b/integrationTests/ts/__tests__/utils.ts @@ -116,4 +116,41 @@ const genTestUserCommands = ( return usersCommands; } -export { exec, delay, loadYaml, genTestAccounts, genTestUserCommands } +interface Tally { + provider: string, + maci: string, + pollId: number, + newTallyCommitment: string, + results: { + tally: string[], + salt: string + } + totalSpentVoiceCredits: { + spent: string, + salt: string + } + perVOSpentVoiceCredits: { + tally: string[], + salt: string + } +} + +const expectTally = ( + maxMessages: number, + expectedTally: number[], + expectedSpentVoiceCredits: number[], + expectedTotalSpentVoiceCredits: number, + tallyFile: Tally +) => { + let genTally: string[] = Array(maxMessages).fill('0') + const calculateTally = + expectedTally.map((voteOption) => { + if (voteOption != 0) genTally[voteOption - 1] = (parseInt(genTally[voteOption - 1]) + voteOption).toString() + }) + + expect(tallyFile.results.tally).toEqual(genTally) + expect(tallyFile.totalSpentVoiceCredits.spent).toEqual(expectedTotalSpentVoiceCredits.toString()) +} + + +export { exec, delay, loadYaml, genTestAccounts, genTestUserCommands, expectTally }