Skip to content

Commit

Permalink
Expect tally in suites
Browse files Browse the repository at this point in the history
  • Loading branch information
corydickson committed Sep 13, 2021
1 parent e61c684 commit 80a066b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
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 80a066b

Please sign in to comment.