Skip to content

Commit

Permalink
refactor(cli): print relevant values when comparing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
baumstern committed Nov 6, 2023
1 parent 9275481 commit 2e26126
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 57 deletions.
92 changes: 42 additions & 50 deletions cli/ts/proveOnChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'maci-contracts'

import {
compareOnChainValue,
validateEthAddress,
contractExists,
delay,
Expand Down Expand Up @@ -304,18 +305,14 @@ const proveOnChain = async (args: any) => {
currentMessageBatchIndex -= messageBatchSize
}

const txErr = 'Error: processMessages() failed'
const { proof, circuitInputs, publicInputs } = data.processProofs[i]

// Perform checks
if (circuitInputs.pollEndTimestamp !== pollEndTimestampOnChain.toString()) {
console.error('Error: pollEndTimestamp mismatch.')
return
if (!compareOnChainValue("pollEndTimestamp", pollEndTimestampOnChain.toString(), circuitInputs.pollEndTimestamp)) {
return
}

if (BigInt(circuitInputs.msgRoot).toString() !== messageRootOnChain.toString()) {
console.error('Error: message root mismatch.')
return
if (!compareOnChainValue("message root", messageRootOnChain.toString(), BigInt(circuitInputs.msgRoot).toString())) {
return
}

let currentSbCommitmentOnChain
Expand All @@ -325,21 +322,20 @@ const proveOnChain = async (args: any) => {
} else {
currentSbCommitmentOnChain = BigInt(await mpContract.sbCommitment())
}

if (currentSbCommitmentOnChain.toString() !== circuitInputs.currentSbCommitment) {
console.error('Error: currentSbCommitment mismatch.')
return

if (!compareOnChainValue("currentSbCommitment", currentSbCommitmentOnChain.toString(), circuitInputs.currentSbCommitment)) {
return
}


const coordPubKeyHashOnChain = BigInt(await pollContract.coordinatorPubKeyHash())
if (
hashLeftRight(
BigInt(circuitInputs.coordPubKey[0]),
BigInt(circuitInputs.coordPubKey[1]),
).toString() !== coordPubKeyHashOnChain.toString()
) {
console.error('Error: coordPubKey mismatch.')
return
const coordPubKeyHashOffChain = hashLeftRight(
BigInt(circuitInputs.coordPubKey[0]),
BigInt(circuitInputs.coordPubKey[1]),
).toString()

if (!compareOnChainValue("coordPubKey", coordPubKeyHashOnChain.toString(), coordPubKeyHashOffChain)) {
return
}

const packedValsOnChain = BigInt(await mpContract.genProcessMessagesPackedVals(
Expand All @@ -348,9 +344,9 @@ const proveOnChain = async (args: any) => {
numSignUps,
)).toString()

if (circuitInputs.packedVals !== packedValsOnChain) {
console.error('Error: packedVals mismatch.')
return

if (!compareOnChainValue("packedVals", packedValsOnChain, circuitInputs.packedVals)) {
return
}

const formattedProof = formatProofForVerifierContract(proof)
Expand All @@ -364,9 +360,8 @@ const proveOnChain = async (args: any) => {
circuitInputs.newSbCommitment,
))

if (publicInputHashOnChain.toString() !== publicInputs[0].toString()) {
console.error('Public input mismatch.')
return
if (!compareOnChainValue("Public input", publicInputHashOnChain.toString(), publicInputs[0].toString())) {
return
}

const isValidOnChain = await verifierContract.verify(
Expand All @@ -380,6 +375,7 @@ const proveOnChain = async (args: any) => {
return
}

const txErr = "Error: processMessages() failed"
let tx
try {
tx = await mpContract.processMessages(
Expand Down Expand Up @@ -439,30 +435,26 @@ const proveOnChain = async (args: any) => {
const { proof, circuitInputs, publicInputs } = data.subsidyProofs[i]

const subsidyCommitmentOnChain = await subsidyContract.subsidyCommitment()
if (subsidyCommitmentOnChain.toString() !== circuitInputs.currentSubsidyCommitment) {
console.error(`Error: subsidycommitment mismatch`)
return
if (!compareOnChainValue("subsidycommitment", subsidyCommitmentOnChain.toString(), circuitInputs.currentSubsidyCommitment)) {
return
}
const packedValsOnChain = BigInt(
await subsidyContract.genSubsidyPackedVals(numSignUps)
)
if (circuitInputs.packedVals !== packedValsOnChain.toString()) {
console.error('Error: subsidy packedVals mismatch.')
return
if (!compareOnChainValue("subsidy packedVals", packedValsOnChain.toString(), circuitInputs.packedVals)) {
return
}
const currentSbCommitmentOnChain = await subsidyContract.sbCommitment()
if (currentSbCommitmentOnChain.toString() !== circuitInputs.sbCommitment) {
console.error('Error: currentSbCommitment mismatch.')
return
if (!compareOnChainValue("currentSbCommitment", currentSbCommitmentOnChain.toString(), circuitInputs.sbCommitment)) {
return
}
const publicInputHashOnChain = await subsidyContract.genSubsidyPublicInputHash(
numSignUps,
circuitInputs.newSubsidyCommitment,
)

if (publicInputHashOnChain.toString() !== publicInputs[0]) {
console.error('Error: public input mismatch.')
return
if (!compareOnChainValue("public input", publicInputHashOnChain.toString(), publicInputs[0])) {
return
}

const txErr = 'Error: updateSubsidy() failed...'
Expand Down Expand Up @@ -542,9 +534,9 @@ const proveOnChain = async (args: any) => {
const { proof, circuitInputs, publicInputs } = data.tallyProofs[i]

const currentTallyCommitmentOnChain = await tallyContract.tallyCommitment()
if (currentTallyCommitmentOnChain.toString() !== circuitInputs.currentTallyCommitment) {
console.error('Error: currentTallyCommitment mismatch.')
return

if (!compareOnChainValue("currentTallyCommitment", currentTallyCommitmentOnChain.toString(), circuitInputs.currentTallyCommitment)) {
return
}

const packedValsOnChain = BigInt(
Expand All @@ -554,15 +546,15 @@ const proveOnChain = async (args: any) => {
tallyBatchSize,
)
)
if (circuitInputs.packedVals !== packedValsOnChain.toString()) {
console.error('Error: packedVals mismatch.')
return

if (!compareOnChainValue("packedVals", packedValsOnChain.toString(), circuitInputs.packedVals)) {
return
}

const currentSbCommitmentOnChain = await mpContract.sbCommitment()
if (currentSbCommitmentOnChain.toString() !== circuitInputs.sbCommitment) {
console.error('Error: currentSbCommitment mismatch.')
return

if (!compareOnChainValue("currentSbCommitment", currentSbCommitmentOnChain.toString(), circuitInputs.sbCommitment)) {
return
}

const publicInputHashOnChain = await tallyContract.genTallyVotesPublicInputHash(
Expand All @@ -571,9 +563,9 @@ const proveOnChain = async (args: any) => {
tallyBatchSize,
circuitInputs.newTallyCommitment,
)
if (publicInputHashOnChain.toString() !== publicInputs[0]) {
console.error(`Error: public input mismatch. tallyBatchNum=${i}, onchain=${publicInputHashOnChain.toString()}, offchain=${publicInputs[0]}`)
return

if (!compareOnChainValue("public input", publicInputHashOnChain.toString(), publicInputs[0])) {
return
}

const formattedProof = formatProofForVerifierContract(proof)
Expand Down
16 changes: 16 additions & 0 deletions cli/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ const isPathExist = (paths: Array<string>): [boolean, string] => {
return [true, null]
}

const compareOnChainValue = (
name: string,
onChainValue: any,
offChainValue: any,
): boolean => {
if (onChainValue !== offChainValue) {
console.error(`Error: ${name} mismatch.`)
console.error(" onchainValue: " + onChainValue)
console.error(" offchainValue: " + offChainValue)
return false
}
return true
}


export {
promptPwd,
calcBinaryTreeDepthFromMaxLeaves,
Expand All @@ -155,5 +170,6 @@ export {
currentBlockTimestamp,
batchTransactionRequests,
delay,
compareOnChainValue,
isPathExist,
}
14 changes: 7 additions & 7 deletions cli/ts/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'maci-contracts'

import {
compareOnChainValue,
validateEthAddress,
contractExists,
} from './utils'
Expand Down Expand Up @@ -241,9 +242,9 @@ const verify = async (args: any) => {
newPerVOSpentVoiceCreditsCommitment,
])

if (onChainTallyCommitment !== newTallyCommitment) {
console.log('Error: the on-chain tally commitment does not match.')
return

if (!compareOnChainValue("tally commitment", onChainTallyCommitment, newTallyCommitment)) {
return
}

// ----------------------------------------------
Expand Down Expand Up @@ -289,10 +290,9 @@ const verify = async (args: any) => {
data.results.salt,
voteOptionTreeDepth
)

if (onChainSubsidyCommitment !== newSubsidyCommitment) {
console.log('Error: the on-chain subsidy commitment does not match.')
return

if (!compareOnChainValue("subsidy commitment", onChainSubsidyCommitment, newSubsidyCommitment)) {
return
}
}

Expand Down

0 comments on commit 2e26126

Please sign in to comment.