Skip to content

Commit

Permalink
fix(vms): fixed wrong path in blake3 bin command and various fixes on…
Browse files Browse the repository at this point in the history
… the verification CF
  • Loading branch information
ctrlc03 committed Jun 28, 2023
1 parent b5a17bd commit 9aec4e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
9 changes: 2 additions & 7 deletions packages/actions/src/helpers/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const vmContributionVerificationCommand = (
`aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} /var/tmp/lastZKey.zkey &>/dev/null`,
`snarkjs zkvi /var/tmp/genesisZkey.zkey /var/tmp/pot.ptau /var/tmp/lastZKey.zkey > /var/tmp/verification_transcript.log`,
`aws s3 cp /var/tmp/verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename} &>/dev/null`,
`./var/tmp/blake3.bin /var/tmp/verification_transcript.log | awk '{print $1}'`,
`/var/tmp/blake3.bin /var/tmp/verification_transcript.log | awk '{print $1}'`,
`rm /var/tmp/lastZKey.zkey /var/tmp/verification_transcript.log &>/dev/null`
]

Expand Down Expand Up @@ -332,6 +332,7 @@ export const retrieveCommandOutput = async (ssm: SSMClient, instanceId: string,
try {
// Run the command.
const response = await ssm.send(command)
console.log("DEBUG", response)

if (response.$metadata.httpStatusCode !== 200)
throw new Error(
Expand Down Expand Up @@ -363,12 +364,6 @@ export const retrieveCommandStatus = async (ssm: SSMClient, instanceId: string,
try {
// Run the command.
const response = await ssm.send(command)

if (response.$metadata.httpStatusCode !== 200)
throw new Error(
`Something went wrong when trying to retrieve the command ${commandId} status on the EC2 instance (${instanceId}). More details ${response}`
)

return response.Status!
} catch (error: any) {
throw new Error(
Expand Down
62 changes: 40 additions & 22 deletions packages/backend/src/functions/circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ import {
getDocumentById,
getFinalContribution,
sleep,
uploadFileToBucket
uploadFileToBucket,
uploadFileToBucketNoFile
} from "../lib/utils"

dotenv.config()
Expand Down Expand Up @@ -485,14 +486,20 @@ export const verifycontribution = functionsV2.https.onCall(

// @todo check sleep
await sleep(1000)

console.log("COMMAND SENT DEBUG")
let success: boolean = false
// Wait until the command completes with a success status.
const interval = setInterval(async () => {
printLog("I started the intrarval function", LogLevel.DEBUG)
try {
const cmdStatus = await retrieveCommandStatus(ssm, commandId, vmInstanceId)
printLog("CMD STATUS" + cmdStatus, LogLevel.DEBUG)
// TODO: make an enum.
if (cmdStatus === "Success") clearInterval(interval)
if (cmdStatus === "Success") {
printLog("DEBUG SUCCESS command", LogLevel.DEBUG)
success = true
clearInterval(interval)
}
else if (cmdStatus === "Failed" || cmdStatus === "AccessDenied")
// Refactoring error.
logAndThrowError(makeError("aborted", `Invalid command execution ${cmdStatus}`))
Expand All @@ -502,13 +509,26 @@ export const verifycontribution = functionsV2.https.onCall(
}
}, 60000)

// TODO To be deleted
// // Retrieve the command output.
// const commandOutput = await retrieveCommandOutput(ssm, commandId, vmInstanceId)

// // Check contribution validity.
// if (commandOutput.includes("ZKey Ok!")) isContributionValid = true
// else isContributionValid = false
printLog("EXITED INTERVAL", LogLevel.DEBUG)
printLog("Success " + success, LogLevel.DEBUG)
// if the command was successful we need to check whether the zKey is valid or not
if (success) {
// download verification transcript which would have been uploaded to S3 by the VM
verificationTranscriptTemporaryLocalPath = createTemporaryLocalPath(verificationTranscriptCompleteFilename)
printLog("DOWNLOADING ARTIFACT", LogLevel.DEBUG)
await downloadArtifactFromS3Bucket(bucketName, verificationTranscriptStoragePathAndFilename, verificationTranscriptCompleteFilename)
// read the transcript and check if it contains the string "ZKey Ok!"
const content = fs.readFileSync(verificationTranscriptTemporaryLocalPath, "utf-8")
if (content.includes("ZKey Ok!")) isContributionValid = true
printLog("is valid " + isContributionValid, LogLevel.DEBUG)

// if the contribution is valid then format the transcript and upload save it again to disk
if (isContributionValid) {
const updated = content.replace(/\x1b\[[0-9;]*m/g, "");
fs.writeFileSync(verificationTranscriptTemporaryLocalPath, updated)
}

}

// Stop the VM.
// await stopEC2Instance(ec2, vmInstanceId)
Expand Down Expand Up @@ -591,24 +611,22 @@ export const verifycontribution = functionsV2.https.onCall(
true
)
} else {
// Download verification transcript file from S3.
verificationTranscriptTemporaryLocalPath = createTemporaryLocalPath(
verificationTranscriptCompleteFilename
)
await downloadArtifactFromS3Bucket(
bucketName,
verificationTranscriptStoragePathAndFilename,
verificationTranscriptTemporaryLocalPath
)

// Retrieve the contribution hash from the command output.
lastZkeyBlake2bHash = await retrieveCommandOutput(await createSSMClient(), commandId, vmInstanceId)
// re upload the formatted verification transcript
await uploadFileToBucket(
bucketName,
verificationTranscriptStoragePathAndFilename,
verificationTranscriptTemporaryLocalPath,
true
)
fs.unlinkSync(verificationTranscriptTemporaryLocalPath)
}

// Compute verification transcript hash.
transcriptBlake2bHash = await blake512FromPath(verificationTranscriptTemporaryLocalPath)

// Free resources by unlinking transcript temporary folder.
// Free resources by unlinking transcript temporary file.
fs.unlinkSync(verificationTranscriptTemporaryLocalPath)

// Filter participant contributions to find the data related to the one verified.
Expand Down Expand Up @@ -636,7 +654,7 @@ export const verifycontribution = functionsV2.https.onCall(
transcriptStoragePath: verificationTranscriptStoragePathAndFilename,
lastZkeyStoragePath,
transcriptBlake2bHash,
lastZkeyBlake2bHash // @todo we need the hash of the last zkey
lastZkeyBlake2bHash
},
verificationSoftware: {
name: String(process.env.CUSTOM_CONTRIBUTION_VERIFICATION_SOFTWARE_NAME),
Expand Down

0 comments on commit 9aec4e7

Please sign in to comment.