Skip to content

Commit

Permalink
integration tests for signup and publishMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
weijiekoh committed Apr 9, 2020
1 parent dabb41f commit 7bbe1b4
Show file tree
Hide file tree
Showing 18 changed files with 5,374 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,9 @@ jobs:
name: Run contract tests
command: cd contracts && ./scripts/runTestsInCircleCi.sh

- run:
name: Run integration tests
command: cd integrationTests && ./scripts/runTestsInCircleCi.sh

- store_artifacts:
path: circuits/build
6 changes: 3 additions & 3 deletions contracts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"maci-circuits": "1.0.0",
"maci-core": "1.0.0",
"maci-domainobjs": "1.0.0",
"ethers": "^4.0.45",
"@types/node": "^13.1.2",
"etherlime": "^2.2.4",
"@types/jest": "^24.0.25",
Expand Down
12 changes: 12 additions & 0 deletions contracts/sol/gatekeepers/FreeForAllSignUpGatekeeper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma solidity ^0.5.0;

import { SignUpGatekeeper } from './SignUpGatekeeper.sol';

contract FreeForAllGatekeeper is SignUpGatekeeper {

/*
* Registers the user without any restrictions.
*/
function register(address, bytes memory) public {
}
}
6 changes: 3 additions & 3 deletions contracts/ts/__tests__/Maci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as ethers from 'ethers'
import * as etherlime from 'etherlime-lib'

import { genAccounts, genTestAccounts } from '../accounts'
import { timeTravel } from '../../node_modules/etherlime/cli-commands/etherlime-test/time-travel.js'
import { timeTravel } from '../'

import { config } from 'maci-config'

Expand All @@ -32,7 +32,7 @@ import {
deployMaci,
deploySignupToken,
deploySignupTokenGatekeeper,
deployInitialVoiceCreditProxy,
deployConstantInitialVoiceCreditProxy,
genDeployer,
} from '../deploy'

Expand Down Expand Up @@ -198,7 +198,7 @@ describe('MACI', () => {
beforeAll(async () => {
qvtCircuit = await compileAndLoadCircuit('quadVoteTally_test.circom')
signUpTokenContract = await deploySignupToken(deployer)
constantIntialVoiceCreditProxyContract = await deployInitialVoiceCreditProxy(
constantIntialVoiceCreditProxyContract = await deployConstantInitialVoiceCreditProxy(
deployer,
config.maci.initialVoiceCreditBalance,
)
Expand Down
22 changes: 17 additions & 5 deletions contracts/ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const MiMC = require('@maci-contracts/compiled/MiMC.json')
const Hasher = require('@maci-contracts/compiled/Hasher.json')
const SignUpToken = require('@maci-contracts/compiled/SignUpToken.json')
const SignUpTokenGatekeeper = require('@maci-contracts/compiled/SignUpTokenGatekeeper.json')
const FreeForAllSignUpGatekeeper = require('@maci-contracts/compiled/FreeForAllGatekeeper.json')
const ConstantInitialVoiceCreditProxy = require('@maci-contracts/compiled/ConstantInitialVoiceCreditProxy.json')
const BatchUpdateStateTreeVerifier = require('@maci-contracts/compiled/BatchUpdateStateTreeVerifier.json')
const QuadVoteTallyVerifier = require('@maci-contracts/compiled/QuadVoteTallyVerifier.json')
Expand All @@ -37,7 +38,7 @@ const genDeployer = (
)
}

const deployInitialVoiceCreditProxy = async (deployer, amount: number) => {
const deployConstantInitialVoiceCreditProxy = async (deployer, amount: number) => {
console.log('Deploying InitialVoiceCreditProxy')
return await deployer.deploy(ConstantInitialVoiceCreditProxy, {}, amount)
}
Expand All @@ -61,9 +62,19 @@ const deploySignupTokenGatekeeper = async (
return signUpTokenGatekeeperContract
}

const deployFreeForAllSignUpGatekeeper = async (
deployer,
) => {
console.log('Deploying FreeForAllSignUpGatekeeper')
return await deployer.deploy(
FreeForAllSignUpGatekeeper,
{},
)
}

const deployMaci = async (
deployer,
signUpTokenGatekeeperAddress: string,
signUpGatekeeperAddress: string,
initialVoiceCreditProxy: string,
) => {
console.log('Deploying MiMC')
Expand All @@ -86,7 +97,7 @@ const deployMaci = async (
2 ** config.maci.merkleTrees.intermediateStateTreeDepth,
config.maci.messageBatchSize,
config.maci.voteOptionsMaxLeafIndex,
signUpTokenGatekeeperAddress,
signUpGatekeeperAddress,
batchUstVerifierContract.contractAddress,
quadVoteTallyVerifierContract.contractAddress,
config.maci.signUpDurationInSeconds.toString(),
Expand Down Expand Up @@ -165,7 +176,7 @@ const main = async () => {
if (initialVoiceCreditProxy) {
initialVoiceCreditBalanceAddress = initialVoiceCreditProxy
} else {
const initialVoiceCreditProxyContract = await deployInitialVoiceCreditProxy(
const initialVoiceCreditProxyContract = await deployConstantInitialVoiceCreditProxy(
deployer,
config.maci.initialVoiceCreditBalance,
)
Expand Down Expand Up @@ -216,7 +227,8 @@ export {
deployMaci,
deploySignupToken,
deploySignupTokenGatekeeper,
deployInitialVoiceCreditProxy,
deployConstantInitialVoiceCreditProxy,
deployFreeForAllSignUpGatekeeper,
genDeployer,
genProvider,
}
22 changes: 22 additions & 0 deletions contracts/ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
genDeployer,
deployMaci,
deploySignupToken,
deploySignupTokenGatekeeper,
deployConstantInitialVoiceCreditProxy,
deployFreeForAllSignUpGatekeeper,
} from './deploy'
import { genAccounts, genTestAccounts } from './accounts'
import { timeTravel } from '../node_modules/etherlime/cli-commands/etherlime-test/time-travel.js'

export {
timeTravel,
genDeployer,
genAccounts,
genTestAccounts,
deployMaci,
deploySignupToken,
deploySignupTokenGatekeeper,
deployFreeForAllSignUpGatekeeper,
deployConstantInitialVoiceCreditProxy,
}
49 changes: 43 additions & 6 deletions core/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
bigInt,
SnarkBigInt,
NOTHING_UP_MY_SLEEVE,
IncrementalMerkleTree,
} from 'maci-crypto'

Expand Down Expand Up @@ -65,30 +66,40 @@ class MaciState implements IMaciState {
public coordinatorKeypair: Keypair
public users: User[] = []
public stateTreeDepth: SnarkBigInt
public messageTreeDepth: SnarkBigInt
public voteOptionTreeDepth: SnarkBigInt
public messages: Message[] = []
public zerothStateLeaf: SnarkBigInt

// Public keys used to generate ephemeral shared keys which encrypt each
// message
public encPubKeys: PubKey[] = []
private emptyVoteOptionTreeRoot

constructor(
_coordinatorKeypair: Keypair,
_stateTreeDepth: SnarkBigInt,
_messageTreeDepth: SnarkBigInt,
_voteOptionTreeDepth: SnarkBigInt,
_zerothStateLeaf: SnarkBigInt,
) {

this.coordinatorKeypair = _coordinatorKeypair
this.stateTreeDepth = _stateTreeDepth
this.voteOptionTreeDepth = _voteOptionTreeDepth
this.zerothStateLeaf = _zerothStateLeaf
this.stateTreeDepth = bigInt(_stateTreeDepth)
this.messageTreeDepth = bigInt(_messageTreeDepth)
this.voteOptionTreeDepth = bigInt(_voteOptionTreeDepth)
this.zerothStateLeaf = bigInt(_zerothStateLeaf)

const emptyVoteOptionTree = new IncrementalMerkleTree(
this.voteOptionTreeDepth,
bigInt(0),
)
this.emptyVoteOptionTreeRoot = emptyVoteOptionTree.root
}

private genBlankVotes = () => {
let votes: SnarkBigInt[] = []
for (let i = 0; i < bigInt(2) ** this.voteOptionTreeDepth; i ++) {
for (let i = 0; i < bigInt(2).pow(this.voteOptionTreeDepth); i ++) {
votes.push(bigInt(0))
}

Expand All @@ -99,10 +110,15 @@ class MaciState implements IMaciState {
* Computes the state root
*/
public genStateRoot = (): SnarkBigInt => {
const blankStateLeaf = StateLeaf.genFreshLeaf(
new PubKey([0, 0]),
this.emptyVoteOptionTreeRoot,
bigInt(0),
)

const stateTree = new IncrementalMerkleTree(
this.stateTreeDepth,
StateLeaf.blankStateLeaf.hash(),
blankStateLeaf.hash(),
)

stateTree.insert(this.zerothStateLeaf)
Expand All @@ -118,11 +134,26 @@ class MaciState implements IMaciState {

const stateLeaf = new StateLeaf(
user.pubKey,
voteOptionTree.root(),
voteOptionTree.root,
user.voiceCreditBalance,
user.nonce,
)
stateTree.insert(stateLeaf.hash())
}
return stateTree.root
}

public genMessageRoot = (): SnarkBigInt => {
const messageTree = new IncrementalMerkleTree(
this.messageTreeDepth,
NOTHING_UP_MY_SLEEVE,
)

for (let message of this.messages) {
messageTree.insert(message.hash())
}

return messageTree.root
}

/*
Expand All @@ -132,6 +163,7 @@ class MaciState implements IMaciState {
const copied = new MaciState(
this.coordinatorKeypair.copy(),
bigInt(this.stateTreeDepth.toString()),
bigInt(this.messageTreeDepth.toString()),
bigInt(this.voteOptionTreeDepth.toString()),
bigInt(this.zerothStateLeaf.toString()),
)
Expand Down Expand Up @@ -164,6 +196,11 @@ class MaciState implements IMaciState {
)
}

/*
* Inserts a Message into the list of messages, as well as the
* corresponding public key used to generate the ECDH shared key which was
* used to encrypt said message.
*/
public publishMessage = (
_message: Message,
_encPubKey: PubKey,
Expand Down
4 changes: 2 additions & 2 deletions crypto/ts/IncrementalMerkleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class IncrementalMerkleTree {
public hashLeftRight: (left: SnarkBigInt, right: SnarkBigInt) => SnarkBigInt

constructor (
_depth: number,
_depth: number | SnarkBigInt,
_zeroValue: SnarkBigInt,
_hashLeftRight: (left: SnarkBigInt, right: SnarkBigInt) => SnarkBigInt = hashLeftRight,
) {
this.depth = _depth
this.depth = parseInt(_depth.toString(), 10)
this.zeroValue = _zeroValue
this.nextIndex = 0
this.zeros = { 0: this.zeroValue }
Expand Down
4 changes: 2 additions & 2 deletions crypto/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ interface Signature {

const bigInt = snarkjs.bigInt

// A nothing-up-my-sleeve zero value
// Should be equal to 5503045433092194285660061905880311622788666850989422096966288514930349325741
const SNARK_FIELD_SIZE = snarkjs.bigInt(
'21888242871839275222246405745257275088548364400416034343698204186575808495617'
)

// A nothing-up-my-sleeve zero value
// Should be equal to 5503045433092194285660061905880311622788666850989422096966288514930349325741
const NOTHING_UP_MY_SLEEVE =
bigInt(ethers.utils.solidityKeccak256(['bytes'], [ethers.utils.toUtf8Bytes('Maci')])) % SNARK_FIELD_SIZE

Expand Down
32 changes: 32 additions & 0 deletions integrationTests/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
verbose: true,
transform: {
"^.+\\.tsx?$": 'ts-jest'
},
testPathIgnorePatterns: [
"/build/",
"/node_modules/",
],
testRegex: '/__tests__/.*\\.test\\.ts$',
moduleFileExtensions: [
'ts',
'tsx',
'js',
'jsx',
'json',
'node'
],
moduleNameMapper: {
"^@maci-contracts(.*)$": "<rootDir>../contracts/$1",
},
globals: {
'ts-jest': {
diagnostics: {
// Do not fail on TS compilation errors
// https://kulshekhar.github.io/ts-jest/user/config/diagnostics#do-not-fail-on-first-error
warnOnly: true
}
}
},
testEnvironment: 'node'
}
Loading

0 comments on commit 7bbe1b4

Please sign in to comment.