Skip to content

Commit

Permalink
Merge pull request #258 from corydickson/cd/tally-votes
Browse files Browse the repository at this point in the history
Expect tally in suites
  • Loading branch information
corydickson authored Sep 13, 2021
2 parents e61c684 + 6261178 commit 4bc217d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
16 changes: 8 additions & 8 deletions integrationTests/ts/__tests__/suites.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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
}
]
}
1 change: 0 additions & 1 deletion integrationTests/ts/__tests__/suites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 14 additions & 5 deletions integrationTests/ts/__tests__/suites.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { ethers } = require('hardhat')
import * as path from 'path'
import * as fs from 'fs'
import {
PubKey,
PrivKey,
Expand All @@ -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()

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

Expand Down Expand Up @@ -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}` +
Expand Down
39 changes: 38 additions & 1 deletion integrationTests/ts/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 4bc217d

Please sign in to comment.